IDEA-147454 Smart completion add parentheses to class declaration in throws statement
authorpeter <peter@jetbrains.com>
Fri, 6 Nov 2015 10:51:14 +0000 (11:51 +0100)
committerpeter <peter@jetbrains.com>
Fri, 6 Nov 2015 15:07:41 +0000 (16:07 +0100)
java/java-impl/src/com/intellij/codeInsight/completion/JavaSmartCompletionContributor.java
java/java-tests/testData/codeInsight/completion/smartType/MethodThrows-out.java [new file with mode: 0644]
java/java-tests/testData/codeInsight/completion/smartType/MethodThrows.java [new file with mode: 0644]
java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartTypeCompletionTest.java

index d181a34e635836f804e38851da349eb4106c70d3..85d45e784f15595d057376ae4e74b6511adf442b 100644 (file)
@@ -85,12 +85,6 @@ public class JavaSmartCompletionContributor extends CompletionContributor {
       return THROWABLES_FILTER;
     }
     
-    //throws list
-    PsiMethod method = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
-    if (method != null && PsiTreeUtil.isAncestor(method.getThrowsList(), element, true)) {
-      return THROWABLES_FILTER;
-    }
-
     //new xxx.yyy
     if (psiElement().afterLeaf(psiElement().withText(".")).withSuperParent(2, psiElement(PsiNewExpression.class)).accepts(element)) {
       if (((PsiNewExpression)element.getParent().getParent()).getClassReference() == element.getParent()) {
@@ -107,6 +101,10 @@ public class JavaSmartCompletionContributor extends CompletionContributor {
     return null;
   }
 
+  private static boolean isInsideThrowsList(PsiElement element) {
+    PsiMethod method = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
+    return method != null && PsiTreeUtil.isAncestor(method.getThrowsList(), element, true);
+  }
 
 
   public JavaSmartCompletionContributor() {
@@ -128,11 +126,18 @@ public class JavaSmartCompletionContributor extends CompletionContributor {
           PsiTreeUtil.findElementOfClassAtOffset(element.getContainingFile(), parameters.getOffset(), PsiJavaCodeReferenceElement.class, false);
         if (reference != null) {
           ElementFilter filter = getClassReferenceFilter(element);
+          boolean completeConstructor = filter != null;
+          if (filter == null && isInsideThrowsList(element)) {
+            filter = THROWABLES_FILTER;
+          }
           if (filter != null) {
             final List<ExpectedTypeInfo> infos = Arrays.asList(getExpectedTypes(parameters));
-            for (final LookupElement item : completeReference(element, reference, filter, true, false, parameters, result.getPrefixMatcher())) {
+            for (LookupElement item : completeReference(element, reference, filter, true, false, parameters, result.getPrefixMatcher())) {
               if (item.getObject() instanceof PsiClass) {
-                result.addElement(decorate(LookupElementDecorator.withInsertHandler(item, ConstructorInsertHandler.SMART_INSTANCE), infos));
+                if (completeConstructor) {
+                  item = LookupElementDecorator.withInsertHandler(item, ConstructorInsertHandler.SMART_INSTANCE);
+                }
+                result.addElement(decorate(item, infos));
               }
             }
           }
diff --git a/java/java-tests/testData/codeInsight/completion/smartType/MethodThrows-out.java b/java/java-tests/testData/codeInsight/completion/smartType/MethodThrows-out.java
new file mode 100644 (file)
index 0000000..752a63d
--- /dev/null
@@ -0,0 +1,10 @@
+class FirstException extends Exception {}
+class SecondException extends Exception {}
+
+interface I {
+  void method() throws FirstException, SecondException;
+}
+
+class C implements I {
+  void method() throws FirstException, SecondException<caret>;
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/codeInsight/completion/smartType/MethodThrows.java b/java/java-tests/testData/codeInsight/completion/smartType/MethodThrows.java
new file mode 100644 (file)
index 0000000..d533295
--- /dev/null
@@ -0,0 +1,10 @@
+class FirstException extends Exception {}
+class SecondException extends Exception {}
+
+interface I {
+  void method() throws FirstException, SecondException;
+}
+
+class C implements I {
+  void method() throws FirstException, Sec<caret>;
+}
\ No newline at end of file
index e45a9866d2c9c96de39d3553f831e6fe15ccb7b1..a48109935ecfb1bc8d4fdad759dceca14c164cd5 100644 (file)
@@ -428,6 +428,8 @@ public class SmartTypeCompletionTest extends LightFixtureCompletionTestCase {
 
   public void testJavadocThrows() throws Throwable { doTest(); }
 
+  public void testMethodThrows() throws Throwable { doTest(); }
+
   public void testDoNotExcludeAssignedVariable() throws Throwable { doTest(); }
 
   public void testArrayIndexTailType() throws Throwable { doTest(); }