mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Completion in try-with-resources
This commit is contained in:
+19
-14
@@ -38,7 +38,6 @@ import com.intellij.util.ProcessingContext;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import static com.intellij.patterns.PsiJavaPatterns.psiElement;
|
||||
import static com.intellij.patterns.StandardPatterns.or;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
@@ -72,15 +71,22 @@ public class JavaClassNameCompletionContributor extends CompletionContributor {
|
||||
public static void addAllClasses(CompletionParameters parameters, final CompletionResultSet result, @NotNull final Consumer<LookupElement> consumer) {
|
||||
final PsiElement insertedElement = parameters.getPosition();
|
||||
|
||||
final ElementFilter filter =
|
||||
or(JavaSmartCompletionContributor.AFTER_THROW_NEW,
|
||||
JavaCompletionContributor.INSIDE_METHOD_THROWS_CLAUSE,
|
||||
JavaCompletionContributor.IN_CATCH_TYPE,
|
||||
JavaCompletionContributor.IN_MULTI_CATCH_TYPE).accepts(insertedElement)
|
||||
? new AssignableFromFilter(CommonClassNames.JAVA_LANG_THROWABLE)
|
||||
: IN_TYPE_PARAMETER.accepts(insertedElement)
|
||||
? new ExcludeDeclaredFilter(new ClassFilter(PsiTypeParameter.class))
|
||||
: TrueFilter.INSTANCE;
|
||||
final ElementFilter filter;
|
||||
if (JavaSmartCompletionContributor.AFTER_THROW_NEW.accepts(insertedElement) ||
|
||||
JavaCompletionContributor.INSIDE_METHOD_THROWS_CLAUSE.accepts(insertedElement) ||
|
||||
JavaCompletionContributor.IN_CATCH_TYPE.accepts(insertedElement) ||
|
||||
JavaCompletionContributor.IN_MULTI_CATCH_TYPE.accepts(insertedElement)) {
|
||||
filter = new AssignableFromFilter(CommonClassNames.JAVA_LANG_THROWABLE);
|
||||
}
|
||||
else if (JavaCompletionContributor.IN_RESOURCE_TYPE.accepts(insertedElement)) {
|
||||
filter = new AssignableFromFilter(CommonClassNames.JAVA_LANG_AUTO_CLOSEABLE);
|
||||
}
|
||||
else if (IN_TYPE_PARAMETER.accepts(insertedElement)) {
|
||||
filter = new ExcludeDeclaredFilter(new ClassFilter(PsiTypeParameter.class));
|
||||
}
|
||||
else {
|
||||
filter = TrueFilter.INSTANCE;
|
||||
}
|
||||
|
||||
final boolean inJavaContext = parameters.getPosition() instanceof PsiIdentifier;
|
||||
if (AFTER_NEW.accepts(insertedElement)) {
|
||||
@@ -102,9 +108,7 @@ public class JavaClassNameCompletionContributor extends CompletionContributor {
|
||||
}
|
||||
|
||||
final boolean lookingForAnnotations = PsiJavaPatterns.psiElement().afterLeaf("@").accepts(insertedElement);
|
||||
|
||||
AllClassesGetter
|
||||
.processJavaClasses(parameters, result.getPrefixMatcher(), parameters.getInvocationCount() <= 1, new Consumer<PsiClass>() {
|
||||
AllClassesGetter.processJavaClasses(parameters, result.getPrefixMatcher(), parameters.getInvocationCount() <= 1, new Consumer<PsiClass>() {
|
||||
@Override
|
||||
public void consume(PsiClass psiClass) {
|
||||
if (lookingForAnnotations && !psiClass.isAnnotationType()) return;
|
||||
@@ -117,7 +121,8 @@ public class JavaClassNameCompletionContributor extends CompletionContributor {
|
||||
}
|
||||
|
||||
public static JavaPsiClassReferenceElement createClassLookupItem(final PsiClass psiClass, final boolean inJavaContext) {
|
||||
return AllClassesGetter.createLookupItem(psiClass, inJavaContext ? JavaClassNameInsertHandler.JAVA_CLASS_INSERT_HANDLER : AllClassesGetter.TRY_SHORTENING);
|
||||
return AllClassesGetter.createLookupItem(psiClass, inJavaContext ? JavaClassNameInsertHandler.JAVA_CLASS_INSERT_HANDLER
|
||||
: AllClassesGetter.TRY_SHORTENING);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+20
-17
@@ -30,10 +30,7 @@ import com.intellij.openapi.editor.highlighter.HighlighterIterator;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Comparing;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.patterns.ElementPattern;
|
||||
import com.intellij.patterns.PatternCondition;
|
||||
import com.intellij.patterns.PsiJavaElementPattern;
|
||||
import com.intellij.patterns.PsiNameValuePairPattern;
|
||||
import com.intellij.patterns.*;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.NameUtil;
|
||||
import com.intellij.psi.filters.*;
|
||||
@@ -64,15 +61,16 @@ import static com.intellij.patterns.PsiJavaPatterns.*;
|
||||
*/
|
||||
public class JavaCompletionContributor extends CompletionContributor {
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.completion.JavaCompletionContributor");
|
||||
|
||||
private static final Java15CompletionData ourJava15CompletionData = new Java15CompletionData();
|
||||
private static final JavaCompletionData ourJavaCompletionData = new JavaCompletionData();
|
||||
private static final PsiNameValuePairPattern NAME_VALUE_PAIR = psiNameValuePair().withSuperParent(
|
||||
2,
|
||||
psiElement(PsiAnnotation.class));
|
||||
|
||||
private static final PsiNameValuePairPattern NAME_VALUE_PAIR =
|
||||
psiNameValuePair().withSuperParent(2, psiElement(PsiAnnotation.class));
|
||||
private static final ElementPattern<PsiElement> ANNOTATION_ATTRIBUTE_NAME =
|
||||
or(psiElement(PsiIdentifier.class).withParent(NAME_VALUE_PAIR),
|
||||
psiElement().afterLeaf("(").withParent(psiReferenceExpression().withParent(NAME_VALUE_PAIR)));
|
||||
public static final ElementPattern SWITCH_LABEL =
|
||||
private static final ElementPattern SWITCH_LABEL =
|
||||
psiElement().withSuperParent(2, psiElement(PsiSwitchLabelStatement.class).withSuperParent(2,
|
||||
psiElement(PsiSwitchStatement.class).with(new PatternCondition<PsiSwitchStatement>("enumExpressionType") {
|
||||
@Override
|
||||
@@ -88,15 +86,20 @@ public class JavaCompletionContributor extends CompletionContributor {
|
||||
elementType().oneOf(JavaTokenType.DOUBLE_LITERAL, JavaTokenType.LONG_LITERAL, JavaTokenType.FLOAT_LITERAL, JavaTokenType.INTEGER_LITERAL)));
|
||||
private static final PsiJavaElementPattern.Capture<PsiElement> IMPORT_REFERENCE =
|
||||
psiElement().withParent(psiElement(PsiJavaCodeReferenceElement.class).withParent(PsiImportStatementBase.class));
|
||||
|
||||
static final PsiJavaElementPattern.Capture<PsiElement> IN_CATCH_TYPE =
|
||||
psiElement().afterLeaf(psiElement().withText("(").withParent(PsiCatchSection.class));
|
||||
static final ElementPattern<PsiElement> IN_MULTI_CATCH_TYPE =
|
||||
or(psiElement().afterLeaf(psiElement().withText("|").withParent(PsiTypeElement.class).withSuperParent(2, PsiCatchSection.class)),
|
||||
psiElement().afterLeaf(psiElement().withText("|").withParent(PsiTypeElement.class).withSuperParent(2, PsiParameter.class).withSuperParent(3, PsiCatchSection.class)));
|
||||
static final PsiJavaElementPattern.Capture<PsiElement> INSIDE_METHOD_THROWS_CLAUSE = psiElement().afterLeaf(PsiKeyword.THROWS, ",").inside(
|
||||
PsiMethod.class).andNot(psiElement().inside(PsiCodeBlock.class)).andNot(psiElement().inside(PsiParameterList.class));
|
||||
static final PsiJavaElementPattern.Capture<PsiElement> INSIDE_METHOD_THROWS_CLAUSE =
|
||||
psiElement().afterLeaf(PsiKeyword.THROWS, ",").inside(PsiMethod.class).andNot(psiElement().inside(PsiCodeBlock.class)).andNot(psiElement().inside(PsiParameterList.class));
|
||||
static final ElementPattern<PsiElement> IN_RESOURCE_TYPE =
|
||||
psiElement().withParent(psiElement(PsiJavaCodeReferenceElement.class).
|
||||
withParent(psiElement(PsiTypeElement.class).
|
||||
withParent(or(psiElement(PsiResourceVariable.class), psiElement(PsiResourceList.class)))));
|
||||
|
||||
@Nullable
|
||||
@Nullable
|
||||
private static ElementFilter getReferenceFilter(PsiElement position) {
|
||||
// Completion after extends in interface, type parameter and implements in class
|
||||
final PsiClass containingClass = PsiTreeUtil.getParentOfType(position, PsiClass.class, false, PsiCodeBlock.class, PsiMethod.class, PsiExpressionList.class, PsiVariable.class);
|
||||
@@ -135,6 +138,10 @@ public class JavaCompletionContributor extends CompletionContributor {
|
||||
return new AssignableFromFilter(CommonClassNames.JAVA_LANG_THROWABLE);
|
||||
}
|
||||
|
||||
if (IN_RESOURCE_TYPE.accepts(position)) {
|
||||
return new AssignableFromFilter(CommonClassNames.JAVA_LANG_AUTO_CLOSEABLE);
|
||||
}
|
||||
|
||||
if (JavaSmartCompletionContributor.AFTER_THROW_NEW.accepts(position)) {
|
||||
return new AssignableFromFilter(CommonClassNames.JAVA_LANG_THROWABLE);
|
||||
}
|
||||
@@ -265,7 +272,6 @@ public class JavaCompletionContributor extends CompletionContributor {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
final Object[] variants = reference.getVariants();
|
||||
if (variants == null) {
|
||||
LOG.error("Reference=" + reference);
|
||||
@@ -328,7 +334,6 @@ public class JavaCompletionContributor extends CompletionContributor {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
private static void completeAnnotationAttributeName(CompletionResultSet result, PsiElement insertedElement,
|
||||
CompletionParameters parameters) {
|
||||
PsiNameValuePair pair = PsiTreeUtil.getParentOfType(insertedElement, PsiNameValuePair.class);
|
||||
@@ -348,12 +353,11 @@ public class JavaCompletionContributor extends CompletionContributor {
|
||||
}
|
||||
|
||||
if (showClasses && insertedElement.getParent() instanceof PsiReferenceExpression) {
|
||||
final Set<LookupElement> set = JavaCompletionUtil.processJavaReference(insertedElement, (PsiJavaReference)insertedElement.getParent(), TrueFilter.INSTANCE, true, result.getPrefixMatcher(), parameters);
|
||||
|
||||
final Set<LookupElement> set = JavaCompletionUtil.processJavaReference(
|
||||
insertedElement, (PsiJavaReference)insertedElement.getParent(), TrueFilter.INSTANCE, true, result.getPrefixMatcher(), parameters);
|
||||
for (final LookupElement element : set) {
|
||||
result.addElement(element);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (annoClass != null) {
|
||||
@@ -375,7 +379,6 @@ public class JavaCompletionContributor extends CompletionContributor {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String advertise(@NotNull final CompletionParameters parameters) {
|
||||
if (!(parameters.getOriginalFile() instanceof PsiJavaFile)) return null;
|
||||
|
||||
|
||||
@@ -1152,10 +1152,8 @@ public class HighlightUtil {
|
||||
@Nullable
|
||||
public static HighlightInfo checkTryResourceIsAutoCloseable(@NotNull final PsiResourceVariable resource) {
|
||||
final PsiType type = resource.getType();
|
||||
if (type == null) return null;
|
||||
|
||||
final PsiElementFactory factory = JavaPsiFacade.getInstance(resource.getProject()).getElementFactory();
|
||||
final PsiClassType autoCloseable = factory.createTypeByFQClassName("java.lang.AutoCloseable", resource.getResolveScope());
|
||||
final PsiClassType autoCloseable = factory.createTypeByFQClassName(CommonClassNames.JAVA_LANG_AUTO_CLOSEABLE, resource.getResolveScope());
|
||||
if (TypeConversionUtil.isAssignable(autoCloseable, type)) return null;
|
||||
|
||||
return createIncompatibleTypeHighlightInfo(autoCloseable, type, resource.getTextRange());
|
||||
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
class MyClass {
|
||||
void f() {
|
||||
try (AutoCloseable<caret>) {
|
||||
}
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
class MyClass {
|
||||
void f() {
|
||||
try (<caret>) {
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
class MyClass {
|
||||
static class MyResource implements AutoCloseable { }
|
||||
|
||||
void f() {
|
||||
try (final MyResource<caret>) {
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
class MyClass {
|
||||
static class MyResource implements AutoCloseable { }
|
||||
|
||||
void f() {
|
||||
try (final My<caret>) {
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
class MyClass {
|
||||
static class MyResource implements AutoCloseable { }
|
||||
|
||||
void f() {
|
||||
try (MyResource<caret> r) {
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
class MyClass {
|
||||
static class MyResource implements AutoCloseable { }
|
||||
|
||||
void f() {
|
||||
try (My<caret> r) {
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
-8
@@ -19,6 +19,7 @@ import java.io.File;
|
||||
@TestDataPath("$CONTENT_ROOT/testData")
|
||||
public class ClassNameCompletionTest extends CompletionTestCase {
|
||||
private static final String BASE_PATH = "/codeInsight/completion/className/";
|
||||
|
||||
protected boolean myOldSetting;
|
||||
|
||||
@Override
|
||||
@@ -27,6 +28,7 @@ public class ClassNameCompletionTest extends CompletionTestCase {
|
||||
setType(CompletionType.CLASS_NAME);
|
||||
myOldSetting = CodeInsightSettings.getInstance().AUTOCOMPLETE_ON_CLASS_NAME_COMPLETION;
|
||||
CodeInsightSettings.getInstance().AUTOCOMPLETE_ON_CLASS_NAME_COMPLETION = true;
|
||||
LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.JDK_1_7);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -223,15 +225,15 @@ public class ClassNameCompletionTest extends CompletionTestCase {
|
||||
|
||||
public void testInCatchType2() throws Exception { doJavaTest(); }
|
||||
|
||||
public void testInMultiCatchType1() throws Exception {
|
||||
LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.JDK_1_7);
|
||||
doJavaTest();
|
||||
}
|
||||
public void testInMultiCatchType1() throws Exception { doJavaTest(); }
|
||||
|
||||
public void testInMultiCatchType2() throws Exception {
|
||||
LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.JDK_1_7);
|
||||
doJavaTest();
|
||||
}
|
||||
public void testInMultiCatchType2() throws Exception { doJavaTest(); }
|
||||
|
||||
public void testInResourceList1() throws Exception { doJavaTest(); }
|
||||
|
||||
public void testInResourceList2() throws Exception { doJavaTest(); }
|
||||
|
||||
public void testInResourceList3() throws Exception { doJavaTest(); }
|
||||
|
||||
private void doJavaTest() throws Exception {
|
||||
final String path = BASE_PATH + "/nameCompletion/java";
|
||||
|
||||
@@ -23,17 +23,21 @@ import org.jetbrains.annotations.NonNls;
|
||||
public interface CommonClassNames {
|
||||
@NonNls String JAVA_LANG_OBJECT = "java.lang.Object";
|
||||
@NonNls String JAVA_LANG_CLASS = "java.lang.Class";
|
||||
@NonNls String JAVA_LANG_STRING = "java.lang.String";
|
||||
@NonNls String JAVA_LANG_ENUM = "java.lang.Enum";
|
||||
|
||||
@NonNls String JAVA_LANG_THROWABLE = "java.lang.Throwable";
|
||||
@NonNls String JAVA_LANG_ANNOTATION_ANNOTATION = "java.lang.annotation.Annotation";
|
||||
@NonNls String JAVA_LANG_EXCEPTION = "java.lang.Exception";
|
||||
@NonNls String JAVA_LANG_ERROR = "java.lang.Error";
|
||||
@NonNls String JAVA_LANG_RUNTIME_EXCEPTION = "java.lang.RuntimeException";
|
||||
@NonNls String JAVA_LANG_ENUM = "java.lang.Enum";
|
||||
@NonNls String JAVA_LANG_AUTO_CLOSEABLE = "java.lang.AutoCloseable";
|
||||
|
||||
@NonNls String JAVA_LANG_ITERABLE = "java.lang.Iterable";
|
||||
@NonNls String JAVA_UTIL_ITERATOR = "java.util.Iterator";
|
||||
|
||||
@NonNls String JAVA_LANG_DEPRECATED = "java.lang.Deprecated";
|
||||
|
||||
@NonNls String JAVA_LANG_ANNOTATION_INHERITED = "java.lang.annotation.Inherited";
|
||||
@NonNls String JAVA_LANG_ANNOTATION_ANNOTATION = "java.lang.annotation.Annotation";
|
||||
|
||||
@NonNls String JAVA_LANG_REFLECT_ARRAY = "java.lang.reflect.Array";
|
||||
|
||||
@@ -46,16 +50,18 @@ public interface CommonClassNames {
|
||||
@NonNls String JAVA_UTIL_PROPERTIES = "java.util.Properties";
|
||||
@NonNls String JAVA_UTIL_PROPERTY_RESOURCE_BUNDLE = "java.util.PropertyResourceBundle";
|
||||
@NonNls String JAVA_UTIL_DATE = "java.util.Date";
|
||||
@NonNls String JAVA_SQL_DATE = "java.sql.Date";
|
||||
@NonNls String JAVA_UTIL_CALENDAR = "java.util.Calendar";
|
||||
@NonNls String JAVA_UTIL_DICTIONARY = "java.util.Dictionary";
|
||||
@NonNls String JAVA_UTIL_COMPARATOR = "java.util.Comparator";
|
||||
|
||||
@NonNls String JAVA_SQL_DATE = "java.sql.Date";
|
||||
|
||||
@NonNls String JAVA_IO_SERIALIZABLE = "java.io.Serializable";
|
||||
@NonNls String JAVA_IO_EXTERNALIZABLE = "java.io.Externalizable";
|
||||
|
||||
@NonNls String JAVA_LANG_STRING = "java.lang.String";
|
||||
@NonNls String JAVA_LANG_STRING_SHORT = "String";
|
||||
@NonNls String JAVA_LANG_NUMBER = "java.lang.Number";
|
||||
|
||||
@NonNls String JAVA_LANG_BOOLEAN = "java.lang.Boolean";
|
||||
@NonNls String JAVA_LANG_BYTE = "java.lang.Byte";
|
||||
@NonNls String JAVA_LANG_SHORT = "java.lang.Short";
|
||||
@@ -68,13 +74,9 @@ public interface CommonClassNames {
|
||||
@NonNls String JAVA_LANG_STRING_BUFFER = "java.lang.StringBuffer";
|
||||
@NonNls String JAVA_LANG_ABSTRACT_STRING_BUILDER = "java.lang.AbstractStringBuilder";
|
||||
|
||||
@NonNls String JAVA_LANG_EXCEPTION = "java.lang.Exception";
|
||||
|
||||
@NonNls String JAVA_LANG_CLONEABLE = "java.lang.Cloneable";
|
||||
@NonNls String JAVA_LANG_COMPARABLE = "java.lang.Comparable";
|
||||
@NonNls String CLASS_FILE_EXTENSION = ".class";
|
||||
|
||||
@NonNls String JAVA_LANG_STRING_SHORT = "String";
|
||||
|
||||
@NonNls String JAVA_UTIL_CONCURRENT_FUTURE = "java.util.concurrent.Future";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user