Merge branch 'master' into no-reformat-dialog-on-reformat-code-action
[idea/community.git] / platform / lang-impl / src / com / intellij / codeInsight / actions / RearrangeCodeProcessor.java
index 4867d85abbd7e94f5afa105e24a5446890e9277a..f8ae0ac7869eced95d406cdda8f5dce9a97be29f 100644 (file)
@@ -17,6 +17,7 @@ package com.intellij.codeInsight.actions;
 
 import com.intellij.openapi.command.CommandProcessor;
 import com.intellij.openapi.components.ServiceManager;
+import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.editor.Document;
 import com.intellij.openapi.editor.SelectionModel;
 import com.intellij.openapi.project.Project;
@@ -25,12 +26,12 @@ import com.intellij.psi.PsiDocumentManager;
 import com.intellij.psi.PsiFile;
 import com.intellij.psi.codeStyle.arrangement.Rearranger;
 import com.intellij.psi.codeStyle.arrangement.engine.ArrangementEngine;
-import com.intellij.util.SmartList;
+import com.intellij.util.containers.ContainerUtil;
+import com.intellij.util.diff.FilesTooBigForDiffException;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
 import java.util.Collection;
-import java.util.List;
 import java.util.concurrent.Callable;
 import java.util.concurrent.FutureTask;
 
@@ -39,12 +40,23 @@ public class RearrangeCodeProcessor extends AbstractLayoutCodeProcessor {
   public static final String COMMAND_NAME = "Rearrange code";
   public static final String PROGRESS_TEXT = "Rearranging code...";
 
-  @Nullable private SelectionModel mySelectionModel;
+  private static final Logger LOG = Logger.getInstance(RearrangeCodeProcessor.class);
+  private SelectionModel mySelectionModel;
 
   public RearrangeCodeProcessor(@NotNull AbstractLayoutCodeProcessor previousProcessor) {
     super(previousProcessor, COMMAND_NAME, PROGRESS_TEXT);
   }
 
+  public RearrangeCodeProcessor(@NotNull AbstractLayoutCodeProcessor previousProcessor, @NotNull SelectionModel model) {
+    super(previousProcessor, COMMAND_NAME, PROGRESS_TEXT);
+    mySelectionModel = model;
+  }
+
+  public RearrangeCodeProcessor(@NotNull PsiFile file,
+                                @NotNull SelectionModel selectionModel)
+  {
+    super(file.getProject(), file, PROGRESS_TEXT, COMMAND_NAME, false);
+
   public RearrangeCodeProcessor(@NotNull AbstractLayoutCodeProcessor previousProcessor, @NotNull SelectionModel selectionModel) {
     super(previousProcessor, COMMAND_NAME, PROGRESS_TEXT);
     mySelectionModel = selectionModel;
@@ -57,6 +69,10 @@ public class RearrangeCodeProcessor extends AbstractLayoutCodeProcessor {
     mySelectionModel = selectionModel;
   }
 
+  public RearrangeCodeProcessor(@NotNull PsiFile file) {
+    super(file.getProject(), file, PROGRESS_TEXT, COMMAND_NAME, false);
+  }
+
   public RearrangeCodeProcessor(@NotNull Project project,
                                 @NotNull PsiFile[] files,
                                 @NotNull String commandName,
@@ -70,27 +86,32 @@ public class RearrangeCodeProcessor extends AbstractLayoutCodeProcessor {
     return new FutureTask<Boolean>(new Callable<Boolean>() {
       @Override
       public Boolean call() throws Exception {
-        Collection<TextRange> ranges = processChangedTextOnly ? FormatChangedTextUtil.getChangedTextRanges(myProject, file)
-                                                              : getRangesToFormat(file);
-
-        RearrangeCommand rearranger = new RearrangeCommand(myProject, file, COMMAND_NAME, ranges);
-        if (rearranger.couldRearrange()) {
-          rearranger.run();
+        try {
+          Collection<TextRange> ranges = getRangesToFormat(file, processChangedTextOnly);
+          RearrangeCommand rearranger = new RearrangeCommand(myProject, file, COMMAND_NAME, ranges);
+          if (rearranger.couldRearrange()) {
+            rearranger.run();
+          }
+          return true;
+        }
+        catch (FilesTooBigForDiffException e) {
+          handleFileTooBigException(LOG, e, file);
+          return false;
         }
-        return true;
       }
     });
   }
 
-  public Collection<TextRange> getRangesToFormat(@NotNull PsiFile file) {
-    final List<TextRange> ranges = new SmartList<TextRange>();
-    if (mySelectionModel != null && mySelectionModel.hasSelection()) {
-      ranges.add(TextRange.create(mySelectionModel.getSelectionStart(), mySelectionModel.getSelectionEnd()));
+  public Collection<TextRange> getRangesToFormat(@NotNull PsiFile file, boolean processChangedTextOnly) throws FilesTooBigForDiffException {
+    if (mySelectionModel != null) {
+      return getSelectedRanges(mySelectionModel);
     }
-    else {
-      ranges.add(TextRange.create(0, file.getTextLength()));
+
+    if (processChangedTextOnly) {
+      return FormatChangedTextUtil.getChangedTextRanges(myProject, file);
     }
-    return ranges;
+
+    return ContainerUtil.newSmartList(file.getTextRange());
   }
 }