From 7ababdf766101fed6189a5102dcca2aa3bd56415 Mon Sep 17 00:00:00 2001 From: Youhei Sakurai Date: Sat, 25 Nov 2023 23:00:55 +0900 Subject: [PATCH] Add check of previous token type --- .../public/lib/autocomplete/autocomplete.ts | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/plugins/console/public/lib/autocomplete/autocomplete.ts b/src/plugins/console/public/lib/autocomplete/autocomplete.ts index 49affb51087a2..e37a7ce74faa8 100644 --- a/src/plugins/console/public/lib/autocomplete/autocomplete.ts +++ b/src/plugins/console/public/lib/autocomplete/autocomplete.ts @@ -984,15 +984,22 @@ export default function ({ let urlTokenPath = context.urlTokenPath; let predicate: (term: ReturnType[0]) => boolean = () => true; - if (Array.isArray(urlTokenPath) && editor.getTokenAt(pos)?.type === 'url.comma') { - 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 suggestion to indices only + if (Array.isArray(urlTokenPath)) { + const tokenIter = createTokenIterator({ editor, position: pos }); + const isCurrentOrPreviousTokenTypeUrlComma = + tokenIter.getCurrentToken()?.type === 'url.comma' || + tokenIter.stepBackward()?.type === 'url.comma'; + + if (isCurrentOrPreviousTokenTypeUrlComma) { + 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 suggestion to indices only + } } }