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

[ES|QL] Fix new variables being suggested in incorrect places #192405

Merged
merged 10 commits into from
Sep 19, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,8 +554,11 @@ async function getExpressionSuggestionsByType(
const fieldsMap: Map<string, ESQLRealField> = 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 };

Expand Down Expand Up @@ -1422,10 +1425,9 @@ async function getFunctionArgsSuggestions(
suggestions.push(
...comparisonFunctions.map<SuggestionRawDefinition>(({ name, description }) => ({
label: name,
text: name + ' ',
text: name,
kind: 'Function' as ItemKind,
detail: description,
command: TRIGGER_SUGGESTION_COMMAND,
}))
);
}
Expand Down Expand Up @@ -1786,10 +1788,13 @@ async function getOptionArgsSuggestions(
openSuggestions: true,
}))
);
// Checks if cursor is still within function ()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A great candidate for using the AST. I'm assuming the node wasn't available in this context?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct 👍

// 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',
Expand Down Expand Up @@ -1820,12 +1825,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)));
}
}
Expand Down