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

Commit a170fc0

Browse files
committed
Move workspace_util.ts under utils/ directory
1 parent 001d117 commit a170fc0

File tree

2 files changed

+49
-43
lines changed

2 files changed

+49
-43
lines changed

src/extension.ts

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ import { checkForRls, ensureToolchain, rustupUpdate } from './rustup';
2828
import { startSpinner, stopSpinner } from './spinner';
2929
import { activateTaskProvider, Execution, runRlsCommand } from './tasks';
3030
import { withWsl } from './utils/child_process';
31+
import {
32+
getOuterMostWorkspaceFolder,
33+
nearestParentWorkspace,
34+
} from './utils/workspace';
3135
import { uriWindowsToWsl, uriWslToWindows } from './utils/wslpath';
32-
import * as workspace_util from './workspace_util';
3336

3437
/**
3538
* Parameter type to `window/progress` request as issued by the RLS.
@@ -82,7 +85,7 @@ function whenOpeningTextDocument(document: TextDocument) {
8285
.get<boolean>('rust-client.nestedMultiRootConfigInOutermost', true);
8386

8487
if (inMultiProjectMode) {
85-
folder = workspace_util.nearestParentWorkspace(folder, document.uri.fsPath);
88+
folder = nearestParentWorkspace(folder, document.uri.fsPath);
8689
} else if (inNestedOuterProjectMode) {
8790
folder = getOuterMostWorkspaceFolder(folder);
8891
}
@@ -105,50 +108,11 @@ function whenOpeningTextDocument(document: TextDocument) {
105108
}
106109
}
107110

108-
// This is an intermediate, lazy cache used by `getOuterMostWorkspaceFolder`
109-
// and cleared when VSCode workspaces change.
110-
let _sortedWorkspaceFolders: string[] | undefined;
111-
112-
function sortedWorkspaceFolders(): string[] {
113-
// TODO: decouple the global state such that it can be moved to workspace_util
114-
if (!_sortedWorkspaceFolders && workspace.workspaceFolders) {
115-
_sortedWorkspaceFolders = workspace.workspaceFolders
116-
.map(folder => {
117-
let result = folder.uri.toString();
118-
if (result.charAt(result.length - 1) !== '/') {
119-
result = result + '/';
120-
}
121-
return result;
122-
})
123-
.sort((a, b) => {
124-
return a.length - b.length;
125-
});
126-
}
127-
return _sortedWorkspaceFolders || [];
128-
}
129-
130-
function getOuterMostWorkspaceFolder(folder: WorkspaceFolder): WorkspaceFolder {
131-
// TODO: decouple the global state such that it can be moved to workspace_util
132-
const sorted = sortedWorkspaceFolders();
133-
for (const element of sorted) {
134-
let uri = folder.uri.toString();
135-
if (uri.charAt(uri.length - 1) !== '/') {
136-
uri = uri + '/';
137-
}
138-
if (uri.startsWith(element)) {
139-
return workspace.getWorkspaceFolder(Uri.parse(element)) || folder;
140-
}
141-
}
142-
return folder;
143-
}
144-
145111
function whenChangingWorkspaceFolders(e: WorkspaceFoldersChangeEvent) {
146-
_sortedWorkspaceFolders = undefined;
147-
148112
// If a VSCode workspace has been added, check to see if it is part of an existing one, and
149113
// if not, and it is a Rust project (i.e., has a Cargo.toml), then create a new client.
150114
for (let folder of e.added) {
151-
folder = getOuterMostWorkspaceFolder(folder);
115+
folder = getOuterMostWorkspaceFolder(folder, { cached: false });
152116
if (workspaces.has(folder.uri.toString())) {
153117
continue;
154118
}

src/workspace_util.ts renamed to src/utils/workspace.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
3-
import { Uri, WorkspaceFolder } from 'vscode';
3+
import { Uri, workspace, WorkspaceFolder } from 'vscode';
44

55
// searches up the folder structure until it finds a Cargo.toml
66
export function nearestParentWorkspace(
@@ -44,3 +44,45 @@ export function nearestParentWorkspace(
4444

4545
return curWorkspace;
4646
}
47+
48+
// This is an intermediate, lazy cache used by `getOuterMostWorkspaceFolder`
49+
// and cleared when VSCode workspaces change.
50+
let _sortedWorkspaceFolders: string[] | undefined;
51+
52+
function sortedWorkspaceFolders(): string[] {
53+
if (!_sortedWorkspaceFolders && workspace.workspaceFolders) {
54+
_sortedWorkspaceFolders = workspace.workspaceFolders
55+
.map(folder => {
56+
let result = folder.uri.toString();
57+
if (result.charAt(result.length - 1) !== '/') {
58+
result = result + '/';
59+
}
60+
return result;
61+
})
62+
.sort((a, b) => {
63+
return a.length - b.length;
64+
});
65+
}
66+
return _sortedWorkspaceFolders || [];
67+
}
68+
69+
export function getOuterMostWorkspaceFolder(
70+
folder: WorkspaceFolder,
71+
options?: { cached: boolean },
72+
): WorkspaceFolder {
73+
if (!options || !options.cached) {
74+
_sortedWorkspaceFolders = undefined;
75+
}
76+
77+
const sorted = sortedWorkspaceFolders();
78+
for (const element of sorted) {
79+
let uri = folder.uri.toString();
80+
if (uri.charAt(uri.length - 1) !== '/') {
81+
uri = uri + '/';
82+
}
83+
if (uri.startsWith(element)) {
84+
return workspace.getWorkspaceFolder(Uri.parse(element)) || folder;
85+
}
86+
}
87+
return folder;
88+
}

0 commit comments

Comments
 (0)