Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(QueryEditor): add error highlighting #1833

Merged
merged 5 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/containers/Tenant/Query/QueryEditor/QueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
import {useChangedQuerySettings} from '../../../../utils/hooks/useChangedQuerySettings';
import {useLastQueryExecutionSettings} from '../../../../utils/hooks/useLastQueryExecutionSettings';
import {YQL_LANGUAGE_ID} from '../../../../utils/monaco/constats';
import {updateErrorsHighlighting} from '../../../../utils/monaco/highlightErrors';
import {QUERY_ACTIONS} from '../../../../utils/query';
import type {InitialPaneState} from '../../utils/paneVisibilityToggleHelpers';
import {
Expand Down Expand Up @@ -294,6 +295,7 @@ export default function QueryEditor(props: QueryEditorProps) {
};

const onChange = (newValue: string) => {
updateErrorsHighlighting();
changeUserInput({input: newValue});
};

Expand Down
1 change: 1 addition & 0 deletions src/containers/Tenant/Query/QueryEditor/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const EDITOR_OPTIONS: EditorOptions = {
minimap: {
enabled: false,
},
fixedOverflowWidgets: true,
};

export function useEditorOptions() {
Expand Down
47 changes: 47 additions & 0 deletions src/utils/monaco/highlightErrors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {parseYqlQueryWithoutCursor} from '@gravity-ui/websql-autocomplete/yql';
import {MarkerSeverity, editor} from 'monaco-editor';

import i18n from './i18n';

const owner = 'ydb';

let errorsHighlightingTimeoutId: ReturnType<typeof setTimeout>;

export function updateErrorsHighlighting() {
unHighlightErrors();

clearTimeout(errorsHighlightingTimeoutId);
errorsHighlightingTimeoutId = setTimeout(() => highlightErrors(), 500);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will it highlight incorrect errors if I change editor within 500 ms?

}

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 => {
const markerColumn = error.startColumn + 1;
return {
message: i18n('context_syntax-error'),
source: error.message,
severity: MarkerSeverity.Error,
startLineNumber: error.startLine,
startColumn: markerColumn,
endLineNumber: error.endLine,
endColumn: markerColumn,
astandrik marked this conversation as resolved.
Show resolved Hide resolved
};
});
editor.setModelMarkers(model, owner, markers);
}

function unHighlightErrors(): void {
editor.removeAllMarkers(owner);
}
3 changes: 3 additions & 0 deletions src/utils/monaco/i18n/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"context_syntax-error": "Syntax error"
}
7 changes: 7 additions & 0 deletions src/utils/monaco/i18n/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {registerKeysets} from '../../i18n';

import en from './en.json';

const COMPONENT = 'ydb-monaco';

export default registerKeysets(COMPONENT, {en});
Loading