preserve comments: replace cast with var

This commit is contained in:
Anna.Kozlova
2017-12-14 10:29:52 +01:00
parent 003763b55f
commit 0bc1e140a3
3 changed files with 6 additions and 3 deletions

View File

@@ -26,6 +26,7 @@ import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ig.psiutils.CommentTracker;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -76,7 +77,7 @@ public class ReplaceCastWithVariableAction extends PsiElementBaseIntentionAction
final PsiElement toReplace = typeCastExpression.getParent() instanceof PsiParenthesizedExpression ? typeCastExpression.getParent() : typeCastExpression;
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
toReplace.replace(factory.createExpressionFromText(myReplaceVariableName, toReplace));
new CommentTracker().replaceAndRestoreComments(toReplace, factory.createExpressionFromText(myReplaceVariableName, toReplace));
}
@Nullable

View File

@@ -7,6 +7,7 @@ class FooBar {
FooBar foobar = (FooBar)foo;
foobar = null;
FooBar foobar2 = (FooBar)foo;
return foobar2.baz;
//comment
return foobar2.baz;
}
}

View File

@@ -7,6 +7,7 @@ class FooBar {
FooBar foobar = (FooBar)foo;
foobar = null;
FooBar foobar2 = (FooBar)foo;
return ((FooBar<caret>)foo).baz;
return ((FooBar<caret>)foo//comment
).baz;
}
}