Skip to content

Commit

Permalink
Add more autocomplete tests for case() and update advance cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
qn895 committed Sep 4, 2024
1 parent 935e42f commit 950a9d3
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -549,5 +549,87 @@ describe('autocomplete.suggest', () => {
{ triggerCharacter: ' ' }
);
});

test('case', async () => {
const { assertSuggestions } = await setup();
const comparisonOperators = ['==', '!=', '>', '<', '>=', '<=']
.map((op) => `${op} `)
.concat(',');

// case( / ) suggest any field/eval function in this position as first argument
await assertSuggestions(
'from a | eval case(/)',
[
// With extra space after field name to open suggestions
...getFieldNamesByType('any').map((field) => `${field} `),
...getFunctionSignaturesByReturnType('eval', 'boolean', { scalar: true }, undefined, []),
],
{
triggerCharacter: ' ',
}
);

// case( field /) suggest comparison operators at this point to converge to a boolean
await assertSuggestions('from a | eval case( textField /)', comparisonOperators, {
triggerCharacter: ' ',
});
await assertSuggestions('from a | eval case( doubleField /)', comparisonOperators, {
triggerCharacter: ' ',
});
await assertSuggestions('from a | eval case( booleanField /)', comparisonOperators, {
triggerCharacter: ' ',
});

// case( field > /) suggest field/function of the same type of the right hand side to complete the boolean expression
await assertSuggestions(
'from a | eval case( keywordField != /)',
[
// Notice no extra space after field name
...getFieldNamesByType(['keyword', 'text', 'boolean']).map((field) => `${field}`),
...getFunctionSignaturesByReturnType(
'eval',
['keyword', 'text', 'boolean'],
{ scalar: true },
undefined,
[]
),
],
{
triggerCharacter: ' ',
}
);
await assertSuggestions(
'from a | eval case( integerField != /)',
[
// Notice no extra space after field name
...getFieldNamesByType(ESQL_COMMON_NUMERIC_TYPES).map((field) => `${field}`),
...getFunctionSignaturesByReturnType(
'eval',
ESQL_COMMON_NUMERIC_TYPES,
{ scalar: true },
undefined,
[]
),
],
{
triggerCharacter: ' ',
}
);

// case( field > 0, >) suggests fields like normal
await assertSuggestions(
'from a | eval case( integerField != doubleField, /)',
[
// With extra space after field name to open suggestions
...getFieldNamesByType('any').map((field) => `${field}`),
...getFunctionSignaturesByReturnType('eval', 'any', { scalar: true }, undefined, [
'case',
]),
],
{
triggerCharacter: ' ',
}
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,7 @@ async function getFunctionArgsSuggestions(
fnDefinition.type !== 'builtin' &&
!alreadyHasComma &&
!shouldBeBooleanCondition;
const shouldAdvanceCursor = hasMoreMandatoryArgs && fnDefinition.type !== 'builtin';

const suggestedConstants = uniq(
typesToSuggestNext
Expand Down Expand Up @@ -1437,8 +1438,8 @@ async function getFunctionArgsSuggestions(
[],
{
addComma: shouldAddComma,
advanceCursor: shouldAddComma,
openSuggestions: shouldAddComma,
advanceCursor: shouldAdvanceCursor,
openSuggestions: shouldAdvanceCursor,
}
),
true
Expand Down Expand Up @@ -1492,6 +1493,7 @@ async function getFunctionArgsSuggestions(
text: name + ' ',
kind: 'Function' as ItemKind,
detail: description,
command: TRIGGER_SUGGESTION_COMMAND,
}))
);
}
Expand Down

0 comments on commit 950a9d3

Please sign in to comment.