PY-1526 Fixed: Basic completion for kwarg in a method call should put variable by name as the first suggestion

Update CompletionVariantsProcessor to increase priority for items with same name as keyword.
This commit is contained in:
Semyon Proshev
2017-08-18 16:05:25 +03:00
parent 8a5f550986
commit 4acdf3b35e
2 changed files with 18 additions and 4 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -31,6 +31,9 @@ import org.jetbrains.annotations.NotNull;
* Date: 11/11/10 4:24 PM
*/
public class PythonCompletionWeigher extends CompletionWeigher {
public static final int WEIGHT_DELTA = 5;
@NonNls private static final String DOUBLE_UNDER = "__";
@Override
@@ -46,8 +49,8 @@ public class PythonCompletionWeigher extends CompletionWeigher {
return element.getLookupString().length();
}
if (name.startsWith(DOUBLE_UNDER)) {
if (name.endsWith(DOUBLE_UNDER)) return -10; // __foo__ is lowest
else return -5; // __foo is lower than normal
if (name.endsWith(DOUBLE_UNDER)) return -2 * WEIGHT_DELTA; // __foo__ is lowest
else return -WEIGHT_DELTA; // __foo is lower than normal
}
return 0; // default
}
@@ -15,6 +15,7 @@
*/
package com.jetbrains.python.psi.resolve;
import com.intellij.codeInsight.completion.PrioritizedLookupElement;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.openapi.project.Project;
@@ -28,6 +29,7 @@ import com.intellij.util.PlatformIcons;
import com.jetbrains.python.PyNames;
import com.jetbrains.python.codeInsight.completion.PyClassInsertHandler;
import com.jetbrains.python.codeInsight.completion.PyFunctionInsertHandler;
import com.jetbrains.python.codeInsight.completion.PythonCompletionWeigher;
import com.jetbrains.python.codeInsight.controlflow.ScopeOwner;
import com.jetbrains.python.codeInsight.dataflow.scope.ScopeUtil;
import com.jetbrains.python.psi.*;
@@ -73,7 +75,7 @@ public class CompletionVariantsProcessor extends VariantsProcessor {
}
@NotNull
private LookupElementBuilder setupItem(@NotNull LookupElementBuilder item) {
private LookupElement setupItem(@NotNull LookupElementBuilder item) {
final PsiElement element = item.getPsiElement();
if (!myPlainNamesOnly) {
if (!mySuppressParentheses &&
@@ -124,6 +126,15 @@ public class CompletionVariantsProcessor extends VariantsProcessor {
if (source != null) {
item = item.withTypeText(source);
}
final PsiElement parent = myContext != null ? myContext.getParent() : null;
if (parent instanceof PyKeywordArgument) {
final String keyword = ((PyKeywordArgument)parent).getKeyword();
if (item.getLookupString().equals(keyword)) {
return PrioritizedLookupElement.withPriority(item, PythonCompletionWeigher.WEIGHT_DELTA);
}
}
return item;
}