Search in sources :

Example 56 with DocumentEx

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

the class ProblemPreviewEditorPresentation method appendFoldings.

private void appendFoldings(CommonProblemDescriptor[] descriptors) {
    List<UsageInfo> usages = Arrays.stream(descriptors).filter(ProblemDescriptorBase.class::isInstance).map(ProblemDescriptorBase.class::cast).map(d -> {
        final PsiElement psi = d.getPsiElement();
        if (psi == null) {
            return null;
        }
        final TextRange range = d.getTextRangeInElement();
        return range == null ? new UsageInfo(psi) : new UsageInfo(psi, range.getStartOffset(), range.getEndOffset());
    }).collect(Collectors.toList());
    boolean isUpdated = false;
    for (UsageInfo usage : usages) {
        if (usage == null) {
            return;
        }
        isUpdated |= appendFoldings(usage.getSegment());
    }
    if (isUpdated) {
        updateFoldings();
    }
    List<UsageInfo> validUsages = usages.stream().filter(Objects::nonNull).collect(Collectors.toList());
    PsiDocumentManager.getInstance(myView.getProject()).performLaterWhenAllCommitted(() -> {
        if (!myEditor.isDisposed()) {
            myView.invalidate();
            myView.validate();
            UsagePreviewPanel.highlight(validUsages, myEditor, myView.getProject(), false, HighlighterLayer.SELECTION);
            if (validUsages.size() == 1) {
                final PsiElement element = validUsages.get(0).getElement();
                if (element != null) {
                    final DocumentEx document = myEditor.getDocument();
                    final int offset = Math.min(element.getTextRange().getEndOffset() + VIEW_ADDITIONAL_OFFSET, document.getLineEndOffset(document.getLineNumber(element.getTextRange().getEndOffset())));
                    myEditor.getScrollingModel().scrollTo(myEditor.offsetToLogicalPosition(offset), ScrollType.CENTER);
                    return;
                }
            }
            myEditor.getScrollingModel().scrollTo(myEditor.offsetToLogicalPosition(0), ScrollType.CENTER_UP);
        }
    });
}
Also used : ProblemDescriptorBase(com.intellij.codeInspection.ProblemDescriptorBase) java.util(java.util) ProblemDescriptorBase(com.intellij.codeInspection.ProblemDescriptorBase) FoldRegion(com.intellij.openapi.editor.FoldRegion) DiffDrawUtil(com.intellij.diff.util.DiffDrawUtil) UsageInfo(com.intellij.usageView.UsageInfo) TextRange(com.intellij.openapi.util.TextRange) CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) Collectors(java.util.stream.Collectors) ScrollType(com.intellij.openapi.editor.ScrollType) HighlighterLayer(com.intellij.openapi.editor.markup.HighlighterLayer) FoldingModelSupport(com.intellij.diff.tools.util.FoldingModelSupport) PsiElement(com.intellij.psi.PsiElement) UsagePreviewPanel(com.intellij.usages.impl.UsagePreviewPanel) DocumentEx(com.intellij.openapi.editor.ex.DocumentEx) EditorEx(com.intellij.openapi.editor.ex.EditorEx) Segment(com.intellij.openapi.util.Segment) Logger(com.intellij.openapi.diagnostic.Logger) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) DocumentEx(com.intellij.openapi.editor.ex.DocumentEx) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) UsageInfo(com.intellij.usageView.UsageInfo)

Example 57 with DocumentEx

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

the class DocumentUtil method executeInBulk.

/**
   * Ensures that given task is executed when given document is at the given 'in bulk' mode.
   * 
   * @param document       target document
   * @param executeInBulk  {@code true} to force given document to be in bulk mode when given task is executed;
   *                       {@code false} to force given document to be <b>not</b> in bulk mode when given task is executed
   * @param task           task to execute
   */
public static void executeInBulk(@NotNull Document document, final boolean executeInBulk, @NotNull Runnable task) {
    if (!(document instanceof DocumentEx)) {
        task.run();
        return;
    }
    DocumentEx documentEx = (DocumentEx) document;
    if (executeInBulk == documentEx.isInBulkUpdate()) {
        task.run();
        return;
    }
    documentEx.setInBulkUpdate(executeInBulk);
    try {
        task.run();
    } finally {
        documentEx.setInBulkUpdate(!executeInBulk);
    }
}
Also used : DocumentEx(com.intellij.openapi.editor.ex.DocumentEx)

Example 58 with DocumentEx

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

the class MultiHostRegistrarImpl method getModificationCount.

// for CachedValue
@Override
public long getModificationCount() {
    List<PsiLanguageInjectionHost.Shred> shredList = shreds;
    if (shredList != null) {
        for (PsiLanguageInjectionHost.Shred shred : shredList) {
            if (!shred.isValid())
                return -1;
        }
    }
    DocumentEx hostDocument = myHostDocument;
    return hostDocument == null ? -1 : hostDocument.getModificationStamp();
}
Also used : DocumentEx(com.intellij.openapi.editor.ex.DocumentEx)

Example 59 with DocumentEx

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

the class UndoableGroup method doUndoOrRedo.

private void doUndoOrRedo(final boolean isUndo) {
    final boolean wrapInBulkUpdate = myActions.size() > 50;
    // perform undo action by action, setting bulk update flag if possible
    // if multiple consecutive actions share a document, then set the bulk flag only once
    final UnexpectedUndoException[] exception = { null };
    ApplicationManager.getApplication().runWriteAction(() -> {
        final Set<DocumentEx> bulkDocuments = new THashSet<>();
        try {
            for (final UndoableAction action : isUndo ? ContainerUtil.iterateBackward(myActions) : myActions) {
                if (wrapInBulkUpdate) {
                    DocumentEx newDocument = getDocumentToSetBulkMode(action);
                    if (newDocument == null) {
                        for (DocumentEx document : bulkDocuments) {
                            document.setInBulkUpdate(false);
                        }
                        bulkDocuments.clear();
                    } else if (bulkDocuments.add(newDocument)) {
                        newDocument.setInBulkUpdate(true);
                    }
                }
                if (isUndo) {
                    action.undo();
                } else {
                    action.redo();
                }
            }
        } catch (UnexpectedUndoException e) {
            exception[0] = e;
        } finally {
            for (DocumentEx bulkDocument : bulkDocuments) {
                bulkDocument.setInBulkUpdate(false);
            }
        }
    });
    if (exception[0] != null)
        reportUndoProblem(exception[0], isUndo);
    commitAllDocuments();
}
Also used : DocumentEx(com.intellij.openapi.editor.ex.DocumentEx) UnexpectedUndoException(com.intellij.openapi.command.undo.UnexpectedUndoException) UndoableAction(com.intellij.openapi.command.undo.UndoableAction) THashSet(gnu.trove.THashSet)

Example 60 with DocumentEx

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

the class MergeList method finishBulkUpdate.

public void finishBulkUpdate() {
    Document document1 = myBaseToLeftChangeList.getDocument(BRANCH_SIDE);
    Document document2 = myBaseToRightChangeList.getDocument(BRANCH_SIDE);
    Document document3 = myBaseToLeftChangeList.getDocument(BASE_SIDE);
    assert document3 == myBaseToRightChangeList.getDocument(BASE_SIDE);
    ((DocumentEx) document1).setInBulkUpdate(false);
    ((DocumentEx) document2).setInBulkUpdate(false);
    ((DocumentEx) document3).setInBulkUpdate(false);
}
Also used : DocumentEx(com.intellij.openapi.editor.ex.DocumentEx) Document(com.intellij.openapi.editor.Document)

Aggregations

DocumentEx (com.intellij.openapi.editor.ex.DocumentEx)87 Document (com.intellij.openapi.editor.Document)12 NotNull (org.jetbrains.annotations.NotNull)8 RangeMarkerEx (com.intellij.openapi.editor.ex.RangeMarkerEx)7 Project (com.intellij.openapi.project.Project)7 TextRange (com.intellij.openapi.util.TextRange)7 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)7 PsiFile (com.intellij.psi.PsiFile)6 Nullable (org.jetbrains.annotations.Nullable)5 EditorEx (com.intellij.openapi.editor.ex.EditorEx)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 MockVirtualFile (com.intellij.mock.MockVirtualFile)3 FoldRegion (com.intellij.openapi.editor.FoldRegion)3 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)3 DocumentWindow (com.intellij.injected.editor.DocumentWindow)2 DocumentBulkUpdateListener (com.intellij.openapi.editor.ex.DocumentBulkUpdateListener)2 DocumentImpl (com.intellij.openapi.editor.impl.DocumentImpl)2 DocumentMarkupModel (com.intellij.openapi.editor.impl.DocumentMarkupModel)2 RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)2 FileDocumentManagerAdapter (com.intellij.openapi.fileEditor.FileDocumentManagerAdapter)2