Search in sources :

Example 31 with FoldRegion

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

the class CollapseExpandDocCommentsHandler method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull final Editor editor, @NotNull PsiFile file) {
    CodeFoldingManager foldingManager = CodeFoldingManager.getInstance(project);
    foldingManager.updateFoldRegions(editor);
    final FoldRegion[] allFoldRegions = editor.getFoldingModel().getAllFoldRegions();
    Runnable processor = () -> {
        for (FoldRegion region : allFoldRegions) {
            PsiElement element = EditorFoldingInfo.get(editor).getPsiElement(region);
            if (element instanceof PsiDocCommentBase) {
                region.setExpanded(myExpand);
            }
        }
    };
    editor.getFoldingModel().runBatchFoldingOperation(processor);
}
Also used : CodeFoldingManager(com.intellij.codeInsight.folding.CodeFoldingManager) PsiDocCommentBase(com.intellij.psi.PsiDocCommentBase) FoldRegion(com.intellij.openapi.editor.FoldRegion) PsiElement(com.intellij.psi.PsiElement)

Example 32 with FoldRegion

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

the class CollapseSelectionHandler method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull final Editor editor, @NotNull PsiFile file) {
    editor.getFoldingModel().runBatchFoldingOperation(() -> {
        final EditorFoldingInfo info = EditorFoldingInfo.get(editor);
        FoldingModelEx foldingModel = (FoldingModelEx) editor.getFoldingModel();
        if (editor.getSelectionModel().hasSelection()) {
            int start = editor.getSelectionModel().getSelectionStart();
            int end = editor.getSelectionModel().getSelectionEnd();
            if (start + 1 >= end) {
                return;
            }
            Document doc = editor.getDocument();
            if (start < end && doc.getCharsSequence().charAt(end - 1) == '\n')
                end--;
            FoldRegion region;
            if ((region = FoldingUtil.findFoldRegion(editor, start, end)) != null) {
                if (info.getPsiElement(region) == null) {
                    editor.getFoldingModel().removeFoldRegion(region);
                    info.removeRegion(region);
                }
            } else if (!foldingModel.intersectsRegion(start, end)) {
                region = foldingModel.addFoldRegion(start, end, ourPlaceHolderText);
                LOG.assertTrue(region != null, "Fold region is not created. Folding model: " + foldingModel);
                region.setExpanded(false);
                int offset = Math.min(start + ourPlaceHolderText.length(), doc.getTextLength());
                editor.getCaretModel().moveToOffset(offset);
            }
        } else {
            FoldRegion[] regions = FoldingUtil.getFoldRegionsAtOffset(editor, editor.getCaretModel().getOffset());
            if (regions.length > 0) {
                FoldRegion region = regions[0];
                if (info.getPsiElement(region) == null) {
                    editor.getFoldingModel().removeFoldRegion(region);
                    info.removeRegion(region);
                } else {
                    region.setExpanded(!region.isExpanded());
                }
            }
        }
    });
}
Also used : FoldRegion(com.intellij.openapi.editor.FoldRegion) FoldingModelEx(com.intellij.openapi.editor.ex.FoldingModelEx) Document(com.intellij.openapi.editor.Document)

Example 33 with FoldRegion

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

the class DocumentFoldingInfo method loadFromEditor.

void loadFromEditor(@NotNull Editor editor) {
    assertDispatchThread();
    LOG.assertTrue(!editor.isDisposed());
    clear();
    PsiFile file = PsiDocumentManager.getInstance(myProject).getPsiFile(editor.getDocument());
    SmartPointerManager smartPointerManager = SmartPointerManager.getInstance(myProject);
    EditorFoldingInfo info = EditorFoldingInfo.get(editor);
    FoldRegion[] foldRegions = editor.getFoldingModel().getAllFoldRegions();
    for (FoldRegion region : foldRegions) {
        if (!region.isValid())
            continue;
        PsiElement element = info.getPsiElement(region);
        boolean expanded = region.isExpanded();
        boolean collapseByDefault = element != null && FoldingPolicy.isCollapseByDefault(element) && !FoldingUtil.caretInsideRange(editor, TextRange.create(region));
        if (collapseByDefault == expanded || element == null) {
            FoldingInfo fi = new FoldingInfo(region.getPlaceholderText(), expanded);
            if (element != null) {
                myPsiElements.add(smartPointerManager.createSmartPsiElementPointer(element, file));
                element.putUserData(FOLDING_INFO_KEY, fi);
            } else {
                RangeMarker marker = editor.getDocument().createRangeMarker(region.getStartOffset(), region.getEndOffset());
                myRangeMarkers.add(marker);
                marker.putUserData(FOLDING_INFO_KEY, fi);
            }
        }
    }
}
Also used : FoldRegion(com.intellij.openapi.editor.FoldRegion) RangeMarker(com.intellij.openapi.editor.RangeMarker)

Example 34 with FoldRegion

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

the class CopyPasteFoldingProcessor method collectTransferableData.

@NotNull
@Override
public List<FoldingTransferableData> collectTransferableData(final PsiFile file, final Editor editor, final int[] startOffsets, final int[] endOffsets) {
    // might be slow
    //CodeFoldingManager.getInstance(file.getManager().getProject()).updateFoldRegions(editor);
    final ArrayList<FoldingData> list = new ArrayList<>();
    final FoldRegion[] regions = editor.getFoldingModel().getAllFoldRegions();
    for (final FoldRegion region : regions) {
        if (!region.isValid())
            continue;
        int refOffset = 0;
        for (int j = 0; j < startOffsets.length; j++) {
            refOffset += startOffsets[j];
            if (startOffsets[j] <= region.getStartOffset() && region.getEndOffset() <= endOffsets[j]) {
                list.add(new FoldingData(// offsets should be relative to clipboard contents start
                region.getStartOffset() - refOffset, region.getEndOffset() - refOffset, region.isExpanded()));
                break;
            }
            // 1 accounts for line break inserted between contents corresponding to different carets
            refOffset -= endOffsets[j] + 1;
        }
    }
    return Collections.singletonList(new FoldingTransferableData(list.toArray(new FoldingData[list.size()])));
}
Also used : ArrayList(java.util.ArrayList) FoldRegion(com.intellij.openapi.editor.FoldRegion) NotNull(org.jetbrains.annotations.NotNull)

Example 35 with FoldRegion

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

the class UnSelectWordHandler method doAction.

private static void doAction(final Editor editor, PsiFile file) {
    if (!editor.getSelectionModel().hasSelection()) {
        return;
    }
    CharSequence text = editor.getDocument().getCharsSequence();
    int cursorOffset = editor.getCaretModel().getOffset();
    if (cursorOffset > 0 && cursorOffset < text.length() && !Character.isJavaIdentifierPart(text.charAt(cursorOffset)) && Character.isJavaIdentifierPart(text.charAt(cursorOffset - 1))) {
        cursorOffset--;
    }
    PsiElement element = file.findElementAt(cursorOffset);
    if (element instanceof PsiWhiteSpace && cursorOffset > 0) {
        PsiElement anotherElement = file.findElementAt(cursorOffset - 1);
        if (!(anotherElement instanceof PsiWhiteSpace)) {
            element = anotherElement;
        }
    }
    if (element instanceof PsiWhiteSpace) {
        PsiElement nextSibling = element.getNextSibling();
        if (nextSibling == null) {
            element = element.getParent();
            if (element == null || element instanceof PsiFile) {
                return;
            }
            nextSibling = element.getNextSibling();
            if (nextSibling == null) {
                return;
            }
        }
        element = nextSibling;
        cursorOffset = element.getTextRange().getStartOffset();
    }
    final TextRange selectionRange = new TextRange(editor.getSelectionModel().getSelectionStart(), editor.getSelectionModel().getSelectionEnd());
    final Ref<TextRange> maximumRange = new Ref<>();
    final int finalCursorOffset = cursorOffset;
    SelectWordUtil.processRanges(element, text, cursorOffset, editor, new Processor<TextRange>() {

        @Override
        public boolean process(TextRange range) {
            range = expandToFoldingBoundaries(range);
            if (selectionRange.contains(range) && !range.equals(selectionRange) && (range.contains(finalCursorOffset) || finalCursorOffset == range.getEndOffset())) {
                if (maximumRange.get() == null || range.contains(maximumRange.get())) {
                    maximumRange.set(range);
                }
            }
            return false;
        }

        private TextRange expandToFoldingBoundaries(TextRange range) {
            int startOffset = range.getStartOffset();
            FoldRegion region = editor.getFoldingModel().getCollapsedRegionAtOffset(startOffset);
            if (region != null)
                startOffset = region.getStartOffset();
            int endOffset = range.getEndOffset();
            region = editor.getFoldingModel().getCollapsedRegionAtOffset(endOffset);
            if (region != null && endOffset > region.getStartOffset())
                endOffset = region.getEndOffset();
            return new TextRange(startOffset, endOffset);
        }
    });
    TextRange range = maximumRange.get();
    if (range == null) {
        editor.getSelectionModel().setSelection(cursorOffset, cursorOffset);
    } else {
        editor.getSelectionModel().setSelection(range.getStartOffset(), range.getEndOffset());
    }
}
Also used : Ref(com.intellij.openapi.util.Ref) FoldRegion(com.intellij.openapi.editor.FoldRegion) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Aggregations

FoldRegion (com.intellij.openapi.editor.FoldRegion)47 TextRange (com.intellij.openapi.util.TextRange)7 ArrayList (java.util.ArrayList)7 FoldingModel (com.intellij.openapi.editor.FoldingModel)6 NotNull (org.jetbrains.annotations.NotNull)6 FoldingGroup (com.intellij.openapi.editor.FoldingGroup)5 FoldingModelEx (com.intellij.openapi.editor.ex.FoldingModelEx)5 PsiElement (com.intellij.psi.PsiElement)5 CodeFoldingManager (com.intellij.codeInsight.folding.CodeFoldingManager)3 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)3 Document (com.intellij.openapi.editor.Document)3 Editor (com.intellij.openapi.editor.Editor)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 Nullable (org.jetbrains.annotations.Nullable)3 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)2 RangeMarker (com.intellij.openapi.editor.RangeMarker)2 SoftWrap (com.intellij.openapi.editor.SoftWrap)2 DocumentEx (com.intellij.openapi.editor.ex.DocumentEx)2 Project (com.intellij.openapi.project.Project)2 PsiFile (com.intellij.psi.PsiFile)2