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

import type { EditorStore } from '../editor/EditorStore';
import { getLogger } from '../logging';
import type { UpdateService } from './UpdateService';
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((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);
  }
}