@@ -42,7 +42,7 @@ interface TaskProperties {
42
42
* or '' if not found.
43
43
*/
44
44
const limitSubp = async ( ) : Promise < string > => {
45
- return getSubprogramSymbol ( vscode . window . activeTextEditor ) . then ( ( Symbol ) => {
45
+ return getEnclosingSymbol ( vscode . window . activeTextEditor , [ SymbolKind . Function ] ) . then ( ( Symbol ) => {
46
46
if ( Symbol ) {
47
47
const subprogram_line : string = ( Symbol . range . start . line + 1 ) . toString ( ) ;
48
48
return `--limit-subp=\${fileBasename}:${ subprogram_line } ` ;
@@ -282,16 +282,16 @@ async function getTasks(): Promise<vscode.Task[]> {
282
282
}
283
283
284
284
/**
285
- * Return the DocumentSymbol associated to the subprogram enclosing the
285
+ * Return the closest DocumentSymbol of the given kinds enclosing the
286
286
* the given editor's cursor position, if any.
287
287
* @param editor - The editor in which we want
288
- * to find the suprogram's body enclosing the cursor's position.
289
- * @returns Return the symbol corresponding to the
290
- * enclosing subprogram or null if not found.
288
+ * to find the closest symbol enclosing the cursor's position.
289
+ * @returns Return the closest enclosing symbol.
291
290
*/
292
- export const getSubprogramSymbol = async (
293
- editor : vscode . TextEditor | undefined
294
- ) : Promise < vscode . DocumentSymbol | null > => {
291
+ export async function getEnclosingSymbol (
292
+ editor : vscode . TextEditor | undefined ,
293
+ kinds : vscode . SymbolKind [ ]
294
+ ) : Promise < vscode . DocumentSymbol | null > {
295
295
if ( editor ) {
296
296
const line = editor . selection . active . line ;
297
297
@@ -301,25 +301,25 @@ export const getSubprogramSymbol = async (
301
301
editor . document . uri
302
302
) ;
303
303
304
- // Then select all subprograms
305
- const subprograms : vscode . DocumentSymbol [ ] = [ ] ;
304
+ // Then filter them according to the specified kinds
305
+ const filtered_symbols : vscode . DocumentSymbol [ ] = [ ] ;
306
306
307
- const getAllSubprograms = ( symbols : vscode . DocumentSymbol [ ] ) => {
307
+ const getAllSymbols = ( symbols : vscode . DocumentSymbol [ ] ) => {
308
308
let sym ;
309
309
for ( sym of symbols ) {
310
- if ( sym . kind == SymbolKind . Function ) {
311
- subprograms . push ( sym ) ;
310
+ if ( sym . kind in kinds ) {
311
+ filtered_symbols . push ( sym ) ;
312
312
}
313
313
if ( sym . kind == SymbolKind . Function || sym . kind == SymbolKind . Module ) {
314
- getAllSubprograms ( sym . children ) ;
314
+ getAllSymbols ( sym . children ) ;
315
315
}
316
316
}
317
317
} ;
318
318
319
- getAllSubprograms ( symbols ) ;
319
+ getAllSymbols ( symbols ) ;
320
320
321
- // Finally select from the subprograms the smallest one containing the current line
322
- const scopeSymbols = subprograms . filter (
321
+ // Finally select from the filtered symbols the smallest one containing the current line
322
+ const scopeSymbols = filtered_symbols . filter (
323
323
( sym ) => line >= sym . range . start . line && line <= sym . range . end . line
324
324
) ;
325
325
if ( scopeSymbols . length > 0 ) {
0 commit comments