diff --git a/changelogs/fragments/7758.yml b/changelogs/fragments/7758.yml new file mode 100644 index 000000000000..ecc1f7b1defa --- /dev/null +++ b/changelogs/fragments/7758.yml @@ -0,0 +1,2 @@ +feat: +- Minor interface change and move suggestion provider registration location ([#7758](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7758)) \ No newline at end of file diff --git a/packages/osd-monaco/src/xjson/lexer_rules/opensearchsql.ts b/packages/osd-monaco/src/xjson/lexer_rules/opensearchsql.ts index b204c63e83c4..0ff29b71c09d 100644 --- a/packages/osd-monaco/src/xjson/lexer_rules/opensearchsql.ts +++ b/packages/osd-monaco/src/xjson/lexer_rules/opensearchsql.ts @@ -155,3 +155,7 @@ export const lexerRules = { ], }, } as monaco.languages.IMonarchLanguage; + +monaco.languages.register({ + id: ID, +}); 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 d9aeedf727cf..e41767dcf868 100644 --- a/src/plugins/data/public/antlr/opensearch_sql/code_completion.ts +++ b/src/plugins/data/public/antlr/opensearch_sql/code_completion.ts @@ -25,6 +25,7 @@ import { SQL_SYMBOLS } from './constants'; import { QuerySuggestion, QuerySuggestionGetFnArgs } from '../../autocomplete'; import { fetchTableSchemas } from '../shared/utils'; import { IDataFrameResponse, IFieldType } from '../../../common'; +import { SuggestionItemDetailsTags } from '../shared/constants'; export interface SuggestionParams { position: monaco.Position; @@ -65,13 +66,17 @@ export const getSuggestions = async ({ (schemas as IDataFrameResponse[]).forEach((schema: IDataFrameResponse) => { if ('body' in schema && schema.body && 'fields' in schema.body) { const columns = schema.body.fields.find((col: IFieldType) => col.name === 'COLUMN_NAME'); - const fieldTypes = schema.body.fields.find((col: IFieldType) => col.name === 'DATA_TYPE'); + const fieldTypes = schema.body.fields.find((col: IFieldType) => col.name === 'TYPE_NAME'); if (columns && fieldTypes) { finalSuggestions.push( - ...columns.values.map((col: string) => ({ + ...columns.values.map((col: string, index: number) => ({ text: col, type: monaco.languages.CompletionItemKind.Field, + insertText: col, + detail: fieldTypes.values[index], + start: 0, + end: 0, })) ); } @@ -85,6 +90,10 @@ export const getSuggestions = async ({ ...SQL_SYMBOLS.AGREGATE_FUNCTIONS.map((af) => ({ text: af, type: monaco.languages.CompletionItemKind.Function, + insertText: af, + detail: SuggestionItemDetailsTags.AggregateFunction, + start: 0, + end: 0, })) ); } @@ -95,6 +104,10 @@ export const getSuggestions = async ({ ...suggestions.suggestKeywords.map((sk) => ({ text: sk.value, type: monaco.languages.CompletionItemKind.Keyword, + insertText: sk.value, + detail: SuggestionItemDetailsTags.Keyword, + start: 0, + end: 0, })) ); } diff --git a/src/plugins/data/public/antlr/shared/constants.ts b/src/plugins/data/public/antlr/shared/constants.ts index d0dbc0f70eac..f990441aee97 100644 --- a/src/plugins/data/public/antlr/shared/constants.ts +++ b/src/plugins/data/public/antlr/shared/constants.ts @@ -190,3 +190,9 @@ export const fieldNameSuggestions: Array<{ text: string; type: number; insertTex ]; export const fieldNameWithNotSuggestions = fieldNameSuggestions.concat(notOperatorSuggestion); + +// suggestion item details tags +export const enum SuggestionItemDetailsTags { + Keyword = 'Keyword', + AggregateFunction = 'Aggregate Function', +} 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 9b41fe8bb67f..166760e093a7 100644 --- a/src/plugins/data/public/autocomplete/providers/query_suggestion_provider.ts +++ b/src/plugins/data/public/autocomplete/providers/query_suggestion_provider.ts @@ -61,7 +61,7 @@ export interface QuerySuggestionGetFnArgs { /** @public **/ export interface QuerySuggestionBasic { - type: QuerySuggestionTypes | monaco.languages.CompletionItemKind; + type: QuerySuggestionTypes; description?: string | JSX.Element; end: number; start: number; @@ -76,5 +76,15 @@ export interface QuerySuggestionField extends QuerySuggestionBasic { field: IFieldType; } +export interface SqlMonacoCompatibleQuerySuggestion + extends Pick { + type: monaco.languages.CompletionItemKind; + text: string; + detail: string; +} + /** @public **/ -export type QuerySuggestion = QuerySuggestionBasic | QuerySuggestionField; +export type QuerySuggestion = + | QuerySuggestionBasic + | QuerySuggestionField + | SqlMonacoCompatibleQuerySuggestion; 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 2238abbc5b1b..e93a13169c53 100644 --- a/src/plugins/data/public/ui/query_editor/query_editor.tsx +++ b/src/plugins/data/public/ui/query_editor/query_editor.tsx @@ -22,9 +22,6 @@ import { SimpleDataSet } from '../../../common'; import { createDQLEditor, createDefaultEditor } from './editors'; import { getQueryService, getIndexPatterns } from '../../services'; -const LANGUAGE_ID_SQL = 'SQL'; -monaco.languages.register({ id: LANGUAGE_ID_SQL }); - const LANGUAGE_ID_KUERY = 'kuery'; monaco.languages.register({ id: LANGUAGE_ID_KUERY }); @@ -295,6 +292,7 @@ export default class QueryEditorUI extends Component { kind: s.type as monaco.languages.CompletionItemKind, insertText: s.insertText ?? s.text, range, + detail: 'detail' in s ? s.detail : '', })) : [], incomplete: false,