Skip to content
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

[Step 2][ESQL] Autocomplete part #148006

Merged
merged 15 commits into from
Jan 4, 2023
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
9 changes: 0 additions & 9 deletions packages/kbn-monaco/src/es_ql/constants.ts

This file was deleted.

14 changes: 0 additions & 14 deletions packages/kbn-monaco/src/es_ql/index.ts

This file was deleted.

77 changes: 0 additions & 77 deletions packages/kbn-monaco/src/es_ql/lexer_rules/es_ql.ts

This file was deleted.

9 changes: 0 additions & 9 deletions packages/kbn-monaco/src/es_ql/lexer_rules/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@
* Side Public License, v 1.
*/

import React, { useRef, memo, useEffect, useState, useCallback } from 'react';
import React, { useRef, memo, useEffect, useState, useCallback, useMemo } from 'react';
import classNames from 'classnames';
import { SQLLang, monaco, ESQL_LANG_ID, ESQL_THEME_ID } from '@kbn/monaco';
import {
SQLLang,
monaco,
ESQL_LANG_ID,
ESQL_THEME_ID,
ESQLLang,
ESQLCustomAutocompleteCallbacks,
} from '@kbn/monaco';
import type { AggregateQuery } from '@kbn/es-query';
import { getAggregateQueryMode } from '@kbn/es-query';
import {
Expand Down Expand Up @@ -111,7 +118,7 @@ export const TextBasedLanguagesEditor = memo(function TextBasedLanguagesEditor({
const [documentationSections, setDocumentationSections] =
useState<LanguageDocumentationSections>();
const kibana = useKibana<IUnifiedSearchPluginServices>();
const { uiSettings } = kibana.services;
const { uiSettings, dataViews } = kibana.services;

const styles = textBasedLanguagedEditorStyles(
euiTheme,
Expand Down Expand Up @@ -331,6 +338,42 @@ export const TextBasedLanguagesEditor = memo(function TextBasedLanguagesEditor({
getDocumentation();
}, [language]);

const getSourceIdentifiers: ESQLCustomAutocompleteCallbacks['getSourceIdentifiers'] =
useCallback(() => {
return dataViews.getTitles();
}, [dataViews]);

const getFieldsIdentifiers: ESQLCustomAutocompleteCallbacks['getFieldsIdentifiers'] =
useMemo(() => {
let source: string;
let cachedSuggestions: string[] = [];

return async (ctx) => {
let data: string[] = [];
const sourceKey = ctx.userDefinedVariables.sourceIdentifiers.join(',');
if (sourceKey === source) {
return cachedSuggestions;
}
for (const s of ctx.userDefinedVariables.sourceIdentifiers) {
if (s) {
try {
const [dataView] = await dataViews.find(s, 1);
if (dataView) {
data = [...data, ...dataView.fields.map((f) => f.name)];
}
} catch (e) {
// nothing to be here
}
}
}

cachedSuggestions = data;
source = sourceKey;

return cachedSuggestions;
};
}, [dataViews]);

const codeEditorOptions: CodeEditorProps['options'] = {
automaticLayout: false,
accessibilitySupport: 'off',
Expand Down Expand Up @@ -520,6 +563,14 @@ export const TextBasedLanguagesEditor = memo(function TextBasedLanguagesEditor({
value={codeOneLiner || code}
options={codeEditorOptions}
width="100%"
suggestionProvider={
language === 'esql'
? ESQLLang.getSuggestionProvider?.({
getSourceIdentifiers,
getFieldsIdentifiers,
})
: undefined
}
onChange={onQueryUpdate}
editorDidMount={(editor) => {
editor1.current = editor;
Expand Down