From 26e1c19777f47cccff420d2c83204341a13db05a Mon Sep 17 00:00:00 2001 From: Nicolay Mitropolsky Date: Wed, 20 Jun 2018 17:08:47 +0300 Subject: [PATCH] intelliLang: fixes for "undo" action for JavaLanguageInjectionSupport (IDEA-109366, IDEA-CR-33537) --- .../java/JavaLanguageInjectionSupport.java | 11 ++++-- .../plugins/intelliLang/Configuration.java | 39 +++++++++++++++---- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/inject/java/JavaLanguageInjectionSupport.java b/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/inject/java/JavaLanguageInjectionSupport.java index c73fd1ada945..d3defc27be1c 100644 --- a/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/inject/java/JavaLanguageInjectionSupport.java +++ b/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/inject/java/JavaLanguageInjectionSupport.java @@ -129,7 +129,7 @@ public class JavaLanguageInjectionSupport extends AbstractLanguageInjectionSuppo newInjection.setPlaceEnabled(placeText, false); return InjectorUtils.canBeRemoved(newInjection)? null : newInjection; }); - configuration.replaceInjectionsWithUndo(project, newInjections, originalInjections, annotations); + configuration.replaceInjectionsWithUndo(project, host.getContainingFile(), newInjections, originalInjections, annotations); return true; } @@ -152,7 +152,8 @@ public class JavaLanguageInjectionSupport extends AbstractLanguageInjectionSuppo newInjection.mergeOriginalPlacesFrom(copy, false); newInjection.mergeOriginalPlacesFrom(originalInjection, true); configuration.replaceInjectionsWithUndo( - project, Collections.singletonList(newInjection), Collections.singletonList(originalInjection), Collections.emptyList()); + project, psiElement.getContainingFile(), Collections.singletonList(newInjection), Collections.singletonList(originalInjection), + Collections.emptyList()); } return true; @@ -378,10 +379,12 @@ public class JavaLanguageInjectionSupport extends AbstractLanguageInjectionSuppo originalCopy.setPlaceEnabled(currentPlace.getText(), true); methodParameterInjection = createFrom(project, originalCopy, contextMethod, false); } - mergePlacesAndAddToConfiguration(project, configuration, methodParameterInjection, originalInjection); + mergePlacesAndAddToConfiguration(project, contextMethod.getContainingFile(), configuration, methodParameterInjection, + originalInjection); } private static void mergePlacesAndAddToConfiguration(@NotNull Project project, + @Nullable PsiFile psiFile, @NotNull Configuration configuration, @NotNull MethodParameterInjection injection, @Nullable BaseInjection originalInjection) { @@ -390,7 +393,7 @@ public class JavaLanguageInjectionSupport extends AbstractLanguageInjectionSuppo newInjection.mergeOriginalPlacesFrom(originalInjection, true); } configuration.replaceInjectionsWithUndo( - project, Collections.singletonList(newInjection), + project, psiFile, Collections.singletonList(newInjection), ContainerUtil.createMaybeSingletonList(originalInjection), Collections.emptyList()); } diff --git a/plugins/IntelliLang/src/org/intellij/plugins/intelliLang/Configuration.java b/plugins/IntelliLang/src/org/intellij/plugins/intelliLang/Configuration.java index d78334a9061f..d0555e363f7e 100644 --- a/plugins/IntelliLang/src/org/intellij/plugins/intelliLang/Configuration.java +++ b/plugins/IntelliLang/src/org/intellij/plugins/intelliLang/Configuration.java @@ -468,7 +468,7 @@ public class Configuration extends SimpleModificationTracker implements Persiste } } if (!originalInjections.isEmpty()) { - replaceInjectionsWithUndo(host.getProject(), newInjections, originalInjections, Collections.emptyList()); + replaceInjectionsWithUndo(host.getProject(), host.getContainingFile(), newInjections, originalInjections, Collections.emptyList()); return true; } return false; @@ -488,15 +488,33 @@ public class Configuration extends SimpleModificationTracker implements Persiste return Collections.unmodifiableList(myInjections.get(injectorId)); } + /** + * @deprecated use {@link #replaceInjectionsWithUndo(Project, PsiFile, List, List, List)}, + * and consider passing non-null {@code hostFile} to make undo-redo registered for this file, + * especially when {@code psiElementsToRemove} is null (IDEA-109366) + * To be removed in IDEA 2019.2 + */ + @Deprecated public void replaceInjectionsWithUndo(final Project project, - final List newInjections, - final List originalInjections, - final List psiElementsToRemove) { - replaceInjectionsWithUndo(project, newInjections, originalInjections, psiElementsToRemove, + final List newInjections, + final List originalInjections, + final List psiElementsToRemove) { + replaceInjectionsWithUndo(project, null, newInjections, originalInjections, psiElementsToRemove); + } + + /** + * @since 2018.3 + */ + public void replaceInjectionsWithUndo(Project project, + @Nullable PsiFile hostFile, + List newInjections, + List originalInjections, + List psiElementsToRemove) { + replaceInjectionsWithUndo(project, hostFile, newInjections, originalInjections, psiElementsToRemove, (add, remove) -> { replaceInjectionsWithUndoInner(add, remove); - if (ContainerUtil.find(add, LANGUAGE_INJECTION_CONDITION) != null || ContainerUtil.find(remove, - LANGUAGE_INJECTION_CONDITION) != null) { + if (ContainerUtil.find(add, LANGUAGE_INJECTION_CONDITION) != null || + ContainerUtil.find(remove, LANGUAGE_INJECTION_CONDITION) != null) { FileContentUtil.reparseOpenedFiles(); } return true; @@ -507,6 +525,13 @@ public class Configuration extends SimpleModificationTracker implements Persiste replaceInjections(add, remove, false); } + /** + * @deprecated use {@link #replaceInjectionsWithUndo(Project, PsiFile, Object, Object, List, PairProcessor)}, + * and consider passing non-null {@code hostFile} to make undo-redo registered for this file, + * especially when {@code psiElementsToRemove} is null (IDEA-109366) + * To be removed in IDEA 2019.2 + */ + @Deprecated public static void replaceInjectionsWithUndo(final Project project, final T add, final T remove, final List psiElementsToRemove, final PairProcessor actualProcessor) {