Search in sources :

Example 26 with FoldRegion

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

the class UpdateFoldRegionsOperation method addNewRegions.

private List<FoldRegion> addNewRegions(@NotNull EditorFoldingInfo info, @NotNull FoldingModelEx foldingModel, FoldingUpdate.FoldingMap elementsToFold, @NotNull Map<TextRange, Boolean> rangeToExpandStatusMap, @NotNull Map<FoldRegion, Boolean> shouldExpand, @NotNull Map<FoldingGroup, Boolean> groupExpand) {
    List<FoldRegion> newRegions = new ArrayList<>();
    SmartPointerManager smartPointerManager = SmartPointerManager.getInstance(myProject);
    for (PsiElement element : elementsToFold.keySet()) {
        ProgressManager.checkCanceled();
        final Collection<FoldingDescriptor> descriptors = elementsToFold.get(element);
        for (FoldingDescriptor descriptor : descriptors) {
            FoldingGroup group = descriptor.getGroup();
            TextRange range = descriptor.getRange();
            String placeholder = null;
            try {
                placeholder = descriptor.getPlaceholderText();
            } catch (IndexNotReadyException ignore) {
            }
            if (range.getEndOffset() > myEditor.getDocument().getTextLength()) {
                LOG.error(String.format("Invalid folding descriptor detected (%s). It ends beyond the document range (%d)", descriptor, myEditor.getDocument().getTextLength()));
                continue;
            }
            FoldRegion region = foldingModel.createFoldRegion(range.getStartOffset(), range.getEndOffset(), placeholder == null ? "..." : placeholder, group, descriptor.isNonExpandable());
            if (region == null)
                continue;
            PsiElement psi = descriptor.getElement().getPsi();
            if (psi == null || !psi.isValid() || !foldingModel.addFoldRegion(region) || !myFile.isValid()) {
                region.dispose();
                continue;
            }
            if (descriptor.canBeRemovedWhenCollapsed())
                region.putUserData(CAN_BE_REMOVED_WHEN_COLLAPSED, Boolean.TRUE);
            info.addRegion(region, smartPointerManager.createSmartPsiElementPointer(psi));
            newRegions.add(region);
            boolean expandStatus = !descriptor.isNonExpandable() && shouldExpandNewRegion(element, range, rangeToExpandStatusMap);
            if (group == null) {
                shouldExpand.put(region, expandStatus);
            } else {
                final Boolean alreadyExpanded = groupExpand.get(group);
                groupExpand.put(group, alreadyExpanded == null ? expandStatus : alreadyExpanded.booleanValue() || expandStatus);
            }
        }
    }
    return newRegions;
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) FoldingGroup(com.intellij.openapi.editor.FoldingGroup) FoldRegion(com.intellij.openapi.editor.FoldRegion) TextRange(com.intellij.openapi.util.TextRange) SmartPointerManager(com.intellij.psi.SmartPointerManager) IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException) PsiElement(com.intellij.psi.PsiElement)

Example 27 with FoldRegion

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

the class UpdateFoldRegionsOperation method run.

@Override
public void run() {
    EditorFoldingInfo info = EditorFoldingInfo.get(myEditor);
    FoldingModelEx foldingModel = (FoldingModelEx) myEditor.getFoldingModel();
    Map<TextRange, Boolean> rangeToExpandStatusMap = new THashMap<>();
    // FoldingUpdate caches instances of our object, so they must be immutable.
    FoldingUpdate.FoldingMap elementsToFold = new FoldingUpdate.FoldingMap(myElementsToFoldMap);
    removeInvalidRegions(info, foldingModel, elementsToFold, rangeToExpandStatusMap);
    Map<FoldRegion, Boolean> shouldExpand = new THashMap<>();
    Map<FoldingGroup, Boolean> groupExpand = new THashMap<>();
    List<FoldRegion> newRegions = addNewRegions(info, foldingModel, elementsToFold, rangeToExpandStatusMap, shouldExpand, groupExpand);
    applyExpandStatus(newRegions, shouldExpand, groupExpand);
    foldingModel.clearDocumentRangesModificationStatus();
}
Also used : FoldingGroup(com.intellij.openapi.editor.FoldingGroup) FoldRegion(com.intellij.openapi.editor.FoldRegion) TextRange(com.intellij.openapi.util.TextRange) FoldingModelEx(com.intellij.openapi.editor.ex.FoldingModelEx) THashMap(gnu.trove.THashMap)

Example 28 with FoldRegion

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

the class FoldingProcessingOnDocumentModificationTest method testUnexpectedClassLevelJavadocExpandingOnClassSignatureChange.

public void testUnexpectedClassLevelJavadocExpandingOnClassSignatureChange() throws IOException {
    // Inspired by IDEA-61275
    String text = "/**\n" + " * This is a test comment\n" + " */\n" + "public <caret>class Test {\n" + "}";
    init(text, TestFileType.JAVA);
    CaretModel caretModel = myEditor.getCaretModel();
    int caretOffset = caretModel.getOffset();
    assertEquals(caretOffset, caretModel.getOffset());
    updateFoldRegions();
    toggleFoldRegionState(getFoldRegion(0), false);
    type('a');
    updateFoldRegions();
    assertEquals(caretOffset + 1, caretModel.getOffset());
    assertEquals(1, myEditor.getFoldingModel().getAllFoldRegions().length);
    FoldRegion foldRegion = getFoldRegion(0);
    assertFalse(foldRegion.isExpanded());
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel) FoldRegion(com.intellij.openapi.editor.FoldRegion)

Example 29 with FoldRegion

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

the class JavaFileEditorManagerTest method testFoldingIsNotBlinkingOnNavigationToSingleLineMethod.

public void testFoldingIsNotBlinkingOnNavigationToSingleLineMethod() {
    VirtualFile file = getFile("/src/Bar.java");
    PsiJavaFile psiFile = (PsiJavaFile) getPsiManager().findFile(file);
    assertNotNull(psiFile);
    PsiMethod method = psiFile.getClasses()[0].getMethods()[0];
    method.navigate(true);
    FileEditor[] editors = myManager.getEditors(file);
    assertEquals(1, editors.length);
    Editor editor = ((TextEditor) editors[0]).getEditor();
    EditorTestUtil.waitForLoading(editor);
    FoldRegion[] regions = editor.getFoldingModel().getAllFoldRegions();
    assertEquals(2, regions.length);
    assertTrue(regions[0].isExpanded());
    assertTrue(regions[1].isExpanded());
    CodeInsightTestFixtureImpl.instantiateAndRun(psiFile, editor, new int[] { Pass.UPDATE_ALL, Pass.LOCAL_INSPECTIONS }, false);
    regions = editor.getFoldingModel().getAllFoldRegions();
    assertEquals(2, regions.length);
    assertTrue(regions[0].isExpanded());
    assertTrue(regions[1].isExpanded());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileEditor(com.intellij.openapi.fileEditor.FileEditor) TextEditor(com.intellij.openapi.fileEditor.TextEditor) PsiMethod(com.intellij.psi.PsiMethod) FoldRegion(com.intellij.openapi.editor.FoldRegion) PsiJavaFile(com.intellij.psi.PsiJavaFile) Editor(com.intellij.openapi.editor.Editor) FileEditor(com.intellij.openapi.fileEditor.FileEditor) TextEditor(com.intellij.openapi.fileEditor.TextEditor)

Example 30 with FoldRegion

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

the class FoldingModelSupport method addFolding.

@Nullable
public static FoldRegion addFolding(@NotNull EditorEx editor, int start, int end, boolean expanded) {
    DocumentEx document = editor.getDocument();
    final int startOffset = document.getLineStartOffset(start);
    final int endOffset = document.getLineEndOffset(end - 1);
    FoldRegion value = editor.getFoldingModel().addFoldRegion(startOffset, endOffset, PLACEHOLDER);
    if (value != null)
        value.setExpanded(expanded);
    return value;
}
Also used : DocumentEx(com.intellij.openapi.editor.ex.DocumentEx) FoldRegion(com.intellij.openapi.editor.FoldRegion) Nullable(org.jetbrains.annotations.Nullable)

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