diff --git a/packages/kbn-esql-validation-autocomplete/src/autocomplete/__tests__/autocomplete.suggest.eval.test.ts b/packages/kbn-esql-validation-autocomplete/src/autocomplete/__tests__/autocomplete.suggest.eval.test.ts index c07b065b23ce..cc04ccb407e6 100644 --- a/packages/kbn-esql-validation-autocomplete/src/autocomplete/__tests__/autocomplete.suggest.eval.test.ts +++ b/packages/kbn-esql-validation-autocomplete/src/autocomplete/__tests__/autocomplete.suggest.eval.test.ts @@ -559,7 +559,7 @@ describe('autocomplete.suggest', () => { test('case', async () => { const { assertSuggestions } = await setup(); const comparisonOperators = ['==', '!=', '>', '<', '>=', '<='] - .map((op) => `${op} `) + .map((op) => `${op}`) .concat(','); // case( / ) suggest any field/eval function in this position as first argument @@ -626,7 +626,6 @@ describe('autocomplete.suggest', () => { // Notice no extra space after field name ...getFieldNamesByType('any').map((field) => `${field}`), ...getFunctionSignaturesByReturnType('eval', 'any', { scalar: true }, undefined, []), - 'var0 = ', ]); // case( field > 0, >) suggests fields like normal diff --git a/packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.ts b/packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.ts index 50937abbde9f..a6ee324fd262 100644 --- a/packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.ts +++ b/packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.ts @@ -112,6 +112,7 @@ import { } from '../definitions/types'; import { metadataOption } from '../definitions/options'; import { comparisonFunctions } from '../definitions/builtin'; +import { countBracketsUnclosed } from '../shared/helpers'; type GetFieldsByTypeFn = ( type: string | string[], @@ -559,8 +560,11 @@ async function getExpressionSuggestionsByType( const fieldsMap: Map = await (argDef ? getFieldsMap() : new Map()); const anyVariables = collectVariables(commands, fieldsMap, innerText); + const previousWord = findPreviousWord(innerText); // enrich with assignment has some special rules who are handled somewhere else - const canHaveAssignments = ['eval', 'stats', 'row'].includes(command.name); + const canHaveAssignments = + ['eval', 'stats', 'row'].includes(command.name) && + !comparisonFunctions.map((fn) => fn.name).includes(previousWord); const references = { fields: fieldsMap, variables: anyVariables }; @@ -1427,10 +1431,9 @@ async function getFunctionArgsSuggestions( suggestions.push( ...comparisonFunctions.map(({ name, description }) => ({ label: name, - text: name + ' ', + text: name, kind: 'Function' as ItemKind, detail: description, - command: TRIGGER_SUGGESTION_COMMAND, })) ); } @@ -1791,10 +1794,13 @@ async function getOptionArgsSuggestions( openSuggestions: true, })) ); + // Checks if cursor is still within function () + // by checking if the marker editor/cursor is within an unclosed parenthesis + const canHaveAssignment = countBracketsUnclosed('(', innerText) === 0; if (option.name === 'by') { // Add quick snippet for for stats ... by bucket(<>) - if (command.name === 'stats') { + if (command.name === 'stats' && canHaveAssignment) { suggestions.push({ label: i18n.translate( 'kbn-esql-validation-autocomplete.esql.autocomplete.addDateHistogram', @@ -1825,12 +1831,13 @@ async function getOptionArgsSuggestions( { functions: true, fields: false, - } + }, + { ignoreFn: canHaveAssignment ? [] : ['bucket', 'case'] } )) ); } - if (command.name === 'stats' && isNewExpression) { + if (command.name === 'stats' && isNewExpression && canHaveAssignment) { suggestions.push(buildNewVarDefinition(findNewVariable(anyVariables))); } }