Search in sources :

Example 11 with FoldingModelEx

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

the class DocumentFragmentTooltipRenderer method show.

@Override
public LightweightHint show(@NotNull final Editor editor, @NotNull Point p, boolean alignToRight, @NotNull TooltipGroup group, @NotNull HintHint intInfo) {
    LightweightHint hint;
    final JComponent editorComponent = editor.getComponent();
    TextRange range = myDocumentFragment.getTextRange();
    int startOffset = range.getStartOffset();
    int endOffset = range.getEndOffset();
    Document doc = myDocumentFragment.getDocument();
    int endLine = doc.getLineNumber(endOffset);
    int startLine = doc.getLineNumber(startOffset);
    JLayeredPane layeredPane = editorComponent.getRootPane().getLayeredPane();
    // There is a possible case that collapsed folding region is soft wrapped, hence, we need to anchor
    // not logical but visual line start.
    VisualPosition visual = editor.offsetToVisualPosition(startOffset);
    p = editor.visualPositionToXY(visual);
    p = SwingUtilities.convertPoint(((EditorEx) editor).getGutterComponentEx(), p, layeredPane);
    p.x -= 3;
    p.y += editor.getLineHeight();
    Point screenPoint = new Point(p);
    SwingUtilities.convertPointToScreen(screenPoint, layeredPane);
    int maxLineCount = (ScreenUtil.getScreenRectangle(screenPoint).height - screenPoint.y) / editor.getLineHeight();
    if (endLine - startLine > maxLineCount) {
        endOffset = doc.getLineEndOffset(Math.max(0, Math.min(startLine + maxLineCount, doc.getLineCount() - 1)));
    }
    if (endOffset < startOffset)
        return null;
    FoldingModelEx foldingModel = (FoldingModelEx) editor.getFoldingModel();
    foldingModel.setFoldingEnabled(false);
    TextRange textRange = new TextRange(startOffset, endOffset);
    hint = EditorFragmentComponent.showEditorFragmentHintAt(editor, textRange, p.y, false, false, true, true, true);
    foldingModel.setFoldingEnabled(true);
    return hint;
}
Also used : EditorEx(com.intellij.openapi.editor.ex.EditorEx) LightweightHint(com.intellij.ui.LightweightHint) TextRange(com.intellij.openapi.util.TextRange) FoldingModelEx(com.intellij.openapi.editor.ex.FoldingModelEx) HintHint(com.intellij.ui.HintHint) LightweightHint(com.intellij.ui.LightweightHint)

Example 12 with FoldingModelEx

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

the class CaretImpl method doSetSelection.

private void doSetSelection(@NotNull final VisualPosition startPosition, final int _startOffset, @NotNull final VisualPosition endPosition, final int _endOffset, final boolean visualPositionAware, final boolean updateSystemSelection) {
    myEditor.getCaretModel().doWithCaretMerging(() -> {
        int startOffset = DocumentUtil.alignToCodePointBoundary(myEditor.getDocument(), _startOffset);
        int endOffset = DocumentUtil.alignToCodePointBoundary(myEditor.getDocument(), _endOffset);
        myUnknownDirection = false;
        final Document doc = myEditor.getDocument();
        validateContext(true);
        int textLength = doc.getTextLength();
        if (startOffset < 0 || startOffset > textLength) {
            LOG.error("Wrong startOffset: " + startOffset + ", textLength=" + textLength);
        }
        if (endOffset < 0 || endOffset > textLength) {
            LOG.error("Wrong endOffset: " + endOffset + ", textLength=" + textLength);
        }
        if (!visualPositionAware && startOffset == endOffset) {
            removeSelection();
            return;
        }
        /* Normalize selection */
        boolean switchedOffsets = false;
        if (startOffset > endOffset) {
            int tmp = startOffset;
            startOffset = endOffset;
            endOffset = tmp;
            switchedOffsets = true;
        }
        FoldingModelEx foldingModel = myEditor.getFoldingModel();
        FoldRegion startFold = foldingModel.getCollapsedRegionAtOffset(startOffset);
        if (startFold != null && startFold.getStartOffset() < startOffset) {
            startOffset = startFold.getStartOffset();
        }
        FoldRegion endFold = foldingModel.getCollapsedRegionAtOffset(endOffset);
        if (endFold != null && endFold.getStartOffset() < endOffset) {
            // All visual positions that lay at collapsed fold region placeholder are mapped to the same offset. Hence, there are
            // at least two distinct situations - selection end is located inside collapsed fold region placeholder and just before it.
            // We want to expand selection to the fold region end at the former case and keep selection as-is at the latest one.
            endOffset = endFold.getEndOffset();
        }
        int oldSelectionStart;
        int oldSelectionEnd;
        if (hasSelection()) {
            oldSelectionStart = getSelectionStart();
            oldSelectionEnd = getSelectionEnd();
            if (oldSelectionStart == startOffset && oldSelectionEnd == endOffset && !visualPositionAware)
                return;
        } else {
            oldSelectionStart = oldSelectionEnd = getOffset();
        }
        SelectionMarker marker = new SelectionMarker(startOffset, endOffset);
        if (visualPositionAware) {
            if (endPosition.after(startPosition)) {
                setRangeMarkerStartPosition(startPosition);
                setRangeMarkerEndPosition(endPosition);
                setRangeMarkerEndPositionIsLead(false);
            } else {
                setRangeMarkerStartPosition(endPosition);
                setRangeMarkerEndPosition(startPosition);
                setRangeMarkerEndPositionIsLead(true);
            }
            if (isVirtualSelectionEnabled() && myEditor.getDocument().getLineNumber(startOffset) == myEditor.getDocument().getLineNumber(endOffset)) {
                int endLineColumn = myEditor.offsetToVisualPosition(endOffset).column;
                int startDiff = EditorUtil.isAtLineEnd(myEditor, switchedOffsets ? endOffset : startOffset) ? startPosition.column - endLineColumn : 0;
                int endDiff = EditorUtil.isAtLineEnd(myEditor, switchedOffsets ? startOffset : endOffset) ? endPosition.column - endLineColumn : 0;
                marker.startVirtualOffset = Math.max(0, Math.min(startDiff, endDiff));
                marker.endVirtualOffset = Math.max(0, Math.max(startDiff, endDiff));
            }
        }
        mySelectionMarker = marker;
        myEditor.getSelectionModel().fireSelectionChanged(oldSelectionStart, oldSelectionEnd, startOffset, endOffset);
        if (updateSystemSelection) {
            updateSystemSelection();
        }
    });
}
Also used : FoldingModelEx(com.intellij.openapi.editor.ex.FoldingModelEx)

Example 13 with FoldingModelEx

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

the class VisualLineFragmentsIterator method init.

private void init(EditorView view, int startOffset, int startLogicalLine, int currentOrPrevWrapIndex, int nextFoldingIndex, @Nullable Runnable quickEvaluationListener) {
    myQuickEvaluationListener = quickEvaluationListener;
    myView = view;
    EditorImpl editor = view.getEditor();
    myDocument = editor.getDocument();
    FoldingModelEx foldingModel = editor.getFoldingModel();
    FoldRegion[] regions = foldingModel.fetchTopLevel();
    myRegions = regions == null ? FoldRegion.EMPTY_ARRAY : regions;
    SoftWrapModelImpl softWrapModel = editor.getSoftWrapModel();
    List<? extends SoftWrap> softWraps = softWrapModel.getRegisteredSoftWraps();
    SoftWrap currentOrPrevWrap = currentOrPrevWrapIndex < 0 || currentOrPrevWrapIndex >= softWraps.size() ? null : softWraps.get(currentOrPrevWrapIndex);
    SoftWrap followingWrap = (currentOrPrevWrapIndex + 1) < 0 || (currentOrPrevWrapIndex + 1) >= softWraps.size() ? null : softWraps.get(currentOrPrevWrapIndex + 1);
    myVisualLineStartOffset = mySegmentStartOffset = startOffset;
    myCurrentFoldRegionIndex = nextFoldingIndex;
    myCurrentEndLogicalLine = startLogicalLine;
    myCurrentX = myView.getInsets().left;
    if (mySegmentStartOffset == 0) {
        myCurrentX += myView.getPrefixTextWidthInPixels();
    } else if (currentOrPrevWrap != null && mySegmentStartOffset == currentOrPrevWrap.getStart()) {
        myCurrentX += currentOrPrevWrap.getIndentInPixels();
        myCurrentVisualColumn = currentOrPrevWrap.getIndentInColumns();
    }
    myNextWrapOffset = followingWrap == null ? Integer.MAX_VALUE : followingWrap.getStart();
    setInlaysAndFragmentIterator();
}
Also used : EditorImpl(com.intellij.openapi.editor.impl.EditorImpl) FoldRegion(com.intellij.openapi.editor.FoldRegion) SoftWrapModelImpl(com.intellij.openapi.editor.impl.SoftWrapModelImpl) SoftWrap(com.intellij.openapi.editor.SoftWrap) FoldingModelEx(com.intellij.openapi.editor.ex.FoldingModelEx)

Aggregations

FoldingModelEx (com.intellij.openapi.editor.ex.FoldingModelEx)13 FoldRegion (com.intellij.openapi.editor.FoldRegion)5 LightweightHint (com.intellij.ui.LightweightHint)4 Document (com.intellij.openapi.editor.Document)2 EditorEx (com.intellij.openapi.editor.ex.EditorEx)2 EditorImpl (com.intellij.openapi.editor.impl.EditorImpl)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiElement (com.intellij.psi.PsiElement)2 PsiFile (com.intellij.psi.PsiFile)2 HintHint (com.intellij.ui.HintHint)2 CodeFoldingManager (com.intellij.codeInsight.folding.CodeFoldingManager)1 EditorFragmentComponent (com.intellij.codeInsight.hint.EditorFragmentComponent)1 HintManager (com.intellij.codeInsight.hint.HintManager)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 ApplicationManagerEx (com.intellij.openapi.application.ex.ApplicationManagerEx)1 ProjectComponent (com.intellij.openapi.components.ProjectComponent)1 com.intellij.openapi.editor (com.intellij.openapi.editor)1 Editor (com.intellij.openapi.editor.Editor)1 FoldingGroup (com.intellij.openapi.editor.FoldingGroup)1 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)1