Skip to content

Commit

Permalink
[Console] Fix autocomplete on typing in every letter (elastic#171952)
Browse files Browse the repository at this point in the history
Closes elastic#171951

## Summary

This PR fixes autocomplete to show suggestions even if user types in
every letter.

![autocomplete-typing-in](https://github.com/elastic/kibana/assets/721858/c2d2dfb9-bc81-4285-b5c1-444d634f9af2)

### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

### For maintainers

- [x] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

## Release note

Fixes autocomplete to show suggestions even if user types in every
letter

(cherry picked from commit 1f5a3a0)
  • Loading branch information
sakurai-youhei committed Nov 29, 2023
1 parent ddd3e8d commit ebb9e2a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>>>([
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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;
}
}
}

Expand Down

0 comments on commit ebb9e2a

Please sign in to comment.