From b02d107ec1c5d34ffdb6e6b8765a3576015e24e6 Mon Sep 17 00:00:00 2001 From: Youhei Sakurai Date: Mon, 27 Nov 2023 00:20:12 +0900 Subject: [PATCH] Fix autocomplete on typing in every letter --- .../autocomplete/looks_like_typing_in.test.ts | 4 ++++ .../lib/autocomplete/looks_like_typing_in.ts | 21 ++++++++++++------- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/plugins/console/public/lib/autocomplete/looks_like_typing_in.test.ts b/src/plugins/console/public/lib/autocomplete/looks_like_typing_in.test.ts index 969f06f8d994f..cbcfb724798df 100644 --- a/src/plugins/console/public/lib/autocomplete/looks_like_typing_in.test.ts +++ b/src/plugins/console/public/lib/autocomplete/looks_like_typing_in.test.ts @@ -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 = diff --git a/src/plugins/console/public/lib/autocomplete/looks_like_typing_in.ts b/src/plugins/console/public/lib/autocomplete/looks_like_typing_in.ts index a679aa2eda117..44dbb99025c73 100644 --- a/src/plugins/console/public/lib/autocomplete/looks_like_typing_in.ts +++ b/src/plugins/console/public/lib/autocomplete/looks_like_typing_in.ts @@ -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>>([ @@ -43,6 +44,10 @@ const knownTypingInTokenTypes = new Map>>([ ['whitespace', new Set(['url.comma', 'url.questionmark', 'url.slash'])], ]), ], + [ + Move.ForwardTwoTokens, + new Map>([['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>(); - const currentTokenTypes = tokenTypesPairs.get(lastEvaluatedToken.type) ?? new Set(); - 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>(); + const currentTokenTypes = tokenTypesPairs.get(lastEvaluatedToken.type) ?? new Set(); + if (currentTokenTypes.has(currentToken.type)) { + return true; + } } }