mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-79863 jUnit 3: class/smart completion of classnames in test-sources should prefer *TestCase classes
This commit is contained in:
@@ -15,7 +15,8 @@
|
||||
*/
|
||||
package com.intellij.psi.util.proximity;
|
||||
|
||||
import com.intellij.openapi.util.Condition;
|
||||
import com.intellij.openapi.roots.ProjectFileIndex;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.patterns.PlatformPatterns;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
@@ -28,40 +29,25 @@ import org.jetbrains.annotations.Nullable;
|
||||
*/
|
||||
public class ReferenceListWeigher extends ProximityWeigher {
|
||||
|
||||
protected static final Condition<PsiClass> PREFER_INTERFACES = new Condition<PsiClass>() {
|
||||
@Override
|
||||
public boolean value(PsiClass psiClass) {
|
||||
return psiClass.isInterface();
|
||||
}
|
||||
};
|
||||
protected static final Condition<PsiClass> PREFER_CLASSES = new Condition<PsiClass>() {
|
||||
@Override
|
||||
public boolean value(PsiClass psiClass) {
|
||||
return !psiClass.isInterface();
|
||||
}
|
||||
};
|
||||
protected static final Condition<PsiClass> PREFER_EXCEPTIONS = new Condition<PsiClass>() {
|
||||
@Override
|
||||
public boolean value(PsiClass psiClass) {
|
||||
return InheritanceUtil.isInheritor(psiClass, CommonClassNames.JAVA_LANG_THROWABLE);
|
||||
}
|
||||
};
|
||||
protected enum Preference {
|
||||
Interfaces, Classes, Exceptions
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected Condition<PsiClass> getPreferredCondition(@NotNull ProximityLocation location) {
|
||||
protected Preference getPreferredCondition(@NotNull ProximityLocation location) {
|
||||
PsiElement position = location.getPosition();
|
||||
if (PlatformPatterns.psiElement().withParents(PsiJavaCodeReferenceElement.class, PsiReferenceList.class).accepts(position)) {
|
||||
assert position != null;
|
||||
PsiReferenceList list = (PsiReferenceList)position.getParent().getParent();
|
||||
PsiReferenceList.Role role = list.getRole();
|
||||
if (shouldContainInterfaces(list, role)) {
|
||||
return PREFER_INTERFACES;
|
||||
return Preference.Interfaces;
|
||||
}
|
||||
if (role == PsiReferenceList.Role.EXTENDS_LIST) {
|
||||
return PREFER_CLASSES;
|
||||
return Preference.Classes;
|
||||
}
|
||||
if (role == PsiReferenceList.Role.THROWS_LIST) {
|
||||
return PREFER_EXCEPTIONS;
|
||||
return Preference.Exceptions;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -82,10 +68,20 @@ public class ReferenceListWeigher extends ProximityWeigher {
|
||||
@Override
|
||||
public Integer weigh(@NotNull PsiElement element, @NotNull ProximityLocation location) {
|
||||
if (element instanceof PsiClass) {
|
||||
Condition<PsiClass> condition = getPreferredCondition(location);
|
||||
if (condition != null) {
|
||||
return condition.value((PsiClass)element) ? 1 : -1;
|
||||
Preference condition = getPreferredCondition(location);
|
||||
PsiClass aClass = (PsiClass)element;
|
||||
if (condition == Preference.Interfaces) return aClass.isInterface() ? 1 : -1;
|
||||
if (condition == Preference.Classes) {
|
||||
if (aClass.isInterface()) return -1;
|
||||
if (aClass.getName().endsWith("TestCase")) {
|
||||
VirtualFile vFile = aClass.getContainingFile().getVirtualFile();
|
||||
if (vFile != null && ProjectFileIndex.SERVICE.getInstance(location.getProject()).isInTestSourceContent(vFile)) {
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
if (condition == Preference.Exceptions) return InheritanceUtil.isInheritor(aClass, CommonClassNames.JAVA_LANG_THROWABLE) ? 1 : -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
class Foo extends Some<caret>
|
||||
+27
-1
@@ -9,9 +9,12 @@ import com.intellij.codeInsight.lookup.LookupElement;
|
||||
import com.intellij.codeInsight.lookup.LookupManager;
|
||||
import com.intellij.codeInsight.lookup.impl.LookupImpl;
|
||||
import com.intellij.openapi.command.WriteCommandAction;
|
||||
import com.intellij.openapi.roots.ContentEntry;
|
||||
import com.intellij.openapi.roots.ModifiableRootModel;
|
||||
import com.intellij.openapi.roots.ModuleRootManager;
|
||||
import com.intellij.openapi.roots.SourceFolder;
|
||||
import com.intellij.openapi.vfs.LocalFileSystem;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.JavaPsiFacade;
|
||||
import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase;
|
||||
|
||||
@@ -42,7 +45,30 @@ public class HeavyNormalCompletionTest extends JavaCodeInsightFixtureTestCase {
|
||||
assertTrue(JavaPsiFacade.getInstance(getProject()).findPackage("foo.bar").isValid());
|
||||
assertTrue(JavaPsiFacade.getInstance(getProject()).findPackage("foo.bar.goo").isValid());
|
||||
}
|
||||
|
||||
|
||||
public void testPreferTestCases() throws Throwable {
|
||||
myFixture.configureByFile("/codeInsight/completion/normal/" + getTestName(false) + ".java");
|
||||
new WriteCommandAction.Simple(getProject()) {
|
||||
@Override
|
||||
protected void run() throws Throwable {
|
||||
final ModifiableRootModel model = ModuleRootManager.getInstance(myFixture.getModule()).getModifiableModel();
|
||||
ContentEntry contentEntry = model.getContentEntries()[0];
|
||||
SourceFolder sourceFolder = contentEntry.getSourceFolders()[0];
|
||||
VirtualFile file = sourceFolder.getFile();
|
||||
contentEntry.removeSourceFolder(sourceFolder);
|
||||
contentEntry.addSourceFolder(file, true);
|
||||
model.commit();
|
||||
}
|
||||
}.execute().throwException();
|
||||
|
||||
myFixture.addClass("package foo; public class SomeTestCase {}");
|
||||
myFixture.addClass("package bar; public class SomeTestec {}");
|
||||
myFixture.addClass("package goo; public class SomeAnchor {}");
|
||||
|
||||
myFixture.completeBasic();
|
||||
myFixture.assertPreferredCompletionItems(0, "SomeTestCase", "SomeAnchor", "SomeTestec");
|
||||
}
|
||||
|
||||
public void testAllClassesWhenNothingIsFound() throws Throwable {
|
||||
myFixture.addClass("package foo.bar; public class AxBxCxDxEx {}");
|
||||
|
||||
|
||||
+4
-6
@@ -15,9 +15,7 @@
|
||||
*/
|
||||
package org.jetbrains.plugins.groovy.lang.completion.weighers;
|
||||
|
||||
import com.intellij.openapi.util.Condition;
|
||||
import com.intellij.patterns.PlatformPatterns;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.util.ProximityLocation;
|
||||
import com.intellij.psi.util.proximity.ReferenceListWeigher;
|
||||
@@ -32,7 +30,7 @@ import org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement;
|
||||
*/
|
||||
public class GrReferenceListWeigher extends ReferenceListWeigher {
|
||||
@Override
|
||||
protected Condition<PsiClass> getPreferredCondition(@NotNull ProximityLocation location) {
|
||||
protected Preference getPreferredCondition(@NotNull ProximityLocation location) {
|
||||
PsiElement position = location.getPosition();
|
||||
if (PlatformPatterns.psiElement().withParents(GrCodeReferenceElement.class, GrReferenceList.class).accepts(position)) {
|
||||
assert position != null;
|
||||
@@ -41,14 +39,14 @@ public class GrReferenceListWeigher extends ReferenceListWeigher {
|
||||
if (parent instanceof GrTypeDefinition) {
|
||||
GrTypeDefinition cls = (GrTypeDefinition)parent;
|
||||
if (cls.isInterface() && list == cls.getExtendsClause() || list == cls.getImplementsClause()) {
|
||||
return PREFER_INTERFACES;
|
||||
return Preference.Interfaces;
|
||||
}
|
||||
if (list == cls.getExtendsClause()) {
|
||||
return PREFER_CLASSES;
|
||||
return Preference.Classes;
|
||||
}
|
||||
}
|
||||
if (parent instanceof GrMethod && ((GrMethod)parent).getThrowsList() == list) {
|
||||
return PREFER_EXCEPTIONS;
|
||||
return Preference.Exceptions;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user