Skip to content

Commit

Permalink
[8.11] [Console] Fix wrong autocomplete suggestions on typing in slash (
Browse files Browse the repository at this point in the history
#171948) (#172177)

# Backport

This will backport the following commits from `main` to `8.11`:
- [[Console] Fix wrong autocomplete suggestions on typing in slash
(#171948)](#171948)

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

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

<!--BACKPORT [{"author":{"name":"Youhei
Sakurai","email":"[email protected]"},"sourceCommit":{"committedDate":"2023-11-29T14:22:40Z","message":"[Console]
Fix wrong autocomplete suggestions on typing in slash
(#171948)\n\nCloses #171947\r\n\r\n## Summary\r\n\r\nThis PR fixes wrong
autocomplete suggestions on typing in slash in some\r\nspecific
cases.\r\n\r\n![fix-suggestion-on-slash](https://github.com/elastic/kibana/assets/721858/f3cab482-63ca-4ee7-99b7-0e3fc37e8940)\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 wrong autocomplete suggestions on typing in
slash","sha":"11136b688061fd3f4914915ea0da6e3917b5e66b","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":171948,"url":"https://github.com/elastic/kibana/pull/171948","mergeCommit":{"message":"[Console]
Fix wrong autocomplete suggestions on typing in slash
(#171948)\n\nCloses #171947\r\n\r\n## Summary\r\n\r\nThis PR fixes wrong
autocomplete suggestions on typing in slash in some\r\nspecific
cases.\r\n\r\n![fix-suggestion-on-slash](https://github.com/elastic/kibana/assets/721858/f3cab482-63ca-4ee7-99b7-0e3fc37e8940)\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 wrong autocomplete suggestions on typing in
slash","sha":"11136b688061fd3f4914915ea0da6e3917b5e66b"}},"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/171948","number":171948,"mergeCommit":{"message":"[Console]
Fix wrong autocomplete suggestions on typing in slash
(#171948)\n\nCloses #171947\r\n\r\n## Summary\r\n\r\nThis PR fixes wrong
autocomplete suggestions on typing in slash in some\r\nspecific
cases.\r\n\r\n![fix-suggestion-on-slash](https://github.com/elastic/kibana/assets/721858/f3cab482-63ca-4ee7-99b7-0e3fc37e8940)\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 wrong autocomplete suggestions on typing in
slash","sha":"11136b688061fd3f4914915ea0da6e3917b5e66b"}}]}] BACKPORT-->

Co-authored-by: Youhei Sakurai <[email protected]>
  • Loading branch information
kibanamachine and sakurai-youhei authored Nov 29, 2023
1 parent 1fc0998 commit 05a2b88
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions src/plugins/console/public/lib/autocomplete/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -983,32 +983,33 @@ export default function ({
context.urlTokenPath = ret.urlTokenPath;

const components = getTopLevelUrlCompleteComponents(context.method);
const { tokenPath, predicate } = (() => {
const lastUrlTokenPath =
Array.isArray(context.urlTokenPath) && context.urlTokenPath.length !== 0
? context.urlTokenPath[context.urlTokenPath.length - 1]
: null;
// Checking the last chunk of path like 'c,d,' of 'GET /a/b/c,d,'
if (
Array.isArray(lastUrlTokenPath) &&
// true if neither c nor d equals to every ConstantComponent's name (such as _search)
!_.find(
components,
(c) => c instanceof ConstantComponent && _.find(lastUrlTokenPath, (p) => c.name === p)
)
) {
// will simulate autocomplete on 'GET /a/b/' with a filter by index
return {
tokenPath: context.urlTokenPath?.slice(0, -1),
predicate: (term: ReturnType<typeof addMetaToTermsList>[0]) => term.meta === 'index',
};
} else {
// will do nothing special
return { tokenPath: context.urlTokenPath, predicate: () => true };
let urlTokenPath = context.urlTokenPath;
let predicate: (term: ReturnType<typeof addMetaToTermsList>[0]) => boolean = () => true;

const tokenIter = createTokenIterator({ editor, position: pos });
const currentTokenType = tokenIter.getCurrentToken()?.type;
const previousTokenType = tokenIter.stepBackward()?.type;
if (!Array.isArray(urlTokenPath)) {
// skip checks for url.comma
} else if (previousTokenType === 'url.comma' && currentTokenType === 'url.comma') {
predicate = () => false; // two consecutive commas empty the autocomplete
} else if (
(previousTokenType === 'url.part' && currentTokenType === 'url.comma') ||
(previousTokenType === 'url.slash' && currentTokenType === 'url.comma') ||
(previousTokenType === 'url.comma' && currentTokenType === 'url.part')
) {
const lastUrlTokenPath = _.last(urlTokenPath) || []; // ['c', 'd'] from 'GET /a/b/c,d,'
const constantComponents = _.filter(components, (c) => c instanceof ConstantComponent);
const constantComponentNames = _.map(constantComponents, 'name');

// check if neither 'c' nor 'd' is a constant component name such as '_search'
if (_.every(lastUrlTokenPath, (token) => !_.includes(constantComponentNames, token))) {
urlTokenPath = urlTokenPath.slice(0, -1); // drop the last 'c,d,' part from the url path
predicate = (term) => term.meta === 'index'; // limit the autocomplete to indices only
}
})();
}

populateContext(tokenPath, context, editor, true, components);
populateContext(urlTokenPath, context, editor, true, components);
context.autoCompleteSet = _.filter(
addMetaToTermsList(context.autoCompleteSet!, 'endpoint'),
predicate
Expand Down

0 comments on commit 05a2b88

Please sign in to comment.