@@ -3,6 +3,7 @@ import * as path from "node:path";
33import * as fs from "node:fs" ;
44import * as os from "node:os" ;
55import { execSync } from "node:child_process" ;
6+ import { get_configuration , get_configuration_with_scope } from "./vscode_utils" ;
67
78export function get_editor_data_dir ( ) : string {
89 // from: https://stackoverflow.com/a/26227660
@@ -18,9 +19,80 @@ export function get_editor_data_dir(): string {
1819let projectDir : string | undefined = undefined ;
1920let 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 ) {
0 commit comments