Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'editors/code/src/ctx.ts')
| -rw-r--r-- | editors/code/src/ctx.ts | 69 |
1 files changed, 68 insertions, 1 deletions
diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index 7b5cc9f106..5550bfa655 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts @@ -19,6 +19,7 @@ import { RustDependenciesProvider, type DependencyId, } from "./dependencies_provider"; +import { SyntaxTreeProvider, type SyntaxElement } from "./syntax_tree_provider"; import { execRevealDependency } from "./commands"; import { PersistentState } from "./persistent_state"; import { bootstrap } from "./bootstrap"; @@ -85,7 +86,11 @@ export class Ctx implements RustAnalyzerExtensionApi { private commandDisposables: Disposable[]; private unlinkedFiles: vscode.Uri[]; private _dependenciesProvider: RustDependenciesProvider | undefined; - private _dependencyTreeView: vscode.TreeView<Dependency | DependencyFile | DependencyId> | undefined; + private _dependencyTreeView: + | vscode.TreeView<Dependency | DependencyFile | DependencyId> + | undefined; + private _syntaxTreeProvider: SyntaxTreeProvider | undefined; + private _syntaxTreeView: vscode.TreeView<SyntaxElement> | undefined; private lastStatus: ServerStatusParams | { health: "stopped" } = { health: "stopped" }; private _serverVersion: string; private statusBarActiveEditorListener: Disposable; @@ -110,6 +115,14 @@ export class Ctx implements RustAnalyzerExtensionApi { return this._dependenciesProvider; } + get syntaxTreeView() { + return this._syntaxTreeView; + } + + get syntaxTreeProvider() { + return this._syntaxTreeProvider; + } + constructor( readonly extCtx: vscode.ExtensionContext, commandFactories: Record<string, CommandFactory>, @@ -278,6 +291,9 @@ export class Ctx implements RustAnalyzerExtensionApi { if (this.config.showDependenciesExplorer) { this.prepareTreeDependenciesView(client); } + if (this.config.showSyntaxTree) { + this.prepareSyntaxTreeView(client); + } } private prepareTreeDependenciesView(client: lc.LanguageClient) { @@ -326,6 +342,56 @@ export class Ctx implements RustAnalyzerExtensionApi { ); } + private prepareSyntaxTreeView(client: lc.LanguageClient) { + const ctxInit: CtxInit = { + ...this, + client: client, + }; + this._syntaxTreeProvider = new SyntaxTreeProvider(ctxInit); + this._syntaxTreeView = vscode.window.createTreeView("rustSyntaxTree", { + treeDataProvider: this._syntaxTreeProvider, + showCollapseAll: true, + }); + + this.pushExtCleanup(this._syntaxTreeView); + + vscode.window.onDidChangeActiveTextEditor(async () => { + if (this.syntaxTreeView?.visible) { + await this.syntaxTreeProvider?.refresh(); + } + }); + + vscode.workspace.onDidChangeTextDocument(async () => { + if (this.syntaxTreeView?.visible) { + await this.syntaxTreeProvider?.refresh(); + } + }); + + vscode.window.onDidChangeTextEditorSelection(async (e) => { + if (!this.syntaxTreeView?.visible || !isRustEditor(e.textEditor)) { + return; + } + + const selection = e.selections[0]; + if (selection === undefined) { + return; + } + + const start = e.textEditor.document.offsetAt(selection.start); + const end = e.textEditor.document.offsetAt(selection.end); + const result = this.syntaxTreeProvider?.getElementByRange(start, end); + if (result !== undefined) { + await this.syntaxTreeView?.reveal(result); + } + }); + + this._syntaxTreeView.onDidChangeVisibility(async (e) => { + if (e.visible) { + await this.syntaxTreeProvider?.refresh(); + } + }); + } + async restart() { // FIXME: We should re-use the client, that is ctx.deactivate() if none of the configs have changed await this.stopAndDispose(); @@ -424,6 +490,7 @@ export class Ctx implements RustAnalyzerExtensionApi { statusBar.command = "rust-analyzer.openLogs"; } this.dependenciesProvider?.refresh(); + void this.syntaxTreeProvider?.refresh(); break; case "warning": statusBar.color = new vscode.ThemeColor("statusBarItem.warningForeground"); |