-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathhighlightErrors.ts
54 lines (43 loc) · 1.53 KB
/
highlightErrors.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
import React from 'react';
import {parseYqlQueryWithoutCursor} from '@gravity-ui/websql-autocomplete/yql';
import {debounce} from 'lodash';
import {MarkerSeverity, editor} from 'monaco-editor';
import {useCancellableFunction} from '../hooks/useCancellable';
import i18n from './i18n';
const owner = 'ydb';
const debouncedHighlightErrors = debounce(highlightErrors, 500);
export function useUpdateErrorsHighlighting() {
const highlightErrors = useCancellableFunction(debouncedHighlightErrors);
const updateErrorsHighlighting = React.useCallback(() => {
unHighlightErrors();
highlightErrors();
}, [highlightErrors]);
return updateErrorsHighlighting;
}
function highlightErrors() {
const model = window.ydbEditor?.getModel();
if (!model) {
console.error('unable to retrieve model when highlighting errors');
return;
}
const errors = parseYqlQueryWithoutCursor(model.getValue()).errors;
if (!errors.length) {
unHighlightErrors();
return;
}
const markers = errors.map(
(error): editor.IMarkerData => ({
message: i18n('context_syntax-error'),
source: error.message,
severity: MarkerSeverity.Error,
startLineNumber: error.startLine,
startColumn: error.startColumn + 1,
endLineNumber: error.endLine,
endColumn: error.endColumn + 1,
}),
);
editor.setModelMarkers(model, owner, markers);
}
function unHighlightErrors(): void {
editor.removeAllMarkers(owner);
}