mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-13 21:55:01 +07:00
IDEA-134518 Insert "instanceof" completion variant on typing "!" (exclamation mark)
This commit is contained in:
@@ -27,7 +27,6 @@ package com.intellij.codeInsight.completion;
|
||||
import com.intellij.codeInsight.lookup.CharFilter;
|
||||
import com.intellij.codeInsight.lookup.Lookup;
|
||||
import com.intellij.codeInsight.lookup.LookupElement;
|
||||
import com.intellij.codeInsight.lookup.impl.LookupImpl;
|
||||
import com.intellij.lang.java.JavaLanguage;
|
||||
import com.intellij.patterns.PsiJavaPatterns;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
@@ -61,6 +60,9 @@ public class JavaCharFilter extends CharFilter {
|
||||
final PsiType type = ((PsiMethod)o).getReturnType();
|
||||
if (type != null && PsiType.BOOLEAN.isAssignableFrom(type)) return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
|
||||
}
|
||||
if (o instanceof PsiKeyword && ((PsiKeyword)o).textMatches(PsiKeyword.INSTANCEOF)) {
|
||||
return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -19,10 +19,7 @@ import com.intellij.codeInsight.ExpectedTypeInfo;
|
||||
import com.intellij.codeInsight.TailType;
|
||||
import com.intellij.codeInsight.TailTypes;
|
||||
import com.intellij.codeInsight.completion.util.ParenthesesInsertHandler;
|
||||
import com.intellij.codeInsight.lookup.LookupElement;
|
||||
import com.intellij.codeInsight.lookup.LookupItem;
|
||||
import com.intellij.codeInsight.lookup.PsiTypeLookupItem;
|
||||
import com.intellij.codeInsight.lookup.TailTypeDecorator;
|
||||
import com.intellij.codeInsight.lookup.*;
|
||||
import com.intellij.patterns.ElementPattern;
|
||||
import com.intellij.patterns.PsiJavaElementPattern;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
@@ -491,7 +488,29 @@ public class JavaCompletionData extends JavaAwareCompletionData {
|
||||
}
|
||||
|
||||
if (isInstanceofPlace(position)) {
|
||||
result.addElement(TailTypeDecorator.withTail(createKeyword(position, PsiKeyword.INSTANCEOF), TailType.HUMBLE_SPACE_BEFORE_WORD));
|
||||
result.addElement(LookupElementDecorator.withInsertHandler(
|
||||
createKeyword(position, PsiKeyword.INSTANCEOF),
|
||||
new InsertHandler<LookupElementDecorator<LookupElement>>() {
|
||||
@Override
|
||||
public void handleInsert(InsertionContext context, LookupElementDecorator<LookupElement> item) {
|
||||
TailType tailType = TailType.HUMBLE_SPACE_BEFORE_WORD;
|
||||
if (tailType.isApplicable(context)) {
|
||||
tailType.processTail(context.getEditor(), context.getTailOffset());
|
||||
}
|
||||
|
||||
if ('!' == context.getCompletionChar()) {
|
||||
context.setAddCompletionChar(false);
|
||||
context.commitDocument();
|
||||
PsiInstanceOfExpression expr =
|
||||
PsiTreeUtil.findElementOfClassAtOffset(context.getFile(), context.getStartOffset(), PsiInstanceOfExpression.class, false);
|
||||
if (expr != null) {
|
||||
String space = context.getCodeStyleSettings().SPACE_WITHIN_PARENTHESES ? " " : "";
|
||||
context.getDocument().insertString(expr.getTextRange().getStartOffset(), "!(" + space);
|
||||
context.getDocument().insertString(context.getTailOffset(), space + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
if (isSuitableForClass(position)) {
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
class Foo {
|
||||
void test() {
|
||||
if (o in<caret>x) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
class Foo {
|
||||
void test() {
|
||||
if (!(o instanceof <caret>)x) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -101,6 +101,13 @@ public class KeywordCompletionTest extends LightCompletionTestCase {
|
||||
public void testSpaceAfterInstanceof() throws Exception { doTest(false); }
|
||||
public void testInstanceofAfterUnresolved() throws Exception { doTest(1, "instanceof"); }
|
||||
public void testInstanceofAfterStatementStart() throws Exception { doTest(1, "instanceof"); }
|
||||
|
||||
public void testInstanceofNegation() {
|
||||
configureByFile(BASE_PATH + "/" + getTestName(true) + ".java");
|
||||
selectItem(myItems[0], '!');
|
||||
checkResultByFile(BASE_PATH + "/" + getTestName(true) + "_after.java");
|
||||
}
|
||||
|
||||
public void testAbstractInInterface() throws Exception { doTest(1, "abstract"); }
|
||||
public void testCharInAnnotatedParameter() throws Exception { doTest(1, "char"); }
|
||||
public void testReturnInTernary() throws Exception { doTest(1, "return"); }
|
||||
|
||||
Reference in New Issue
Block a user