-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d4dfe34
commit 88e9226
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
packages/kbn-esql-validation-autocomplete/src/autocomplete/commands/drop/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
})); | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters