mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
inplace introduce: append semicolon if needed (to avoid parsing assignment as local variable)
This commit is contained in:
+2
-1
@@ -721,7 +721,8 @@ public abstract class BaseExpressionToFieldHandler extends IntroduceHandlerBase
|
||||
initializerPlace == InitializationPlace.IN_CONSTRUCTOR && enclosingConstructor != null && initializer != null) {
|
||||
if (myReplaceAll) {
|
||||
if (enclosingConstructor != null) {
|
||||
final PsiElement anchorInConstructor = myOccurenceManager.getAnchorStatementForAllInScope(enclosingConstructor);
|
||||
final PsiElement anchorInConstructor = RefactoringUtil.getAnchorElementForMultipleExpressions(mySettings.myOccurrences,
|
||||
enclosingConstructor);
|
||||
anchorElementHere = anchorInConstructor != null ? anchorInConstructor : myAnchorStatementIfAll;
|
||||
}
|
||||
else {
|
||||
|
||||
+23
-3
@@ -6,11 +6,13 @@ import com.intellij.codeInsight.template.Expression;
|
||||
import com.intellij.codeInsight.template.ExpressionContext;
|
||||
import com.intellij.codeInsight.template.Result;
|
||||
import com.intellij.codeInsight.template.TextResult;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.RangeMarker;
|
||||
import com.intellij.openapi.fileTypes.StdFileTypes;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Comparing;
|
||||
import com.intellij.openapi.util.Computable;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.codeStyle.VariableKind;
|
||||
@@ -56,6 +58,19 @@ public abstract class AbstractJavaInplaceIntroducer extends AbstractInplaceIntro
|
||||
return createFieldToStartTemplateOn(names, getType());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void correctExpression() {
|
||||
final PsiElement parent = getExpr().getParent();
|
||||
if (parent instanceof PsiExpressionStatement && parent.getLastChild() instanceof PsiErrorElement) {
|
||||
myExpr = ((PsiExpressionStatement)ApplicationManager.getApplication().runWriteAction(new Computable<PsiElement>() {
|
||||
@Override
|
||||
public PsiElement compute() {
|
||||
return parent.replace(JavaPsiFacade.getElementFactory(myProject).createStatementFromText(parent.getText() + ";", parent));
|
||||
}
|
||||
})).getExpression();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public PsiExpression restoreExpression(PsiFile containingFile, PsiVariable psiVariable, RangeMarker marker, String exprText) {
|
||||
return restoreExpression(containingFile, psiVariable, JavaPsiFacade.getElementFactory(myProject), marker, exprText);
|
||||
@@ -65,9 +80,14 @@ public abstract class AbstractJavaInplaceIntroducer extends AbstractInplaceIntro
|
||||
protected void restoreState(PsiVariable psiField) {
|
||||
final SmartTypePointer typePointer = SmartTypePointerManager.getInstance(myProject).createSmartTypePointer(getType());
|
||||
super.restoreState(psiField);
|
||||
myTypeSelectorManager = myExpr != null
|
||||
? new TypeSelectorManagerImpl(myProject, typePointer.getType(), myExpr, myOccurrences)
|
||||
: new TypeSelectorManagerImpl(myProject, typePointer.getType(), myOccurrences);
|
||||
try {
|
||||
myTypeSelectorManager = myExpr != null
|
||||
? new TypeSelectorManagerImpl(myProject, typePointer.getType(), myExpr, myOccurrences)
|
||||
: new TypeSelectorManagerImpl(myProject, typePointer.getType(), myOccurrences);
|
||||
}
|
||||
catch (Exception e) {
|
||||
LOG.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
class Test {
|
||||
private String myTimer;
|
||||
|
||||
Test() {
|
||||
"<caret>"
|
||||
myTimer = "abc";
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
class Test {
|
||||
private String myTimer;
|
||||
|
||||
Test() {
|
||||
"<caret>"
|
||||
myTimer = "";
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
class Test {
|
||||
private Foo myFoo;
|
||||
|
||||
Test() {
|
||||
new Fo<caret>o()
|
||||
myFoo = new Foo();
|
||||
}
|
||||
}
|
||||
|
||||
class Foo{}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
class Test {
|
||||
private Foo myFoo;
|
||||
private final Foo foo;
|
||||
|
||||
Test() {
|
||||
foo = new Foo();
|
||||
myFoo = foo;
|
||||
}
|
||||
}
|
||||
|
||||
class Foo{}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
class Test {
|
||||
private String myTimer;
|
||||
private final String string;
|
||||
|
||||
Test() {
|
||||
string =<caret> "";
|
||||
myTimer = string;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
class Test {
|
||||
private String myTimer;
|
||||
private final String string;
|
||||
|
||||
Test() {
|
||||
string = "";
|
||||
myTimer = "abc";
|
||||
}
|
||||
}
|
||||
@@ -41,6 +41,35 @@ public class InplaceIntroduceFieldTest extends AbstractInplaceIntroduceTest {
|
||||
});
|
||||
}
|
||||
|
||||
public void testBeforeAssignment() throws Exception {
|
||||
|
||||
doTest(new Pass<AbstractInplaceIntroducer>() {
|
||||
@Override
|
||||
public void pass(AbstractInplaceIntroducer inplaceIntroduceFieldPopup) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void testBeforeAssignmentReplaceAll() throws Exception {
|
||||
|
||||
doTest(new Pass<AbstractInplaceIntroducer>() {
|
||||
@Override
|
||||
public void pass(AbstractInplaceIntroducer inplaceIntroduceFieldPopup) {
|
||||
inplaceIntroduceFieldPopup.setReplaceAllOccurrences(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void testBeforeAssignmentReplaceAllCall() throws Exception {
|
||||
|
||||
doTest(new Pass<AbstractInplaceIntroducer>() {
|
||||
@Override
|
||||
public void pass(AbstractInplaceIntroducer inplaceIntroduceFieldPopup) {
|
||||
inplaceIntroduceFieldPopup.setReplaceAllOccurrences(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void testReplaceAll() throws Exception {
|
||||
|
||||
doTest(new Pass<AbstractInplaceIntroducer>() {
|
||||
|
||||
+4
-2
@@ -330,8 +330,8 @@ public abstract class AbstractInplaceIntroducer<V extends PsiNameIdentifierOwner
|
||||
for (E expression : getOccurrences()) {
|
||||
stringUsages.add(Pair.<PsiElement, TextRange>create(expression, new TextRange(0, expression.getTextLength())));
|
||||
}
|
||||
}
|
||||
else if (getExpr() != null) {
|
||||
} else if (getExpr() != null) {
|
||||
correctExpression();
|
||||
stringUsages.add(Pair.<PsiElement, TextRange>create(getExpr(), new TextRange(0, getExpr().getTextLength())));
|
||||
}
|
||||
|
||||
@@ -345,6 +345,8 @@ public abstract class AbstractInplaceIntroducer<V extends PsiNameIdentifierOwner
|
||||
}
|
||||
}
|
||||
|
||||
protected void correctExpression() {}
|
||||
|
||||
@Override
|
||||
protected void collectAdditionalRangesToHighlight(Map<TextRange, TextAttributes> rangesToHighlight,
|
||||
Collection<Pair<PsiElement, TextRange>> stringUsages,
|
||||
|
||||
Reference in New Issue
Block a user