private void prepareUserNotificationMessage( @NotNull Document document, @NotNull CharSequence before) { LOG.assertTrue(getInfoCollector() != null); int number = FormatChangedTextUtil.getInstance().calculateChangedLinesNumber(document, before); if (number > 0) { String message = "formatted " + number + " line" + (number > 1 ? "s" : ""); getInfoCollector().setReformatCodeNotification(message); } }
@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(); } }); }