aboutsummaryrefslogtreecommitdiffstats
path: root/language-web/src/main/js/xtext/ValidationService.ts
blob: 31c8f71632733f5f521528311ae244f3d92cf085 (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
import type { Diagnostic } from '@codemirror/lint';

import type { EditorStore } from '../editor/EditorStore';
import type { UpdateService } from './UpdateService';
import { getLogger } from '../utils/logger';
import { isValidationResult } from './xtextServiceResults';

const log = getLogger('xtext.ValidationService');

export class ValidationService {
  private store: EditorStore;

  private updateService: UpdateService;

  constructor(store: EditorStore, updateService: UpdateService) {
    this.store = store;
    this.updateService = updateService;
  }

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