[ES|QL] improve SORT command suggestions (#188579)

## Summary

- Suggests options in uppercase
- Applies syntax highlighting

**Before**


https://github.com/user-attachments/assets/5f04d8fc-d61a-4779-906b-a7f4f42b4014

**After**


https://github.com/user-attachments/assets/cd585306-020a-4a55-867a-affe373666f6

---------

Co-authored-by: Stratoula Kalafateli <efstratia.kalafateli@elastic.co>
This commit is contained in:
Drew Tate 2024-07-22 08:06:27 -06:00 committed by GitHub
parent 7f3f757a38
commit 2438c36fd9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 24 additions and 11 deletions

View file

@ -329,8 +329,8 @@ describe('autocomplete', () => {
...getFieldNamesByType('any'),
...getFunctionSignaturesByReturnType('sort', 'any', { evalMath: true }),
]);
testSuggestions('from a | sort stringField ', ['asc', 'desc', ',', '|']);
testSuggestions('from a | sort stringField desc ', ['nulls first', 'nulls last', ',', '|']);
testSuggestions('from a | sort stringField ', ['ASC', 'DESC', ',', '|']);
testSuggestions('from a | sort stringField desc ', ['NULLS FIRST', 'NULLS LAST', ',', '|']);
// @TODO: improve here
// testSuggestions('from a | sort stringField desc ', ['first', 'last']);
});

View file

@ -372,8 +372,8 @@ export const commandDefinitions: CommandDefinition[] = [
multipleParams: true,
params: [
{ name: 'expression', type: 'any' },
{ name: 'direction', type: 'string', optional: true, values: ['asc', 'desc'] },
{ name: 'nulls', type: 'string', optional: true, values: ['nulls first', 'nulls last'] },
{ name: 'direction', type: 'string', optional: true, values: ['ASC', 'DESC'] },
{ name: 'nulls', type: 'string', optional: true, values: ['NULLS FIRST', 'NULLS LAST'] },
],
},
},

View file

@ -78,14 +78,13 @@ export const buildESQlTheme = (): monaco.editor.IStandaloneThemeData => ({
'as',
'expr_ws',
'limit',
'nulls_ordering_direction',
'nulls_ordering',
'null',
'enrich',
'on',
'with',
'asc',
'desc',
'nulls_order',
],
euiThemeVars.euiColorAccentText,
true // isBold

View file

@ -13,9 +13,7 @@ function nonNullable<T>(value: T | undefined): value is T {
return value != null;
}
export function enrichTokensWithFunctionsMetadata(
tokens: monaco.languages.IToken[]
): monaco.languages.IToken[] {
export function addFunctionTokens(tokens: monaco.languages.IToken[]): monaco.languages.IToken[] {
// need to trim spaces as "abs (arg)" is still valid as function
const myTokensWithoutSpaces = tokens.filter(
({ scopes }) => scopes !== 'expr_ws' + ESQL_TOKEN_POSTFIX
@ -34,3 +32,18 @@ export function enrichTokensWithFunctionsMetadata(
}
return [...tokens];
}
export function addNullsOrder(tokens: monaco.languages.IToken[]): void {
const nullsIndex = tokens.findIndex((token) => token.scopes === 'nulls' + ESQL_TOKEN_POSTFIX);
if (
// did we find a "nulls"?
nullsIndex > -1 &&
// is the next non-whitespace token an order?
['first' + ESQL_TOKEN_POSTFIX, 'last' + ESQL_TOKEN_POSTFIX].includes(
tokens[nullsIndex + 2]?.scopes
)
) {
tokens[nullsIndex].scopes = 'nulls_order' + ESQL_TOKEN_POSTFIX;
tokens.splice(nullsIndex + 1, 2);
}
}

View file

@ -15,7 +15,7 @@ import { ESQLLineTokens } from './esql_line_tokens';
import { ESQLState } from './esql_state';
import { ESQL_TOKEN_POSTFIX } from './constants';
import { enrichTokensWithFunctionsMetadata } from './esql_token_helpers';
import { addFunctionTokens, addNullsOrder } from './esql_token_helpers';
const EOF = -1;
@ -77,7 +77,8 @@ export class ESQLTokensProvider implements monaco.languages.TokensProvider {
// special treatment for functions
// the previous custom Kibana grammar baked functions directly as tokens, so highlight was easier
// The ES grammar doesn't have the token concept of "function"
const tokensWithFunctions = enrichTokensWithFunctionsMetadata(myTokens);
const tokensWithFunctions = addFunctionTokens(myTokens);
addNullsOrder(tokensWithFunctions);
return new ESQLLineTokens(tokensWithFunctions, prevState.getLineNumber() + 1);
}