diff --git a/idea/src/org/jetbrains/jet/plugin/completion/JetCompletionContributor.java b/idea/src/org/jetbrains/jet/plugin/completion/JetCompletionContributor.java index a4321a5cb06d5d9153ccedc35cfb4706b7854ed7..22f8900c06e93d81fd1e4d4f5270e71e103f2c4c 100644 --- a/idea/src/org/jetbrains/jet/plugin/completion/JetCompletionContributor.java +++ b/idea/src/org/jetbrains/jet/plugin/completion/JetCompletionContributor.java @@ -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(); diff --git a/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetCompletionSorting.java b/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetCompletionSorting.java new file mode 100644 index 0000000000000000000000000000000000000000..5c9cf07ea61cfbcdb7301c02ff84775721efd3e2 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetCompletionSorting.java @@ -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); + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetLocalPreferableWeigher.java b/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetLocalPreferableWeigher.java new file mode 100644 index 0000000000000000000000000000000000000000..e94ddf657971cc7c0757b6932cc065eeeb2958d4 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetLocalPreferableWeigher.java @@ -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; + } +} diff --git a/idea/testData/completion/weighers/LocalValuesAndParams.kt b/idea/testData/completion/weighers/LocalValuesAndParams.kt new file mode 100644 index 0000000000000000000000000000000000000000..dc3d4ca6ecfc253560e33c17e7d35812c4057a9a --- /dev/null +++ b/idea/testData/completion/weighers/LocalValuesAndParams.kt @@ -0,0 +1,6 @@ +val initGlobal = 12 + +fun test(initParam : Int) { + val initLocal = "Test" + +} \ No newline at end of file diff --git a/idea/testData/completion/weighers/TemplatesAndKeywordsFirst.kt b/idea/testData/completion/weighers/TemplatesAndKeywordsFirst.kt new file mode 100644 index 0000000000000000000000000000000000000000..98d92ebcf7f44b55cd3123c3e9c5b941bb75c4e3 --- /dev/null +++ b/idea/testData/completion/weighers/TemplatesAndKeywordsFirst.kt @@ -0,0 +1,4 @@ +fun main(variables: Array) { + val values = "" + +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/completion/weighers/CompletionWeigherTest.java b/idea/tests/org/jetbrains/jet/completion/weighers/CompletionWeigherTest.java new file mode 100644 index 0000000000000000000000000000000000000000..c6ef289dcb556c4407373ac21d7c0a715d4fe2d3 --- /dev/null +++ b/idea/tests/org/jetbrains/jet/completion/weighers/CompletionWeigherTest.java @@ -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); + } +}