Skip to content

Commit 6868cf3

Browse files
committed
Add setting to override project directory location
1 parent 45db62b commit 6868cf3

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,11 @@
375375
"type": "boolean",
376376
"default": true,
377377
"description": "Whether to enable inlay hints in GDResource (.tscn, .tres, etc) files"
378+
},
379+
"godotTools.workspace.overrideProjectDir": {
380+
"type": "string",
381+
"default": "",
382+
"description": "Absolute path to a directory with a project.godot file. If set, this will override the auto-detected project directory."
378383
}
379384
}
380385
},

src/utils/godot_utils.ts

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as path from "node:path";
33
import * as fs from "node:fs";
44
import * as os from "node:os";
55
import { execSync } from "node:child_process";
6+
import { get_configuration, get_configuration_with_scope } from "./vscode_utils";
67

78
export function get_editor_data_dir(): string {
89
// from: https://stackoverflow.com/a/26227660
@@ -18,9 +19,80 @@ export function get_editor_data_dir(): string {
1819
let projectDir: string | undefined = undefined;
1920
let projectFile: string | undefined = undefined;
2021

21-
export async function get_project_dir(): Promise<string | undefined> {
22-
let file = "";
22+
// Returns the workspace folder that contains the currently opened file, or undefined if no file is opened
23+
async function get_current_workspace_folder(): Promise<vscode.WorkspaceFolder | undefined> {
24+
if (vscode.workspace.workspaceFolders === undefined) {
25+
return undefined;
26+
}
27+
const editor = vscode.window.activeTextEditor;
28+
if (editor) {
29+
const open_file = editor.document.uri.fsPath;
30+
for (const folder of vscode.workspace.workspaceFolders) {
31+
if (open_file.startsWith(folder.uri.fsPath)) {
32+
return folder;
33+
}
34+
}
35+
}
36+
return undefined;
37+
}
38+
39+
function substitute_variables(file: string, folder: vscode.WorkspaceFolder | undefined): string {
40+
if (file.includes("${userHome}")) {
41+
file = file.replace("${userHome}", os.homedir());
42+
}
43+
if (file.includes("${fileWorkspaceFolder}")) {
44+
file = file.replace("${fileWorkspaceFolder}", folder.uri.fsPath);
45+
}
46+
if (file.includes("${workspaceFolder}")) {
47+
file = file.replace("${workspaceFolder}", folder.uri.fsPath);
48+
}
49+
if (file.includes("${workspaceFolderBasename}")) {
50+
file = file.replace("${workspaceFolderBasename}", folder.name);
51+
}
52+
if (!path.isAbsolute(file)) {
53+
file = path.join(folder.uri.fsPath, file);
54+
}
55+
return file;
56+
}
57+
58+
async function get_override_project_file(): Promise<string | undefined> {
59+
if (!get_configuration("workspace.overrideProjectDir")) {
60+
return undefined;
61+
}
2362
if (vscode.workspace.workspaceFolders !== undefined) {
63+
const current_folder = await get_current_workspace_folder();
64+
if (current_folder) {
65+
let file = get_configuration_with_scope("workspace.overrideProjectDir", current_folder);
66+
if (file) {
67+
if (!file.endsWith("project.godot")) {
68+
file = path.join(file, "project.godot");
69+
}
70+
file = substitute_variables(file, current_folder);
71+
if (fs.existsSync(file) && fs.statSync(file).isFile()) {
72+
return file;
73+
}
74+
}
75+
}
76+
for (const folder of vscode.workspace.workspaceFolders) {
77+
let file = get_configuration_with_scope("workspace.overrideProjectDir", folder);
78+
if (file) {
79+
if (!file.endsWith("project.godot")) {
80+
file = path.join(file, "project.godot");
81+
}
82+
file = substitute_variables(file, folder);
83+
if (fs.existsSync(file) && fs.statSync(file).isFile()) {
84+
return file;
85+
}
86+
}
87+
}
88+
}
89+
90+
return undefined;
91+
}
92+
93+
export async function get_project_dir(): Promise<string | undefined> {
94+
let file = await get_override_project_file();
95+
if (!file && vscode.workspace.workspaceFolders !== undefined) {
2496
const files = await vscode.workspace.findFiles("**/project.godot", null);
2597

2698
if (files.length === 0) {

src/utils/vscode_utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ export function get_configuration(name: string, defaultValue?: any) {
1111
return configValue;
1212
}
1313

14+
export function get_configuration_with_scope(name: string, scope: vscode.ConfigurationScope, defaultValue?: any) {
15+
const configValue = vscode.workspace.getConfiguration(EXTENSION_PREFIX, scope).get(name, null);
16+
if (defaultValue && configValue === null) {
17+
return defaultValue;
18+
}
19+
return configValue;
20+
}
21+
22+
1423
export function set_configuration(name: string, value: any) {
1524
return vscode.workspace.getConfiguration(EXTENSION_PREFIX).update(name, value);
1625
}

0 commit comments

Comments
 (0)