public void testOccurrenceInCollapsedRegion() throws Exception { init("normal <selection><caret>line</selection>\n" + "collapsed line"); final FoldingModel foldingModel = myFixture.getEditor().getFoldingModel(); final Document document = myFixture.getEditor().getDocument(); foldingModel.runBatchFoldingOperation(new Runnable() { @Override public void run() { FoldRegion foldRegion = foldingModel.addFoldRegion(document.getLineStartOffset(1), document.getLineEndOffset(1), "..."); assertNotNull(foldRegion); foldRegion.setExpanded(false); } }); executeAction(); checkResult("normal <selection><caret>line</selection>\n" + "collapsed <selection><caret>line</selection>"); FoldRegion[] foldRegions = foldingModel.getAllFoldRegions(); assertEquals(1, foldRegions.length); assertTrue(foldRegions[0].isExpanded()); }
public void testStoringCaretStateForFileWithFoldingsWithNoTabs() throws Exception { int savedValue = UISettings.getInstance().EDITOR_TAB_PLACEMENT; UISettings.getInstance().EDITOR_TAB_PLACEMENT = 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(); final FoldingModel foldingModel = editor.getFoldingModel(); assertEquals(2, foldingModel.getAllFoldRegions().length); foldingModel.runBatchFoldingOperation(new Runnable() { @Override public void run() { 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(); assertEquals(textLength, editor.getCaretModel().getOffset()); assertEquals(textLength - 1, editor.getSelectionModel().getSelectionStart()); assertEquals(textLength, editor.getSelectionModel().getSelectionEnd()); } finally { UISettings.getInstance().EDITOR_TAB_PLACEMENT = savedValue; } }
public void testWithEmptyLastLine() { myFixture.configureByText(FileTypes.PLAIN_TEXT, "some text\n"); final FoldingModel foldingModel = myFixture.getEditor().getFoldingModel(); foldingModel.runBatchFoldingOperation(new Runnable() { @Override public void run() { foldingModel.addFoldRegion(0, 10, "..."); } }); verifyAnchors(null, 0, Type.EXPANDED_TOP, 1, Type.EXPANDED_BOTTOM); }
private void collapseFoldingRegion(int n) { FoldingModel foldingModel = myFixture.getEditor().getFoldingModel(); final FoldRegion foldRegion = foldingModel.getAllFoldRegions()[n]; foldingModel.runBatchFoldingOperation(new Runnable() { @Override public void run() { foldRegion.setExpanded(false); } }); }
/** * There is a possible case that current cache update event reflects particular document change. It's also possible that * current object is constructed before document change and we need to normalize 'after change' data later then. * <p/> * This method allows to do that, i.e. it's assumed that current cache update event will be used within the cache that is * bound to the given document and normalizes 'new offsets' if necessary when the document is really changed. * * @param document document which change caused current cache update event construction * @param foldingModel fold model to use */ public void updateNewOffsetsIfNecessary(@NotNull Document document, @NotNull FoldingModel foldingModel) { myNewStartOffset = getLineStartOffset(myNewExactStartOffset, document); myNewEndOffset = getLineEndOffset(myNewExactEndOffset, document); for ( FoldRegion region = foldingModel.getCollapsedRegionAtOffset(myNewEndOffset); region != null; region = foldingModel.getCollapsedRegionAtOffset(myNewEndOffset)) { myNewEndOffset = getLineEndOffset(region.getEndOffset(), document); } myNewLogicalLinesDiff = document.getLineNumber(myNewExactEndOffset) - document.getLineNumber(myNewExactStartOffset); }
public static void resetInfo(@NotNull Editor editor) { EditorFoldingInfo info = editor.getUserData(KEY); if (info != null) { info.dispose(); } FoldingModel foldingModel = editor.getFoldingModel(); if (foldingModel instanceof FoldingModelEx) { ((FoldingModelEx)foldingModel).rebuild(); } editor.putUserData(KEY, null); }
@Nonnull private String getFoldingDescription(@Nonnull String content, @Nonnull String fileName, boolean doCheckCollapseStatus) { FileType fileTypeByFileName = FileTypeManager.getInstance().getFileTypeByFileName(fileName); PsiFile file = createFile(fileName, fileTypeByFileName, content); final FileEditorManager fileEditorManager = FileEditorManager.getInstance(myProject); Editor editor = fileEditorManager.openTextEditor(new OpenFileDescriptor(myProject, file.getVirtualFile()), false); CodeFoldingManager.getInstance(myProject).buildInitialFoldings(editor); final FoldingModel model = editor.getFoldingModel(); final FoldRegion[] foldingRegions = model.getAllFoldRegions(); final List<Border> borders = new LinkedList<Border>(); for (FoldRegion region : foldingRegions) { borders.add(new Border(Border.LEFT, region.getStartOffset(), region.getPlaceholderText(), region.isExpanded())); borders.add(new Border(Border.RIGHT, region.getEndOffset(), "", region.isExpanded())); } Collections.sort(borders); StringBuilder result = new StringBuilder(editor.getDocument().getText()); for (Border border : borders) { result.insert(border.getOffset(), border.isSide() == Border.LEFT ? "<fold text=\'" + border.getText() + "\'" + (doCheckCollapseStatus ? " expand=\'" + border.isExpanded() + "\'" : "") + ">" : END_FOLD); } return result.toString(); }
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(new Runnable() { @Override public void run() { 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); engine.arrange(myFixture.getEditor(), myFixture.getFile(), info.ranges); // Check expectation. info = parse(expected); assertEquals(info.text, myFixture.getEditor().getDocument().getText()); for (FoldingInfo it : info.foldings) { FoldRegion foldRegion = foldingModel.getCollapsedRegionAtOffset(it.start); assertNotNull("Expected to find fold region at offset " + it.start, foldRegion); assertEquals(it.end, foldRegion.getEndOffset()); } }
public FoldingGaps(FoldingModel foldingModel) { myFoldingModel = foldingModel; }