@@ -10,6 +10,7 @@ import {
10
10
languages ,
11
11
RelativePattern ,
12
12
TextDocument ,
13
+ TextEditor ,
13
14
Uri ,
14
15
window ,
15
16
workspace ,
@@ -29,10 +30,7 @@ import { checkForRls, ensureToolchain, rustupUpdate } from './rustup';
29
30
import { startSpinner , stopSpinner } from './spinner' ;
30
31
import { activateTaskProvider , Execution , runRlsCommand } from './tasks' ;
31
32
import { withWsl } from './utils/child_process' ;
32
- import {
33
- getOuterMostWorkspaceFolder ,
34
- nearestParentWorkspace ,
35
- } from './utils/workspace' ;
33
+ import { nearestParentWorkspace } from './utils/workspace' ;
36
34
import { uriWindowsToWsl , uriWslToWindows } from './utils/wslpath' ;
37
35
38
36
/**
@@ -53,12 +51,15 @@ export async function activate(context: ExtensionContext) {
53
51
54
52
workspace . onDidOpenTextDocument ( doc => whenOpeningTextDocument ( doc ) ) ;
55
53
workspace . onDidChangeWorkspaceFolders ( whenChangingWorkspaceFolders ) ;
54
+ window . onDidChangeActiveTextEditor ( onDidChangeActiveTextEditor ) ;
56
55
window . onDidChangeActiveTextEditor (
57
56
ed => ed && whenOpeningTextDocument ( ed . document ) ,
58
57
) ;
59
58
// Installed listeners don't fire immediately for already opened files, so
60
59
// trigger an open event manually to fire up RLS instances where needed
61
60
workspace . textDocuments . forEach ( whenOpeningTextDocument ) ;
61
+ // Trigger manually logic for opening the first active editor
62
+ onDidChangeActiveTextEditor ( window . activeTextEditor ) ;
62
63
63
64
// Migrate the users of multi-project setup for RLS to disable the setting
64
65
// entirely (it's always on now)
@@ -98,28 +99,17 @@ function whenOpeningTextDocument(document: TextDocument) {
98
99
return ;
99
100
}
100
101
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
+ }
107
104
108
- if ( ! folder ) {
109
- stopSpinner ( `Cargo.toml missing` ) ;
105
+ function onDidChangeActiveTextEditor ( editor : TextEditor | undefined ) {
106
+ if ( ! editor ) {
110
107
return ;
111
108
}
112
109
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 ) {
117
112
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 ;
123
113
}
124
114
}
125
115
@@ -137,6 +127,34 @@ function whenChangingWorkspaceFolders(e: WorkspaceFoldersChangeEvent) {
137
127
// Don't use URI as it's unreliable the same path might not become the same URI.
138
128
const workspaces : Map < string , ClientWorkspace > = new Map ( ) ;
139
129
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
+
140
158
// We run one RLS and one corresponding language client per workspace folder
141
159
// (VSCode workspace, not Cargo workspace). This class contains all the per-client
142
160
// and per-workspace stuff.
0 commit comments