CodeInsightUtil#findExpressionInRange: tolerate single error element at the end of selection

Fixes IDEA-51483 Extract Variable: does nothing on a block with invalid parentheses

GitOrigin-RevId: 4d69e9ec9521b1c962024512deef8fd0f71fe85e
This commit is contained in:
Tagir Valeev
2019-07-02 06:52:16 +03:00
committed by intellij-monorepo-bot
parent aeeb1ebd38
commit f68c88e6cf
4 changed files with 15 additions and 1 deletions
@@ -42,7 +42,7 @@ public class CodeInsightUtil {
if (element2 instanceof PsiJavaToken) {
final PsiJavaToken token = (PsiJavaToken)element2;
final IElementType tokenType = token.getTokenType();
if (tokenType.equals(JavaTokenType.SEMICOLON)) {
if (tokenType.equals(JavaTokenType.SEMICOLON) || element2.getParent() instanceof PsiErrorElement) {
expression = findElementInRange(file, startOffset, element2.getTextRange().getStartOffset(), PsiExpression.class);
}
}
@@ -0,0 +1,5 @@
class Test {
void foo(String s) {
String x = s.trim();)
}
}
@@ -0,0 +1,5 @@
class Test {
void foo(String s) {
<selection>s.trim())</selection>
}
}
@@ -190,6 +190,10 @@ public class IntroduceVariableTest extends LightJavaCodeInsightTestCase {
doTest(new MockIntroduceVariableHandler("mi5", false, false, false, "int"));
}
public void testSkipErroneousParen() {
doTest(new MockIntroduceVariableHandler("x", false, false, false, CommonClassNames.JAVA_LANG_STRING));
}
public void testInsideIf() {
doTest(new MockIntroduceVariableHandler("s1", false, false, false, CommonClassNames.JAVA_LANG_STRING));
}