void () { Document document = myConsoleView.getEditor().getDocument(); if (doNotAddFoldingAgain || document.getTextLength() == 0) { return; } if (myNumberOfCommandExecuted >= myNumberOfCommandToStop) { document.removeDocumentListener(this); return; } FoldingModel foldingModel = myConsoleView.getEditor().getFoldingModel(); foldingModel.runBatchFoldingOperation(() -> { int start = getStartDefaultValue(); int startLine = 0; int finish = start; int finishLine = 0; String placeholderText = PyBundle.message("python.console"); int firstLine = document.getLineNumber(myStartLineOffset); for (int line = firstLine; line < document.getLineCount(); line++) { String lineText = document.getText(DocumentUtil.getLineTextRange(document, line)); String prevLineText = null; if (line > 0) { prevLineText = document.getText(DocumentUtil.getLineTextRange(document, line - 1)); } if (start == getStartDefaultValue()) { for (String prefix : firstLinePrefix) { if (lineText.startsWith(prefix)) { start = document.getLineStartOffset(line); startLine = line; if (prefix.equals(PYTHON_PREFIX)) { placeholderText = lineText; } break; } } } if (!doNotAddFoldingAgain) { for (String prefix : lastLinePrefix) { if (lineText.startsWith(prefix) && (!prefix.equals("[")) || (prefix.equals("[") && prevLineText != null && prevLineText.startsWith(PYTHON_PREFIX))) { finish = document.getLineEndOffset(line); finishLine = line; doNotAddFoldingAgain = myAddOnce; break; } } } } if (myStartFoldRegion != null) { foldingModel.removeFoldRegion(myStartFoldRegion); } if ((start >= finish) || (startLine == finishLine)) return; FoldRegion foldRegion = foldingModel.addFoldRegion(start, finish, placeholderText); if (foldRegion != null) { foldRegion.setExpanded(false); myStartFoldRegion = foldRegion; } }); }