KT-2122 Code completion after val with space should not add local variable's name (set priority in completion for local variables and parameters)
#KT-2122 fixed
This commit is contained in:
@@ -39,6 +39,7 @@ import org.jetbrains.jet.lang.resolve.scopes.JetScope;
|
||||
import org.jetbrains.jet.lexer.JetTokens;
|
||||
import org.jetbrains.jet.plugin.caches.JetCacheManager;
|
||||
import org.jetbrains.jet.plugin.caches.JetShortNamesCache;
|
||||
import org.jetbrains.jet.plugin.completion.weigher.JetCompletionSorting;
|
||||
import org.jetbrains.jet.plugin.project.WholeProjectAnalyzerFacade;
|
||||
import org.jetbrains.jet.plugin.references.JetSimpleNameReference;
|
||||
|
||||
@@ -64,6 +65,7 @@ public class JetCompletionContributor extends CompletionContributor {
|
||||
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context,
|
||||
@NotNull CompletionResultSet result) {
|
||||
result.restartCompletionWhenNothingMatches();
|
||||
result = JetCompletionSorting.addJetSorting(parameters, result);
|
||||
|
||||
CompletionSession session = new CompletionSession();
|
||||
session.customInvocationCount = parameters.getInvocationCount();
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 2010-2012 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.plugin.completion.weigher;
|
||||
|
||||
import com.intellij.codeInsight.completion.CompletionParameters;
|
||||
import com.intellij.codeInsight.completion.CompletionResultSet;
|
||||
import com.intellij.codeInsight.completion.CompletionSorter;
|
||||
|
||||
/**
|
||||
* @author Nikolay Krasko
|
||||
*/
|
||||
public final class JetCompletionSorting {
|
||||
private JetCompletionSorting() {
|
||||
}
|
||||
|
||||
public static CompletionResultSet addJetSorting(CompletionParameters parameters, CompletionResultSet result) {
|
||||
CompletionSorter sorter = CompletionSorter.defaultSorter(parameters, result.getPrefixMatcher());
|
||||
sorter = sorter.weighAfter("negativeStats", new JetLocalPreferableWeigher());
|
||||
return result.withRelevanceSorter(sorter);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright 2010-2012 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.plugin.completion.weigher;
|
||||
|
||||
import com.intellij.codeInsight.lookup.LookupElement;
|
||||
import com.intellij.codeInsight.lookup.LookupElementWeigher;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.LocalVariableDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
|
||||
import org.jetbrains.jet.plugin.completion.JetLookupObject;
|
||||
|
||||
/**
|
||||
* @author Nikolay Krasko
|
||||
*/
|
||||
class JetLocalPreferableWeigher extends LookupElementWeigher {
|
||||
JetLocalPreferableWeigher() {
|
||||
super("JetLocalElementWeigher");
|
||||
}
|
||||
|
||||
private enum MyResult {
|
||||
probableKeyword,
|
||||
localOrParameter,
|
||||
normal,
|
||||
packages
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public MyResult weigh(@NotNull LookupElement element) {
|
||||
Object object = element.getObject();
|
||||
if (object instanceof JetLookupObject) {
|
||||
JetLookupObject lookupObject = (JetLookupObject) object;
|
||||
DeclarationDescriptor descriptor = lookupObject.getDescriptor();
|
||||
if (descriptor != null) {
|
||||
if (descriptor instanceof LocalVariableDescriptor || descriptor instanceof ValueParameterDescriptor) {
|
||||
return MyResult.localOrParameter;
|
||||
}
|
||||
if (descriptor instanceof NamespaceDescriptor) {
|
||||
return MyResult.packages;
|
||||
}
|
||||
}
|
||||
} else if (object instanceof String) {
|
||||
return MyResult.probableKeyword;
|
||||
}
|
||||
|
||||
return MyResult.normal;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
val initGlobal = 12
|
||||
|
||||
fun test(initParam : Int) {
|
||||
val initLocal = "Test"
|
||||
<caret>
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun main(variables: Array<String>) {
|
||||
val values = ""
|
||||
<caret>
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2010-2012 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.completion.weighers;
|
||||
|
||||
import com.intellij.codeInsight.completion.CompletionAutoPopupTestCase;
|
||||
import com.intellij.openapi.application.Result;
|
||||
import com.intellij.openapi.command.WriteCommandAction;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.jet.plugin.PluginTestCaseBase;
|
||||
|
||||
/**
|
||||
* @author Nikolay Krasko
|
||||
*/
|
||||
public class CompletionWeigherTest extends CompletionAutoPopupTestCase {
|
||||
public void testLocalValuesAndParams() {
|
||||
doTest("init", "initLocal", "initParam", "initGlobal");
|
||||
}
|
||||
|
||||
public void testTemplatesAndKeywordsFirst() {
|
||||
doTest("va", "val ... = ...", "var ... = ...", "vararg", "values", "variables");
|
||||
}
|
||||
|
||||
public void doTest(String type, @NonNls String... expected) {
|
||||
new WriteCommandAction(myFixture.getProject(), myFixture.getFile()) {
|
||||
@Override
|
||||
protected void run(Result result) throws Throwable {
|
||||
myFixture.setTestDataPath(PluginTestCaseBase.getTestDataPathBase() + "/completion/weighers/");
|
||||
myFixture.configureByFile(getTestName(false) + ".kt");
|
||||
}
|
||||
}.execute();
|
||||
|
||||
type(type);
|
||||
myFixture.assertPreferredCompletionItems(0, expected);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user