diff --git a/platform/lang-impl/src/com/intellij/codeInsight/actions/FormatChangedTextUtil.java b/platform/lang-impl/src/com/intellij/codeInsight/actions/FormatChangedTextUtil.java index 3b51cc9ad976..dadafc7a7d84 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/actions/FormatChangedTextUtil.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/actions/FormatChangedTextUtil.java @@ -250,7 +250,7 @@ public class FormatChangedTextUtil { } @NotNull - public static List getChangedTextRanges(@NotNull Project project, @NotNull PsiFile file) { + public static List getChangedTextRanges(@NotNull Project project, @NotNull PsiFile file) throws FilesTooBigForDiffException { Change change = ChangeListManager.getInstance(project).getChange(file.getVirtualFile()); if (change == null) { return ContainerUtilRt.emptyList(); @@ -281,7 +281,10 @@ public class FormatChangedTextUtil { } @NotNull - private static List calculateChangedTextRanges(@NotNull Project project, @NotNull PsiFile file, @NotNull String contentFromVcs) { + private static List calculateChangedTextRanges(@NotNull Project project, + @NotNull PsiFile file, + @NotNull String contentFromVcs) throws FilesTooBigForDiffException + { Document documentFromVcs = ((EditorFactoryImpl)EditorFactory.getInstance()).createDocument(contentFromVcs, true, false); Document document = PsiDocumentManager.getInstance(project).getDocument(file); @@ -289,23 +292,16 @@ public class FormatChangedTextUtil { return ContainerUtil.emptyList(); } - try { - List changedRanges; - - LineStatusTracker tracker = LineStatusTrackerManager.getInstance(project).getLineStatusTracker(document); - if (tracker != null) { - changedRanges = tracker.getRanges(); - } - else { - changedRanges = new RangesBuilder(document, documentFromVcs).getRanges(); - } - - return getChangedTextRanges(document, changedRanges); + List changedRanges; + LineStatusTracker tracker = LineStatusTrackerManager.getInstance(project).getLineStatusTracker(document); + if (tracker != null) { + changedRanges = tracker.getRanges(); } - catch (FilesTooBigForDiffException e) { - LOG.error("Error while calculating changed ranges for: " + file.getVirtualFile(), e); - return ContainerUtil.emptyList(); + else { + changedRanges = new RangesBuilder(document, documentFromVcs).getRanges(); } + + return getChangedTextRanges(document, changedRanges); } @NotNull diff --git a/platform/lang-impl/src/com/intellij/codeInsight/actions/ReformatCodeProcessor.java b/platform/lang-impl/src/com/intellij/codeInsight/actions/ReformatCodeProcessor.java index ff032c22747b..270f2f92f3cf 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/actions/ReformatCodeProcessor.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/actions/ReformatCodeProcessor.java @@ -18,6 +18,10 @@ package com.intellij.codeInsight.actions; import com.intellij.codeInsight.CodeInsightBundle; import com.intellij.formatting.FormattingProgressTask; +import com.intellij.notification.Notification; +import com.intellij.notification.NotificationType; +import com.intellij.openapi.application.ApplicationBundle; +import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.module.Module; import com.intellij.openapi.project.Project; @@ -27,6 +31,7 @@ import com.intellij.psi.PsiFile; import com.intellij.psi.codeStyle.CodeStyleManager; import com.intellij.util.IncorrectOperationException; import com.intellij.util.containers.ContainerUtil; +import com.intellij.util.diff.FilesTooBigForDiffException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -90,6 +95,17 @@ public class ReformatCodeProcessor extends AbstractLayoutCodeProcessor { CodeStyleManager.getInstance(myProject).reformatText(file, ranges); return !FormattingProgressTask.FORMATTING_CANCELLED_FLAG.get(); } + catch (FilesTooBigForDiffException e) { + LOG.info("Error while calculating changed ranges for: " + file.getVirtualFile(), e); + if (!ApplicationManager.getApplication().isUnitTestMode()) { + Notification notification = new Notification(ApplicationBundle.message("reformat.changed.text.file.too.big.notification.groupId"), + ApplicationBundle.message("reformat.changed.text.file.too.big.notification.title"), + ApplicationBundle.message("reformat.changed.text.file.too.big.notification.text", file.getName()), + NotificationType.INFORMATION); + notification.notify(file.getProject()); + } + return false; + } catch (IncorrectOperationException e) { LOG.error(e); return false; @@ -102,7 +118,7 @@ public class ReformatCodeProcessor extends AbstractLayoutCodeProcessor { } @NotNull - private Collection getRangesToFormat(boolean processChangedTextOnly, PsiFile file) { + private Collection getRangesToFormat(boolean processChangedTextOnly, PsiFile file) throws FilesTooBigForDiffException { if (processChangedTextOnly) { return FormatChangedTextUtil.getChangedTextRanges(myProject, file); } diff --git a/platform/platform-resources-en/src/messages/ApplicationBundle.properties b/platform/platform-resources-en/src/messages/ApplicationBundle.properties index 75d0f242e38c..053657bf7738 100644 --- a/platform/platform-resources-en/src/messages/ApplicationBundle.properties +++ b/platform/platform-resources-en/src/messages/ApplicationBundle.properties @@ -644,4 +644,8 @@ group.richcopy=Rich-text copy combobox.richcopy.color.scheme=Color scheme combobox.richcopy.color.scheme.active=Active scheme -settings.code.style.default.general=Default (General) \ No newline at end of file +settings.code.style.default.general=Default (General) + +reformat.changed.text.file.too.big.notification.groupId=Reformat changed text +reformat.changed.text.file.too.big.notification.title=Couldn't calculate changed ranges +reformat.changed.text.file.too.big.notification.text=file {0} is too big or there are too many changes