Give more priority to declarations from the same file
This commit is contained in:
@@ -465,7 +465,7 @@ public class GenerateTests {
|
|||||||
"idea/tests",
|
"idea/tests",
|
||||||
"CompletionWeigherTestGenerated",
|
"CompletionWeigherTestGenerated",
|
||||||
AbstractCompletionWeigherTest.class,
|
AbstractCompletionWeigherTest.class,
|
||||||
testModel("idea/testData/completion/weighers", true, "doTest")
|
testModelWithPattern("idea/testData/completion/weighers", "^([^\\.]+).kt$", "doTest")
|
||||||
);
|
);
|
||||||
|
|
||||||
generateTest(
|
generateTest(
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ public fun CompletionResultSet.addJetSorting(parameters : CompletionParameters)
|
|||||||
|
|
||||||
sorter = sorter.weighAfter(
|
sorter = sorter.weighAfter(
|
||||||
"stats",
|
"stats",
|
||||||
JetExplicitlyImportedWeigher(parameters.getOriginalFile() as JetFile),
|
JetDeclarationRemotenessWeigher(parameters.getOriginalFile() as JetFile),
|
||||||
JetAccessibleWeigher())
|
JetAccessibleWeigher())
|
||||||
|
|
||||||
return withRelevanceSorter(sorter)
|
return withRelevanceSorter(sorter)
|
||||||
|
|||||||
+16
-6
@@ -18,6 +18,8 @@ package org.jetbrains.jet.plugin.completion.weigher;
|
|||||||
|
|
||||||
import com.intellij.codeInsight.lookup.LookupElement;
|
import com.intellij.codeInsight.lookup.LookupElement;
|
||||||
import com.intellij.codeInsight.lookup.LookupElementWeigher;
|
import com.intellij.codeInsight.lookup.LookupElementWeigher;
|
||||||
|
import com.intellij.psi.PsiElement;
|
||||||
|
import com.intellij.psi.PsiFile;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
||||||
import org.jetbrains.jet.lang.psi.JetFile;
|
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.plugin.quickfix.ImportInsertHelper;
|
||||||
import org.jetbrains.jet.util.QualifiedNamesUtil;
|
import org.jetbrains.jet.util.QualifiedNamesUtil;
|
||||||
|
|
||||||
// class ExplicitlyImportedWeigher extends ProximityWeigher {
|
public class JetDeclarationRemotenessWeigher extends LookupElementWeigher {
|
||||||
public class JetExplicitlyImportedWeigher extends LookupElementWeigher {
|
|
||||||
private final JetFile file;
|
private final JetFile file;
|
||||||
|
|
||||||
protected JetExplicitlyImportedWeigher(JetFile file) {
|
protected JetDeclarationRemotenessWeigher(JetFile file) {
|
||||||
super("JetExplicitlyWeigher");
|
super(JetDeclarationRemotenessWeigher.class.getSimpleName());
|
||||||
this.file = file;
|
this.file = file;
|
||||||
}
|
}
|
||||||
|
|
||||||
private enum MyResult {
|
private enum MyResult {
|
||||||
kotlinDefaultImport,
|
kotlinDefaultImport,
|
||||||
|
thisFile,
|
||||||
imported,
|
imported,
|
||||||
|
normal,
|
||||||
notImported,
|
notImported,
|
||||||
normal
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public Comparable weigh(@NotNull LookupElement element) {
|
public Comparable weigh(@NotNull LookupElement element) {
|
||||||
Object object = element.getObject();
|
Object object = element.getObject();
|
||||||
if (object instanceof JetLookupObject) {
|
if (object instanceof JetLookupObject) {
|
||||||
JetLookupObject lookupObject = (JetLookupObject) object;
|
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();
|
DeclarationDescriptor descriptor = lookupObject.getDescriptor();
|
||||||
if (descriptor != null) {
|
if (descriptor != null) {
|
||||||
FqNameUnsafe fqName = DescriptorUtils.getFQName(descriptor);
|
FqNameUnsafe fqName = DescriptorUtils.getFQName(descriptor);
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package some
|
||||||
|
|
||||||
|
fun foo() = 12
|
||||||
|
fun fooImported() = 12
|
||||||
|
fun fooNotImported() = 12
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import some.fooImported
|
||||||
|
|
||||||
|
val fooVar = 12
|
||||||
|
fun fooLocal() = 12
|
||||||
|
|
||||||
|
val some = foo<caret>
|
||||||
|
|
||||||
|
// "foo" is before other elements because of exact prefix match
|
||||||
|
|
||||||
|
// ORDER: foo, fooLocal, fooVar, fooImported, fooNotImported
|
||||||
|
// NUMBER: 2
|
||||||
|
// SELECTED: 1
|
||||||
@@ -22,19 +22,31 @@ import java.io.File
|
|||||||
import org.jetbrains.jet.plugin.PluginTestCaseBase
|
import org.jetbrains.jet.plugin.PluginTestCaseBase
|
||||||
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
|
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
|
||||||
import org.testng.Assert
|
import org.testng.Assert
|
||||||
|
import com.intellij.openapi.util.io.FileUtil
|
||||||
|
|
||||||
public abstract class AbstractCompletionWeigherTest() : LightCodeInsightFixtureTestCase() {
|
public abstract class AbstractCompletionWeigherTest() : LightCodeInsightFixtureTestCase() {
|
||||||
fun doTest(path: String) {
|
fun doTest(path: String) {
|
||||||
val fixture = myFixture!!
|
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 text = fixture.getEditor()!!.getDocument().getText()
|
||||||
|
|
||||||
val items = InTextDirectivesUtils.findArrayWithPrefixes(text, "// ORDER:")
|
val items = InTextDirectivesUtils.findArrayWithPrefixes(text, "// ORDER:")
|
||||||
Assert.assertTrue(!items.isEmpty(), """Some items should be defined with "// ORDER:" directive""")
|
Assert.assertTrue(!items.isEmpty(), """Some items should be defined with "// ORDER:" directive""")
|
||||||
|
|
||||||
fixture.complete(CompletionType.BASIC, 1)
|
fixture.complete(CompletionType.BASIC, InTextDirectivesUtils.getPrefixedInt(text, "// NUMBER:") ?: 1)
|
||||||
fixture.assertPreferredCompletionItems(0, *items)
|
fixture.assertPreferredCompletionItems(InTextDirectivesUtils.getPrefixedInt(text, "// SELECTED:") ?: 0, *items)
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override fun getTestDataPath() : String? {
|
protected override fun getTestDataPath() : String? {
|
||||||
|
|||||||
+14
-3
@@ -16,18 +16,24 @@
|
|||||||
|
|
||||||
package org.jetbrains.jet.completion.weighers;
|
package org.jetbrains.jet.completion.weighers;
|
||||||
|
|
||||||
import org.jetbrains.jet.JetTestUtils;
|
import junit.framework.Assert;
|
||||||
import org.jetbrains.jet.test.TestMetadata;
|
import junit.framework.Test;
|
||||||
|
import junit.framework.TestSuite;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.regex.Pattern;
|
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 */
|
/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */
|
||||||
@SuppressWarnings("all")
|
@SuppressWarnings("all")
|
||||||
@TestMetadata("idea/testData/completion/weighers")
|
@TestMetadata("idea/testData/completion/weighers")
|
||||||
public class CompletionWeigherTestGenerated extends AbstractCompletionWeigherTest {
|
public class CompletionWeigherTestGenerated extends AbstractCompletionWeigherTest {
|
||||||
public void testAllFilesPresentInWeighers() throws Exception {
|
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")
|
@TestMetadata("DeprecatedFun.kt")
|
||||||
@@ -35,6 +41,11 @@ public class CompletionWeigherTestGenerated extends AbstractCompletionWeigherTes
|
|||||||
doTest("idea/testData/completion/weighers/DeprecatedFun.kt");
|
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")
|
@TestMetadata("LocalValuesAndParams.kt")
|
||||||
public void testLocalValuesAndParams() throws Exception {
|
public void testLocalValuesAndParams() throws Exception {
|
||||||
doTest("idea/testData/completion/weighers/LocalValuesAndParams.kt");
|
doTest("idea/testData/completion/weighers/LocalValuesAndParams.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user