mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
extract variable: ensure replace all occurrences survive parenthesis around value (IDEA-122041)
This commit is contained in:
+3
-1
@@ -139,7 +139,9 @@ public abstract class AbstractJavaInplaceIntroducer extends AbstractInplaceIntro
|
||||
final PsiElement refVariableElementParent = refVariableElement != null ? refVariableElement.getParent() : null;
|
||||
PsiExpression expression = refVariableElement instanceof PsiKeyword && refVariableElementParent instanceof PsiNewExpression
|
||||
? (PsiNewExpression)refVariableElementParent
|
||||
: PsiTreeUtil.getParentOfType(refVariableElement, PsiReferenceExpression.class);
|
||||
: refVariableElementParent instanceof PsiParenthesizedExpression
|
||||
? ((PsiParenthesizedExpression)refVariableElementParent).getExpression()
|
||||
: PsiTreeUtil.getParentOfType(refVariableElement, PsiReferenceExpression.class);
|
||||
if (expression instanceof PsiReferenceExpression && !(expression.getParent() instanceof PsiMethodCallExpression)) {
|
||||
final String referenceName = ((PsiReferenceExpression)expression).getReferenceName();
|
||||
if (((PsiReferenceExpression)expression).resolve() == psiVariable ||
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
class Test {
|
||||
|
||||
Test foo(long l) {
|
||||
return this;
|
||||
}
|
||||
|
||||
{
|
||||
Test t = new Test()
|
||||
.foo(-(5<caret>L))
|
||||
.foo(7L)
|
||||
.foo(-(5L));
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
class Test {
|
||||
|
||||
Test foo(long l) {
|
||||
return this;
|
||||
}
|
||||
|
||||
public static final long LONG = 5L;
|
||||
|
||||
{
|
||||
Test t = new Test()
|
||||
.foo(-LONG)
|
||||
.foo(7L)
|
||||
.foo(-LONG);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -42,7 +42,7 @@ public abstract class AbstractJavaInplaceIntroduceTest extends AbstractInplaceIn
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected static PsiExpression getExpressionFromEditor() {
|
||||
protected PsiExpression getExpressionFromEditor() {
|
||||
final PsiExpression expression = PsiTreeUtil.getParentOfType(getFile().findElementAt(getEditor().getCaretModel().getOffset()), PsiExpression.class);
|
||||
if (expression instanceof PsiReferenceExpression && expression.getParent() instanceof PsiMethodCallExpression) {
|
||||
return (PsiExpression)expression.getParent();
|
||||
|
||||
@@ -19,10 +19,13 @@ import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Pass;
|
||||
import com.intellij.psi.PsiExpression;
|
||||
import com.intellij.psi.PsiLiteralExpression;
|
||||
import com.intellij.psi.PsiLocalVariable;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer;
|
||||
import com.intellij.refactoring.introduceField.IntroduceConstantHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* User: anna
|
||||
@@ -43,6 +46,28 @@ public class InplaceIntroduceConstantTest extends AbstractJavaInplaceIntroduceTe
|
||||
});
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
protected PsiExpression getExpressionFromEditor() {
|
||||
final PsiExpression expression = super.getExpressionFromEditor();
|
||||
if (expression != null) {
|
||||
return expression;
|
||||
}
|
||||
final PsiExpression expr = PsiTreeUtil.getParentOfType(getFile().findElementAt(getEditor().getCaretModel().getOffset()), PsiExpression.class);
|
||||
return expr instanceof PsiLiteralExpression ? expr : null;
|
||||
}
|
||||
|
||||
public void testReplaceAllInsideParenthesized() throws Exception {
|
||||
|
||||
doTest(new Pass<AbstractInplaceIntroducer>() {
|
||||
@Override
|
||||
public void pass(AbstractInplaceIntroducer inplaceIntroduceFieldPopup) {
|
||||
inplaceIntroduceFieldPopup.setReplaceAllOccurrences(true);
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void testReplaceAllWithClassRefType() throws Exception {
|
||||
|
||||
doTest(new Pass<AbstractInplaceIntroducer>() {
|
||||
|
||||
+3
-5
@@ -35,10 +35,7 @@ import org.jetbrains.plugins.groovy.GroovyFileType;
|
||||
import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.*;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression;
|
||||
import org.jetbrains.plugins.groovy.refactoring.GroovyRefactoringUtil;
|
||||
|
||||
@@ -85,7 +82,8 @@ public abstract class GrAbstractInplaceIntroducer<Settings extends GrIntroduceSe
|
||||
GrExpression expression =
|
||||
refVariableElementParent instanceof GrNewExpression && refVariableElement.getNode().getElementType() == GroovyTokenTypes.kNEW
|
||||
? (GrNewExpression)refVariableElementParent
|
||||
: PsiTreeUtil.getParentOfType(refVariableElement, GrReferenceExpression.class);
|
||||
: refVariableElementParent instanceof GrParenthesizedExpression ? ((GrParenthesizedExpression)refVariableElementParent).getOperand()
|
||||
: PsiTreeUtil.getParentOfType(refVariableElement, GrReferenceExpression.class);
|
||||
if (expression instanceof GrReferenceExpression && !(expression.getParent() instanceof GrMethodCall)) {
|
||||
final String referenceName = ((GrReferenceExpression)expression).getReferenceName();
|
||||
if (((GrReferenceExpression)expression).resolve() == variable ||
|
||||
|
||||
Reference in New Issue
Block a user