aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/src/xtext/ValidationService.ts
blob: 64fb63eb1b5264877a8d3fa88cb85284a5290533 (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
/*
 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */

import type { Diagnostic } from '@codemirror/lint';

import type EditorStore from '../editor/EditorStore';

import type UpdateService from './UpdateService';
import { ValidationResult } from './xtextServiceResults';

export default class ValidationService {
  constructor(
    private readonly store: EditorStore,
    private readonly updateService: UpdateService,
  ) {}

  onPush(push: unknown): void {
    const { issues } = ValidationResult.parse(push);
    const allChanges = this.updateService.computeChangesSinceLastUpdate();
    const diagnostics: Diagnostic[] = [];
    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);
  }

  onDisconnect(): void {
    this.store.updateDiagnostics([]);
  }
}