mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +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());
|
||||
|
||||
Reference in New Issue
Block a user