Skip to content

Commit

Permalink
[ES|QL] Suggest triple quotes when the user selects the KQL / QSTR (
Browse files Browse the repository at this point in the history
  • Loading branch information
darnautov authored Feb 20, 2025
1 parent 33fd527 commit c8b75e4
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@ const functionEnrichments: Record<string, RecursivePartial<FunctionDefinition>>
count: {
signatures: [{ params: [{ supportsWildcard: true }] }],
},
qstr: {
customParametersSnippet: `"""$0"""`,
},
kql: {
customParametersSnippet: `"""$0"""`,
},
};

const convertDateTime = (s: string) => (s === 'datetime' ? 'date' : s);
Expand Down Expand Up @@ -330,7 +336,7 @@ function getFunctionDefinition(ESFunctionDefinition: Record<string, any>): Funct
description: undefined,
...(FULL_TEXT_SEARCH_FUNCTIONS.includes(ESFunctionDefinition.name)
? // Default to false. If set to true, this parameter does not accept a function or literal, only fields.
idx === 0
param.name === 'field'
? { fieldsOnly: true }
: { constantOnly: true }
: {}),
Expand Down Expand Up @@ -810,7 +816,8 @@ function printGeneratedFunctionsFile(
functionDefinition: FunctionDefinition,
functionNames: string[]
) => {
const { type, name, description, alias, signatures, operator } = functionDefinition;
const { type, name, description, alias, signatures, operator, customParametersSnippet } =
functionDefinition;

let functionName = operator?.toLowerCase() ?? name.toLowerCase();
if (functionName.includes('not')) {
Expand All @@ -832,7 +839,11 @@ function printGeneratedFunctionsFile(
supportedCommands: ${JSON.stringify(functionDefinition.supportedCommands)},
supportedOptions: ${JSON.stringify(functionDefinition.supportedOptions)},
validate: ${functionDefinition.validate || 'undefined'},
examples: ${JSON.stringify(functionDefinition.examples || [])},
examples: ${JSON.stringify(functionDefinition.examples || [])},${
customParametersSnippet
? `\ncustomParametersSnippet: ${JSON.stringify(customParametersSnippet)},`
: ''
}
}`;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
getFunctionSignaturesByReturnType,
setup,
} from './helpers';
import { FULL_TEXT_SEARCH_FUNCTIONS } from '../../shared/constants';

describe('WHERE <expression>', () => {
const allEvalFns = getFunctionSignaturesByReturnType('where', 'any', {
Expand All @@ -40,7 +41,7 @@ describe('WHERE <expression>', () => {
.map((name) => `${name} `)
.map(attachTriggerCommand),
attachTriggerCommand('var0 '),
...allEvalFns.filter((fn) => fn.label !== 'QSTR'),
...allEvalFns.filter((fn) => fn.label !== 'QSTR' && fn.label !== 'KQL'),
],
{
callbacks: {
Expand Down Expand Up @@ -150,7 +151,7 @@ describe('WHERE <expression>', () => {
...getFieldNamesByType('any')
.map((field) => `${field} `)
.map(attachTriggerCommand),
...allEvalFns.filter((fn) => fn.label !== 'QSTR' && fn.label !== 'MATCH'),
...allEvalFns.filter((fn) => !FULL_TEXT_SEARCH_FUNCTIONS.includes(fn.label!.toLowerCase())),
]);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export function getFunctionSignaturesByReturnType(
})
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
.map<PartialSuggestionWithText>((definition) => {
const { type, name, signatures } = definition;
const { type, name, signatures, customParametersSnippet } = definition;

if (type === 'operator') {
return {
Expand All @@ -226,7 +226,9 @@ export function getFunctionSignaturesByReturnType(
};
}
return {
text: `${name.toUpperCase()}($0)`,
text: customParametersSnippet
? `${name.toUpperCase()}(${customParametersSnippet})`
: `${name.toUpperCase()}($0)`,
label: name.toUpperCase(),
};
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,14 @@ export async function suggest({
break;

case 'empty_expression':
// Don't suggest MATCH or QSTR after unsupported commands
// Don't suggest MATCH, QSTR or KQL after unsupported commands
const priorCommands = previousCommands?.map((a) => a.name) ?? [];
const ignored = [];
if (priorCommands.some((c) => UNSUPPORTED_COMMANDS_BEFORE_MATCH.has(c))) {
ignored.push('match');
}
if (priorCommands.some((c) => UNSUPPORTED_COMMANDS_BEFORE_QSTR.has(c))) {
ignored.push('qstr');
ignored.push('kql', 'qstr');
}
const last = previousCommands?.[previousCommands.length - 1];
let columnSuggestions: SuggestionRawDefinition[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,14 @@ export function getFunctionSuggestion(fn: FunctionDefinition): SuggestionRawDefi
detail = `[${techPreviewLabel}] ${detail}`;
}
const fullSignatures = getFunctionSignatures(fn, { capitalize: true, withTypes: true });

let text = `${fn.name.toUpperCase()}($0)`;
if (fn.customParametersSnippet) {
text = `${fn.name.toUpperCase()}(${fn.customParametersSnippet})`;
}
return {
label: fn.name.toUpperCase(),
text: `${fn.name.toUpperCase()}($0)`,
text,
asSnippet: true,
kind: 'Function',
detail,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2734,7 +2734,7 @@ const kqlDefinition: FunctionDefinition = {
name: 'query',
type: 'keyword',
optional: false,
fieldsOnly: true,
constantOnly: true,
},
],
returnType: 'boolean',
Expand All @@ -2745,7 +2745,7 @@ const kqlDefinition: FunctionDefinition = {
name: 'query',
type: 'text',
optional: false,
fieldsOnly: true,
constantOnly: true,
},
],
returnType: 'boolean',
Expand All @@ -2757,6 +2757,7 @@ const kqlDefinition: FunctionDefinition = {
examples: [
'FROM books \n| WHERE KQL("author: Faulkner")\n| KEEP book_no, author \n| SORT book_no \n| LIMIT 5',
],
customParametersSnippet: '"""$0"""',
};

// Do not edit this manually... generated by scripts/generate_function_definitions.ts
Expand Down Expand Up @@ -7244,7 +7245,7 @@ const qstrDefinition: FunctionDefinition = {
name: 'query',
type: 'keyword',
optional: false,
fieldsOnly: true,
constantOnly: true,
},
],
returnType: 'boolean',
Expand All @@ -7255,7 +7256,7 @@ const qstrDefinition: FunctionDefinition = {
name: 'query',
type: 'text',
optional: false,
fieldsOnly: true,
constantOnly: true,
},
],
returnType: 'boolean',
Expand All @@ -7267,6 +7268,7 @@ const qstrDefinition: FunctionDefinition = {
examples: [
'FROM books \n| WHERE QSTR("author: Faulkner")\n| KEEP book_no, author \n| SORT book_no \n| LIMIT 5',
],
customParametersSnippet: '"""$0"""',
};

// Do not edit this manually... generated by scripts/generate_function_definitions.ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ export interface FunctionDefinition {
examples?: string[];
validate?: (fnDef: ESQLFunction) => ESQLMessage[];
operator?: string;
customParametersSnippet?: string;
}

export interface CommandSuggestParams<CommandName extends string> {
Expand Down

0 comments on commit c8b75e4

Please sign in to comment.