diff --git a/src/plugins/data/public/antlr/dql/code_completion.ts b/src/plugins/data/public/antlr/dql/code_completion.ts index 0dc566cdb947..a14310f56979 100644 --- a/src/plugins/data/public/antlr/dql/code_completion.ts +++ b/src/plugins/data/public/antlr/dql/code_completion.ts @@ -241,6 +241,12 @@ export const getSuggestions = async ({ text: val, type: monaco.languages.CompletionItemKind.Value, detail: SuggestionItemDetailsTags.Value, + replacePosition: new monaco.Range( + cursorLine, + cursorColumn - lastValue.length + 1, + cursorLine, + cursorColumn + 1 + ), }; }) ); diff --git a/src/plugins/data/public/antlr/opensearch_sql/code_completion.ts b/src/plugins/data/public/antlr/opensearch_sql/code_completion.ts index 4dc72ca25a2f..a0695a424393 100644 --- a/src/plugins/data/public/antlr/opensearch_sql/code_completion.ts +++ b/src/plugins/data/public/antlr/opensearch_sql/code_completion.ts @@ -4,27 +4,11 @@ */ import { monaco } from '@osd/monaco'; -import { Lexer as LexerType, ParserRuleContext, Parser as ParserType } from 'antlr4ng'; -import { CodeCompletionCore } from 'antlr4-c3'; -import { - AutocompleteResultBase, - CursorPosition, - EnrichAutocompleteResult, - GetParseTree, - KeywordSuggestion, - LexerConstructor, - OpenSearchSqlAutocompleteResult, - ParserConstructor, -} from '../shared/types'; -import { TokenDictionary } from './table'; -import { createParser } from './parse'; -import { SqlErrorListener } from './sql_error_listerner'; -import { findCursorTokenIndex } from '../shared/cursor'; +import { CursorPosition, OpenSearchSqlAutocompleteResult } from '../shared/types'; import { openSearchSqlAutocompleteData } from './opensearch_sql_autocomplete'; import { SQL_SYMBOLS } from './constants'; import { QuerySuggestion, QuerySuggestionGetFnArgs } from '../../autocomplete'; -import { fetchFieldSuggestions, fetchTableSchemas, parseQuery } from '../shared/utils'; -import { IDataFrameResponse, IFieldType } from '../../../common'; +import { fetchFieldSuggestions, parseQuery } from '../shared/utils'; import { SuggestionItemDetailsTags } from '../shared/constants'; export interface SuggestionParams { @@ -71,8 +55,6 @@ export const getSuggestions = async ({ type: monaco.languages.CompletionItemKind.Function, insertText: af, detail: SuggestionItemDetailsTags.AggregateFunction, - start: 0, - end: 0, })) ); } @@ -85,8 +67,6 @@ export const getSuggestions = async ({ type: monaco.languages.CompletionItemKind.Keyword, insertText: sk.value, detail: SuggestionItemDetailsTags.Keyword, - start: 0, - end: 0, })) ); } diff --git a/src/plugins/data/public/autocomplete/providers/query_suggestion_provider.ts b/src/plugins/data/public/autocomplete/providers/query_suggestion_provider.ts index c60b2d683def..aebb4087d9db 100644 --- a/src/plugins/data/public/autocomplete/providers/query_suggestion_provider.ts +++ b/src/plugins/data/public/autocomplete/providers/query_suggestion_provider.ts @@ -79,6 +79,7 @@ export interface MonacoCompatibleQuerySuggestion text: string; detail: string; insertText?: string; + replacePosition?: monaco.Range; } /** @public **/ diff --git a/src/plugins/data/public/ui/query_editor/query_editor.tsx b/src/plugins/data/public/ui/query_editor/query_editor.tsx index 1f1f4213132c..dd0fec884f6a 100644 --- a/src/plugins/data/public/ui/query_editor/query_editor.tsx +++ b/src/plugins/data/public/ui/query_editor/query_editor.tsx @@ -18,9 +18,15 @@ import classNames from 'classnames'; import { isEqual } from 'lodash'; import React, { Component, createRef, RefObject } from 'react'; import { monaco } from '@osd/monaco'; -import { IDataPluginServices, IFieldType, IIndexPattern, Query, TimeRange } from '../..'; +import { + IDataPluginServices, + IFieldType, + IIndexPattern, + Query, + QuerySuggestion, + TimeRange, +} from '../..'; import { OpenSearchDashboardsReactContextValue } from '../../../../opensearch_dashboards_react/public'; -import { QuerySuggestion } from '../../autocomplete'; import { fromUser, getQueryLog, PersistedLog, toUser } from '../../query'; import { SuggestionsListSize } from '../typeahead/suggestions_component'; import { QueryLanguageSelector } from './language_selector'; @@ -30,6 +36,7 @@ import { DatasetSelector } from '../dataset_selector'; import { QueryControls } from '../../query/query_string/language_service/get_query_control_links'; import { RecentQueriesTable } from '../../query/query_string/language_service/recent_query'; import { DefaultInputProps } from './editors'; +import { MonacoCompatibleQuerySuggestion } from '../../autocomplete/providers/query_suggestion_provider'; export interface QueryEditorProps { query: Query; @@ -279,7 +286,7 @@ export default class QueryEditorUI extends Component { // current completion item range being given as last 'word' at pos const wordUntil = model.getWordUntilPosition(position); - const range = new monaco.Range( + const defaultRange = new monaco.Range( position.lineNumber, wordUntil.startColumn, position.lineNumber, @@ -289,13 +296,17 @@ export default class QueryEditorUI extends Component { return { suggestions: suggestions && suggestions.length > 0 - ? suggestions.map((s: QuerySuggestion) => ({ - label: s.text, - kind: s.type as monaco.languages.CompletionItemKind, - insertText: s.insertText ?? s.text, - range, - detail: 'detail' in s ? s.detail : '', - })) + ? suggestions + .filter((s) => 'detail' in s) // remove suggestion not of type MonacoCompatible + .map((s: MonacoCompatibleQuerySuggestion) => { + return { + label: s.text, + kind: s.type as monaco.languages.CompletionItemKind, + insertText: s.insertText ?? s.text, + range: s.replacePosition ?? defaultRange, + detail: s.detail, + }; + }) : [], incomplete: false, };