Search in sources :

Example 1 with FoldingModel

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

the class ExternalProjectPathField method collapse.

public static void collapse(@NotNull final Editor editor, @NotNull final String placeholder) {
    final FoldingModel foldingModel = editor.getFoldingModel();
    foldingModel.runBatchFoldingOperation(() -> {
        for (FoldRegion region : foldingModel.getAllFoldRegions()) {
            foldingModel.removeFoldRegion(region);
        }
        FoldRegion region = foldingModel.addFoldRegion(0, editor.getDocument().getTextLength(), placeholder);
        if (region != null) {
            region.setExpanded(false);
        }
    });
}
Also used : FoldingModel(com.intellij.openapi.editor.FoldingModel) FoldRegion(com.intellij.openapi.editor.FoldRegion)

Example 2 with FoldingModel

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

the class FoldingAnchorsOverlayStrategyTest method collapseFoldingRegion.

private void collapseFoldingRegion(int n) {
    FoldingModel foldingModel = myFixture.getEditor().getFoldingModel();
    final FoldRegion foldRegion = foldingModel.getAllFoldRegions()[n];
    foldingModel.runBatchFoldingOperation(() -> foldRegion.setExpanded(false));
}
Also used : FoldingModel(com.intellij.openapi.editor.FoldingModel) FoldRegion(com.intellij.openapi.editor.FoldRegion)

Example 3 with FoldingModel

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

the class FoldingAnchorsOverlayStrategyTest method testWithEmptyLastLine.

public void testWithEmptyLastLine() {
    myFixture.configureByText(FileTypes.PLAIN_TEXT, "some text\n");
    final FoldingModel foldingModel = myFixture.getEditor().getFoldingModel();
    foldingModel.runBatchFoldingOperation(() -> foldingModel.addFoldRegion(0, 10, "..."));
    verifyAnchors(null, 0, Type.EXPANDED_TOP, 1, Type.EXPANDED_BOTTOM);
}
Also used : FoldingModel(com.intellij.openapi.editor.FoldingModel)

Example 4 with FoldingModel

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

the class FileEditorManagerTest method testStoringCaretStateForFileWithFoldingsWithNoTabs.

public void testStoringCaretStateForFileWithFoldingsWithNoTabs() throws Exception {
    int savedValue = UISettings.getInstance().getEditorTabPlacement();
    UISettings.getInstance().setEditorTabPlacement(UISettings.TABS_NONE);
    try {
        VirtualFile file = getFile("/src/Test.java");
        assertNotNull(file);
        FileEditor[] editors = myManager.openFile(file, false);
        assertEquals(1, editors.length);
        assertTrue(editors[0] instanceof TextEditor);
        Editor editor = ((TextEditor) editors[0]).getEditor();
        EditorTestUtil.waitForLoading(editor);
        final FoldingModel foldingModel = editor.getFoldingModel();
        assertEquals(2, foldingModel.getAllFoldRegions().length);
        foldingModel.runBatchFoldingOperation(() -> {
            for (FoldRegion region : foldingModel.getAllFoldRegions()) {
                region.setExpanded(false);
            }
        });
        int textLength = editor.getDocument().getTextLength();
        editor.getCaretModel().moveToOffset(textLength);
        editor.getSelectionModel().setSelection(textLength - 1, textLength);
        myManager.openFile(getFile("/src/1.txt"), false);
        assertEquals(0, myManager.getEditors(file).length);
        editors = myManager.openFile(file, false);
        assertEquals(1, editors.length);
        assertTrue(editors[0] instanceof TextEditor);
        editor = ((TextEditor) editors[0]).getEditor();
        EditorTestUtil.waitForLoading(editor);
        assertEquals(textLength, editor.getCaretModel().getOffset());
        assertEquals(textLength - 1, editor.getSelectionModel().getSelectionStart());
        assertEquals(textLength, editor.getSelectionModel().getSelectionEnd());
    } finally {
        UISettings.getInstance().setEditorTabPlacement(savedValue);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FoldingModel(com.intellij.openapi.editor.FoldingModel) FoldRegion(com.intellij.openapi.editor.FoldRegion) Editor(com.intellij.openapi.editor.Editor)

Example 5 with FoldingModel

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

the class AbstractRearrangerTest method doTest.

protected void doTest(@NotNull Map<String, ?> args) {
    String text = (String) args.get("initial");
    String expected = (String) args.get("expected");
    @SuppressWarnings("unchecked") List<TextRange> ranges = (List<TextRange>) args.get("ranges");
    Info info = parse(text);
    if (!isEmpty(ranges) && !isEmpty(info.ranges)) {
        fail("Duplicate ranges set: explicit: " + ranges + ", " + "derived: " + info.ranges + ", text:\n" + text);
    }
    if (isEmpty(info.ranges)) {
        info.ranges = !isEmpty(ranges) ? ranges : Arrays.asList(TextRange.from(0, text.length()));
    }
    myFixture.configureByText(fileType, info.text);
    final FoldingModel foldingModel = myFixture.getEditor().getFoldingModel();
    for (final FoldingInfo foldingInfo : info.foldings) {
        foldingModel.runBatchFoldingOperation(() -> {
            FoldRegion region = foldingModel.addFoldRegion(foldingInfo.start, foldingInfo.end, foldingInfo.placeholder);
            if (region != null)
                region.setExpanded(false);
        });
    }
    @SuppressWarnings("unchecked") List<ArrangementGroupingRule> groupingRules = (List<ArrangementGroupingRule>) args.get("groups");
    if (groupingRules == null)
        groupingRules = Collections.emptyList();
    List<?> rules = (List<?>) args.get("rules");
    List<ArrangementSectionRule> sectionRules = getSectionRules(rules);
    @SuppressWarnings("unchecked") List<StdArrangementRuleAliasToken> aliases = (List<StdArrangementRuleAliasToken>) args.get("aliases");
    CommonCodeStyleSettings settings = CodeStyleSettingsManager.getInstance(myFixture.getProject()).getCurrentSettings().getCommonSettings(language);
    final StdArrangementSettings arrangementSettings = aliases == null ? new StdArrangementSettings(groupingRules, sectionRules) : new StdArrangementExtendableSettings(groupingRules, sectionRules, aliases);
    settings.setArrangementSettings(arrangementSettings);
    ArrangementEngine engine = ServiceManager.getService(myFixture.getProject(), ArrangementEngine.class);
    CommandProcessor.getInstance().executeCommand(getProject(), () -> engine.arrange(myFixture.getEditor(), myFixture.getFile(), info.ranges), null, null);
    // Check expectation.
    Info after = parse(expected);
    assertEquals(after.text, myFixture.getEditor().getDocument().getText());
    for (FoldingInfo it : after.foldings) {
        FoldRegion foldRegion = foldingModel.getCollapsedRegionAtOffset(it.start);
        assertNotNull("Expected to find fold region at offset " + it.start, foldRegion);
        assertEquals(it.end, foldRegion.getEndOffset());
    }
}
Also used : ArrangementGroupingRule(com.intellij.psi.codeStyle.arrangement.group.ArrangementGroupingRule) ArrangementEngine(com.intellij.psi.codeStyle.arrangement.engine.ArrangementEngine) FoldRegion(com.intellij.openapi.editor.FoldRegion) TextRange(com.intellij.openapi.util.TextRange) FoldingModel(com.intellij.openapi.editor.FoldingModel) CommonCodeStyleSettings(com.intellij.psi.codeStyle.CommonCodeStyleSettings) ArrangementSectionRule(com.intellij.psi.codeStyle.arrangement.match.ArrangementSectionRule)

Aggregations

FoldingModel (com.intellij.openapi.editor.FoldingModel)8 FoldRegion (com.intellij.openapi.editor.FoldRegion)6 Document (com.intellij.openapi.editor.Document)3 Editor (com.intellij.openapi.editor.Editor)3 Project (com.intellij.openapi.project.Project)2 DocumentWindow (com.intellij.injected.editor.DocumentWindow)1 EditorWindow (com.intellij.injected.editor.EditorWindow)1 TextRange (com.intellij.openapi.util.TextRange)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 CommonCodeStyleSettings (com.intellij.psi.codeStyle.CommonCodeStyleSettings)1 ArrangementEngine (com.intellij.psi.codeStyle.arrangement.engine.ArrangementEngine)1 ArrangementGroupingRule (com.intellij.psi.codeStyle.arrangement.group.ArrangementGroupingRule)1 ArrangementSectionRule (com.intellij.psi.codeStyle.arrangement.match.ArrangementSectionRule)1 Nullable (org.jetbrains.annotations.Nullable)1