diff --git a/packages/kbn-esql-validation-autocomplete/src/autocomplete/__tests__/autocomplete.command.stats.test.ts b/packages/kbn-esql-validation-autocomplete/src/autocomplete/__tests__/autocomplete.command.stats.test.ts index dba7785c7ae58..1f6bd93359311 100644 --- a/packages/kbn-esql-validation-autocomplete/src/autocomplete/__tests__/autocomplete.command.stats.test.ts +++ b/packages/kbn-esql-validation-autocomplete/src/autocomplete/__tests__/autocomplete.command.stats.test.ts @@ -7,6 +7,7 @@ */ import { ESQL_COMMON_NUMERIC_TYPES, ESQL_NUMBER_TYPES } from '../../shared/esql_types'; +import { ADD_DATE_HISTOGRAM_SNIPPET } from '../factories'; import { roundParameterTypes } from './constants'; import { setup, getFunctionSignaturesByReturnType, getFieldNamesByType } from './helpers'; @@ -213,6 +214,7 @@ describe('autocomplete.suggest', () => { const { assertSuggestions } = await setup(); const expected = [ 'var0 = ', + ADD_DATE_HISTOGRAM_SNIPPET, ...getFieldNamesByType('any').map((field) => `${field} `), ...allEvaFunctions, ...allGroupingFunctions, @@ -235,6 +237,7 @@ describe('autocomplete.suggest', () => { const fields = getFieldNamesByType('any').map((field) => `${field} `); await assertSuggestions('from a | stats a=c by d, /', [ 'var0 = ', + ADD_DATE_HISTOGRAM_SNIPPET, ...fields, ...allEvaFunctions, ...allGroupingFunctions, @@ -246,6 +249,7 @@ describe('autocomplete.suggest', () => { ]); await assertSuggestions('from a | stats avg(b) by c, /', [ 'var0 = ', + ADD_DATE_HISTOGRAM_SNIPPET, ...fields, ...getFunctionSignaturesByReturnType('eval', 'any', { scalar: true }), ...allGroupingFunctions, @@ -267,11 +271,13 @@ describe('autocomplete.suggest', () => { ...allGroupingFunctions, ]); await assertSuggestions('from a | stats avg(b) by var0 = /', [ + ADD_DATE_HISTOGRAM_SNIPPET, ...getFieldNamesByType('any').map((field) => `${field} `), ...allEvaFunctions, ...allGroupingFunctions, ]); await assertSuggestions('from a | stats avg(b) by c, var0 = /', [ + ADD_DATE_HISTOGRAM_SNIPPET, ...getFieldNamesByType('any').map((field) => `${field} `), ...allEvaFunctions, ...allGroupingFunctions, diff --git a/packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.test.ts b/packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.test.ts index a213cbf96de1c..5ca845a8c586c 100644 --- a/packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.test.ts +++ b/packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.test.ts @@ -10,7 +10,12 @@ import { suggest } from './autocomplete'; import { evalFunctionDefinitions } from '../definitions/functions'; import { timeUnitsToSuggest } from '../definitions/literals'; import { commandDefinitions as unmodifiedCommandDefinitions } from '../definitions/commands'; -import { getSafeInsertText, TIME_SYSTEM_PARAMS, TRIGGER_SUGGESTION_COMMAND } from './factories'; +import { + ADD_DATE_HISTOGRAM_SNIPPET, + getSafeInsertText, + TIME_SYSTEM_PARAMS, + TRIGGER_SUGGESTION_COMMAND, +} from './factories'; import { camelCase } from 'lodash'; import { getAstAndSyntaxErrors } from '@kbn/esql-ast'; import { @@ -638,6 +643,7 @@ describe('autocomplete', () => { // STATS argument BY expression testSuggestions('FROM index1 | STATS field BY f/', [ 'var0 = ', + ADD_DATE_HISTOGRAM_SNIPPET, ...getFunctionSignaturesByReturnType('stats', 'any', { grouping: true, scalar: true }), ...getFieldNamesByType('any').map((field) => `${field} `), ]); @@ -850,6 +856,7 @@ describe('autocomplete', () => { 'by' ); testSuggestions('FROM a | STATS AVG(numberField) BY /', [ + 'BUCKET($0, 20, ?start, ?end)', attachTriggerCommand('var0 = '), ...getFieldNamesByType('any') .map((field) => `${field} `) @@ -859,6 +866,7 @@ describe('autocomplete', () => { // STATS argument BY assignment (checking field suggestions) testSuggestions('FROM a | STATS AVG(numberField) BY var0 = /', [ + 'BUCKET($0, 20, ?start, ?end)', ...getFieldNamesByType('any') .map((field) => `${field} `) .map(attachTriggerCommand), diff --git a/packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.ts b/packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.ts index 0197efec74ef3..d6d465e27d630 100644 --- a/packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.ts +++ b/packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.ts @@ -78,6 +78,7 @@ import { getDateLiterals, buildFieldsDefinitionsWithMetadata, TRIGGER_SUGGESTION_COMMAND, + ADD_DATE_HISTOGRAM_SNIPPET, } from './factories'; import { EDITOR_MARKER, SINGLE_BACKTICK, METADATA_FIELDS } from '../shared/constants'; import { getAstContext, removeMarkerArgFromArgsList } from '../shared/context'; @@ -1767,7 +1768,7 @@ async function getOptionArgsSuggestions( defaultMessage: 'Add date histogram', } ), - text: `BUCKET($0, 20, ?start, ?end)`, + text: ADD_DATE_HISTOGRAM_SNIPPET, asSnippet: true, kind: 'Function', detail: i18n.translate( diff --git a/packages/kbn-esql-validation-autocomplete/src/autocomplete/factories.ts b/packages/kbn-esql-validation-autocomplete/src/autocomplete/factories.ts index 4ac3b3b24d1f6..06238a8feb335 100644 --- a/packages/kbn-esql-validation-autocomplete/src/autocomplete/factories.ts +++ b/packages/kbn-esql-validation-autocomplete/src/autocomplete/factories.ts @@ -30,6 +30,7 @@ const allFunctions = statsAggregationFunctionDefinitions .concat(groupingFunctionDefinitions); export const TIME_SYSTEM_PARAMS = ['?t_start', '?t_end']; +export const ADD_DATE_HISTOGRAM_SNIPPET = 'BUCKET($0, 20, ?start, ?end)'; export const TRIGGER_SUGGESTION_COMMAND = { title: 'Trigger Suggestion Dialog',