Skip to content

Commit 39b9340

Browse files
authored
fix: always use last active lean editor as active client (#750)
1 parent 6f89d35 commit 39b9340

File tree

1 file changed

+10
-89
lines changed

1 file changed

+10
-89
lines changed

vscode-lean4/src/utils/clientProvider.ts

Lines changed: 10 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ import { findLeanProjectRootInfo, willUseLakeServer } from './projectInfo'
1313
async function checkLean4ProjectPreconditions(
1414
channel: OutputChannel,
1515
context: string,
16-
existingFolderUris: ExtUri[],
1716
folderUri: ExtUri,
18-
fileUri: ExtUri,
19-
stopOtherServer: (folderUri: FileUri) => Promise<void>,
2017
): Promise<PreconditionCheckResult> {
2118
const options: SetupNotificationOptions = {
2219
errorMode: { mode: 'NonModal' },
@@ -43,10 +40,6 @@ export class LeanClientProvider implements Disposable {
4340
private outputChannel: OutputChannel
4441
private clients: Map<string, LeanClient> = new Map()
4542
private pending: Map<string, boolean> = new Map()
46-
private pendingInstallChanged: FileUri[] = []
47-
private processingInstallChanged: boolean = false
48-
private activeClient: LeanClient | undefined = undefined
49-
5043
private progressChangedEmitter = new EventEmitter<[string, LeanFileProgressProcessingInfo[]]>()
5144
progressChanged = this.progressChangedEmitter.event
5245

@@ -67,15 +60,6 @@ export class LeanClientProvider implements Disposable {
6760

6861
lean.visibleLeanEditors.forEach(e => this.ensureClient(e.documentExtUri))
6962

70-
this.subscriptions.push(
71-
lean.onDidChangeActiveLeanEditor(async e => {
72-
if (e === undefined) {
73-
return
74-
}
75-
await this.ensureClient(e.documentExtUri)
76-
}),
77-
)
78-
7963
this.subscriptions.push(
8064
commands.registerCommand('lean4.restartFile', () => this.restartActiveFile()),
8165
commands.registerCommand('lean4.refreshFileDependencies', () => this.restartActiveFile()),
@@ -87,33 +71,11 @@ export class LeanClientProvider implements Disposable {
8771
}
8872

8973
getActiveClient(): LeanClient | undefined {
90-
// TODO: Most callers of this function probably don't need an active client, just the folder URI.
91-
return this.activeClient
92-
}
93-
94-
private async onInstallChanged(uri: FileUri) {
95-
this.pendingInstallChanged.push(uri)
96-
if (this.processingInstallChanged) {
97-
// avoid re-entrancy.
98-
return
74+
const activeEditor = lean.lastActiveLeanEditor
75+
if (activeEditor === undefined) {
76+
return undefined
9977
}
100-
this.processingInstallChanged = true
101-
102-
while (true) {
103-
const uri = this.pendingInstallChanged.pop()
104-
if (!uri) {
105-
break
106-
}
107-
try {
108-
const [cached, client] = await this.ensureClient(uri)
109-
if (cached && client) {
110-
await client.restart()
111-
}
112-
} catch (e) {
113-
logger.log(`[ClientProvider] Exception checking lean version: ${e}`)
114-
}
115-
}
116-
this.processingInstallChanged = false
78+
return this.findClient(activeEditor.documentExtUri)
11779
}
11880

11981
restartFile(uri: ExtUri) {
@@ -147,7 +109,7 @@ export class LeanClientProvider implements Disposable {
147109
}
148110

149111
private async stopActiveClient() {
150-
const client = this.activeClient
112+
const client = this.getActiveClient()
151113
if (client === undefined) {
152114
displayNotification('Error', 'Cannot stop language server: No active client.')
153115
return
@@ -157,45 +119,16 @@ export class LeanClientProvider implements Disposable {
157119
}
158120
}
159121

160-
private async eraseClient(folderUri: ExtUri) {
161-
const client = this.getClientForFolder(folderUri)
122+
private async restartActiveClient() {
123+
const client = this.getActiveClient()
162124
if (client === undefined) {
163125
displayNotification(
164126
'Error',
165-
`Cannot stop language server: No client for project at '${folderUri.toString()}'.`,
127+
'Cannot restart server: No focused Lean tab. Please focus the Lean tab for which you wish to restart the server.',
166128
)
167129
return
168130
}
169-
if (client.isStarted()) {
170-
await client.stop()
171-
}
172-
const key = client.folderUri.toString()
173-
this.clients.delete(key)
174-
this.pending.delete(key)
175-
if (client === this.activeClient) {
176-
this.activeClient = undefined
177-
}
178-
}
179-
180-
private async restartActiveClient() {
181-
if (this.activeClient === undefined) {
182-
const activeUri = lean.lastActiveLeanDocument?.extUri
183-
if (activeUri === undefined) {
184-
displayNotification(
185-
'Error',
186-
'Cannot restart server: No focused Lean tab. Please focus the Lean tab for which you wish to restart the server.',
187-
)
188-
return
189-
}
190-
191-
const [cached, client] = await this.ensureClient(activeUri)
192-
if (cached) {
193-
await client?.restart()
194-
}
195-
return
196-
}
197-
198-
await this.activeClient?.restart()
131+
await client.restart()
199132
}
200133

201134
// Find the client for a given document.
@@ -265,7 +198,6 @@ export class LeanClientProvider implements Disposable {
265198
const folderUri = projectInfo.projectRootUri
266199
let client = this.getClientForFolder(folderUri)
267200
if (client) {
268-
this.activeClient = client
269201
return [true, client]
270202
}
271203

@@ -278,17 +210,10 @@ export class LeanClientProvider implements Disposable {
278210
const preconditionCheckResult = await checkLean4ProjectPreconditions(
279211
this.outputChannel,
280212
'Client Startup',
281-
this.getClients().map(client => client.folderUri),
282213
folderUri,
283-
uri,
284-
async (folderUriToStop: FileUri) => {
285-
await this.eraseClient(folderUriToStop)
286-
await this.ensureClient(uri)
287-
},
288214
)
289215
if (preconditionCheckResult === 'Fatal') {
290216
this.pending.delete(key)
291-
this.activeClient = undefined
292217
return [false, undefined]
293218
}
294219

@@ -298,16 +223,13 @@ export class LeanClientProvider implements Disposable {
298223
this.clients.set(key, client)
299224

300225
client.serverFailed(err => {
301-
if (this.activeClient === client) {
302-
this.activeClient = undefined
303-
}
304226
this.clients.delete(key)
305227
client.dispose()
306228
displayNotification('Error', err)
307229
})
308230

309231
client.stopped(reason => {
310-
this.clientStoppedEmitter.fire([client, client === this.activeClient, reason])
232+
this.clientStoppedEmitter.fire([client, client === this.getActiveClient(), reason])
311233
})
312234

313235
// aggregate progress changed events.
@@ -326,7 +248,6 @@ export class LeanClientProvider implements Disposable {
326248
await client.start()
327249

328250
this.pending.delete(key)
329-
this.activeClient = client
330251

331252
return [false, client]
332253
}

0 commit comments

Comments
 (0)