Search in sources :

Example 1 with CaretVisualPositionKeeper

use of com.intellij.openapi.editor.ex.util.CaretVisualPositionKeeper in project intellij-community by JetBrains.

the class ReformatCodeProcessor method prepareTask.

@Override
@NotNull
protected FutureTask<Boolean> prepareTask(@NotNull final PsiFile file, final boolean processChangedTextOnly) throws IncorrectOperationException {
    return new FutureTask<>(() -> {
        FormattingProgressTask.FORMATTING_CANCELLED_FLAG.set(false);
        try {
            CharSequence before = null;
            Document document = PsiDocumentManager.getInstance(myProject).getDocument(file);
            if (getInfoCollector() != null) {
                LOG.assertTrue(document != null);
                before = document.getImmutableCharSequence();
            }
            CaretVisualPositionKeeper caretPositionKeeper = new CaretVisualPositionKeeper(document);
            if (processChangedTextOnly) {
                ChangedRangesInfo info = FormatChangedTextUtil.getInstance().getChangedRangesInfo(file);
                if (info != null) {
                    CodeStyleManager.getInstance(myProject).reformatTextWithContext(file, info);
                }
            } else {
                Collection<TextRange> ranges = getRangesToFormat(file);
                CodeStyleManager.getInstance(myProject).reformatText(file, ranges);
            }
            caretPositionKeeper.restoreOriginalLocation();
            if (before != null) {
                prepareUserNotificationMessage(document, before);
            }
            return !FormattingProgressTask.FORMATTING_CANCELLED_FLAG.get();
        } catch (FilesTooBigForDiffException e) {
            handleFileTooBigException(LOG, e, file);
            return false;
        } catch (IncorrectOperationException e) {
            LOG.error(e);
            return false;
        } finally {
            myRanges.clear();
        }
    });
}
Also used : FilesTooBigForDiffException(com.intellij.util.diff.FilesTooBigForDiffException) FutureTask(java.util.concurrent.FutureTask) TextRange(com.intellij.openapi.util.TextRange) IncorrectOperationException(com.intellij.util.IncorrectOperationException) ChangedRangesInfo(com.intellij.psi.codeStyle.ChangedRangesInfo) Document(com.intellij.openapi.editor.Document) CaretVisualPositionKeeper(com.intellij.openapi.editor.ex.util.CaretVisualPositionKeeper) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Document (com.intellij.openapi.editor.Document)1 CaretVisualPositionKeeper (com.intellij.openapi.editor.ex.util.CaretVisualPositionKeeper)1 TextRange (com.intellij.openapi.util.TextRange)1 ChangedRangesInfo (com.intellij.psi.codeStyle.ChangedRangesInfo)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 FilesTooBigForDiffException (com.intellij.util.diff.FilesTooBigForDiffException)1 FutureTask (java.util.concurrent.FutureTask)1 NotNull (org.jetbrains.annotations.NotNull)1