mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-20 13:31:28 +07:00
Insert casted declaration in a nice place without exceptions (IDEA-176211)
This commit is contained in:
@@ -30,15 +30,18 @@ import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.actions.EnterAction;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.codeStyle.SuggestedNameInfo;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.psi.util.TypeConversionUtil;
|
||||
import com.intellij.refactoring.JavaRefactoringSettings;
|
||||
import com.intellij.refactoring.introduceVariable.IntroduceVariableBase;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -363,19 +366,43 @@ public class CreateLocalVarFromInstanceofAction extends BaseIntentionAction {
|
||||
anchorAfter = ((PsiBlockStatement)whileStatement.getBody()).getCodeBlock().getLBrace();
|
||||
}
|
||||
}
|
||||
|
||||
if (anchorAfter == null) {
|
||||
return null;
|
||||
}
|
||||
PsiElement nextSibling = PsiTreeUtil.skipWhitespacesForward(anchorAfter);
|
||||
anchorAfter = nextSibling instanceof PsiComment ? PsiTreeUtil.skipSiblingsForward(nextSibling, PsiComment.class) : anchorAfter;
|
||||
nextSibling = PsiTreeUtil.getNextSiblingOfType(anchorAfter, PsiStatement.class);
|
||||
while (nextSibling instanceof PsiDeclarationStatement) {
|
||||
PsiElement nextSibling = anchorAfter.getNextSibling();
|
||||
while (nextSibling != null) {
|
||||
if (nextSibling instanceof PsiWhiteSpace) {
|
||||
final String text = nextSibling.getText();
|
||||
if (StringUtil.countNewLines(text) > 1) {
|
||||
final PsiElement newWhitespace = PsiParserFacade.SERVICE.getInstance(nextSibling.getProject())
|
||||
.createWhiteSpaceFromText(text.substring(0, text.lastIndexOf('\n')));
|
||||
nextSibling.replace(newWhitespace);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (!isValidDeclarationStatement(nextSibling) && !(nextSibling instanceof PsiComment)) {
|
||||
break;
|
||||
}
|
||||
anchorAfter = nextSibling;
|
||||
nextSibling = PsiTreeUtil.getNextSiblingOfType(anchorAfter, PsiStatement.class);
|
||||
nextSibling = anchorAfter.getNextSibling();
|
||||
}
|
||||
return anchorAfter.getParent().addAfter(toInsert, anchorAfter);
|
||||
}
|
||||
|
||||
private static boolean isValidDeclarationStatement(PsiElement nextSibling) {
|
||||
if (!(nextSibling instanceof PsiDeclarationStatement)) {
|
||||
return false;
|
||||
}
|
||||
final PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement)nextSibling;
|
||||
final PsiElement[] elements = declarationStatement.getDeclaredElements();
|
||||
if (elements.length == 0) {
|
||||
return false;
|
||||
}
|
||||
final PsiElement lastElement = elements[elements.length - 1];
|
||||
return !(lastElement instanceof PsiClass) && PsiUtil.isJavaToken(lastElement.getLastChild(), JavaTokenType.SEMICOLON);
|
||||
}
|
||||
|
||||
private static void reformatNewCodeBlockBraces(final PsiElement start, final PsiBlockStatement end)
|
||||
throws IncorrectOperationException {
|
||||
CodeStyleManager.getInstance(end.getProject()).reformatRange(end.getContainingFile(),
|
||||
|
||||
@@ -5,7 +5,6 @@ class C {
|
||||
if (s instanceof String) {
|
||||
String s1 = (String) s;
|
||||
<caret>
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Insert '(String)o' declaration" "true"
|
||||
class X {
|
||||
void foo(Object o) {
|
||||
if (o instanceof String) {
|
||||
String o1 = (String) o;
|
||||
<caret>
|
||||
String substring = o.();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,8 +5,7 @@ class C {
|
||||
void f(Object o) {
|
||||
if (o instanceof IOException) {
|
||||
IOException o1 = (IOException) o;
|
||||
|
||||
|
||||
<caret>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
// "Insert '(String)o' declaration" "true"
|
||||
class X {
|
||||
void foo(Object o) {
|
||||
if (o instanceof String) {
|
||||
String a;
|
||||
String b;
|
||||
String o1 = (String) o;
|
||||
<caret>
|
||||
|
||||
String c;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ class C {
|
||||
if (o instanceof String && f == null) {
|
||||
String o1 = (String) o;
|
||||
<caret>
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,6 @@
|
||||
class C {
|
||||
void f(Object o, Object f) {
|
||||
if (o instanceof String) {//todo comment
|
||||
|
||||
String o1 = (String) o;
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
// "Insert '(String)o' declaration" "true"
|
||||
class X {
|
||||
void foo(Object o) {
|
||||
if (o insta<caret>nceof String) {
|
||||
String substring = o.();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// "Insert '(String)o' declaration" "true"
|
||||
class X {
|
||||
void foo(Object o) {
|
||||
if (o insta<caret>nceof String) {
|
||||
String a;
|
||||
String b;
|
||||
|
||||
|
||||
String c;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user