Search in sources :

Example 1 with ChangedRangesInfo

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;
}
Also used : Project(com.intellij.openapi.project.Project) TextRange(com.intellij.openapi.util.TextRange) ChangedRangesInfo(com.intellij.psi.codeStyle.ChangedRangesInfo) Change(com.intellij.openapi.vcs.changes.Change) Document(com.intellij.openapi.editor.Document) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with ChangedRangesInfo

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);
}
Also used : TextRange(com.intellij.openapi.util.TextRange) ChangedRangesInfo(com.intellij.psi.codeStyle.ChangedRangesInfo) Range(com.intellij.openapi.vcs.ex.Range) TextRange(com.intellij.openapi.util.TextRange) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with ChangedRangesInfo

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

TextRange (com.intellij.openapi.util.TextRange)3 ChangedRangesInfo (com.intellij.psi.codeStyle.ChangedRangesInfo)3 Document (com.intellij.openapi.editor.Document)2 NotNull (org.jetbrains.annotations.NotNull)2 CaretVisualPositionKeeper (com.intellij.openapi.editor.ex.util.CaretVisualPositionKeeper)1 Project (com.intellij.openapi.project.Project)1 Change (com.intellij.openapi.vcs.changes.Change)1 Range (com.intellij.openapi.vcs.ex.Range)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 FilesTooBigForDiffException (com.intellij.util.diff.FilesTooBigForDiffException)1 FutureTask (java.util.concurrent.FutureTask)1 Nullable (org.jetbrains.annotations.Nullable)1