-
Notifications
You must be signed in to change notification settings - Fork 39
/
leanclient.ts
284 lines (247 loc) · 10.8 KB
/
leanclient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import { TextDocument, Position, TextEditor, EventEmitter, Uri, Diagnostic,
workspace, languages, DocumentHighlight, Range, DocumentHighlightKind, window, Disposable } from 'vscode'
import {
DidOpenTextDocumentNotification,
DocumentFilter,
LanguageClient,
LanguageClientOptions,
Protocol2CodeConverter,
ServerOptions,
VersionedTextDocumentIdentifier
} from 'vscode-languageclient/node'
import * as ls from 'vscode-languageserver-protocol'
import { executablePath, addServerEnvPaths, serverLoggingEnabled, serverLoggingPath, getElaborationDelay } from './config'
import { PlainGoal, PlainTermGoal, ServerProgress } from './leanclientTypes'
import { assert } from './utils/assert'
export interface LeanFileProgressProcessingInfo {
/** Range which is still being processed */
range: ls.Range;
}
export interface LeanFileProgressParams {
/** The text document to which this progress notification applies. */
textDocument: VersionedTextDocumentIdentifier;
/**
* Array containing the parts of the file which are still being processed.
* The array should be empty if and only if the server is finished processing.
*/
processing: LeanFileProgressProcessingInfo[];
}
const processingMessage = 'processing...'
const documentSelector: DocumentFilter = {
scheme: 'file',
language: 'lean4',
}
const escapeRegExp = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
interface Lean4Diagnostic extends ls.Diagnostic {
fullRange: ls.Range;
}
export function getFullRange(diag: Diagnostic): Range {
return (diag as any)?.fullRange || diag.range;
}
export class LeanClient implements Disposable {
client: LanguageClient
private restartedEmitter = new EventEmitter()
restarted = this.restartedEmitter.event
private diagnosticsEmitter = new EventEmitter<{uri: Uri, diagnostics: Diagnostic[]}>()
diagnostics = this.diagnosticsEmitter.event
progress: ServerProgress = {}
private progressChangedEmitter = new EventEmitter<ServerProgress>()
progressChanged = this.progressChangedEmitter.event
private subscriptions: Disposable[] = []
private isOpen: Set<string> = new Set()
hasFileProgressSupport: boolean = false
constructor() {
this.subscriptions.push(window.onDidChangeVisibleTextEditors((es) =>
es.forEach((e) => this.open(e.document))));
}
dispose(): void {
this.subscriptions.forEach((s) => s.dispose())
if (this.isStarted()) void this.stop()
}
async restart(): Promise<void> {
if (this.isStarted()) {
await this.stop()
}
const env = addServerEnvPaths(process.env);
if (serverLoggingEnabled()) {
env.LEAN_SERVER_LOG_DIR = serverLoggingPath()
}
const serverOptions: ServerOptions = {
command: executablePath(),
args: ['--server'],
options: {
shell: true,
env
}
}
const clientOptions: LanguageClientOptions = {
documentSelector: [documentSelector],
initializationOptions: {
editDelay: getElaborationDelay(),
},
middleware: {
handleDiagnostics: (uri, diagnostics, next) => {
if (!this.hasFileProgressSupport) {
const processedUntil = diagnostics.find((d) =>
d.message === processingMessage)?.range?.start?.line
this.setProgress({...this.progress, [uri.toString()]: processedUntil})
diagnostics = diagnostics.filter((d) => d.message !== processingMessage);
}
for (const diag of diagnostics) {
if (diag.source === 'Lean 4 server') {
diag.source = 'Lean 4';
}
}
next(uri, diagnostics);
this.diagnosticsEmitter.fire({uri, diagnostics})
},
didOpen: () => {
// Ignore opening of documents for ctrl+hover
// https://github.com/microsoft/vscode/issues/78453
return;
},
didClose: (doc, next) => {
if (!this.isOpen.delete(doc.uri.toString())) return;
next(doc);
},
provideDocumentHighlights: async (doc, pos, ctok, next) => {
const leanHighlights = await next(doc, pos, ctok);
if (leanHighlights?.length) return leanHighlights;
// vscode doesn't fall back to textual highlights,
// so we need to do that manually
await new Promise((res) => setTimeout(res, 250));
if (ctok.isCancellationRequested) return;
const wordRange = doc.getWordRangeAtPosition(pos);
if (!wordRange) return;
const word = doc.getText(wordRange);
const highlights: DocumentHighlight[] = [];
const text = doc.getText();
const nonWordPattern = '[`~@$%^&*()-=+\\[{\\]}⟨⟩⦃⦄⟦⟧⟮⟯‹›\\\\|;:\",./\\s]|^|$'
const regexp = new RegExp(`(?<=${nonWordPattern})${escapeRegExp(word)}(?=${nonWordPattern})`, 'g')
for (const match of text.matchAll(regexp)) {
const start = doc.positionAt(match.index)
highlights.push({
range: new Range(start, start.translate(0, match[0].length)),
kind: DocumentHighlightKind.Text,
})
}
return highlights;
}
},
}
this.setProgress(Object.assign({}, ...workspace.textDocuments
.filter((doc) => languages.match(documentSelector, doc))
.map((doc) => ({[doc.uri.toString()]: 0}))))
this.client = new LanguageClient(
'lean4',
'Lean 4',
serverOptions,
clientOptions
)
this.patchProtocol2CodeConverter(this.client.protocol2CodeConverter)
this.client.start()
this.isOpen = new Set()
await this.client.onReady();
this.client.onNotification('$/lean/fileProgress', (params: LeanFileProgressParams) => {
this.hasFileProgressSupport = true;
const uri = this.client.protocol2CodeConverter.asUri(params.textDocument.uri)
const processedUntil = params.processing.length === 0 ? undefined :
Math.min(...params.processing.map((p) => p.range.start.line));
this.setProgress({...this.progress, [uri.toString()]: processedUntil})
});
// HACK
(this.client as any)._serverProcess.stderr.on('data', () =>
this.client.outputChannel.show(true))
window.visibleTextEditors.forEach((e) => this.open(e.document));
this.restartedEmitter.fire(undefined)
}
private patchProtocol2CodeConverter(p2c: Protocol2CodeConverter) {
// eslint-disable-next-line @typescript-eslint/unbound-method
const oldAsDiagnostic = p2c.asDiagnostic
p2c.asDiagnostic = function (protDiag: Lean4Diagnostic): Diagnostic {
if (!protDiag.message) {
// Fixes: Notification handler 'textDocument/publishDiagnostics' failed with message: message must be set
protDiag.message = ' ';
}
const diag = oldAsDiagnostic.apply(this, [protDiag])
if (protDiag.fullRange) {
diag.fullRange = p2c.asRange(protDiag.fullRange)
}
return diag
}
p2c.asDiagnostics = (diags) => diags.map((d) => p2c.asDiagnostic(d))
}
private async open(doc: TextDocument) {
// All open .lean files of this workspace are assumed to be Lean 4 files.
// We need to do this because by default, .lean is associated with language id `lean`,
// i.e. Lean 3. vscode-lean is expected to yield when isLean4 is true.
if (doc.languageId === 'lean') {
// Only change the id for *visible* documents,
// because this closes and then reopens the document.
await languages.setTextDocumentLanguage(doc, 'lean4')
} else if (doc.languageId !== 'lean4') {
return
}
if (this.isOpen.has(doc.uri.toString())) return;
this.isOpen.add(doc.uri.toString())
this.client.sendNotification(DidOpenTextDocumentNotification.type, {
textDocument: {
uri: doc.uri.toString(),
languageId: doc.languageId,
version: 1,
text: doc.getText(),
},
});
}
start(): Promise<void> {
return this.restart()
}
isStarted(): boolean {
return this.client !== undefined
}
async stop(): Promise<void> {
assert(() => this.isStarted())
await this.client.stop()
this.setProgress({})
this.client = undefined
}
refreshFileDependencies(editor: TextEditor): void {
assert(() => this.isStarted())
const doc = editor.document
const uri = doc.uri.toString()
// This causes a text document version number discontinuity. In
// (didChange (oldVersion) => refreshFileDependencies => didChange (newVersion))
// the client emits newVersion = oldVersion + 1, despite the fact that the
// didOpen packet emitted below initializes the version number to be 1.
// This is not a problem though, since both client and server are fine
// as long as the version numbers are monotonous.
this.client.sendNotification('textDocument/didClose', {
'textDocument': {
uri
}
})
this.client.sendNotification('textDocument/didOpen', {
'textDocument': {
uri,
'languageId': 'lean4',
'version': 1,
'text': doc.getText()
}
})
}
requestPlainGoals(doc: TextDocument, position: Position): Promise<PlainGoal> {
assert(() => this.isStarted())
return this.client.sendRequest(
'$/lean/plainGoal',
this.client.code2ProtocolConverter.asTextDocumentPositionParams(doc, position))
}
requestPlainTermGoal(doc: TextDocument, position: Position): Promise<PlainTermGoal> {
return this.client.sendRequest(
'$/lean/plainTermGoal',
this.client.code2ProtocolConverter.asTextDocumentPositionParams(doc, position))
}
private setProgress(newProgress: ServerProgress) {
this.progress = newProgress
this.progressChangedEmitter.fire(newProgress)
}
}