Skip to content

Commit 8fef307

Browse files
Plugin hook provide workspace symbol (#1118)
* Move symbol creation to symbolUtils * Fix workspaceSymbol handling * Fix tests * Fix lint issues
1 parent ba49d10 commit 8fef307

File tree

11 files changed

+529
-159
lines changed

11 files changed

+529
-159
lines changed

src/LanguageServer.spec.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -969,17 +969,14 @@ describe('LanguageServer', () => {
969969
});
970970

971971
it('should work for nested class as well', async () => {
972-
const nestedNamespace = 'containerNamespace';
973-
const nestedClassName = 'nestedClass';
974-
975972
addScriptFile('nested', `
976-
namespace ${nestedNamespace}
977-
class ${nestedClassName}
978-
function pi()
973+
namespace animals
974+
class dog
975+
function run()
979976
return 3.141592653589793
980977
end function
981978
982-
function buildAwesome()
979+
function speak()
983980
return 42
984981
end function
985982
end class
@@ -991,13 +988,17 @@ describe('LanguageServer', () => {
991988
for (let i = 0; i < 2; i++) {
992989
const symbols = await server['onWorkspaceSymbol']({} as any);
993990
expect(symbols.length).to.equal(4);
994-
expect(symbols[0].name).to.equal(`pi`);
995-
expect(symbols[0].containerName).to.equal(`${nestedNamespace}.${nestedClassName}`);
996-
expect(symbols[1].name).to.equal(`buildAwesome`);
997-
expect(symbols[1].containerName).to.equal(`${nestedNamespace}.${nestedClassName}`);
998-
expect(symbols[2].name).to.equal(`${nestedNamespace}.${nestedClassName}`);
999-
expect(symbols[2].containerName).to.equal(nestedNamespace);
1000-
expect(symbols[3].name).to.equal(nestedNamespace);
991+
expect(
992+
symbols.map(x => ({
993+
name: x.name,
994+
containerName: x.containerName
995+
})).sort((a, b) => a.name.localeCompare(b.name))
996+
).to.eql([
997+
{ name: 'animals', containerName: undefined },
998+
{ name: `dog`, containerName: 'animals' },
999+
{ name: `run`, containerName: 'dog' },
1000+
{ name: 'speak', containerName: 'dog' }
1001+
]);
10011002
}
10021003
});
10031004
});

src/Program.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Scope } from './Scope';
88
import { DiagnosticMessages } from './DiagnosticMessages';
99
import { BrsFile } from './files/BrsFile';
1010
import { XmlFile } from './files/XmlFile';
11-
import type { BsDiagnostic, File, FileReference, FileObj, BscFile, SemanticToken, AfterFileTranspileEvent, FileLink, ProvideHoverEvent, ProvideCompletionsEvent, Hover, ProvideDefinitionEvent, ProvideReferencesEvent, ProvideDocumentSymbolsEvent } from './interfaces';
11+
import type { BsDiagnostic, File, FileReference, FileObj, BscFile, SemanticToken, AfterFileTranspileEvent, FileLink, ProvideHoverEvent, ProvideCompletionsEvent, Hover, ProvideDefinitionEvent, ProvideReferencesEvent, ProvideDocumentSymbolsEvent, ProvideWorkspaceSymbolsEvent } from './interfaces';
1212
import { standardizePath as s, util } from './util';
1313
import { XmlScope } from './XmlScope';
1414
import { DiagnosticFilterer } from './DiagnosticFilterer';
@@ -905,14 +905,14 @@ export class Program {
905905
* Goes through each file and builds a list of workspace symbols for the program. Used by LanguageServer's onWorkspaceSymbol functionality
906906
*/
907907
public getWorkspaceSymbols() {
908-
const results = Object.keys(this.files).map(key => {
909-
const file = this.files[key];
910-
if (isBrsFile(file)) {
911-
return file.getWorkspaceSymbols();
912-
}
913-
return [];
914-
});
915-
return util.flatMap(results, c => c);
908+
const event: ProvideWorkspaceSymbolsEvent = {
909+
program: this,
910+
workspaceSymbols: []
911+
};
912+
this.plugins.emit('beforeProvideWorkspaceSymbols', event);
913+
this.plugins.emit('provideWorkspaceSymbols', event);
914+
this.plugins.emit('afterProvideWorkspaceSymbols', event);
915+
return event.workspaceSymbols;
916916
}
917917

918918
/**
@@ -961,6 +961,10 @@ export class Program {
961961
return result ?? [];
962962
}
963963

964+
/**
965+
* Get full list of document symbols for a file
966+
* @param srcPath path to the file
967+
*/
964968
public getDocumentSymbols(srcPath: string): DocumentSymbol[] | undefined {
965969
let file = this.getFile(srcPath);
966970
if (file) {

src/bscPlugin/BscPlugin.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { isBrsFile, isXmlFile } from '../astUtils/reflection';
2-
import type { BeforeFileTranspileEvent, CompilerPlugin, OnFileValidateEvent, OnGetCodeActionsEvent, ProvideHoverEvent, OnGetSemanticTokensEvent, OnScopeValidateEvent, ProvideCompletionsEvent, ProvideDefinitionEvent, ProvideReferencesEvent, ProvideDocumentSymbolsEvent } from '../interfaces';
2+
import type { BeforeFileTranspileEvent, CompilerPlugin, OnFileValidateEvent, OnGetCodeActionsEvent, ProvideHoverEvent, OnGetSemanticTokensEvent, OnScopeValidateEvent, ProvideCompletionsEvent, ProvideDefinitionEvent, ProvideReferencesEvent, ProvideDocumentSymbolsEvent, ProvideWorkspaceSymbolsEvent } from '../interfaces';
33
import type { Program } from '../Program';
44
import { CodeActionsProcessor } from './codeActions/CodeActionsProcessor';
55
import { CompletionsProcessor } from './completions/CompletionsProcessor';
66
import { DefinitionProvider } from './definition/DefinitionProvider';
7-
import { DocumentSymbolProcessor } from './documentSymbol/DocumentSymbolProcessor';
7+
import { DocumentSymbolProcessor } from './symbols/DocumentSymbolProcessor';
88
import { HoverProcessor } from './hover/HoverProcessor';
99
import { ReferencesProvider } from './references/ReferencesProvider';
1010
import { BrsFileSemanticTokensProcessor } from './semanticTokens/BrsFileSemanticTokensProcessor';
@@ -13,6 +13,7 @@ import { BrsFileValidator } from './validation/BrsFileValidator';
1313
import { ProgramValidator } from './validation/ProgramValidator';
1414
import { ScopeValidator } from './validation/ScopeValidator';
1515
import { XmlFileValidator } from './validation/XmlFileValidator';
16+
import { WorkspaceSymbolProcessor } from './symbols/WorkspaceSymbolProcessor';
1617

1718
export class BscPlugin implements CompilerPlugin {
1819
public name = 'BscPlugin';
@@ -29,6 +30,10 @@ export class BscPlugin implements CompilerPlugin {
2930
return new DocumentSymbolProcessor(event).process();
3031
}
3132

33+
public provideWorkspaceSymbols(event: ProvideWorkspaceSymbolsEvent) {
34+
return new WorkspaceSymbolProcessor(event).process();
35+
}
36+
3237
public provideCompletions(event: ProvideCompletionsEvent) {
3338
new CompletionsProcessor(event).process();
3439
}

src/bscPlugin/documentSymbol/DocumentSymbolProcessor.ts

Lines changed: 0 additions & 77 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { isBrsFile } from '../../astUtils/reflection';
2+
import type { BrsFile } from '../../files/BrsFile';
3+
import type { ProvideDocumentSymbolsEvent } from '../../interfaces';
4+
import { getDocumentSymbolsFromStatement } from './symbolUtils';
5+
6+
export class DocumentSymbolProcessor {
7+
public constructor(
8+
public event: ProvideDocumentSymbolsEvent
9+
) {
10+
11+
}
12+
13+
public process() {
14+
if (isBrsFile(this.event.file)) {
15+
return this.getBrsFileDocumentSymbols(this.event.file);
16+
}
17+
}
18+
19+
private getBrsFileDocumentSymbols(file: BrsFile) {
20+
for (const statement of file.ast.statements) {
21+
const symbol = getDocumentSymbolsFromStatement(statement);
22+
if (symbol) {
23+
this.event.documentSymbols.push(...symbol);
24+
}
25+
}
26+
return this.event.documentSymbols;
27+
}
28+
}

0 commit comments

Comments
 (0)