Skip to content

Commit 6580273

Browse files
committed
Make LeanClientProvider responsible for opening documents.
1 parent 80ff7d0 commit 6580273

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

vscode-lean4/src/leanclient.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ export class LeanClient implements Disposable {
8383
this.outputChannel = outputChannel;
8484
this.workspaceFolder = workspaceFolder;
8585

86-
this.subscriptions.push(window.onDidChangeVisibleTextEditors((es) =>
87-
es.forEach((e) => this.open(e.document))));
88-
8986
this.subscriptions.push(workspace.onDidChangeConfiguration((e) => this.configChanged(e)));
9087
}
9188

@@ -266,7 +263,6 @@ export class LeanClient implements Disposable {
266263
this.client.outputChannel.show(true);
267264
});
268265

269-
window.visibleTextEditors.forEach((e) => this.open(e.document));
270266
this.restartedEmitter.fire(undefined)
271267
}
272268

@@ -294,10 +290,7 @@ export class LeanClient implements Disposable {
294290
c2p.asDiagnostics = (diags) => diags.map(d => c2p.asDiagnostic(d))
295291
}
296292

297-
private async open(doc: TextDocument) {
298-
if (doc.languageId !== 'lean4') {
299-
return
300-
}
293+
async openLean4Document(doc: TextDocument) {
301294
if (!this.running) return; // there was a problem starting lean server.
302295

303296
if (!this.isSameWorkspace(doc.uri)){

vscode-lean4/src/utils/clientProvider.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export class LeanClientProvider implements Disposable {
6767
const client = this.clients.get(path)
6868
void client.restart()
6969
} else {
70-
void this.ensureClient(this.getDocument(path), version);
70+
void this.ensureClient(this.getDocument(path).uri, version);
7171
}
7272
}
7373
} catch {
@@ -102,17 +102,20 @@ export class LeanClientProvider implements Disposable {
102102
void this.activeClient?.restart();
103103
}
104104

105-
didOpenEditor(document: TextDocument) {
105+
async didOpenEditor(document: TextDocument) {
106106
// All open .lean files are assumed to be Lean 4 files.
107107
// We need to do this because by default, .lean is associated with language id `lean`,
108108
// i.e. Lean 3. vscode-lean is expected to yield when isLean4Project is true.
109109
if (document.languageId === 'lean') {
110110
// Only change the document language for *visible* documents,
111111
// because this closes and then reopens the document.
112-
void languages.setTextDocumentLanguage(document, 'lean4');
113-
} else if (document.languageId === 'lean4') {
114-
void this.ensureClient(document, null);
112+
await languages.setTextDocumentLanguage(document, 'lean4');
113+
} else if (document.languageId !== 'lean4') {
114+
return;
115115
}
116+
117+
const client = await this.ensureClient(document.uri, null);
118+
await client.openLean4Document(document)
116119
}
117120

118121
getClient(uri: Uri){
@@ -123,18 +126,18 @@ export class LeanClientProvider implements Disposable {
123126
return Array.from(this.clients.values());
124127
}
125128

126-
async ensureClient(doc: TextDocument, versionInfo: LeanVersion | null) {
127-
let folder = workspace.getWorkspaceFolder(doc.uri);
129+
async ensureClient(uri: Uri, versionInfo: LeanVersion | null): Promise<LeanClient> {
130+
let folder = workspace.getWorkspaceFolder(uri);
128131
if (!folder && workspace.workspaceFolders) {
129132
// Could be that doc.uri.scheme === 'untitled'.
130133
workspace.workspaceFolders.forEach((f) => {
131-
if (f.uri.fsPath && doc.uri.fsPath.startsWith(f.uri.fsPath)) {
134+
if (f.uri.fsPath && uri.fsPath.startsWith(f.uri.fsPath)) {
132135
folder = f;
133136
}
134137
});
135138
}
136139

137-
const folderUri = folder ? folder.uri : doc.uri;
140+
const folderUri = folder ? folder.uri : uri;
138141
const path = folderUri?.toString();
139142
let client: LeanClient = null;
140143
if (this.clients.has(path)) {
@@ -166,18 +169,20 @@ export class LeanClientProvider implements Disposable {
166169
this.progressChangedEmitter.fire(arg);
167170
});
168171

172+
this.pending.delete(path);
173+
this.clientAddedEmitter.fire(client);
174+
169175
if (!versionInfo.error) {
170176
// we are ready to start, otherwise some sort of install might be happening
171177
// as a result of UI options shown by testLeanVersion.
172-
void client.start();
178+
await client.start();
173179
}
174-
175-
this.pending.delete(path);
176-
this.clientAddedEmitter.fire(client);
177180
}
178181

179182
// tell the InfoView about this activated client.
180183
this.activeClient = client;
184+
185+
return client;
181186
}
182187

183188
dispose(): void {

0 commit comments

Comments
 (0)