|
| 1 | +import {parseYqlQueryWithoutCursor} from '@gravity-ui/websql-autocomplete/yql'; |
| 2 | +import {MarkerSeverity, editor} from 'monaco-editor'; |
| 3 | + |
| 4 | +const owner = 'ydb'; |
| 5 | + |
| 6 | +let errorsHighlightingTimeoutId: ReturnType<typeof setTimeout>; |
| 7 | + |
| 8 | +export function disableErrorsHighlighting(): void { |
| 9 | + unHighlightErrors(); |
| 10 | +} |
| 11 | + |
| 12 | +export function updateErrorsHighlighting(): void { |
| 13 | + disableErrorsHighlighting(); |
| 14 | + |
| 15 | + clearTimeout(errorsHighlightingTimeoutId); |
| 16 | + errorsHighlightingTimeoutId = setTimeout(() => highlightErrors(), 500); |
| 17 | +} |
| 18 | + |
| 19 | +function highlightErrors(): void { |
| 20 | + const model = window.ydbEditor?.getModel(); |
| 21 | + if (!model) { |
| 22 | + console.error('unable to retrieve model when highlighting errors'); |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + const errors = parseYqlQueryWithoutCursor(model.getValue()).errors; |
| 27 | + if (!errors.length) { |
| 28 | + unHighlightErrors(); |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + const markers = errors.map( |
| 33 | + (error): editor.IMarkerData => ({ |
| 34 | + message: 'Syntax error', |
| 35 | + source: error.message, |
| 36 | + severity: MarkerSeverity.Error, |
| 37 | + startLineNumber: error.startLine, |
| 38 | + startColumn: convertAutocompleteErrorLocationIndexToMonacoIndex(error.startColumn), |
| 39 | + endLineNumber: error.endLine, |
| 40 | + endColumn: convertAutocompleteErrorLocationIndexToMonacoIndex(error.endColumn), |
| 41 | + }), |
| 42 | + ); |
| 43 | + editor.setModelMarkers(model, owner, markers); |
| 44 | +} |
| 45 | + |
| 46 | +function unHighlightErrors(): void { |
| 47 | + editor.removeAllMarkers(owner); |
| 48 | +} |
| 49 | + |
| 50 | +function convertAutocompleteErrorLocationIndexToMonacoIndex( |
| 51 | + autocompleteLocationIndex: number, |
| 52 | +): number { |
| 53 | + return autocompleteLocationIndex + 1; |
| 54 | +} |
0 commit comments