Skip to content

Commit

Permalink
[Autocomplete - SQL] Minor interface change to add suggestion type an…
Browse files Browse the repository at this point in the history
…d move suggestion provider registration location (opensearch-project#7758)

* add table/source as prefix to suggested fields

Signed-off-by: Eric <menwe@amazon.com>

* add type to column

Signed-off-by: Eric <menwe@amazon.com>

* move registeration to osd/monaco

Signed-off-by: Eric <menwe@amazon.com>

* add detail

Signed-off-by: Eric <menwe@amazon.com>

* Changeset file for PR opensearch-project#7758 created/updated

---------

Signed-off-by: Eric <menwe@amazon.com>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and paulstn committed Aug 30, 2024
1 parent e5c125d commit 98f5f7b
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 7 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/7758.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
feat:
- Minor interface change and move suggestion provider registration location ([#7758](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7758))
4 changes: 4 additions & 0 deletions packages/osd-monaco/src/xjson/lexer_rules/opensearchsql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,7 @@ export const lexerRules = {
],
},
} as monaco.languages.IMonarchLanguage;

monaco.languages.register({
id: ID,
});
17 changes: 15 additions & 2 deletions src/plugins/data/public/antlr/opensearch_sql/code_completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
}))
);
}
Expand All @@ -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,
}))
);
}
Expand All @@ -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,
}))
);
}
Expand Down
10 changes: 10 additions & 0 deletions src/plugins/data/public/antlr/shared/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

// suggestion item details tags
export const enum SuggestionItemDetailsTags {
Keyword = 'Keyword',
AggregateFunction = 'Aggregate Function',
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export interface QuerySuggestionGetFnArgs {

/** @public **/
export interface QuerySuggestionBasic {
type: QuerySuggestionTypes | monaco.languages.CompletionItemKind;
type: QuerySuggestionTypes;
description?: string | JSX.Element;
end: number;
start: number;
Expand All @@ -74,5 +74,15 @@ export interface QuerySuggestionField extends QuerySuggestionBasic {
field: IFieldType;
}

export interface SqlMonacoCompatibleQuerySuggestion
extends Pick<QuerySuggestionBasic, 'description' | 'insertText' | 'cursorIndex'> {
type: monaco.languages.CompletionItemKind;
text: string;
detail: string;
}

/** @public **/
export type QuerySuggestion = QuerySuggestionBasic | QuerySuggestionField;
export type QuerySuggestion =
| QuerySuggestionBasic
| QuerySuggestionField
| SqlMonacoCompatibleQuerySuggestion;
4 changes: 1 addition & 3 deletions src/plugins/data/public/ui/query_editor/query_editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ import { QueryControls } from '../../query/query_string/language_service/get_que
import { RecentQueriesTable } from '../../query/query_string/language_service/recent_query';
import { DefaultInputProps } from './editors';

const LANGUAGE_ID_SQL = 'SQL';
monaco.languages.register({ id: LANGUAGE_ID_SQL });

const LANGUAGE_ID_KUERY = 'kuery';
monaco.languages.register({ id: LANGUAGE_ID_KUERY });

Expand Down Expand Up @@ -300,6 +297,7 @@ export default class QueryEditorUI extends Component<Props, State> {
kind: s.type as monaco.languages.CompletionItemKind,
insertText: s.insertText ?? s.text,
range,
detail: 'detail' in s ? s.detail : '',
}))
: [],
incomplete: false,
Expand Down

0 comments on commit 98f5f7b

Please sign in to comment.