Skip to content

Commit

Permalink
[ES|QL] improve SORT command suggestions (#188579)
Browse files Browse the repository at this point in the history
## 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>
  • Loading branch information
drewdaemon and stratoula authored Jul 22, 2024
1 parent 7f3f757 commit 2438c36
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'] },
],
},
},
Expand Down
3 changes: 1 addition & 2 deletions packages/kbn-monaco/src/esql/lib/esql_theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 16 additions & 3 deletions packages/kbn-monaco/src/esql/lib/esql_token_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
5 changes: 3 additions & 2 deletions packages/kbn-monaco/src/esql/lib/esql_tokens_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 2438c36

Please sign in to comment.