@@ -12,32 +12,43 @@ import { IFeature } from "../feature";
12
12
13
13
const rangeLimit = 5000 ;
14
14
15
- // https://github.com/Microsoft/vscode/issues/46281
16
15
const tm = getCoreNodeModule ( "vscode-textmate" ) ;
17
- /**
18
- * Returns a node module installed with VSCode, or null if it fails.
19
- */
16
+
17
+ // Returns a node module installed with VSCode, or null if it fails.
18
+ // https://github.com/Microsoft/vscode/issues/46281
20
19
function getCoreNodeModule ( moduleName : string ) {
21
20
try {
22
21
return require ( `${ vscode . env . appRoot } /node_modules.asar/${ moduleName } ` ) ;
22
+ // tslint:disable-next-line:no-empty
23
23
} catch ( err ) { }
24
24
25
25
try {
26
26
return require ( `${ vscode . env . appRoot } /node_modules/${ moduleName } ` ) ;
27
+ // tslint:disable-next-line:no-empty
27
28
} catch ( err ) { }
28
29
29
30
return null ;
30
31
}
31
32
32
33
interface IExtensionGrammar {
33
- language ?: string ; scopeName ?: string ; path ?: string ; embeddedLanguages ?: { [ scopeName : string ] : string } ; injectTo ?: string [ ] ;
34
+ language ?: string ;
35
+ scopeName ?: string ;
36
+ path ?: string ;
37
+ embeddedLanguages ?: {
38
+ [ scopeName : string ] : string ,
39
+ } ; injectTo ?: string [ ] ;
40
+ }
41
+
42
+ interface IExtensionLanguage {
43
+ id : string ;
44
+ configuration : string ;
34
45
}
35
46
36
47
interface IExtensionPackage {
37
48
contributes ?: {
38
- languages ?: { id : string ; configuration : string } [ ] ,
49
+ languages ?: IExtensionLanguage [ ] ,
39
50
grammars ?: IExtensionGrammar [ ] ,
40
- }
51
+ } ;
41
52
}
42
53
43
54
// Need to reproduce the IToken interface from vscode-textmate due to
@@ -141,7 +152,11 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
141
152
return foldingRanges ;
142
153
}
143
154
144
- private matchScopeElements ( tokens , startScopeName : string , endScopeName : string , matchType : MatchType , document : vscode . TextDocument ) : IMatchedTokenList {
155
+ private matchScopeElements ( tokens ,
156
+ startScopeName : string ,
157
+ endScopeName : string ,
158
+ matchType : MatchType ,
159
+ document : vscode . TextDocument ) : IMatchedTokenList {
145
160
const result = [ ] ;
146
161
const tokenStack = [ ] ;
147
162
@@ -157,7 +172,10 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
157
172
return result . reverse ( ) ;
158
173
}
159
174
160
- private matchContiguousScopeElements ( tokens , scopeName : string , matchType : MatchType , document ) {
175
+ private matchContiguousScopeElements ( tokens ,
176
+ scopeName : string ,
177
+ matchType : MatchType ,
178
+ document : vscode . TextDocument ) {
161
179
const result = [ ] ;
162
180
let startToken ;
163
181
@@ -178,19 +196,22 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
178
196
}
179
197
180
198
// Given a zero based offset, find the line text preceeding it
181
- private preceedingText ( offset : number , document : vscode . TextDocument ) {
199
+ private preceedingText ( offset : number ,
200
+ document : vscode . TextDocument ) : string {
182
201
const endPos = document . positionAt ( offset ) ;
183
202
const startPos = endPos . translate ( 0 , - endPos . character ) ;
184
203
185
204
return document . getText ( new vscode . Range ( startPos , endPos ) ) ;
186
205
}
187
206
188
207
// Given a zero based offset, return the line number
189
- private lineAtOffest ( offset , document ) : number {
208
+ private lineAtOffest ( offset : number ,
209
+ document : vscode . TextDocument ) : number {
190
210
return document . positionAt ( offset ) . line ;
191
211
}
192
212
193
- private matchBlockCommentScopeElements ( tokens , document : vscode . TextDocument ) {
213
+ private matchBlockCommentScopeElements ( tokens ,
214
+ document : vscode . TextDocument ) {
194
215
const result = [ ] ;
195
216
196
217
const emptyLine = new RegExp ( "^[\\s]+$" ) ;
@@ -214,7 +235,12 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
214
235
if ( lineNum === nextLine ) {
215
236
nextLine = lineNum + 1 ;
216
237
} else {
217
- result . push ( new MatchedToken ( null , null , startLine , nextLine - 1 , MatchType . Comment , document ) ) ;
238
+ result . push ( new MatchedToken ( null ,
239
+ null ,
240
+ startLine ,
241
+ nextLine - 1 ,
242
+ MatchType . Comment ,
243
+ document ) ) ;
218
244
startLine = lineNum ;
219
245
nextLine = lineNum + 1 ;
220
246
}
@@ -230,23 +256,90 @@ export class FoldingProvider implements vscode.FoldingRangeProvider {
230
256
return result ;
231
257
}
232
258
259
+ // Given a zero based offset, find the line text after it
260
+ private subsequentText ( offset : number ,
261
+ document : vscode . TextDocument ) : string {
262
+ const startPos = document . positionAt ( offset ) ;
263
+ // We don't know how long the line is so just return a really long one.
264
+ const endPos = startPos . translate ( 0 , 1000 ) ;
265
+ return document . getText ( new vscode . Range ( startPos , endPos ) ) ;
266
+ }
267
+
268
+ // Create a new token object with an appended scopeName
269
+ private addTokenScope ( token ,
270
+ scopeName : string ) {
271
+ // Only a shallow clone is required
272
+ const tokenClone = Object . assign ( { } , token ) ;
273
+ tokenClone . scopes . push ( scopeName ) ;
274
+ return tokenClone ;
275
+ }
276
+
277
+ private extractRegionScopeElements ( tokens ,
278
+ document : vscode . TextDocument ) {
279
+ const result = [ ] ;
280
+
281
+ const emptyLine = new RegExp ( "^[\\s]+$" ) ;
282
+ const startRegionText = new RegExp ( "^#\\s*region\\b" ) ;
283
+ const endRegionText = new RegExp ( "^#\\s*endregion\\b" ) ;
284
+
285
+ tokens . forEach ( ( token , index ) => {
286
+ if ( token . scopes . includes ( "punctuation.definition.comment.powershell" ) ) {
287
+ if ( emptyLine . test ( this . preceedingText ( token . startIndex , document ) ) ) {
288
+ const commentText = this . subsequentText ( token . startIndex , document ) ;
289
+
290
+ if ( startRegionText . test ( commentText ) ) { result . push ( this . addTokenScope ( token , "custom.start.region" ) ) ; }
291
+ if ( endRegionText . test ( commentText ) ) { result . push ( this . addTokenScope ( token , "custom.end.region" ) ) ; }
292
+ }
293
+ }
294
+ } ) ;
295
+ return result ;
296
+ }
297
+
233
298
private matchGrammarTokens ( tokens , document : vscode . TextDocument ) : IMatchedTokenList {
234
299
const matchedTokens = [ ] ;
235
300
236
301
// Find matching Braces { -> }
237
- this . matchScopeElements ( tokens , "punctuation.section.braces.begin.powershell" , "punctuation.section.braces.end.powershell" , MatchType . Region , document ) . forEach ( ( x ) => { matchedTokens . push ( x ) ; } ) ;
238
- // Find matching brackets ( -> )
239
- this . matchScopeElements ( tokens , "punctuation.section.group.begin.powershell" , "punctuation.section.group.end.powershell" , MatchType . Region , document ) . forEach ( ( x ) => { matchedTokens . push ( x ) ; } ) ;
240
-
241
- // Find contiguous here strings @' -> '@ and @" -> "@
242
- this . matchContiguousScopeElements ( tokens , "string.quoted.single.heredoc.powershell" , MatchType . Region , document ) . forEach ( ( x ) => { matchedTokens . push ( x ) ; } ) ;
243
- this . matchContiguousScopeElements ( tokens , "string.quoted.double.heredoc.powershell" , MatchType . Region , document ) . forEach ( ( x ) => { matchedTokens . push ( x ) ; } ) ;
302
+ this . matchScopeElements ( tokens ,
303
+ "punctuation.section.braces.begin.powershell" ,
304
+ "punctuation.section.braces.end.powershell" ,
305
+ MatchType . Region , document )
306
+ . forEach ( ( x ) => { matchedTokens . push ( x ) ; } ) ;
244
307
245
- // Find blocks of line comments # foo
308
+ // Find matching brackets ( -> )
309
+ this . matchScopeElements ( tokens ,
310
+ "punctuation.section.group.begin.powershell" ,
311
+ "punctuation.section.group.end.powershell" ,
312
+ MatchType . Region , document )
313
+ . forEach ( ( x ) => { matchedTokens . push ( x ) ; } ) ;
314
+
315
+ // Find contiguous here strings @' -> '@
316
+ this . matchContiguousScopeElements ( tokens ,
317
+ "string.quoted.single.heredoc.powershell" ,
318
+ MatchType . Region , document )
319
+ . forEach ( ( x ) => { matchedTokens . push ( x ) ; } ) ;
320
+
321
+ // Find contiguous here strings @" -> "@
322
+ this . matchContiguousScopeElements ( tokens ,
323
+ "string.quoted.double.heredoc.powershell" ,
324
+ MatchType . Region , document )
325
+ . forEach ( ( x ) => { matchedTokens . push ( x ) ; } ) ;
326
+
327
+ // Find matching Braces #region -> #endregion
328
+ this . matchScopeElements ( this . extractRegionScopeElements ( tokens , document ) ,
329
+ "custom.start.region" ,
330
+ "custom.end.region" ,
331
+ MatchType . Region , document )
332
+ . forEach ( ( x ) => { matchedTokens . push ( x ) ; } ) ;
333
+
334
+ // Find blocks of line comments # comment1\n# comment2\n...
246
335
this . matchBlockCommentScopeElements ( tokens , document ) . forEach ( ( x ) => { matchedTokens . push ( x ) ; } ) ;
247
336
248
337
// Find matching function definitions <# -> #>
249
- this . matchScopeElements ( tokens , "punctuation.definition.comment.block.begin.powershell" , "punctuation.definition.comment.block.end.powershell" , MatchType . Comment , document ) . forEach ( ( x ) => { matchedTokens . push ( x ) ; } ) ;
338
+ this . matchScopeElements ( tokens ,
339
+ "punctuation.definition.comment.block.begin.powershell" ,
340
+ "punctuation.definition.comment.block.end.powershell" ,
341
+ MatchType . Comment , document )
342
+ . forEach ( ( x ) => { matchedTokens . push ( x ) ; } ) ;
250
343
251
344
return matchedTokens ;
252
345
}
@@ -286,12 +379,9 @@ export class FoldingFeature implements IFeature {
286
379
this . foldingProvider ) ;
287
380
}
288
381
289
- public dispose ( ) : any {
290
- // Should be empty
291
- }
382
+ // tslint:disable-next-line:no-empty
383
+ public dispose ( ) : any { }
292
384
293
- public setLanguageClient ( languageclient : LanguageClient ) : void {
294
- this . languageClient = languageclient ;
295
- // this.foldingProvider.setLanguageClient(languageclient);
296
- }
385
+ // tslint:disable-next-line:no-empty
386
+ public setLanguageClient ( languageclient : LanguageClient ) : void { }
297
387
}
0 commit comments