Skip to content

Commit

Permalink
separate DROP
Browse files Browse the repository at this point in the history
  • Loading branch information
drewdaemon committed Oct 28, 2024
1 parent d4dfe34 commit 88e9226
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import type { ESQLCommand } from '@kbn/esql-ast';
import {
findPreviousWord,
getLastCharFromTrimmed,
isColumnItem,
noCaseCompare,
} from '../../../shared/helpers';
import type { GetColumnsByTypeFn, SuggestionRawDefinition } from '../../types';
import { commaCompleteItem, pipeCompleteItem } from '../../complete_items';
import { handleFragment } from '../../helper';
import { TRIGGER_SUGGESTION_COMMAND } from '../../factories';

export async function suggest(
innerText: string,
command: ESQLCommand<'drop'>,
getColumnsByType: GetColumnsByTypeFn,
columnExists: (column: string) => boolean
): Promise<SuggestionRawDefinition[]> {
if (
/\s/.test(innerText[innerText.length - 1]) &&
getLastCharFromTrimmed(innerText) !== ',' &&
!noCaseCompare(findPreviousWord(innerText), 'drop')
) {
return [pipeCompleteItem, commaCompleteItem];
}

const alreadyDeclaredFields = command.args.filter(isColumnItem).map((arg) => arg.name);
const fieldSuggestions = await getColumnsByType('any', alreadyDeclaredFields);

return handleFragment(
innerText,
(fragment) => columnExists(fragment),
(_fragment: string, rangeToReplace?: { start: number; end: number }) => {
// KEEP fie<suggest>
return fieldSuggestions.map((suggestion) => ({
...suggestion,
text: suggestion.text,
command: TRIGGER_SUGGESTION_COMMAND,
rangeToReplace,
}));
},
(fragment: string, rangeToReplace: { start: number; end: number }) => {
// KEEP field<suggest>
const finalSuggestions = [{ ...pipeCompleteItem, text: ' | ' }];
if (fieldSuggestions.length > 1)
// when we fix the editor marker, this should probably be checked against 0 instead of 1
// this is because the last field in the AST is currently getting removed (because it contains
// the editor marker) so it is not included in the ignored list which is used to filter out
// existing fields above.
finalSuggestions.push({ ...commaCompleteItem, text: ', ' });

return finalSuggestions.map<SuggestionRawDefinition>((s) => ({
...s,
filterText: fragment,
text: fragment + s.text,
command: TRIGGER_SUGGESTION_COMMAND,
rangeToReplace,
}));
}
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
import type { CommandDefinition } from './types';
import { suggest as suggestForSort } from '../autocomplete/commands/sort';
import { suggest as suggestForKeep } from '../autocomplete/commands/keep';
import { suggest as suggestForDrop } from '../autocomplete/commands/drop';

const statsValidator = (command: ESQLCommand) => {
const messages: ESQLMessage[] = [];
Expand Down Expand Up @@ -333,6 +334,7 @@ export const commandDefinitions: Array<CommandDefinition<any>> = [
multipleParams: true,
params: [{ name: 'column', type: 'column', wildcards: true }],
},
suggest: suggestForDrop,
validate: (command: ESQLCommand) => {
const messages: ESQLMessage[] = [];
const wildcardItems = command.args.filter((arg) => isColumnItem(arg) && arg.name === '*');
Expand Down

0 comments on commit 88e9226

Please sign in to comment.