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 (#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 #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
mengweieric and opensearch-changeset-bot[bot] committed Aug 20, 2024
1 parent 389ad1b commit 23798a0
Show file tree
Hide file tree
Showing 6 changed files with 40 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');

Check warning on line 69 in src/plugins/data/public/antlr/opensearch_sql/code_completion.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/antlr/opensearch_sql/code_completion.ts#L69

Added line #L69 was not covered by tests

if (columns && fieldTypes) {
finalSuggestions.push(
...columns.values.map((col: string) => ({
...columns.values.map((col: string, index: number) => ({

Check warning on line 73 in src/plugins/data/public/antlr/opensearch_sql/code_completion.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/antlr/opensearch_sql/code_completion.ts#L73

Added line #L73 was not covered by tests
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
6 changes: 6 additions & 0 deletions src/plugins/data/public/antlr/shared/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -76,5 +76,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 @@ -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 });

Expand Down Expand Up @@ -295,6 +292,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 23798a0

Please sign in to comment.