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