We are writing a VSCode extension using the API to find methods in an opened file.
Everything works so far, when we open a new file in the Extension Development Host the methods get found, so the DocumentSymbolProvider fires / executes.
The procedure of the extension would be like this:
- Open File A --> new DocumentSymbolProvider which runs the code to collect all methods from a list of wanted methods
- All found methods from File A get displayed in our extension's side panel
- Open File B --> new DocumentSymbolProvider runs again, now for this file, and collects all methods
- All found methods from File A get replaced by found methods from File B
- [This step doesn't work] Going back to File A --> DocumentSymbolProvider should run its code / collect all methods from this file again, and display them in side panel
- But what our code currently does: No data from File A gets collected, the extension still displays all data from the last 'new' file, in this case File B
But It doesn't fire when we switch back to an already opened file. In order to fire again you have to close the file (tab in vscode) and open it again.
Is there something to implement so that the DocumentSymbolProvider fires also when switching file tabs? We alrerady tried putting
onDidChangeEmitter = new vscode.EventEmitter<vscode.Uri>();
onDidChange = this.onDidChangeEmitter.event;
into the DocumentSymbolProvider so it looks like this:
class JavaDocumentSymbolProvider implements vscode.DocumentSymbolProvider {
onDidChangeEmitter = new vscode.EventEmitter<vscode.Uri>();
onDidChange = this.onDidChangeEmitter.event;
public provideDocumentSymbols(document: vscode.TextDocument, token: vscode.CancellationToken): Thenable<vscode.SymbolInformation[]> {
// code to find methods
}
}
CodePudding user response:
The correct logic should be,
- Hook to file open/change event to generate new symbols (and only keep them in memory).
- Hook to active text editor change event to display the symbols from the newly activated document.
For step 2, you should refer to vscode.window.onDidChangeActiveTextEditor
,
https://code.visualstudio.com/api/references/vscode-api#window