Skip to content

Commit

Permalink
Add check of previous token type
Browse files Browse the repository at this point in the history
  • Loading branch information
sakurai-youhei committed Nov 25, 2023
1 parent 7f3197d commit 7ababdf
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/plugins/console/public/lib/autocomplete/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -984,15 +984,22 @@ export default function ({

let urlTokenPath = context.urlTokenPath;
let predicate: (term: ReturnType<typeof addMetaToTermsList>[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
}
}
}

Expand Down

0 comments on commit 7ababdf

Please sign in to comment.