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

Commit 22d70b7

Browse files
committed
Change active client workspace also on changed editor focus
...rather than only on file opening itself.
1 parent 4ed04a8 commit 22d70b7

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### Unreleased
22

3+
* Correctly run tasks based on active text editor rather than last opened Rust file
34
* (!) Don't immediately try to start server instance for newly added workspace folders
45
* Use smooth, universally supported spinner in the status bar ⚙️
56

src/extension.ts

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
languages,
1111
RelativePattern,
1212
TextDocument,
13+
TextEditor,
1314
Uri,
1415
window,
1516
workspace,
@@ -29,10 +30,7 @@ import { checkForRls, ensureToolchain, rustupUpdate } from './rustup';
2930
import { startSpinner, stopSpinner } from './spinner';
3031
import { activateTaskProvider, Execution, runRlsCommand } from './tasks';
3132
import { withWsl } from './utils/child_process';
32-
import {
33-
getOuterMostWorkspaceFolder,
34-
nearestParentWorkspace,
35-
} from './utils/workspace';
33+
import { nearestParentWorkspace } from './utils/workspace';
3634
import { uriWindowsToWsl, uriWslToWindows } from './utils/wslpath';
3735

3836
/**
@@ -53,12 +51,15 @@ export async function activate(context: ExtensionContext) {
5351

5452
workspace.onDidOpenTextDocument(doc => whenOpeningTextDocument(doc));
5553
workspace.onDidChangeWorkspaceFolders(whenChangingWorkspaceFolders);
54+
window.onDidChangeActiveTextEditor(onDidChangeActiveTextEditor);
5655
window.onDidChangeActiveTextEditor(
5756
ed => ed && whenOpeningTextDocument(ed.document),
5857
);
5958
// Installed listeners don't fire immediately for already opened files, so
6059
// trigger an open event manually to fire up RLS instances where needed
6160
workspace.textDocuments.forEach(whenOpeningTextDocument);
61+
// Trigger manually logic for opening the first active editor
62+
onDidChangeActiveTextEditor(window.activeTextEditor);
6263

6364
// Migrate the users of multi-project setup for RLS to disable the setting
6465
// entirely (it's always on now)
@@ -98,28 +99,17 @@ function whenOpeningTextDocument(document: TextDocument) {
9899
return;
99100
}
100101

101-
let folder = workspace.getWorkspaceFolder(document.uri);
102-
if (!folder) {
103-
return;
104-
}
105-
106-
folder = nearestParentWorkspace(folder, document.uri.fsPath);
102+
clientWorkspaceForUri(document.uri, { startIfMissing: true });
103+
}
107104

108-
if (!folder) {
109-
stopSpinner(`Cargo.toml missing`);
105+
function onDidChangeActiveTextEditor(editor: TextEditor | undefined) {
106+
if (!editor) {
110107
return;
111108
}
112109

113-
const folderPath = folder.uri.toString();
114-
115-
if (!workspaces.has(folderPath)) {
116-
const workspace = new ClientWorkspace(folder);
110+
const workspace = clientWorkspaceForUri(editor.document.uri);
111+
if (workspace) {
117112
activeWorkspace = workspace;
118-
workspaces.set(folderPath, workspace);
119-
workspace.start();
120-
} else {
121-
const ws = workspaces.get(folderPath);
122-
activeWorkspace = typeof ws === 'undefined' ? null : ws;
123113
}
124114
}
125115

@@ -137,6 +127,34 @@ function whenChangingWorkspaceFolders(e: WorkspaceFoldersChangeEvent) {
137127
// Don't use URI as it's unreliable the same path might not become the same URI.
138128
const workspaces: Map<string, ClientWorkspace> = new Map();
139129

130+
/**
131+
* Fetches a `ClientWorkspace` for a given URI. If missing and `startIfMissing`
132+
* option was provided, it is additionally initialized beforehand, if applicable.
133+
*/
134+
function clientWorkspaceForUri(
135+
uri: Uri,
136+
options?: { startIfMissing: boolean },
137+
): ClientWorkspace | undefined {
138+
const rootFolder = workspace.getWorkspaceFolder(uri);
139+
if (!rootFolder) {
140+
return;
141+
}
142+
143+
const folder = nearestParentWorkspace(rootFolder, uri.fsPath);
144+
if (!folder) {
145+
return undefined;
146+
}
147+
148+
const existing = workspaces.get(folder.uri.toString());
149+
if (!existing && options && options.startIfMissing) {
150+
const workspace = new ClientWorkspace(folder);
151+
workspaces.set(folder.uri.toString(), workspace);
152+
workspace.start();
153+
}
154+
155+
return workspaces.get(folder.uri.toString());
156+
}
157+
140158
// We run one RLS and one corresponding language client per workspace folder
141159
// (VSCode workspace, not Cargo workspace). This class contains all the per-client
142160
// and per-workspace stuff.

0 commit comments

Comments
 (0)