Search in sources :

Example 16 with FoldRegion

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

the class SoftWrapTest method doTestSoftWraps.

private void doTestSoftWraps(int wrapWidth, String text) throws IOException {
    List<MyFoldRegion> foldRegions = new ArrayList<>();
    List<Integer> wrapPositions = new ArrayList<>();
    int foldInsertPosition = 0;
    int pos = 0;
    int docPos = 0;
    Matcher matcher = Pattern.compile(TAGS_PATTERN).matcher(text);
    StringBuilder cleanedText = new StringBuilder();
    while (matcher.find()) {
        cleanedText.append(text.substring(pos, matcher.start()));
        docPos += matcher.start() - pos;
        pos = matcher.end();
        if (matcher.group(1) != null) {
            // <fold>
            foldRegions.add(foldInsertPosition++, new MyFoldRegion(docPos, matcher.group(3), matcher.group(2) != null));
        } else if (matcher.group(4) != null) {
            // </fold>
            assertTrue("Misplaced closing fold marker tag: " + text, foldInsertPosition > 0);
            foldRegions.get(--foldInsertPosition).endPos = docPos;
        } else {
            // <wrap>
            wrapPositions.add(docPos);
        }
    }
    assertTrue("Missing closing fold marker tag: " + text, foldInsertPosition == 0);
    cleanedText.append(text.substring(pos));
    init(cleanedText.toString(), TestFileType.TEXT);
    for (MyFoldRegion region : foldRegions) {
        FoldRegion r = addFoldRegion(region.startPos, region.endPos, region.placeholder);
        if (region.collapse) {
            toggleFoldRegionState(r, false);
        }
    }
    EditorTestUtil.configureSoftWraps(myEditor, wrapWidth);
    List<Integer> actualWrapPositions = new ArrayList<>();
    for (SoftWrap wrap : myEditor.getSoftWrapModel().getSoftWrapsForRange(0, myEditor.getDocument().getTextLength())) {
        actualWrapPositions.add(wrap.getStart());
    }
    assertEquals("Wrong wrap positions", wrapPositions, actualWrapPositions);
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) FoldRegion(com.intellij.openapi.editor.FoldRegion) SoftWrap(com.intellij.openapi.editor.SoftWrap)

Example 17 with FoldRegion

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

use of com.intellij.openapi.editor.FoldRegion 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)

Example 19 with FoldRegion

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

the class XBreakpointUtil method toggleLineBreakpoint.

/**
   * Toggle line breakpoint with editor support:
   * - unfolds folded block on the line
   * - if folded, checks if line breakpoints could be toggled inside folded text
   */
@NotNull
public static Promise<XLineBreakpoint> toggleLineBreakpoint(@NotNull Project project, @NotNull XSourcePosition position, @Nullable Editor editor, boolean temporary, boolean moveCaret, boolean canRemove) {
    int lineStart = position.getLine();
    VirtualFile file = position.getFile();
    // for folded text check each line and find out type with the biggest priority
    int linesEnd = lineStart;
    if (editor != null) {
        FoldRegion region = FoldingUtil.findFoldRegionStartingAtLine(editor, lineStart);
        if (region != null && !region.isExpanded()) {
            linesEnd = region.getDocument().getLineNumber(region.getEndOffset());
        }
    }
    final XBreakpointManager breakpointManager = XDebuggerManager.getInstance(project).getBreakpointManager();
    XLineBreakpointType<?>[] lineTypes = XDebuggerUtil.getInstance().getLineBreakpointTypes();
    XLineBreakpointType<?> typeWinner = null;
    int lineWinner = -1;
    for (int line = lineStart; line <= linesEnd; line++) {
        int maxPriority = 0;
        for (XLineBreakpointType<?> type : lineTypes) {
            maxPriority = Math.max(maxPriority, type.getPriority());
            XLineBreakpoint<? extends XBreakpointProperties> breakpoint = breakpointManager.findBreakpointAtLine(type, file, line);
            if ((type.canPutAt(file, line, project) || breakpoint != null) && (typeWinner == null || type.getPriority() > typeWinner.getPriority())) {
                typeWinner = type;
                lineWinner = line;
            }
        }
        // already found max priority type - stop
        if (typeWinner != null && typeWinner.getPriority() == maxPriority) {
            break;
        }
    }
    if (typeWinner != null) {
        XSourcePosition winPosition = (lineStart == lineWinner) ? position : XSourcePositionImpl.create(file, lineWinner);
        if (winPosition != null) {
            Promise<XLineBreakpoint> res = XDebuggerUtilImpl.toggleAndReturnLineBreakpoint(project, typeWinner, winPosition, temporary, editor, canRemove);
            if (editor != null && lineStart != lineWinner) {
                int offset = editor.getDocument().getLineStartOffset(lineWinner);
                ExpandRegionAction.expandRegionAtOffset(project, editor, offset);
                if (moveCaret) {
                    editor.getCaretModel().moveToOffset(offset);
                }
            }
            return res;
        }
    }
    return rejectedPromise();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FoldRegion(com.intellij.openapi.editor.FoldRegion) XSourcePosition(com.intellij.xdebugger.XSourcePosition) NotNull(org.jetbrains.annotations.NotNull)

Example 20 with FoldRegion

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

the class ExternalProjectPathLookupElement method handleInsert.

@Override
public void handleInsert(InsertionContext context) {
    Editor editor = context.getEditor();
    final FoldingModel foldingModel = editor.getFoldingModel();
    foldingModel.runBatchFoldingOperation(() -> {
        FoldRegion[] regions = foldingModel.getAllFoldRegions();
        for (FoldRegion region : regions) {
            foldingModel.removeFoldRegion(region);
        }
    });
    final Document document = editor.getDocument();
    final int startOffset = context.getStartOffset();
    document.replaceString(startOffset, document.getTextLength(), myProjectPath);
    final Project project = context.getProject();
    PsiDocumentManager.getInstance(project).commitDocument(document);
    foldingModel.runBatchFoldingOperationDoNotCollapseCaret(() -> {
        FoldRegion region = foldingModel.addFoldRegion(startOffset, startOffset + myProjectPath.length(), myProjectName);
        if (region != null) {
            region.setExpanded(false);
        }
    });
}
Also used : Project(com.intellij.openapi.project.Project) FoldingModel(com.intellij.openapi.editor.FoldingModel) FoldRegion(com.intellij.openapi.editor.FoldRegion) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document)

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