Skip to content

Commit

Permalink
Fixes unnecessary autocompletes on HTTP methods
Browse files Browse the repository at this point in the history
fixes elastic#156254
refixes elastic#120606 - should stay closed
unfixes elastic#19961 - should be reopened
  • Loading branch information
sakurai-youhei committed Aug 6, 2023
1 parent 1bb65b8 commit 3a2c878
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/plugins/console/public/lib/autocomplete/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ export default function ({
addUrlParamsAutoCompleteSetToContext(context, pos);
break;
case 'method':
addMethodAutoCompleteSetToContext(context);
addMethodAutoCompleteSetToContext(context, pos);
break;
case 'body':
addBodyAutoCompleteSetToContext(context, pos);
Expand Down Expand Up @@ -948,8 +948,13 @@ export default function ({
context.suffixToAdd = '';
}

function addMethodAutoCompleteSetToContext(context: AutoCompleteContext) {
context.autoCompleteSet = ['GET', 'PUT', 'POST', 'DELETE', 'HEAD'].map((m, i) => ({
function addMethodAutoCompleteSetToContext(context: AutoCompleteContext, pos: Position) {
const ret = getCurrentMethodAndTokenPaths(editor, pos, parser);
const methods =
ret.method && ret.method === ret.method?.toLowerCase()
? ['get', 'put', 'post', 'delete', 'head']
: ['GET', 'PUT', 'POST', 'DELETE', 'HEAD'];
context.autoCompleteSet = methods.map((m, i) => ({
name: m,
score: -i,
meta: i18n.translate('console.autocomplete.addMethodMetaText', { defaultMessage: 'method' }),
Expand Down Expand Up @@ -1085,12 +1090,12 @@ export default function ({
return; // wait for the next typing.
}

// if the column or the line number have not changed for the last token and
// if the column or the line number have changed for the last token or
// user did not provided a new value, then we should not show autocomplete
// this guards against triggering autocomplete when clicking around the editor
if (
(lastEvaluatedToken.position.column !== currentToken.position.column ||
lastEvaluatedToken.position.lineNumber !== currentToken.position.lineNumber) &&
lastEvaluatedToken.position.column !== currentToken.position.column ||
lastEvaluatedToken.position.lineNumber !== currentToken.position.lineNumber ||
lastEvaluatedToken.value === currentToken.value
) {
// not on the same place or nothing changed, cache and wait for the next time
Expand Down

0 comments on commit 3a2c878

Please sign in to comment.