[8.11] [Console] Fix autocomplete on typing in every letter (#171952) (#172176)

# Backport

This will backport the following commits from `main` to `8.11`:
- [[Console] Fix autocomplete on typing in every letter
(#171952)](https://github.com/elastic/kibana/pull/171952)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Youhei
Sakurai","email":"youhei.sakurai@elastic.co"},"sourceCommit":{"committedDate":"2023-11-29T14:22:14Z","message":"[Console]
Fix autocomplete on typing in every letter (#171952)\n\nCloses
#171951\r\n\r\n## Summary\r\n\r\nThis PR fixes autocomplete to show
suggestions even if user types in\r\nevery
letter.\r\n\r\n![autocomplete-typing-in](c2d2dfb9-bc81-4285-b5c1-444d634f9af2)\r\n\r\n###
Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n\r\n### For
maintainers\r\n\r\n- [x] This was checked for breaking API changes and
was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\r\n\r\n##
Release note\r\n\r\nFixes autocomplete to show suggestions even if user
types in
every\r\nletter","sha":"1f5a3a08a53d3d60cae0eb2aa310ff90217436f7","branchLabelMapping":{"^v8.12.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["bug","Feature:Console","release_note:fix","Team:Deployment
Management","backport:prev-minor","v8.12.0"],"number":171952,"url":"https://github.com/elastic/kibana/pull/171952","mergeCommit":{"message":"[Console]
Fix autocomplete on typing in every letter (#171952)\n\nCloses
#171951\r\n\r\n## Summary\r\n\r\nThis PR fixes autocomplete to show
suggestions even if user types in\r\nevery
letter.\r\n\r\n![autocomplete-typing-in](c2d2dfb9-bc81-4285-b5c1-444d634f9af2)\r\n\r\n###
Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n\r\n### For
maintainers\r\n\r\n- [x] This was checked for breaking API changes and
was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\r\n\r\n##
Release note\r\n\r\nFixes autocomplete to show suggestions even if user
types in
every\r\nletter","sha":"1f5a3a08a53d3d60cae0eb2aa310ff90217436f7"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.12.0","labelRegex":"^v8.12.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/171952","number":171952,"mergeCommit":{"message":"[Console]
Fix autocomplete on typing in every letter (#171952)\n\nCloses
#171951\r\n\r\n## Summary\r\n\r\nThis PR fixes autocomplete to show
suggestions even if user types in\r\nevery
letter.\r\n\r\n![autocomplete-typing-in](c2d2dfb9-bc81-4285-b5c1-444d634f9af2)\r\n\r\n###
Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n\r\n### For
maintainers\r\n\r\n- [x] This was checked for breaking API changes and
was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\r\n\r\n##
Release note\r\n\r\nFixes autocomplete to show suggestions even if user
types in
every\r\nletter","sha":"1f5a3a08a53d3d60cae0eb2aa310ff90217436f7"}}]}]
BACKPORT-->

Co-authored-by: Youhei Sakurai <youhei.sakurai@elastic.co>
This commit is contained in:
Kibana Machine 2023-11-29 10:41:32 -05:00 committed by GitHub
parent ddd3e8dea7
commit 1fc0998eea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 7 deletions

View file

@ -103,6 +103,10 @@ describe('looksLikeTypingIn', () => {
{ preamble: 'GET _cat/indices?s=index&exp', autocomplete: 'and_wildcards', input: '=' },
{ preamble: 'GET _cat/indices?v&', autocomplete: 'expand_wildcards', input: '=' },
{ preamble: 'GET _cat/indices?v&exp', autocomplete: 'and_wildcards', input: '=' },
// autocomplete skips one iteration of token evaluation if user types in every letter
{ preamble: 'GET .kibana', autocomplete: '/', input: '_' }, // token '/' may not be evaluated
{ preamble: 'GET .kibana', autocomplete: ',', input: '.' }, // token ',' may not be evaluated
{ preamble: 'GET .kibana', autocomplete: '?', input: 'k' }, // token '?' may not be evaluated
];
for (const c of cases) {
const name =

View file

@ -11,6 +11,7 @@ import type { CoreEditor, Position, Token } from '../../types';
enum Move {
ForwardOneCharacter = 1,
ForwardOneToken, // the column position may jump to the next token by autocomplete
ForwardTwoTokens, // the column position could jump two tokens due to autocomplete
}
const knownTypingInTokenTypes = new Map<Move, Map<string, Set<string>>>([
@ -43,6 +44,10 @@ const knownTypingInTokenTypes = new Map<Move, Map<string, Set<string>>>([
['whitespace', new Set(['url.comma', 'url.questionmark', 'url.slash'])],
]),
],
[
Move.ForwardTwoTokens,
new Map<string, Set<string>>([['url.part', new Set(['url.param', 'url.part'])]]),
],
]);
const getOneCharacterNextOnTheRight = (pos: Position, coreEditor: CoreEditor): string => {
@ -75,14 +80,16 @@ export const looksLikeTypingIn = (
currentToken.value.length === 1 &&
getOneCharacterNextOnTheRight(currentToken.position, coreEditor) === ''
) {
const move =
const moves =
lastEvaluatedToken.position.column + 1 === currentToken.position.column
? Move.ForwardOneCharacter
: Move.ForwardOneToken;
const tokenTypesPairs = knownTypingInTokenTypes.get(move) ?? new Map<string, Set<string>>();
const currentTokenTypes = tokenTypesPairs.get(lastEvaluatedToken.type) ?? new Set<string>();
if (currentTokenTypes.has(currentToken.type)) {
return true;
? [Move.ForwardOneCharacter]
: [Move.ForwardOneToken, Move.ForwardTwoTokens];
for (const move of moves) {
const tokenTypesPairs = knownTypingInTokenTypes.get(move) ?? new Map<string, Set<string>>();
const currentTokenTypes = tokenTypesPairs.get(lastEvaluatedToken.type) ?? new Set<string>();
if (currentTokenTypes.has(currentToken.type)) {
return true;
}
}
}