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;
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;
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;
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,
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());
}
}