mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 13:02:30 +07:00
split declaration: detect same variables after declaration (IDEA-175261)
This commit is contained in:
@@ -20,6 +20,7 @@ import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
|
||||
import com.intellij.lang.java.JavaLanguage;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Ref;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.javadoc.PsiDocComment;
|
||||
@@ -81,10 +82,31 @@ public class SplitDeclarationAction extends PsiElementBaseIntentionAction {
|
||||
PsiElement parent = decl.getParent();
|
||||
if (parent instanceof PsiForStatement) {
|
||||
String varName = var.getName();
|
||||
if (varName == null ||
|
||||
JavaPsiFacade.getInstance(decl.getProject()).getResolveHelper().resolveReferencedVariable(varName, parent.getParent()) != null) {
|
||||
if (varName == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
parent = parent.getNextSibling();
|
||||
while (parent != null) {
|
||||
Ref<Boolean> conflictFound = new Ref<>(false);
|
||||
parent.accept(new JavaRecursiveElementWalkingVisitor() {
|
||||
@Override
|
||||
public void visitClass(PsiClass aClass) { }
|
||||
|
||||
@Override
|
||||
public void visitVariable(PsiVariable variable) {
|
||||
super.visitVariable(variable);
|
||||
if (varName.equals(variable.getName())) {
|
||||
conflictFound.set(true);
|
||||
stopWalking();
|
||||
}
|
||||
}
|
||||
});
|
||||
if (conflictFound.get()) {
|
||||
return false;
|
||||
}
|
||||
parent = parent.getNextSibling();
|
||||
}
|
||||
}
|
||||
setText(CodeInsightBundle.message("intention.split.declaration.assignment.text"));
|
||||
return true;
|
||||
|
||||
@@ -1005,9 +1005,9 @@ public class JavaCodeStyleManagerImpl extends JavaCodeStyleManager {
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean hasConflictingVariableAfterwards(@Nullable PsiElement scope,
|
||||
@NotNull final String name,
|
||||
@NotNull Predicate<PsiVariable> canBeReused) {
|
||||
public static boolean hasConflictingVariableAfterwards(@Nullable PsiElement scope,
|
||||
@NotNull final String name,
|
||||
@NotNull Predicate<PsiVariable> canBeReused) {
|
||||
PsiElement run = scope;
|
||||
while (run != null) {
|
||||
class CancelException extends RuntimeException {
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
// "Split into declaration and assignment" "true"
|
||||
class Test {
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i<10; i++) {
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
new Runnable() {
|
||||
{
|
||||
int i = 0;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// "Split into declaration and assignment" "true"
|
||||
class Test {
|
||||
{
|
||||
for (int i<caret>=0; i<10; i++) {
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
new Runnable() {
|
||||
{
|
||||
int i = 0;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// "Split into declaration and assignment" "false"
|
||||
class Test {
|
||||
{
|
||||
for (int i<caret>=0; i<10; i++) {
|
||||
System.out.println();
|
||||
}
|
||||
Runnable r = () -> {int i = 0;};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user