Give more priority to declarations from the same file

This commit is contained in:
Nikolay Krasko
2013-09-13 22:40:44 +04:00
parent 39bf53155b
commit b0acf758bc
7 changed files with 64 additions and 14 deletions
@@ -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(
@@ -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)
@@ -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);
@@ -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 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? {
@@ -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");