-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
146 lines (121 loc) · 4.91 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const vscode = require( 'vscode' ),
execall = require( 'execall' );
/**
* Returns a promise that will provide a tet editor with given `content`.
*
* @param {String} content
* @param {Object} [options] Config object.
* @param {String} [options.language='text'] Indicates what language should the editor use.
* @returns {Promise<TextEditor>}
*/
function setContent( content, options ) {
options = options || {
language: 'text'
};
return vscode.workspace.openTextDocument( {
language: options.language
} )
.then( doc => vscode.window.showTextDocument( doc ) )
.then( editor => {
let editBuilder = textEdit => {
textEdit.insert( new vscode.Position( 0, 0 ), String( content ) );
};
return editor.edit( editBuilder, {
undoStopBefore: true,
undoStopAfter: false
} )
.then( () => editor );
} );
}
function* readLines( content ) {
let newLineRegexp = /\r\n?|\n/gm,
curOffset = 0,
lastLineMatch;
while ( ( lastLineMatch = newLineRegexp.exec( content ) ) !== null ) {
yield content.substring( curOffset, lastLineMatch.index );
curOffset = lastLineMatch.index + lastLineMatch[ 0 ].length;
};
if ( curOffset < content.length ) {
yield content.substring( curOffset );
}
}
/**
* Extracts selections and markerless content out of given `inContent`.
*
* @private
* @param {String} inContent Input content with selection markers.
* @returns {Object} ret
* @returns {String} ret.content Content without selection-specific markers.
* @returns {Selection[]} ret.selections Selections picked from `inContent`.
*/
setContent._extractSelections = function( inContent ) {
let selections = [],
ret = {
content: '',
selections: selections
};
let collapsedRegexp = /\^|\[|\{|\]|\}/gm,
updatedContent = '',
lineNumber = -1,
// An array of { pos: Position, anchor: Boolean } objects.
unbalancedRangeOpenings = [],
// A mapping of handling methods to a given marker.
markerHandlers = {
'^': pos => selections.push( new vscode.Selection( pos, pos ) ),
'[': pos => unbalancedRangeOpenings.push( { pos: pos, anchor: true } ),
'{': pos => unbalancedRangeOpenings.push( { pos: pos, anchor: false } ),
']': pos => {
if ( !unbalancedRangeOpenings.length ) {
return;
}
let matchedOpening = unbalancedRangeOpenings.shift();
selections.push( new vscode.Selection( pos, matchedOpening.pos ) );
},
'}': pos => {
if ( !unbalancedRangeOpenings.length ) {
return;
}
let matchedOpening = unbalancedRangeOpenings.shift();
selections.push( new vscode.Selection( matchedOpening.pos, pos ) );
}
};
for ( let line of readLines( inContent ) ) {
let matches = execall( collapsedRegexp, line );
lineNumber += 1;
if ( lineNumber > 0 ) {
// So far unix style line ending only.
updatedContent += '\n';
}
if ( matches && matches.length ) {
updatedContent += matches.reduce( ( accumulator, curMatch, curIndex, array ) => {
// Basically what we're doing here is:
// for n we're adding part from **after** n.index till n+1.index (excluding char matched at n).
// For last iteration we want to match all the way to the end of string, thus undefined.
let nextPartStart = curMatch.index + curMatch.match.length,
nextPartEnd = curIndex === array.length - 1 ? undefined : array[ curIndex + 1 ].index,
// Current accumulated length, is the position of caret in updatedContent.
pos = new vscode.Position( lineNumber, accumulator.length );
markerHandlers[ curMatch.match ]( pos );
return accumulator + line.substring( nextPartStart, nextPartEnd );
}, line.substr( 0, matches[ 0 ].index ) );
} else {
updatedContent += line;
}
}
ret.content = updatedContent;
return ret;
};
setContent.withSelection = function( content, options ) {
let parsedContent = setContent._extractSelections( content );
return setContent( parsedContent.content, options )
.then( editor => {
if ( parsedContent.selections.length ) {
// Set the selections only if we picked some, it's not recommended to set editor.selections to an
// empty array.
editor.selections = parsedContent.selections;
}
// Make the selections.
return editor;
} );
};
module.exports = setContent;