use of com.intellij.psi.codeStyle.ChangedRangesInfo in project intellij-community by JetBrains.
the class VcsAwareFormatChangedTextUtil method getChangedRangesInfo.
@Override
@Nullable
public ChangedRangesInfo getChangedRangesInfo(@NotNull PsiFile file) throws FilesTooBigForDiffException {
Project project = file.getProject();
Document document = PsiDocumentManager.getInstance(project).getDocument(file);
if (document == null)
return null;
ChangedRangesInfo cachedChangedTextHelper = getCachedChangedLines(project, document);
if (cachedChangedTextHelper != null) {
return cachedChangedTextHelper;
}
if (ApplicationManager.getApplication().isUnitTestMode()) {
CharSequence testContent = file.getUserData(TEST_REVISION_CONTENT);
if (testContent != null) {
return calculateContextReformatHelper(document, testContent);
}
}
Change change = ChangeListManager.getInstance(project).getChange(file.getVirtualFile());
if (change == null) {
return null;
}
if (change.getType() == Change.Type.NEW) {
TextRange fileRange = file.getTextRange();
return new ChangedRangesInfo(ContainerUtil.newArrayList(fileRange), null);
}
String contentFromVcs = getRevisionedContentFrom(change);
return contentFromVcs != null ? calculateContextReformatHelper(document, contentFromVcs) : null;
}
use of com.intellij.psi.codeStyle.ChangedRangesInfo in project intellij-community by JetBrains.
the class VcsAwareFormatChangedTextUtil method getChangedTextRanges.
@NotNull
private static ChangedRangesInfo getChangedTextRanges(@NotNull Document document, @NotNull List<Range> changedRanges) {
final List<TextRange> ranges = ContainerUtil.newArrayList();
final List<TextRange> insertedRanges = ContainerUtil.newArrayList();
for (Range range : changedRanges) {
if (range.getType() != Range.DELETED) {
int changeStartLine = range.getLine1();
int changeEndLine = range.getLine2();
int lineStartOffset = document.getLineStartOffset(changeStartLine);
int lineEndOffset = document.getLineEndOffset(changeEndLine - 1);
TextRange changedTextRange = new TextRange(lineStartOffset, lineEndOffset);
ranges.add(changedTextRange);
if (range.getType() == Range.INSERTED) {
insertedRanges.add(changedTextRange);
}
}
}
return new ChangedRangesInfo(ranges, insertedRanges);
}
use of com.intellij.psi.codeStyle.ChangedRangesInfo 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