Skip to content

Commit

Permalink
[ES|QL] insert assignment suggestion as a snippet (#180203)
Browse files Browse the repository at this point in the history
## Summary

Fix #179978

### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
drewdaemon and kibanamachine authored Apr 6, 2024
1 parent 65b5298 commit f04153b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { commandDefinitions } from '../definitions/commands';
import { TRIGGER_SUGGESTION_COMMAND } from './factories';
import { camelCase } from 'lodash';
import { getAstAndSyntaxErrors } from '@kbn/esql-ast';
import { SuggestionRawDefinition } from './types';

const triggerCharacters = [',', '(', '=', ' '];

Expand Down Expand Up @@ -200,14 +201,14 @@ function getPolicyFields(policyName: string) {
describe('autocomplete', () => {
type TestArgs = [
string,
string[],
Array<string | Partial<SuggestionRawDefinition>>,
(string | number)?,
Parameters<typeof createCustomCallbackMocks>?
];

const testSuggestionsFn = (
statement: string,
expected: string[],
expected: Array<string | Partial<SuggestionRawDefinition>>,
triggerCharacter: string | number = '',
customCallbacksArgs: Parameters<typeof createCustomCallbackMocks> = [
undefined,
Expand Down Expand Up @@ -242,10 +243,16 @@ describe('autocomplete', () => {
);
const suggestionInertTextSorted = suggestions
// simulate the editor behaviour for sorting suggestions
.sort((a, b) => (a.sortText || '').localeCompare(b.sortText || ''))
.map((i) => i.text);
.sort((a, b) => (a.sortText || '').localeCompare(b.sortText || ''));
for (const [index, receivedSuggestion] of suggestionInertTextSorted.entries()) {
expect(receivedSuggestion).toEqual(expected[index]);
if (typeof expected[index] === 'string') {
expect(receivedSuggestion.text).toEqual(expected[index]);
} else {
// check all properties that are defined in the expected suggestion
for (const [key, value] of Object.entries(expected[index])) {
expect(receivedSuggestion[key as keyof SuggestionRawDefinition]).toEqual(value);
}
}
}
}
);
Expand Down Expand Up @@ -571,7 +578,9 @@ describe('autocomplete', () => {
evalMath: true,
});
testSuggestions('from a | stats ', ['var0 =', ...allAggFunctions, ...allEvaFunctions]);
testSuggestions('from a | stats a ', ['= $0']);
testSuggestions('from a | stats a ', [
{ text: '= $0', asSnippet: true, command: TRIGGER_SUGGESTION_COMMAND },
]);
testSuggestions('from a | stats a=', [...allAggFunctions, ...allEvaFunctions]);
testSuggestions('from a | stats a=max(b) by ', [
'var0 =',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function getSuggestionBuiltinDefinition(fn: FunctionDefinition): Suggesti
return {
label: fn.name,
text: hasArgs ? `${fn.name} $0` : fn.name,
...(hasArgs ? { insertTextRules: 4 } : {}), // kbn-esql-validation-autocomplete.languages.CompletionItemInsertTextRule.InsertAsSnippet,
asSnippet: hasArgs,
kind: 'Operator',
detail: fn.description,
documentation: {
Expand Down

0 comments on commit f04153b

Please sign in to comment.