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] open the suggestion menu automatically in more places #189585

Merged
merged 22 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 21 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 @@ -14,6 +14,9 @@ const visibleIndices = indexes
.map(({ name, suggestedAs }) => suggestedAs || name)
.sort();

const addTrailingSpace = (strings: string[], predicate: (s: string) => boolean = (_s) => true) =>
strings.map((string) => (predicate(string) ? `${string} ` : string));

const metadataFields = [...METADATA_FIELDS].sort();

describe('autocomplete.suggest', () => {
Expand All @@ -33,17 +36,17 @@ describe('autocomplete.suggest', () => {
test('suggests visible indices on space', async () => {
const { assertSuggestions } = await setup();

await assertSuggestions('from /', visibleIndices);
await assertSuggestions('FROM /', visibleIndices);
await assertSuggestions('from /index', visibleIndices);
await assertSuggestions('from /', addTrailingSpace(visibleIndices));
await assertSuggestions('FROM /', addTrailingSpace(visibleIndices));
await assertSuggestions('from /index', addTrailingSpace(visibleIndices));
});

test('suggests visible indices on comma', async () => {
const { assertSuggestions } = await setup();

await assertSuggestions('FROM a,/', visibleIndices);
await assertSuggestions('FROM a, /', visibleIndices);
await assertSuggestions('from *,/', visibleIndices);
await assertSuggestions('FROM a,/', addTrailingSpace(visibleIndices));
await assertSuggestions('FROM a, /', addTrailingSpace(visibleIndices));
await assertSuggestions('from *,/', addTrailingSpace(visibleIndices));
});

test('can suggest integration data sources', async () => {
Expand All @@ -52,17 +55,21 @@ describe('autocomplete.suggest', () => {
.filter(({ hidden }) => !hidden)
.map(({ name, suggestedAs }) => suggestedAs || name)
.sort();
const expectedSuggestions = addTrailingSpace(
visibleDataSources,
(s) => !integrations.find(({ name }) => name === s)
);
const { assertSuggestions, callbacks } = await setup();
const cb = {
...callbacks,
getSources: jest.fn().mockResolvedValue(dataSources),
};

assertSuggestions('from /', visibleDataSources, { callbacks: cb });
assertSuggestions('FROM /', visibleDataSources, { callbacks: cb });
assertSuggestions('FROM a,/', visibleDataSources, { callbacks: cb });
assertSuggestions('from a, /', visibleDataSources, { callbacks: cb });
assertSuggestions('from *,/', visibleDataSources, { callbacks: cb });
await assertSuggestions('from /', expectedSuggestions, { callbacks: cb });
await assertSuggestions('FROM /', expectedSuggestions, { callbacks: cb });
await assertSuggestions('FROM a,/', expectedSuggestions, { callbacks: cb });
await assertSuggestions('from a, /', expectedSuggestions, { callbacks: cb });
await assertSuggestions('from *,/', expectedSuggestions, { callbacks: cb });
});
});

Expand All @@ -71,7 +78,7 @@ describe('autocomplete.suggest', () => {

test('on <kbd>SPACE</kbd> without comma ",", suggests adding metadata', async () => {
const { assertSuggestions } = await setup();
const expected = ['METADATA $0', ',', '|'].sort();
const expected = ['METADATA $0', ',', '| '].sort();

await assertSuggestions('from a, b /', expected);
});
Expand All @@ -86,10 +93,10 @@ describe('autocomplete.suggest', () => {
test('on <kbd>SPACE</kbd> after "METADATA" column suggests command and pipe operators', async () => {
const { assertSuggestions } = await setup();

await assertSuggestions('from a, b [metadata _index /]', [',', '|']);
await assertSuggestions('from a, b metadata _index /', [',', '|']);
await assertSuggestions('from a, b metadata _index, _source /', [',', '|']);
await assertSuggestions(`from a, b metadata ${METADATA_FIELDS.join(', ')} /`, ['|']);
await assertSuggestions('from a, b [metadata _index /]', [',', '| ']);
await assertSuggestions('from a, b metadata _index /', [',', '| ']);
await assertSuggestions('from a, b metadata _index, _source /', [',', '| ']);
await assertSuggestions(`from a, b metadata ${METADATA_FIELDS.join(', ')} /`, ['| ']);
});

test('filters out already used metadata fields', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('autocomplete.suggest', () => {
describe('... <aggregates> ...', () => {
test('lists possible aggregations on space after command', async () => {
const { assertSuggestions } = await setup();
const expected = ['var0 =', ...allAggFunctions, ...allEvaFunctions];
const expected = ['var0 = ', ...allAggFunctions, ...allEvaFunctions];

await assertSuggestions('from a | stats /', expected);
await assertSuggestions('FROM a | STATS /', expected);
Expand All @@ -60,14 +60,14 @@ describe('autocomplete.suggest', () => {
test('on space after aggregate field', async () => {
const { assertSuggestions } = await setup();

await assertSuggestions('from a | stats a=min(b) /', ['BY $0', ',', '|']);
await assertSuggestions('from a | stats a=min(b) /', ['BY $0', ',', '| ']);
});

test('on space after aggregate field with comma', async () => {
const { assertSuggestions } = await setup();

await assertSuggestions('from a | stats a=max(b), /', [
'var0 =',
'var0 = ',
...allAggFunctions,
...allEvaFunctions,
]);
Expand All @@ -78,7 +78,7 @@ describe('autocomplete.suggest', () => {

await assertSuggestions('from a | stats by bucket(/', [
...getFieldNamesByType([...ESQL_COMMON_NUMERIC_TYPES, 'date']).map(
(field) => `${field},`
(field) => `${field}, `
),
...getFunctionSignaturesByReturnType('eval', ['date', ...ESQL_COMMON_NUMERIC_TYPES], {
scalar: true,
Expand Down Expand Up @@ -172,21 +172,21 @@ describe('autocomplete.suggest', () => {
test('when typing right paren', async () => {
const { assertSuggestions } = await setup();

await assertSuggestions('from a | stats a = min(b)/ | sort b', ['BY $0', ',', '|']);
await assertSuggestions('from a | stats a = min(b)/ | sort b', ['BY $0', ',', '| ']);
});

test('increments suggested variable name counter', async () => {
const { assertSuggestions } = await setup();

await assertSuggestions('from a | eval var0=round(b), var1=round(c) | stats /', [
'var2 =',
'var2 = ',
...allAggFunctions,
'var0',
'var1',
...allEvaFunctions,
]);
await assertSuggestions('from a | stats var0=min(b),var1=c,/', [
'var2 =',
'var2 = ',
...allAggFunctions,
...allEvaFunctions,
]);
Expand All @@ -197,8 +197,8 @@ describe('autocomplete.suggest', () => {
test('on space after "BY" keyword', async () => {
const { assertSuggestions } = await setup();
const expected = [
'var0 =',
...getFieldNamesByType('any'),
'var0 = ',
...getFieldNamesByType('any').map((field) => `${field} `),
...allEvaFunctions,
...allGroupingFunctions,
];
Expand All @@ -211,26 +211,27 @@ describe('autocomplete.suggest', () => {
test('on space after grouping field', async () => {
const { assertSuggestions } = await setup();

await assertSuggestions('from a | stats a=c by d /', [',', '|']);
await assertSuggestions('from a | stats a=c by d /', [',', '| ']);
});

test('after comma "," in grouping fields', async () => {
const { assertSuggestions } = await setup();

const fields = getFieldNamesByType('any').map((field) => `${field} `);
await assertSuggestions('from a | stats a=c by d, /', [
'var0 =',
...getFieldNamesByType('any'),
'var0 = ',
...fields,
...allEvaFunctions,
...allGroupingFunctions,
]);
await assertSuggestions('from a | stats a=min(b),/', [
'var0 =',
'var0 = ',
...allAggFunctions,
...allEvaFunctions,
]);
await assertSuggestions('from a | stats avg(b) by c, /', [
'var0 =',
...getFieldNamesByType('any'),
'var0 = ',
...fields,
...getFunctionSignaturesByReturnType('eval', 'any', { scalar: true }),
...allGroupingFunctions,
]);
Expand All @@ -251,12 +252,12 @@ describe('autocomplete.suggest', () => {
...allGroupingFunctions,
]);
await assertSuggestions('from a | stats avg(b) by var0 = /', [
...getFieldNamesByType('any'),
...getFieldNamesByType('any').map((field) => `${field} `),
...allEvaFunctions,
...allGroupingFunctions,
]);
await assertSuggestions('from a | stats avg(b) by c, var0 = /', [
...getFieldNamesByType('any'),
...getFieldNamesByType('any').map((field) => `${field} `),
...allEvaFunctions,
...allGroupingFunctions,
]);
Expand All @@ -265,11 +266,11 @@ describe('autocomplete.suggest', () => {
test('on space after expression right hand side operand', async () => {
const { assertSuggestions } = await setup();

await assertSuggestions('from a | stats avg(b) by doubleField % 2 /', [',', '|']);
await assertSuggestions('from a | stats avg(b) by doubleField % 2 /', [',', '| ']);

await assertSuggestions(
'from a | stats var0 = AVG(doubleField) BY var1 = BUCKET(dateField, 1 day)/',
[',', '|', '+ $0', '- $0']
[',', '| ', '+ $0', '- $0']
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const triggerCharacters = [',', '(', '=', ' '];
export const fields: Array<{ name: string; type: string; suggestedAs?: string }> = [
...[
'string',
'keyword',
'double',
'date',
'boolean',
Expand Down
Loading