提交 252ba567 编写于 作者: N Nikolay Krasko

KT-2122 Code completion after val with space should not add local variable's...

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
上级 30e44fdc
......@@ -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();
......
/*
* 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);
}
}
/*
* 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;
}
}
val initGlobal = 12
fun test(initParam : Int) {
val initLocal = "Test"
<caret>
}
\ No newline at end of file
fun main(variables: Array<String>) {
val values = ""
<caret>
}
\ No newline at end of file
/*
* 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);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册