瀏覽代碼

Add context menu options to copy resource path (#357)

* Add feature
* Fix "open_workspace_with_editor" command
* Added option to tab title context menu
* Add ability to get resource path using command palette
* Update lockfile

Co-authored-by: Hugo Locurcio <[email protected]>
Daelon Suzuka 3 年之前
父節點
當前提交
f860020c63
共有 3 個文件被更改,包括 1891 次插入7 次删除
  1. 1834 1
      package-lock.json
  2. 21 2
      package.json
  3. 36 4
      src/godot-tools.ts

File diff suppressed because it is too large
+ 1834 - 1
package-lock.json


+ 21 - 2
package.json

@@ -92,6 +92,14 @@
 			{
 				"command": "godot-tool.set_scene_file",
 				"title": "Set as Scene File"
+			},
+			{
+				"command": "godot-tool.copy_resource_path_context",
+				"title": "Copy Resource Path"
+			},
+			{
+				"command": "godot-tool.copy_resource_path",
+				"title": "Godot Tools: Copy Resource Path"
 			}
 		],
 		"configuration": {
@@ -188,7 +196,7 @@
 				],
 				"configuration": "./configurations/gdresource-configuration.json"
 			},
-            {
+			{
 				"id": "gdshader",
 				"aliases": [
 					"gdshader"
@@ -197,7 +205,7 @@
 					".gdshader"
 				],
 				"configuration": "./configurations/gdshader-configuration.json"
-            }
+			}
 		],
 		"grammars": [
 			{
@@ -351,6 +359,16 @@
 				{
 					"command": "godot-tool.set_scene_file",
 					"group": "2_workspace"
+				},
+				{
+					"command": "godot-tool.copy_resource_path_context",
+					"group": "6_copypath"
+				}
+			],
+			"editor/title/context": [
+				{
+					"command": "godot-tool.copy_resource_path_context",
+					"group": "1_godot"
 				}
 			]
 		}
@@ -369,6 +387,7 @@
 	"dependencies": {
 		"await-notify": "^1.0.1",
 		"global": "^4.4.0",
+		"klaw": "^4.0.1",
 		"marked": "^4.0.11",
 		"net": "^1.0.2",
 		"terminate": "^2.5.0",

+ 36 - 4
src/godot-tools.ts

@@ -11,8 +11,12 @@ export class GodotTools {
 	private reconnection_attempts = 0;
 	private context: vscode.ExtensionContext;
 	private client: GDScriptLanguageClient = null;
+	// deprecated, need to replace with "vscode.workspace.workspaceFolders", but
+	// that's an array and not a single value
 	private workspace_dir = vscode.workspace.rootPath;
-	private project_file = "project.godot";
+	private project_file_name = "project.godot";
+	private project_file = "";
+	private project_dir = ""
 	private connection_status: vscode.StatusBarItem = null;
 
 	constructor(p_context: vscode.ExtensionContext) {
@@ -38,11 +42,23 @@ export class GodotTools {
 		});
 		vscode.commands.registerCommand("godot-tool.check_status", this.check_client_status.bind(this));
 		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));
 
 		this.connection_status.text = "$(sync) Initializing";
 		this.connection_status.command = "godot-tool.check_status";
 		this.connection_status.show();
 
+		// TODO: maybe cache this result somehow
+		const klaw = require('klaw');
+		klaw(this.workspace_dir)
+			.on('data', item => {
+				if (path.basename(item.path) == this.project_file_name) {
+					this.project_dir = path.dirname(item.path);
+					this.project_file = item.path;
+				}
+			});
+
 		this.reconnection_attempts = 0;
 		this.client.connect_to_server();
 	}
@@ -55,12 +71,12 @@ export class GodotTools {
 
 		return new Promise<void>((resolve, reject) => {
 			let valid = false;
-			if (this.workspace_dir) {
-				let cfg = path.join(this.workspace_dir, this.project_file);
+			if (this.project_dir) {
+				let cfg = this.project_file;
 				valid = (fs.existsSync(cfg) && fs.statSync(cfg).isFile());
 			}
 			if (valid) {
-				this.run_editor(`--path "${this.workspace_dir}" ${params}`).then(() => resolve()).catch(err => {
+				this.run_editor(`--path "${this.project_dir}" ${params}`).then(() => resolve()).catch(err => {
 					reject(err);
 				});
 			} else {
@@ -69,6 +85,22 @@ export class GodotTools {
 		});
 	}
 
+	private copy_resource_path(uri: vscode.Uri) {
+		if (!this.project_dir) {
+			return;
+		}
+        
+		if (!uri) {
+			uri = vscode.window.activeTextEditor.document.uri
+		}
+
+		let relative_path = path.normalize(path.relative(this.project_dir, uri.fsPath));
+		relative_path = relative_path.split(path.sep).join(path.posix.sep);
+		relative_path = 'res://' + relative_path;
+
+		vscode.env.clipboard.writeText(relative_path);
+    }
+
 	private set_scene_file(uri: vscode.Uri) {
 		let right_clicked_scene_path = uri.fsPath
 		let scene_config = get_configuration("scene_file_config");

Some files were not shown because too many files changed in this diff