java completion: rewrite else/catch/finally keyword suggestions, add tests

This commit is contained in:
peter
2015-08-17 09:06:23 +02:00
parent 425c6230b8
commit 2e7061420f
5 changed files with 55 additions and 50 deletions
@@ -310,49 +310,6 @@ public class JavaCompletionData extends JavaAwareCompletionData {
registerVariant(variant);
}
// Catch/Finally completion
{
final ElementFilter position = AFTER_TRY_BLOCK;
final CompletionVariant variant = new CompletionVariant(position);
variant.includeScopeClass(PsiCodeBlock.class, true);
variant.addCompletion(PsiKeyword.CATCH, TailTypes.CATCH_LPARENTH);
variant.addCompletion(PsiKeyword.FINALLY, TailTypes.FINALLY_LBRACE);
registerVariant(variant);
}
// Catch/Finally completion
{
final ElementFilter position = new LeftNeighbour(new AndFilter(
new TextFilter("}"),
new ParentElementFilter(new AndFilter(
new LeftNeighbour(new NotFilter(new TextFilter(PsiKeyword.TRY))),
new OrFilter(
new ParentElementFilter(new ClassFilter(PsiTryStatement.class)),
new ParentElementFilter(new ClassFilter(PsiCatchSection.class)))
))));
final CompletionVariant variant = new CompletionVariant(position);
variant.includeScopeClass(PsiCodeBlock.class, false);
variant.addCompletion(PsiKeyword.CATCH, TailTypes.CATCH_LPARENTH);
variant.addCompletion(PsiKeyword.FINALLY, TailTypes.FINALLY_LBRACE);
registerVariant(variant);
}
// Completion for else expression
// completion
{
final ElementFilter position = new LeftNeighbour(
new OrFilter(
new AndFilter(new TextFilter("}"),new ParentElementFilter(new ClassFilter(PsiIfStatement.class), 3)),
new AndFilter(new TextFilter(";"),new ParentElementFilter(new ClassFilter(PsiIfStatement.class), 2))
));
final CompletionVariant variant = new CompletionVariant(PsiMethod.class, position);
variant.addCompletion(PsiKeyword.ELSE);
registerVariant(variant);
}
}
private static TailType getReturnTail(PsiElement position) {
@@ -381,7 +338,7 @@ public class JavaCompletionData extends JavaAwareCompletionData {
}
}
private static void addStatementKeywords(Consumer<LookupElement> variant, PsiElement position) {
private static void addStatementKeywords(Consumer<LookupElement> variant, PsiElement position, PsiElement prevLeaf) {
variant.consume(new OverrideableSpace(createKeyword(position, PsiKeyword.SWITCH), TailTypes.SWITCH_LPARENTH));
variant.consume(new OverrideableSpace(createKeyword(position, PsiKeyword.WHILE), TailTypes.WHILE_LPARENTH));
variant.consume(new OverrideableSpace(createKeyword(position, PsiKeyword.DO), TailTypes.DO_LBRACE));
@@ -402,11 +359,22 @@ public class JavaCompletionData extends JavaAwareCompletionData {
ret = new OverrideableSpace(ret, returnTail);
}
variant.consume(ret);
if (psiElement().withText(";").withSuperParent(2, PsiIfStatement.class).accepts(prevLeaf) ||
psiElement().withText("}").withSuperParent(3, PsiIfStatement.class).accepts(prevLeaf)) {
variant.consume(new OverrideableSpace(createKeyword(position, PsiKeyword.ELSE), TailTypes.SYNCHRONIZED_LPARENTH));
}
if (psiElement().withText("}").withParent(psiElement(PsiCodeBlock.class).withParent(or(psiElement(PsiTryStatement.class), psiElement(PsiCatchSection.class)))).accepts(prevLeaf)) {
variant.consume(new OverrideableSpace(createKeyword(position, PsiKeyword.CATCH), TailTypes.CATCH_LPARENTH));
variant.consume(new OverrideableSpace(createKeyword(position, PsiKeyword.FINALLY), TailTypes.FINALLY_LBRACE));
}
}
public void fillCompletions(CompletionParameters parameters, final Consumer<LookupElement> result) {
final PsiElement position = parameters.getPosition();
if (PsiTreeUtil.getParentOfType(position, PsiComment.class, false) != null) {
if (PsiTreeUtil.getNonStrictParentOfType(position, PsiLiteralExpression.class, PsiComment.class) != null) {
return;
}
@@ -420,7 +388,7 @@ public class JavaCompletionData extends JavaAwareCompletionData {
}
addBreakContinue(result, position);
addStatementKeywords(result, position);
addStatementKeywords(result, position, prevLeaf);
}
addThisSuper(result, position);
@@ -764,10 +732,6 @@ public class JavaCompletionData extends JavaAwareCompletionData {
}
private static boolean isStatementPosition(PsiElement position) {
if (PsiTreeUtil.getNonStrictParentOfType(position, PsiLiteralExpression.class, PsiComment.class) != null) {
return false;
}
if (psiElement().withSuperParent(2, PsiConditionalExpression.class).andNot(psiElement().insideStarting(psiElement(PsiConditionalExpression.class))).accepts(position)) {
return false;
}
@@ -0,0 +1,6 @@
public class A {
public void method() {
if (true) { foo(); }
<caret>
}
}
@@ -0,0 +1,6 @@
public class A {
public void method() {
if (true) foo();
<caret>
}
}
@@ -0,0 +1,26 @@
import java.lang.Exception;
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class A{
public void method(){
try{
String str = "";
}
catch (Exception e) {}
<caret>
}
}
@@ -76,6 +76,8 @@ public class KeywordCompletionTest extends LightCompletionTestCase {
public void testMethodScope3() throws Exception { doTest(1, "final", "public", "static", "volatile", "abstract", "throws", "instanceof"); }
public void testMethodScope4() throws Exception { doTest(6, "final", "try", "for", "while", "return", "throw"); }
public void testMethodScope5() throws Exception { doTest(false); }
public void testElseAfterSemicolon() throws Exception { doTest(1, "else"); }
public void testElseAfterRBrace() throws Exception { doTest(1, "else"); }
public void testExtraBracketAfterFinally1() throws Exception { doTest(false); }
public void testExtraBracketAfterFinally2() throws Exception { doTest(false); }
public void testExtendsInCastTypeParameters() throws Exception { doTest(false); }
@@ -89,6 +91,7 @@ public class KeywordCompletionTest extends LightCompletionTestCase {
public void testInstanceOf2() throws Exception { doTest(false); }
public void testInstanceOf3() throws Exception { doTest(false); }
public void testCatchFinally() throws Exception { doTest(2, "catch", "finally"); }
public void testSecondCatch() throws Exception { doTest(2, "catch", "finally"); }
public void testSuper1() throws Exception { doTest(1, "super"); }
public void testSuper2() throws Exception { doTest(0, "super"); }
public void testSuper3() throws Exception { doTest(true); }