Search in sources :

Example 6 with FoldingModelEx

use of com.intellij.openapi.editor.ex.FoldingModelEx 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 7 with FoldingModelEx

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

the class CodeFoldingManagerImpl method buildInitialFoldings.

@Nullable
@Override
public CodeFoldingState buildInitialFoldings(@NotNull final Document document) {
    if (myProject.isDisposed()) {
        return null;
    }
    ApplicationManager.getApplication().assertReadAccessAllowed();
    PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(myProject);
    if (psiDocumentManager.isUncommited(document)) {
        // skip building foldings for uncommitted document, CodeFoldingPass invoked by daemon will do it later
        return null;
    }
    //Do not save/restore folding for code fragments
    final PsiFile file = psiDocumentManager.getPsiFile(document);
    if (file == null || !file.isValid() || !file.getViewProvider().isPhysical() && !ApplicationManager.getApplication().isUnitTestMode()) {
        return null;
    }
    final FoldingUpdate.FoldingMap foldingMap = FoldingUpdate.getFoldingsFor(file, document, true);
    return editor -> {
        ApplicationManagerEx.getApplicationEx().assertIsDispatchThread();
        if (myProject.isDisposed() || editor.isDisposed())
            return;
        final FoldingModelEx foldingModel = (FoldingModelEx) editor.getFoldingModel();
        if (!foldingModel.isFoldingEnabled())
            return;
        if (isFoldingsInitializedInEditor(editor))
            return;
        if (DumbService.isDumb(myProject) && !FoldingUpdate.supportsDumbModeFolding(editor))
            return;
        foldingModel.runBatchFoldingOperationDoNotCollapseCaret(new UpdateFoldRegionsOperation(myProject, editor, file, foldingMap, UpdateFoldRegionsOperation.ApplyDefaultStateMode.YES, false, false));
        initFolding(editor);
    };
}
Also used : com.intellij.openapi.util(com.intellij.openapi.util) EditorMouseMotionAdapter(com.intellij.openapi.editor.event.EditorMouseMotionAdapter) FoldingModelEx(com.intellij.openapi.editor.ex.FoldingModelEx) com.intellij.openapi.editor(com.intellij.openapi.editor) DumbAwareRunnable(com.intellij.openapi.project.DumbAwareRunnable) EditorMouseEventArea(com.intellij.openapi.editor.event.EditorMouseEventArea) StartupManager(com.intellij.openapi.startup.StartupManager) CodeFoldingManager(com.intellij.codeInsight.folding.CodeFoldingManager) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) EditorEx(com.intellij.openapi.editor.ex.EditorEx) ProjectComponent(com.intellij.openapi.components.ProjectComponent) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) DumbService(com.intellij.openapi.project.DumbService) WeakList(com.intellij.util.containers.WeakList) MouseEvent(java.awt.event.MouseEvent) EditorMouseEvent(com.intellij.openapi.editor.event.EditorMouseEvent) CodeFoldingState(com.intellij.openapi.fileEditor.impl.text.CodeFoldingState) java.awt(java.awt) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) ApplicationManager(com.intellij.openapi.application.ApplicationManager) ApplicationManagerEx(com.intellij.openapi.application.ex.ApplicationManagerEx) HintManager(com.intellij.codeInsight.hint.HintManager) NotNull(org.jetbrains.annotations.NotNull) LightweightHint(com.intellij.ui.LightweightHint) Element(org.jdom.Element) EditorFragmentComponent(com.intellij.codeInsight.hint.EditorFragmentComponent) javax.swing(javax.swing) PsiFile(com.intellij.psi.PsiFile) FoldingModelEx(com.intellij.openapi.editor.ex.FoldingModelEx) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with FoldingModelEx

use of com.intellij.openapi.editor.ex.FoldingModelEx 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 9 with FoldingModelEx

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

the class JavaFoldingGotoTest method testIDEA127145.

public void testIDEA127145() {
    PsiFile file = myFixture.addFileToProject("Program.java", "import java.io.InputStream;\n" + "import java.util.HashMap;\n" + "import java.util.Map;\n" + "\n" + "class Program {\n" + "  private static InputStream getFile(String name, Map<String, Object> args) {\n" + "    return Program.class.getResourceAsStream(name);\n" + "  }\n" + "\n" + "  public static void main(String[] args) {\n" + "    // Ctrl + B or Ctrl + Left Mouse Button work correctly for following string:\n" + "    final String name = \"file.sql\";\n" + "    // But it jumps only to folder in following case:\n" + "    final InputStream inputStream = getFile(\"dir/fil<caret>e.sql\", new HashMap<String, Object>());\n" + "  }\n" + "}");
    PsiFile fileSql = myFixture.addFileToProject("dir/file.sql", "select 1;");
    myFixture.configureFromExistingVirtualFile(file.getVirtualFile());
    Editor editor = myFixture.getEditor();
    CodeFoldingManager.getInstance(getProject()).buildInitialFoldings(editor);
    FoldingModelEx foldingModel = (FoldingModelEx) editor.getFoldingModel();
    foldingModel.rebuild();
    myFixture.doHighlighting();
    PsiElement element = GotoDeclarationAction.findTargetElement(getProject(), editor, editor.getCaretModel().getOffset());
    assertTrue("Should navigate to: file.sql instead of " + element, element != null && element.equals(fileSql));
}
Also used : PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) FoldingModelEx(com.intellij.openapi.editor.ex.FoldingModelEx) PsiElement(com.intellij.psi.PsiElement)

Example 10 with FoldingModelEx

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

the class EditorImplTest method testChangingHighlightersAfterClearingFoldingsDuringFoldingBatchUpdate.

public void testChangingHighlightersAfterClearingFoldingsDuringFoldingBatchUpdate() throws Exception {
    initText("abc\n\ndef");
    addCollapsedFoldRegion(2, 6, "...");
    runFoldingOperation(() -> {
        ((FoldingModelEx) myEditor.getFoldingModel()).clearFoldRegions();
        myEditor.getMarkupModel().addRangeHighlighter(7, 8, 0, new TextAttributes(null, null, null, null, Font.BOLD), HighlighterTargetArea.EXACT_RANGE);
    });
    RangeHighlighter[] highlighters = myEditor.getMarkupModel().getAllHighlighters();
    assertEquals(1, highlighters.length);
    assertEquals(7, highlighters[0].getStartOffset());
    assertEquals(8, highlighters[0].getEndOffset());
}
Also used : RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) 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