aboutsummaryrefslogtreecommitdiffstats
path: root/language-web/src/main/js/editor/XtextClient.ts
blob: 6f789fb7ccd91a13b14a718bbd710f2e8f0571d7 (plain) (blame)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import {
  Completion,
  CompletionContext,
  CompletionResult,
} from '@codemirror/autocomplete';
import type { Diagnostic } from '@codemirror/lint';
import {
  ChangeDesc,
  ChangeSet,
  Transaction,
} from '@codemirror/state';
import { nanoid } from 'nanoid';

import type { EditorStore } from './EditorStore';
import { getLogger } from '../logging';
import { Timer } from '../utils/Timer';
import {
  IContentAssistEntry,
  isContentAssistResult,
  isDocumentStateResult,
  isInvalidStateIdConflictResult,
  isValidationResult,
} from './xtextServiceResults';
import { XtextWebSocketClient } from './XtextWebSocketClient';
import { PendingTask } from '../utils/PendingTask';

const UPDATE_TIMEOUT_MS = 500;

const WAIT_FOR_UPDATE_TIMEOUT_MS = 1000;

const log = getLogger('XtextClient');

export class XtextClient {
  resourceName: string;

  webSocketClient: XtextWebSocketClient;

  xtextStateId: string | null = null;

  pendingUpdate: ChangeDesc | null;

  dirtyChanges: ChangeDesc;

  lastCompletion: CompletionResult | null = null;

  updateListeners: PendingTask<void>[] = [];

  updateTimer = new Timer(() => {
    this.handleUpdate();
  }, UPDATE_TIMEOUT_MS);

  store: EditorStore;

  constructor(store: EditorStore) {
    this.resourceName = `${nanoid(7)}.problem`;
    this.pendingUpdate = null;
    this.store = store;
    this.dirtyChanges = this.newEmptyChangeDesc();
    this.webSocketClient = new XtextWebSocketClient(
      async () => {
        this.xtextStateId = null;
        await this.updateFullText();
      },
      async (resource, stateId, service, push) => {
        await this.onPush(resource, stateId, service, push);
      },
    );
  }

  onTransaction(transaction: Transaction): void {
    const { changes } = transaction;
    if (!changes.empty) {
      if (this.shouldInvalidateCachedCompletion(transaction)) {
        log.trace('Invalidating cached completions');
        this.lastCompletion = null;
      }
      this.dirtyChanges = this.dirtyChanges.composeDesc(changes.desc);
      this.updateTimer.reschedule();
    }
  }

  private async onPush(resource: string, stateId: string, service: string, push: unknown) {
    if (resource !== this.resourceName) {
      log.error('Unknown resource name: expected:', this.resourceName, 'got:', resource);
      return;
    }
    if (stateId !== this.xtextStateId) {
      log.error('Unexpected xtext state id: expected:', this.xtextStateId, 'got:', resource);
      await this.updateFullText();
    }
    switch (service) {
      case 'validate':
        this.onValidate(push);
        return;
      case 'highlight':
        // TODO
        return;
      default:
        log.error('Unknown push service:', service);
        break;
    }
  }

  private onValidate(push: unknown) {
    if (!isValidationResult(push)) {
      log.error('Invalid validation result', push);
      return;
    }
    const allChanges = this.computeChangesSinceLastUpdate();
    const diagnostics: Diagnostic[] = [];
    push.issues.forEach((issue) => {
      if (issue.severity === 'ignore') {
        return;
      }
      diagnostics.push({
        from: allChanges.mapPos(issue.offset),
        to: allChanges.mapPos(issue.offset + issue.length),
        severity: issue.severity,
        message: issue.description,
      });
    });
    this.store.updateDiagnostics(diagnostics);
  }

  private computeChangesSinceLastUpdate() {
    return this.pendingUpdate?.composeDesc(this.dirtyChanges) || this.dirtyChanges;
  }

  private handleUpdate() {
    if (!this.webSocketClient.isOpen || this.dirtyChanges.empty) {
      return;
    }
    if (this.pendingUpdate === null) {
      this.update().catch((error) => {
        log.error('Unexpected error during scheduled update', error);
      });
    }
    this.updateTimer.reschedule();
  }

  private newEmptyChangeDesc() {
    const changeSet = ChangeSet.of([], this.store.state.doc.length);
    return changeSet.desc;
  }

  private async updateFullText() {
    await this.withUpdate(() => this.doUpdateFullText());
  }

  private async doUpdateFullText(): Promise<[string, void]> {
    const result = await this.webSocketClient.send({
      resource: this.resourceName,
      serviceType: 'update',
      fullText: this.store.state.doc.sliceString(0),
    });
    if (isDocumentStateResult(result)) {
      return [result.stateId, undefined];
    }
    log.error('Unexpected full text update result:', result);
    throw new Error('Full text update failed');
  }

  async update(): Promise<void> {
    await this.prepareForDeltaUpdate();
    const delta = this.computeDelta();
    if (delta === null) {
      return;
    }
    log.trace('Editor delta', delta);
    await this.withUpdate(async () => {
      const result = await this.webSocketClient.send({
        resource: this.resourceName,
        serviceType: 'update',
        requiredStateId: this.xtextStateId,
        ...delta,
      });
      if (isDocumentStateResult(result)) {
        return [result.stateId, undefined];
      }
      if (isInvalidStateIdConflictResult(result)) {
        return this.doFallbackToUpdateFullText();
      }
      log.error('Unexpected delta text update result:', result);
      throw new Error('Delta text update failed');
    });
  }

  private doFallbackToUpdateFullText() {
    if (this.pendingUpdate === null) {
      throw new Error('Only a pending update can be extended');
    }
    log.warn('Delta update failed, performing full text update');
    this.xtextStateId = null;
    this.pendingUpdate = this.pendingUpdate.composeDesc(this.dirtyChanges);
    this.dirtyChanges = this.newEmptyChangeDesc();
    return this.doUpdateFullText();
  }

  async contentAssist(context: CompletionContext): Promise<CompletionResult> {
    const tokenBefore = context.tokenBefore(['QualifiedName']);
    if (tokenBefore === null && !context.explicit) {
      return {
        from: context.pos,
        options: [],
      };
    }
    const range = {
      from: tokenBefore?.from || context.pos,
      to: tokenBefore?.to || context.pos,
    };
    if (this.shouldReturnCachedCompletion(tokenBefore)) {
      log.trace('Returning cached completion result');
      // Postcondition of `shouldReturnCachedCompletion`: `lastCompletion !== null`
      return {
        ...this.lastCompletion as CompletionResult,
        ...range,
      };
    }
    const entries = await this.fetchContentAssist(context);
    if (context.aborted) {
      return {
        ...range,
        options: [],
      };
    }
    const options: Completion[] = [];
    entries.forEach((entry) => {
      options.push({
        label: entry.proposal,
        detail: entry.description,
        info: entry.documentation,
        type: entry.kind?.toLowerCase(),
        boost: entry.kind === 'KEYWORD' ? -90 : 0,
      });
    });
    log.debug('Fetched', options.length, 'completions from server');
    this.lastCompletion = {
      ...range,
      options,
      span: /[a-zA-Z0-9_:]/,
    };
    return this.lastCompletion;
  }

  private shouldReturnCachedCompletion(
    token: { from: number, to: number, text: string } | null,
  ) {
    if (token === null || this.lastCompletion === null) {
      return false;
    }
    const { from, to, text } = token;
    const { from: lastFrom, to: lastTo, span } = this.lastCompletion;
    if (!lastTo) {
      return true;
    }
    const transformedFrom = this.dirtyChanges.mapPos(lastFrom);
    const transformedTo = this.dirtyChanges.mapPos(lastTo, 1);
    return from >= transformedFrom && to <= transformedTo && span && span.exec(text);
  }

  private shouldInvalidateCachedCompletion(transaction: Transaction) {
    if (this.lastCompletion === null) {
      return false;
    }
    const { from: lastFrom, to: lastTo } = this.lastCompletion;
    if (!lastTo) {
      return true;
    }
    const transformedFrom = this.dirtyChanges.mapPos(lastFrom);
    const transformedTo = this.dirtyChanges.mapPos(lastTo, 1);
    let invalidate = false;
    transaction.changes.iterChangedRanges((fromA, toA) => {
      if (fromA < transformedFrom || toA > transformedTo) {
        invalidate = true;
      }
    });
    return invalidate;
  }

  private async fetchContentAssist(context: CompletionContext) {
    await this.prepareForDeltaUpdate();
    const delta = this.computeDelta();
    if (delta === null) {
      // Poscondition of `prepareForDeltaUpdate`: `xtextStateId !== null`
      return this.doFetchContentAssist(context, this.xtextStateId as string);
    }
    log.trace('Editor delta', delta);
    return await this.withUpdate(async () => {
      const result = await this.webSocketClient.send({
        requiredStateId: this.xtextStateId,
        ...this.computeContentAssistParams(context),
        ...delta,
      });
      if (isContentAssistResult(result)) {
        return [result.stateId, result.entries];
      }
      if (isInvalidStateIdConflictResult(result)) {
        const [newStateId] = await this.doFallbackToUpdateFullText();
        if (context.aborted) {
          return [newStateId, [] as IContentAssistEntry[]];
        }
        const entries = await this.doFetchContentAssist(context, newStateId);
        return [newStateId, entries];
      }
      log.error('Unextpected content assist result with delta update', result);
      throw new Error('Unexpexted content assist result with delta update');
    });
  }

  private async doFetchContentAssist(context: CompletionContext, expectedStateId: string) {
    const result = await this.webSocketClient.send({
      requiredStateId: expectedStateId,
      ...this.computeContentAssistParams(context),
    });
    if (isContentAssistResult(result) && result.stateId === expectedStateId) {
      return result.entries;
    }
    log.error('Unexpected content assist result', result);
    throw new Error('Unexpected content assist result');
  }

  private computeContentAssistParams(context: CompletionContext) {
    const tokenBefore = context.tokenBefore(['QualifiedName']);
    let selection = {};
    if (tokenBefore !== null) {
      selection = {
        selectionStart: tokenBefore.from,
        selectionEnd: tokenBefore.to,
      };
    }
    return {
      resource: this.resourceName,
      serviceType: 'assist',
      caretOffset: tokenBefore?.from || context.pos,
      ...selection,
    };
  }

  private computeDelta() {
    if (this.dirtyChanges.empty) {
      return null;
    }
    let minFromA = Number.MAX_SAFE_INTEGER;
    let maxToA = 0;
    let minFromB = Number.MAX_SAFE_INTEGER;
    let maxToB = 0;
    this.dirtyChanges.iterChangedRanges((fromA, toA, fromB, toB) => {
      minFromA = Math.min(minFromA, fromA);
      maxToA = Math.max(maxToA, toA);
      minFromB = Math.min(minFromB, fromB);
      maxToB = Math.max(maxToB, toB);
    });
    return {
      deltaOffset: minFromA,
      deltaReplaceLength: maxToA - minFromA,
      deltaText: this.store.state.doc.sliceString(minFromB, maxToB),
    };
  }

  private async withUpdate<T>(callback: () => Promise<[string, T]>): Promise<T> {
    if (this.pendingUpdate !== null) {
      throw new Error('Another update is pending, will not perform update');
    }
    this.pendingUpdate = this.dirtyChanges;
    this.dirtyChanges = this.newEmptyChangeDesc();
    let newStateId: string | null = null;
    try {
      let result: T;
      [newStateId, result] = await callback();
      this.xtextStateId = newStateId;
      this.pendingUpdate = null;
      // Copy `updateListeners` so that we don't get into a race condition
      // if one of the listeners adds another listener.
      const listeners = this.updateListeners;
      this.updateListeners = [];
      listeners.forEach((listener) => {
        listener.resolve();
      });
      return result;
    } catch (e) {
      log.error('Error while update', e);
      if (this.pendingUpdate === null) {
        log.error('pendingUpdate was cleared during update');
      } else {
        this.dirtyChanges = this.pendingUpdate.composeDesc(this.dirtyChanges);
      }
      this.pendingUpdate = null;
      this.webSocketClient.forceReconnectOnError();
      const listeners = this.updateListeners;
      this.updateListeners = [];
      listeners.forEach((listener) => {
        listener.reject(e);
      });
      throw e;
    }
  }

  private async prepareForDeltaUpdate() {
    if (this.pendingUpdate === null) {
      if (this.xtextStateId === null) {
        return;
      }
      await this.updateFullText();
    }
    let nowMs = Date.now();
    const endMs = nowMs + WAIT_FOR_UPDATE_TIMEOUT_MS;
    while (this.pendingUpdate !== null && nowMs < endMs) {
      const timeoutMs = endMs - nowMs;
      const promise = new Promise((resolve, reject) => {
        const task = new PendingTask(resolve, reject, timeoutMs);
        this.updateListeners.push(task);
      });
      // We must keep waiting uptil the update has completed,
      // so the tasks can't be started in parallel.
      // eslint-disable-next-line no-await-in-loop
      await promise;
      nowMs = Date.now();
    }
    if (this.pendingUpdate !== null || this.xtextStateId === null) {
      log.error('No successful update in', WAIT_FOR_UPDATE_TIMEOUT_MS, 'ms');
      throw new Error('Failed to wait for successful update');
    }
  }
}