Skip to content
This repository was archived by the owner on Nov 18, 2022. It is now read-only.

Commit 60312e3

Browse files
committed
move nearestParentWorkspace into workspace_util
1 parent c802552 commit 60312e3

File tree

2 files changed

+46
-34
lines changed

2 files changed

+46
-34
lines changed

src/extension.ts

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
ServerOptions,
2222
} from 'vscode-languageclient';
2323

24+
import * as workspace_util from './workspace_util';
2425
import { RLSConfiguration } from './configuration';
2526
import { SignatureHelpProvider } from './providers/signatureHelpProvider';
2627
import { checkForRls, ensureToolchain, rustupUpdate } from './rustup';
@@ -123,40 +124,6 @@ function sortedWorkspaceFolders(): string[] {
123124
return _sortedWorkspaceFolders || [];
124125
}
125126

126-
function getCargoTomlWorkspace(
127-
curWorkspace: WorkspaceFolder,
128-
filePath: string,
129-
): WorkspaceFolder {
130-
if (!curWorkspace) {
131-
return curWorkspace;
132-
}
133-
134-
const workspaceRoot = path.parse(curWorkspace.uri.fsPath).dir;
135-
const rootManifest = path.join(workspaceRoot, 'Cargo.toml');
136-
if (fs.existsSync(rootManifest)) {
137-
return curWorkspace;
138-
}
139-
140-
let current = filePath;
141-
142-
while (true) {
143-
const old = current;
144-
current = path.dirname(current);
145-
if (old === current) {
146-
break;
147-
}
148-
if (workspaceRoot === path.parse(current).dir) {
149-
break;
150-
}
151-
152-
const cargoPath = path.join(current, 'Cargo.toml');
153-
if (fs.existsSync(cargoPath)) {
154-
return { ...curWorkspace, uri: Uri.parse(current) };
155-
}
156-
}
157-
158-
return curWorkspace;
159-
}
160127

161128
function getOuterMostWorkspaceFolder(folder: WorkspaceFolder): WorkspaceFolder {
162129
const sorted = sortedWorkspaceFolders();

src/workspace_util.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import * as fs from 'fs';
2+
import * as path from 'path';
3+
import { WorkspaceFolder, Uri } from 'vscode';
4+
5+
6+
7+
// searches up the folder structure until it finds a Cargo.toml
8+
export function nearestParentWorkspace(
9+
curWorkspace: WorkspaceFolder,
10+
filePath: string,
11+
): WorkspaceFolder {
12+
13+
14+
if (!curWorkspace) {
15+
return curWorkspace;
16+
}
17+
18+
const workspaceRoot = path.parse(curWorkspace.uri.fsPath).dir;
19+
const rootManifest = path.join(workspaceRoot, 'Cargo.toml');
20+
if (fs.existsSync(rootManifest)) {
21+
return curWorkspace;
22+
}
23+
24+
let current = filePath;
25+
26+
while (true) {
27+
const old = current;
28+
current = path.dirname(current);
29+
if (old === current) {
30+
break;
31+
}
32+
if (workspaceRoot === path.parse(current).dir) {
33+
break;
34+
}
35+
36+
const cargoPath = path.join(current, 'Cargo.toml');
37+
if (fs.existsSync(cargoPath)) {
38+
return { ...curWorkspace, uri: Uri.parse(current) };
39+
}
40+
}
41+
42+
return curWorkspace;
43+
}
44+
45+

0 commit comments

Comments
 (0)