Skip to content

feat(QueryEditor): add error highlighting #1833

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

Merged
merged 5 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 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 {useUpdateErrorsHighlighting} from '../../../../utils/monaco/highlightErrors';
import {QUERY_ACTIONS} from '../../../../utils/query';
import type {InitialPaneState} from '../../utils/paneVisibilityToggleHelpers';
import {
Expand Down Expand Up @@ -89,6 +90,8 @@ export default function QueryEditor(props: QueryEditorProps) {
const input = useTypedSelector(selectUserInput);
const showPreview = useTypedSelector(selectShowPreview);

const updateErrorsHighlighting = useUpdateErrorsHighlighting();

const isResultLoaded = Boolean(result);

const [querySettings] = useQueryExecutionSettings();
Expand Down Expand Up @@ -294,6 +297,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
10 changes: 10 additions & 0 deletions src/utils/hooks/useCancellable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';

export function useCancellableFunction<T extends {cancel: () => void}>(cancellable: T) {
React.useEffect(() => {
return () => {
cancellable.cancel();
};
}, [cancellable]);
return cancellable;
}
54 changes: 54 additions & 0 deletions src/utils/monaco/highlightErrors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should cancel it on useEffect return


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);
}
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