inplace introduce variable: correctly create conflict resolver on bigger scope (IDEA-83860)

This commit is contained in:
anna
2012-04-03 11:27:34 +02:00
parent e9c7dda8e2
commit 7621a7af2c
3 changed files with 27 additions and 24 deletions
@@ -98,10 +98,6 @@ public class JavaVariableInplaceIntroducer extends InplaceVariableIntroducer<Psi
rangeMarkers.toArray(new RangeMarker[rangeMarkers.size()]));
}
myExpression = expression;
if (declarationStatement != null) {
final ResolveSnapshotProvider resolveSnapshotProvider = VariableInplaceRenamer.INSTANCE.forLanguage(declarationStatement.getLanguage());
myConflictResolver = resolveSnapshotProvider != null ? resolveSnapshotProvider.createSnapshot(declarationStatement) : null;
}
final PsiType defaultType = elementToRename.getType();
myDefaultType = SmartTypePointerManager.getInstance(project).createSmartTypePointer(defaultType);
setAdvertisementText(getAdvertisementText(declarationStatement, defaultType, hasTypeSuggestion));
@@ -111,6 +107,13 @@ public class JavaVariableInplaceIntroducer extends InplaceVariableIntroducer<Psi
myExpressionText = text;
}
@Override
protected void beforeTemplateStart() {
super.beforeTemplateStart();
final ResolveSnapshotProvider resolveSnapshotProvider = VariableInplaceRenamer.INSTANCE.forLanguage(myScope.getLanguage());
myConflictResolver = resolveSnapshotProvider != null ? resolveSnapshotProvider.createSnapshot(myScope) : null;
}
@Nullable
protected PsiVariable getVariable() {
final PsiDeclarationStatement declarationStatement = myPointer.getElement();
@@ -151,16 +154,16 @@ public class JavaVariableInplaceIntroducer extends InplaceVariableIntroducer<Psi
}
myEditor.getCaretModel().moveToOffset(startOffset);
myEditor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
if (psiVariable.getInitializer() != null) {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
if (psiVariable.getInitializer() != null) {
appendTypeCasts(getOccurrenceMarkers(), file, myProject, psiVariable);
if (myConflictResolver != null) {
myConflictResolver.apply(psiVariable.getName());
}
}
});
}
if (myConflictResolver != null && isIdentifier(myInsertedName, psiVariable.getLanguage())) {
myConflictResolver.apply(psiVariable.getName());
}
}
});
}
else {
RangeMarker exprMarker = getExprMarker();
@@ -24,6 +24,9 @@ import com.intellij.codeInsight.template.*;
import com.intellij.codeInsight.template.impl.TemplateManagerImpl;
import com.intellij.codeInsight.template.impl.TemplateState;
import com.intellij.injected.editor.VirtualFileWindow;
import com.intellij.lang.Language;
import com.intellij.lang.LanguageNamesValidation;
import com.intellij.lang.refactoring.NamesValidator;
import com.intellij.openapi.actionSystem.Shortcut;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
@@ -567,6 +570,11 @@ public abstract class InplaceRefactoring {
myElementToRename = elementToRename;
}
protected boolean isIdentifier(final String newName, final Language language) {
final NamesValidator namesValidator = LanguageNamesValidation.INSTANCE.forLanguage(language);
return namesValidator == null || namesValidator.isIdentifier(newName, myProject);
}
protected static VirtualFile getTopLevelVirtualFile(final FileViewProvider fileViewProvider) {
VirtualFile file = fileViewProvider.getVirtualFile();
if (file instanceof VirtualFileWindow) file = ((VirtualFileWindow)file).getDelegate();
@@ -17,8 +17,6 @@ package com.intellij.refactoring.rename.inplace;
import com.intellij.lang.Language;
import com.intellij.lang.LanguageExtension;
import com.intellij.lang.LanguageNamesValidation;
import com.intellij.lang.refactoring.NamesValidator;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.command.WriteCommandAction;
@@ -191,7 +189,7 @@ public class VariableInplaceRenamer extends InplaceRefactoring {
protected void performRefactoringRename(final String newName,
final StartMarkAction markAction) {
try {
if (!isIdentifier(newName)) {
if (!isIdentifier(newName, myLanguage)) {
return;
}
PsiNamedElement elementToRename = getVariable();
@@ -271,12 +269,12 @@ public class VariableInplaceRenamer extends InplaceRefactoring {
boolean bind = false;
if (myInsertedName != null) {
bind = true;
if (!isIdentifier(myInsertedName)) {
if (!isIdentifier(myInsertedName, myLanguage)) {
performOnInvalidIdentifier(myInsertedName, myNameSuggestions);
}
else {
if (mySnapshot != null) {
if (isIdentifier(myInsertedName)) {
if (isIdentifier(myInsertedName, myLanguage)) {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
mySnapshot.apply(myInsertedName);
@@ -302,14 +300,8 @@ public class VariableInplaceRenamer extends InplaceRefactoring {
}
protected void revertStateOnFinish() {
if (!isIdentifier(myInsertedName)) {
if (!isIdentifier(myInsertedName, myLanguage)) {
revertState();
}
}
private boolean isIdentifier(final String newName) {
final NamesValidator namesValidator = LanguageNamesValidation.INSTANCE.forLanguage(myLanguage);
return namesValidator == null || namesValidator.isIdentifier(newName, myProject);
}
}