diff --git a/generators/org/jetbrains/jet/generators/tests/GenerateTests.java b/generators/org/jetbrains/jet/generators/tests/GenerateTests.java index 25bcd4483c3..ced7ada7367 100644 --- a/generators/org/jetbrains/jet/generators/tests/GenerateTests.java +++ b/generators/org/jetbrains/jet/generators/tests/GenerateTests.java @@ -465,7 +465,7 @@ public class GenerateTests { "idea/tests", "CompletionWeigherTestGenerated", AbstractCompletionWeigherTest.class, - testModel("idea/testData/completion/weighers", true, "doTest") + testModelWithPattern("idea/testData/completion/weighers", "^([^\\.]+).kt$", "doTest") ); generateTest( diff --git a/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetCompletionSorting.kt b/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetCompletionSorting.kt index 129c57f7c44..683f47daa22 100644 --- a/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetCompletionSorting.kt +++ b/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetCompletionSorting.kt @@ -28,7 +28,7 @@ public fun CompletionResultSet.addJetSorting(parameters : CompletionParameters) sorter = sorter.weighAfter( "stats", - JetExplicitlyImportedWeigher(parameters.getOriginalFile() as JetFile), + JetDeclarationRemotenessWeigher(parameters.getOriginalFile() as JetFile), JetAccessibleWeigher()) return withRelevanceSorter(sorter) diff --git a/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetExplicitlyImportedWeigher.java b/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetDeclarationRemotenessWeigher.java similarity index 80% rename from idea/src/org/jetbrains/jet/plugin/completion/weigher/JetExplicitlyImportedWeigher.java rename to idea/src/org/jetbrains/jet/plugin/completion/weigher/JetDeclarationRemotenessWeigher.java index 58c6e6a445c..6ae6f1c22a3 100644 --- a/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetExplicitlyImportedWeigher.java +++ b/idea/src/org/jetbrains/jet/plugin/completion/weigher/JetDeclarationRemotenessWeigher.java @@ -18,6 +18,8 @@ package org.jetbrains.jet.plugin.completion.weigher; import com.intellij.codeInsight.lookup.LookupElement; import com.intellij.codeInsight.lookup.LookupElementWeigher; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; import org.jetbrains.jet.lang.psi.JetFile; @@ -28,29 +30,37 @@ import org.jetbrains.jet.plugin.completion.JetLookupObject; import org.jetbrains.jet.plugin.quickfix.ImportInsertHelper; import org.jetbrains.jet.util.QualifiedNamesUtil; -// class ExplicitlyImportedWeigher extends ProximityWeigher { -public class JetExplicitlyImportedWeigher extends LookupElementWeigher { +public class JetDeclarationRemotenessWeigher extends LookupElementWeigher { private final JetFile file; - protected JetExplicitlyImportedWeigher(JetFile file) { - super("JetExplicitlyWeigher"); + protected JetDeclarationRemotenessWeigher(JetFile file) { + super(JetDeclarationRemotenessWeigher.class.getSimpleName()); this.file = file; } private enum MyResult { kotlinDefaultImport, + thisFile, imported, + normal, notImported, - normal } - @NotNull @Override public Comparable weigh(@NotNull LookupElement element) { Object object = element.getObject(); if (object instanceof JetLookupObject) { JetLookupObject lookupObject = (JetLookupObject) object; + + PsiElement psiElement = lookupObject.getPsiElement(); + if (psiElement != null) { + PsiFile elementFile = psiElement.getContainingFile(); + if (elementFile instanceof JetFile && elementFile.getOriginalFile() == file) { + return MyResult.thisFile; + } + } + DeclarationDescriptor descriptor = lookupObject.getDescriptor(); if (descriptor != null) { FqNameUnsafe fqName = DescriptorUtils.getFQName(descriptor); diff --git a/idea/testData/completion/weighers/LocalFileBeforeImported.Data.kt b/idea/testData/completion/weighers/LocalFileBeforeImported.Data.kt new file mode 100644 index 00000000000..a8203ce4c80 --- /dev/null +++ b/idea/testData/completion/weighers/LocalFileBeforeImported.Data.kt @@ -0,0 +1,5 @@ +package some + +fun foo() = 12 +fun fooImported() = 12 +fun fooNotImported() = 12 \ No newline at end of file diff --git a/idea/testData/completion/weighers/LocalFileBeforeImported.kt b/idea/testData/completion/weighers/LocalFileBeforeImported.kt new file mode 100644 index 00000000000..571a2857d59 --- /dev/null +++ b/idea/testData/completion/weighers/LocalFileBeforeImported.kt @@ -0,0 +1,12 @@ +import some.fooImported + +val fooVar = 12 +fun fooLocal() = 12 + +val some = foo + +// "foo" is before other elements because of exact prefix match + +// ORDER: foo, fooLocal, fooVar, fooImported, fooNotImported +// NUMBER: 2 +// SELECTED: 1 \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/completion/weighers/AbstractCompletionWeigherTest.kt b/idea/tests/org/jetbrains/jet/completion/weighers/AbstractCompletionWeigherTest.kt index 2286e6a7b60..6b991c788ee 100644 --- a/idea/tests/org/jetbrains/jet/completion/weighers/AbstractCompletionWeigherTest.kt +++ b/idea/tests/org/jetbrains/jet/completion/weighers/AbstractCompletionWeigherTest.kt @@ -22,19 +22,31 @@ import java.io.File import org.jetbrains.jet.plugin.PluginTestCaseBase import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase import org.testng.Assert +import com.intellij.openapi.util.io.FileUtil public abstract class AbstractCompletionWeigherTest() : LightCodeInsightFixtureTestCase() { fun doTest(path: String) { val fixture = myFixture!! - fixture.configureByFile(path) + val testFile = File(path) + + val dataFileName = FileUtil.getNameWithoutExtension(testFile) + ".Data.kt" + val dataFile = File(testFile.getParent(), dataFileName) + + if (dataFile.exists()) { + fixture.configureByFiles(path, FileUtil.toSystemIndependentName(dataFile.getPath())) + } + else { + fixture.configureByFile(path) + } + val text = fixture.getEditor()!!.getDocument().getText() val items = InTextDirectivesUtils.findArrayWithPrefixes(text, "// ORDER:") Assert.assertTrue(!items.isEmpty(), """Some items should be defined with "// ORDER:" directive""") - fixture.complete(CompletionType.BASIC, 1) - fixture.assertPreferredCompletionItems(0, *items) + fixture.complete(CompletionType.BASIC, InTextDirectivesUtils.getPrefixedInt(text, "// NUMBER:") ?: 1) + fixture.assertPreferredCompletionItems(InTextDirectivesUtils.getPrefixedInt(text, "// SELECTED:") ?: 0, *items) } protected override fun getTestDataPath() : String? { diff --git a/idea/tests/org/jetbrains/jet/completion/weighers/CompletionWeigherTestGenerated.java b/idea/tests/org/jetbrains/jet/completion/weighers/CompletionWeigherTestGenerated.java index 8ab8a40329e..a61ed488e4e 100644 --- a/idea/tests/org/jetbrains/jet/completion/weighers/CompletionWeigherTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/completion/weighers/CompletionWeigherTestGenerated.java @@ -16,18 +16,24 @@ package org.jetbrains.jet.completion.weighers; -import org.jetbrains.jet.JetTestUtils; -import org.jetbrains.jet.test.TestMetadata; +import junit.framework.Assert; +import junit.framework.Test; +import junit.framework.TestSuite; import java.io.File; import java.util.regex.Pattern; +import org.jetbrains.jet.JetTestUtils; +import org.jetbrains.jet.test.InnerTestClasses; +import org.jetbrains.jet.test.TestMetadata; + +import org.jetbrains.jet.completion.weighers.AbstractCompletionWeigherTest; /** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */ @SuppressWarnings("all") @TestMetadata("idea/testData/completion/weighers") public class CompletionWeigherTestGenerated extends AbstractCompletionWeigherTest { public void testAllFilesPresentInWeighers() throws Exception { - JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/completion/weighers"), Pattern.compile("^(.+)\\.kt$"), true); + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/completion/weighers"), Pattern.compile("^([^\\.]+).kt$"), true); } @TestMetadata("DeprecatedFun.kt") @@ -35,6 +41,11 @@ public class CompletionWeigherTestGenerated extends AbstractCompletionWeigherTes doTest("idea/testData/completion/weighers/DeprecatedFun.kt"); } + @TestMetadata("LocalFileBeforeImported.kt") + public void testLocalFileBeforeImported() throws Exception { + doTest("idea/testData/completion/weighers/LocalFileBeforeImported.kt"); + } + @TestMetadata("LocalValuesAndParams.kt") public void testLocalValuesAndParams() throws Exception { doTest("idea/testData/completion/weighers/LocalValuesAndParams.kt");