introduce over non-valid expression (IDEA-26836)

This commit is contained in:
anna
2010-07-26 15:04:50 +04:00
parent b6c253f29c
commit 0bbbfef619
4 changed files with 39 additions and 6 deletions

View File

@@ -194,12 +194,20 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase impleme
return tempExpr;
}
public static PsiExpression getSelectedExpression(final Project project, final PsiFile file, final int startOffset, final int endOffset) {
public static PsiExpression getSelectedExpression(final Project project, final PsiFile file, int startOffset, int endOffset) {
final PsiElement elementAtStart = file.findElementAt(startOffset);
if (elementAtStart == null) return null;
final PsiElement elementAtEnd = file.findElementAt(endOffset - 1);
if (elementAtEnd == null) return null;
PsiElement elementAtStart = file.findElementAt(startOffset);
if (elementAtStart == null || elementAtStart instanceof PsiWhiteSpace || elementAtStart instanceof PsiComment) {
elementAtStart = PsiTreeUtil.skipSiblingsForward(elementAtStart, PsiWhiteSpace.class, PsiComment.class);
if (elementAtStart == null) return null;
startOffset = elementAtStart.getTextOffset();
}
PsiElement elementAtEnd = file.findElementAt(endOffset - 1);
if (elementAtEnd == null || elementAtEnd instanceof PsiWhiteSpace || elementAtEnd instanceof PsiComment) {
elementAtEnd = PsiTreeUtil.skipSiblingsBackward(elementAtEnd, PsiWhiteSpace.class, PsiComment.class);
if (elementAtEnd == null) return null;
endOffset = elementAtEnd.getTextRange().getEndOffset();
}
PsiExpression tempExpr;
PsiElement elementAt = PsiTreeUtil.findCommonParent(elementAtStart, elementAtEnd);
@@ -295,7 +303,9 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase impleme
tempExpr.putUserData(ElementToWorkOn.PARENT, parent);
}
else {
PsiErrorElement errorElement = PsiTreeUtil.getNextSiblingOfType(elementAtStart, PsiErrorElement.class);
PsiErrorElement errorElement = elementAtStart instanceof PsiErrorElement
? (PsiErrorElement)elementAtStart
: PsiTreeUtil.getNextSiblingOfType(elementAtStart, PsiErrorElement.class);
if (errorElement == null) {
errorElement = PsiTreeUtil.getParentOfType(elementAtStart, PsiErrorElement.class);
}

View File

@@ -0,0 +1,10 @@
class A {
public void test(boolean a, boolean b) {
final boolean ab = a &&
b;
if (true && ab
//some comment
);
}
}

View File

@@ -0,0 +1,9 @@
class A {
public void test(boolean a, boolean b) {
if (true && <selection>a &&
b
//some comment
</selection>
);
}
}

View File

@@ -198,6 +198,10 @@ public class IntroduceVariableTest extends LightCodeInsightTestCase {
public void testAndAndSubexpression() throws Exception {
doTest(new MockIntroduceVariableHandler("ab", true, true, false, "boolean"));
}
public void testSubexpressionWithSpacesInSelection() throws Exception {
doTest(new MockIntroduceVariableHandler("ab", true, true, false, "boolean"));
}
public void testDuplicatesAnonymousClassCreationWithSimilarParameters () throws Exception {
doTest(new MockIntroduceVariableHandler("foo1", true, true, false, "Foo"));