|
1 | 1 | import * as fs from 'fs';
|
2 | 2 | import * as path from 'path';
|
3 |
| -import { WorkspaceFolder, Uri } from 'vscode'; |
4 |
| - |
5 |
| - |
| 3 | +import { Uri, WorkspaceFolder } from 'vscode'; |
6 | 4 |
|
7 | 5 | // searches up the folder structure until it finds a Cargo.toml
|
8 | 6 | export function nearestParentWorkspace(
|
9 |
| - curWorkspace: WorkspaceFolder, |
10 |
| - filePath: string, |
11 |
| - ): WorkspaceFolder { |
12 |
| - |
13 |
| - // check that the workspace folder already contains the "Cargo.toml" |
14 |
| - const workspaceRoot = path.parse(curWorkspace.uri.fsPath).dir; |
15 |
| - const rootManifest = path.join(workspaceRoot, 'Cargo.toml'); |
16 |
| - if (fs.existsSync(rootManifest)) { |
17 |
| - return curWorkspace; |
18 |
| - } |
19 |
| - |
20 |
| - // algorithm that will strip one folder at a time and check if that folder contains "Cargo.toml" |
21 |
| - let current = filePath; |
22 |
| - while (true) { |
23 |
| - const old = current; |
24 |
| - current = path.dirname(current); |
25 |
| - |
26 |
| - // break in case there is a bug that could result in a busy loop |
27 |
| - if (old === current) { |
28 |
| - break; |
29 |
| - } |
| 7 | + curWorkspace: WorkspaceFolder, |
| 8 | + filePath: string, |
| 9 | +): WorkspaceFolder { |
| 10 | + // check that the workspace folder already contains the "Cargo.toml" |
| 11 | + const workspaceRoot = path.parse(curWorkspace.uri.fsPath).dir; |
| 12 | + const rootManifest = path.join(workspaceRoot, 'Cargo.toml'); |
| 13 | + if (fs.existsSync(rootManifest)) { |
| 14 | + return curWorkspace; |
| 15 | + } |
30 | 16 |
|
31 |
| - // break in case the strip folder has not changed |
32 |
| - if (workspaceRoot === path.parse(current).dir) { |
33 |
| - break; |
34 |
| - } |
| 17 | + // algorithm that will strip one folder at a time and check if that folder contains "Cargo.toml" |
| 18 | + let current = filePath; |
| 19 | + while (true) { |
| 20 | + const old = current; |
| 21 | + current = path.dirname(current); |
35 | 22 |
|
36 |
| - // check if "Cargo.toml" is present in the parent folder |
37 |
| - const cargoPath = path.join(current, 'Cargo.toml'); |
38 |
| - if (fs.existsSync(cargoPath)) { |
| 23 | + // break in case there is a bug that could result in a busy loop |
| 24 | + if (old === current) { |
| 25 | + break; |
| 26 | + } |
39 | 27 |
|
40 |
| - // ghetto change the uri on Workspace folder to make vscode think it's located elsewhere |
41 |
| - return { ...curWorkspace, uri: Uri.parse(current) }; |
42 |
| - } |
| 28 | + // break in case the strip folder has not changed |
| 29 | + if (workspaceRoot === path.parse(current).dir) { |
| 30 | + break; |
43 | 31 | }
|
44 | 32 |
|
45 |
| - return curWorkspace; |
| 33 | + // check if "Cargo.toml" is present in the parent folder |
| 34 | + const cargoPath = path.join(current, 'Cargo.toml'); |
| 35 | + if (fs.existsSync(cargoPath)) { |
| 36 | + // ghetto change the uri on Workspace folder to make vscode think it's located elsewhere |
| 37 | + return { ...curWorkspace, uri: Uri.parse(current) }; |
| 38 | + } |
46 | 39 | }
|
47 | 40 |
|
48 |
| - |
| 41 | + return curWorkspace; |
| 42 | +} |
0 commit comments