Эх сурвалжийг харах

Add "Open Type Documentation" context menu option (#405)

Co-authored-by: Hugo Locurcio <[email protected]>
Daelon Suzuka 3 жил өмнө
parent
commit
e7674c12ad

+ 11 - 0
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"
+				}
 			]
 		}
 	},

+ 18 - 2
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.`;
 				}

+ 4 - 0
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`,

+ 9 - 0
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(),