From e7674c12adef55d13d17ee66b057a3f1789a2102 Mon Sep 17 00:00:00 2001 From: Daelon Suzuka Date: Sun, 21 Aug 2022 17:13:31 -0700 Subject: [PATCH] Add "Open Type Documentation" context menu option (#405) Co-authored-by: Hugo Locurcio --- package.json | 11 +++++++++++ src/godot-tools.ts | 20 ++++++++++++++++++-- src/lsp/GDScriptLanguageClient.ts | 4 ++++ src/lsp/NativeDocumentManager.ts | 9 +++++++++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 323144038..b277435bc 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,10 @@ "command": "godot-tool.list_native_classes", "title": "Godot Tools: List native classes of godot" }, + { + "command": "godot-tool.open_type_documentation", + "title": "Godot Tools: Open Type Documentation" + }, { "command": "godot-tool.debugger.inspect_node", "title": "Inspect Remote Node", @@ -392,6 +396,13 @@ "command": "godot-tool.copy_resource_path_context", "group": "1_godot" } + ], + "editor/context": [ + { + "command": "godot-tool.open_type_documentation", + "when": "godotTools.connectedToEditor", + "group": "navigation@9" + } ] } }, diff --git a/src/godot-tools.ts b/src/godot-tools.ts index 15740d394..8b1ae7dc5 100644 --- a/src/godot-tools.ts +++ b/src/godot-tools.ts @@ -49,6 +49,9 @@ export class GodotTools { vscode.commands.registerCommand("godot-tool.set_scene_file", this.set_scene_file.bind(this)); vscode.commands.registerCommand("godot-tool.copy_resource_path_context", this.copy_resource_path.bind(this)); vscode.commands.registerCommand("godot-tool.copy_resource_path", this.copy_resource_path.bind(this)); + vscode.commands.registerCommand("godot-tool.open_type_documentation", this.open_type_documentation.bind(this)); + + vscode.commands.executeCommand('setContext', 'godotTools.connectedToEditor', false); this.connection_status.text = "$(sync) Initializing"; this.connection_status.command = "godot-tool.check_status"; @@ -94,7 +97,7 @@ export class GodotTools { if (!this.project_dir) { return; } - + if (!uri) { uri = vscode.window.activeTextEditor.document.uri; } @@ -104,7 +107,18 @@ export class GodotTools { relative_path = 'res://' + relative_path; vscode.env.clipboard.writeText(relative_path); - } + } + + private open_type_documentation(uri: vscode.Uri) { + // get word under cursor + const activeEditor = vscode.window.activeTextEditor; + const document = activeEditor.document; + const curPos = activeEditor.selection.active; + const wordRange = document.getWordRangeAtPosition(curPos); + const symbolName = document.getText(wordRange); + + this.client.open_documentation(symbolName); + } private set_scene_file(uri: vscode.Uri) { let right_clicked_scene_path = uri.fsPath; @@ -231,6 +245,7 @@ export class GodotTools { break; case ClientStatus.CONNECTED: this.retry = false; + vscode.commands.executeCommand('setContext', 'godotTools.connectedToEditor', true); this.connection_status.text = `$(check) Connected`; this.connection_status.tooltip = `Connected to the GDScript language server.`; if (!this.client.started) { @@ -242,6 +257,7 @@ export class GodotTools { this.connection_status.text = `$(sync) Connecting ` + this.reconnection_attempts; this.connection_status.tooltip = `Connecting to the GDScript language server...`; } else { + vscode.commands.executeCommand('setContext', 'godotTools.connectedToEditor', false); this.connection_status.text = `$(x) Disconnected`; this.connection_status.tooltip = `Disconnected from the GDScript language server.`; } diff --git a/src/lsp/GDScriptLanguageClient.ts b/src/lsp/GDScriptLanguageClient.ts index 002d15433..422d78545 100644 --- a/src/lsp/GDScriptLanguageClient.ts +++ b/src/lsp/GDScriptLanguageClient.ts @@ -42,6 +42,10 @@ export default class GDScriptLanguageClient extends LanguageClient { } } + public open_documentation(symbolName: string) { + this.native_doc_manager.request_documentation(symbolName); + } + constructor(context: vscode.ExtensionContext) { super( `GDScriptLanguageClient`, diff --git a/src/lsp/NativeDocumentManager.ts b/src/lsp/NativeDocumentManager.ts index 779061486..f10fb97ff 100644 --- a/src/lsp/NativeDocumentManager.ts +++ b/src/lsp/NativeDocumentManager.ts @@ -59,6 +59,15 @@ export default class NativeDocumentManager extends EventEmitter { ); } + public request_documentation(symbolName: string) { + if (symbolName in this.native_classes) { + this.inspect_native_symbol({ + native_class: symbolName, + symbol_name: symbolName, + }); + } + } + private async list_native_classes() { let classname = await vscode.window.showQuickPick( Object.keys(this.native_classes).sort(),