use of com.intellij.refactoring.listeners.RefactoringEventData in project intellij-community by JetBrains.
the class VariableInplaceRenamer method performRefactoringRename.
protected void performRefactoringRename(final String newName, final StartMarkAction markAction) {
final String refactoringId = getRefactoringId();
try {
PsiNamedElement elementToRename = getVariable();
if (refactoringId != null) {
final RefactoringEventData beforeData = new RefactoringEventData();
beforeData.addElement(elementToRename);
beforeData.addStringProperties(myOldName);
myProject.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(refactoringId, beforeData);
}
if (!isIdentifier(newName, myLanguage)) {
return;
}
if (elementToRename != null) {
new WriteCommandAction(myProject, getCommandName()) {
@Override
protected void run(@NotNull Result result) throws Throwable {
renameSynthetic(newName);
}
}.execute();
}
AutomaticRenamerFactory[] factories = Extensions.getExtensions(AutomaticRenamerFactory.EP_NAME);
for (AutomaticRenamerFactory renamerFactory : factories) {
if (elementToRename != null && renamerFactory.isApplicable(elementToRename)) {
final List<UsageInfo> usages = new ArrayList<>();
final AutomaticRenamer renamer = renamerFactory.createRenamer(elementToRename, newName, new ArrayList<>());
if (renamer.hasAnythingToRename()) {
if (!ApplicationManager.getApplication().isUnitTestMode()) {
final AutomaticRenamingDialog renamingDialog = new AutomaticRenamingDialog(myProject, renamer);
if (!renamingDialog.showAndGet()) {
continue;
}
}
final Runnable runnable = () -> ApplicationManager.getApplication().runReadAction(() -> renamer.findUsages(usages, false, false));
if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(runnable, RefactoringBundle.message("searching.for.variables"), true, myProject)) {
return;
}
if (!CommonRefactoringUtil.checkReadOnlyStatus(myProject, PsiUtilCore.toPsiElementArray(renamer.getElements())))
return;
final Runnable performAutomaticRename = () -> {
CommandProcessor.getInstance().markCurrentCommandAsGlobal(myProject);
final UsageInfo[] usageInfos = usages.toArray(new UsageInfo[usages.size()]);
final MultiMap<PsiElement, UsageInfo> classified = RenameProcessor.classifyUsages(renamer.getElements(), usageInfos);
for (final PsiNamedElement element : renamer.getElements()) {
final String newElementName = renamer.getNewName(element);
if (newElementName != null) {
final Collection<UsageInfo> infos = classified.get(element);
RenameUtil.doRename(element, newElementName, infos.toArray(new UsageInfo[infos.size()]), myProject, RefactoringElementListener.DEAF);
}
}
};
final WriteCommandAction writeCommandAction = new WriteCommandAction(myProject, getCommandName()) {
@Override
protected void run(@NotNull Result result) throws Throwable {
performAutomaticRename.run();
}
};
if (ApplicationManager.getApplication().isUnitTestMode()) {
writeCommandAction.execute();
} else {
ApplicationManager.getApplication().invokeLater(() -> writeCommandAction.execute());
}
}
}
}
} finally {
if (refactoringId != null) {
final RefactoringEventData afterData = new RefactoringEventData();
afterData.addElement(getVariable());
afterData.addStringProperties(newName);
myProject.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(refactoringId, afterData);
}
try {
((EditorImpl) InjectedLanguageUtil.getTopLevelEditor(myEditor)).stopDumbLater();
} finally {
FinishMarkAction.finish(myProject, myEditor, markAction);
}
}
}
use of com.intellij.refactoring.listeners.RefactoringEventData in project intellij-community by JetBrains.
the class SafeDeleteProcessor method preprocessUsages.
@Override
protected boolean preprocessUsages(@NotNull Ref<UsageInfo[]> refUsages) {
UsageInfo[] usages = refUsages.get();
ArrayList<String> conflicts = new ArrayList<>();
for (PsiElement element : myElements) {
for (SafeDeleteProcessorDelegate delegate : Extensions.getExtensions(SafeDeleteProcessorDelegate.EP_NAME)) {
if (delegate.handlesElement(element)) {
Collection<String> foundConflicts = delegate instanceof SafeDeleteProcessorDelegateBase ? ((SafeDeleteProcessorDelegateBase) delegate).findConflicts(element, myElements, usages) : delegate.findConflicts(element, myElements);
if (foundConflicts != null) {
conflicts.addAll(foundConflicts);
}
break;
}
}
}
final HashMap<PsiElement, UsageHolder> elementsToUsageHolders = sortUsages(usages);
final Collection<UsageHolder> usageHolders = elementsToUsageHolders.values();
for (UsageHolder usageHolder : usageHolders) {
if (usageHolder.hasUnsafeUsagesInCode()) {
conflicts.add(usageHolder.getDescription());
}
}
if (!conflicts.isEmpty()) {
final RefactoringEventData conflictData = new RefactoringEventData();
conflictData.putUserData(RefactoringEventData.CONFLICTS_KEY, conflicts);
myProject.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).conflictsDetected("refactoring.safeDelete", conflictData);
if (ApplicationManager.getApplication().isUnitTestMode()) {
if (!ConflictsInTestsException.isTestIgnore())
throw new ConflictsInTestsException(conflicts);
} else {
UnsafeUsagesDialog dialog = new UnsafeUsagesDialog(ArrayUtil.toStringArray(conflicts), myProject);
if (!dialog.showAndGet()) {
final int exitCode = dialog.getExitCode();
// dialog is always dismissed;
prepareSuccessful();
if (exitCode == UnsafeUsagesDialog.VIEW_USAGES_EXIT_CODE) {
showUsages(Arrays.stream(usages).filter(usage -> usage instanceof SafeDeleteReferenceUsageInfo && !((SafeDeleteReferenceUsageInfo) usage).isSafeDelete()).toArray(UsageInfo[]::new), usages);
}
return false;
} else {
myPreviewNonCodeUsages = false;
}
}
}
UsageInfo[] preprocessedUsages = usages;
for (SafeDeleteProcessorDelegate delegate : Extensions.getExtensions(SafeDeleteProcessorDelegate.EP_NAME)) {
preprocessedUsages = delegate.preprocessUsages(myProject, preprocessedUsages);
if (preprocessedUsages == null)
return false;
}
final UsageInfo[] filteredUsages = UsageViewUtil.removeDuplicatedUsages(preprocessedUsages);
// dialog is always dismissed
prepareSuccessful();
if (filteredUsages == null) {
return false;
}
refUsages.set(filteredUsages);
return true;
}
use of com.intellij.refactoring.listeners.RefactoringEventData in project intellij-community by JetBrains.
the class RenameProcessor method getAfterData.
@Nullable
@Override
protected RefactoringEventData getAfterData(@NotNull UsageInfo[] usages) {
final RefactoringEventData data = new RefactoringEventData();
data.addElement(myPrimaryElement);
return data;
}
use of com.intellij.refactoring.listeners.RefactoringEventData in project intellij-community by JetBrains.
the class AbstractInplaceIntroducer method performRefactoring.
@Override
protected boolean performRefactoring() {
if (!ensureValid())
return false;
CommandProcessor.getInstance().executeCommand(myProject, () -> {
final String refactoringId = getRefactoringId();
if (refactoringId != null) {
final RefactoringEventData beforeData = new RefactoringEventData();
final V localVariable = getLocalVariable();
if (localVariable != null) {
beforeData.addElement(localVariable);
} else {
final E beforeExpr = getBeforeExpr();
if (beforeExpr != null) {
beforeData.addElement(beforeExpr);
}
}
myProject.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(refactoringId, beforeData);
}
performIntroduce();
}, getCommandName(), getCommandName());
V variable = getVariable();
if (variable != null) {
saveSettings(variable);
}
return false;
}
use of com.intellij.refactoring.listeners.RefactoringEventData in project intellij-community by JetBrains.
the class AbstractInplaceIntroducer method moveOffsetAfter.
@Override
protected void moveOffsetAfter(boolean success) {
if (getLocalVariable() != null && getLocalVariable().isValid()) {
myEditor.getCaretModel().moveToOffset(getLocalVariable().getTextOffset());
myEditor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
} else if (getExprMarker() != null) {
final RangeMarker exprMarker = getExprMarker();
if (exprMarker.isValid()) {
myEditor.getCaretModel().moveToOffset(exprMarker.getStartOffset());
myEditor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
}
}
super.moveOffsetAfter(success);
if (myLocalMarker != null && !isRestart()) {
myLocalMarker.dispose();
}
if (success) {
performPostIntroduceTasks();
final String refactoringId = getRefactoringId();
if (refactoringId != null) {
final RefactoringEventData afterData = new RefactoringEventData();
afterData.addElement(getVariable());
myProject.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(refactoringId, afterData);
}
}
}
Aggregations