[java-completion] IDEA-303885 Complete statement on generics generates syntax error

GitOrigin-RevId: 98f4241d73d071480687ec4d5be9a80cb612bb26
This commit is contained in:
Tagir Valeev
2022-10-24 14:46:08 +02:00
committed by intellij-monorepo-bot
parent 5baca85133
commit bfc19c98e4
4 changed files with 34 additions and 0 deletions

View File

@@ -110,6 +110,25 @@ public class SemicolonFixer implements Fixer {
insertionOffset += "()".length();
}
// Like:
// assert x instanceof Type
// String s = "hello";
// Here, String is parsed as name of the pattern variable, and we have an assignment, instead of declaration
if (psiElement.getLastChild() instanceof PsiErrorElement error &&
error.getPrevSibling() instanceof PsiInstanceOfExpression instanceOf &&
instanceOf.getLastChild() instanceof PsiTypeTestPattern typePattern &&
typePattern.getPatternVariable() != null &&
PsiTreeUtil.skipWhitespacesForward(psiElement) instanceof PsiExpressionStatement exprStmt &&
exprStmt.getExpression() instanceof PsiAssignmentExpression assignment &&
assignment.getOperationTokenType().equals(JavaTokenType.EQ)) {
PsiPatternVariable variable = typePattern.getPatternVariable();
PsiIdentifier identifier = variable.getNameIdentifier();
if (identifier.getPrevSibling() instanceof PsiWhiteSpace ws && ws.getText().contains("\n") &&
editor.getCaretModel().getOffset() < identifier.getTextRange().getStartOffset()) {
insertionOffset = ws.getTextRange().getStartOffset();
}
}
if (!StringUtil.endsWithChar(text, ';')) {
PsiElement parent = psiElement.getParent();
String toInsert = ";";

View File

@@ -0,0 +1,7 @@
public class Junk {
public static void main(Object xx) {
assert xx instanceof List<<caret>?>
var x = (List<?>) xx;
}
}

View File

@@ -0,0 +1,7 @@
public class Junk {
public static void main(Object xx) {
assert xx instanceof List<?>;
var x = (List<?>) xx;
}
}

View File

@@ -120,6 +120,7 @@ public class CompleteStatementTest extends EditorActionTestCase {
public void testCompleteIfNextLineBraceStyle2() { myJavaSettings.BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE; doTest(); }
public void testSCR36110() { doTest(); }
public void testSCR37331() { doTest(); }
public void testGenericBeforeVar() { doTest(); }
public void testIDEADEV434() {
mySettings.getCommonSettings(JavaLanguage.INSTANCE).KEEP_SIMPLE_BLOCKS_IN_ONE_LINE = true;