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

[8.x] [ES|QL] Fix new variables being suggested in incorrect places (#192405) #193500

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -111,6 +111,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[],
Expand Down Expand Up @@ -554,8 +555,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 +1426,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 +1789,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',
Expand Down Expand Up @@ -1820,12 +1826,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