use of com.intellij.openapi.editor.FoldRegion in project intellij-community by JetBrains.
the class UpdateFoldRegionsOperation method addNewRegions.
private List<FoldRegion> addNewRegions(@NotNull EditorFoldingInfo info, @NotNull FoldingModelEx foldingModel, FoldingUpdate.FoldingMap elementsToFold, @NotNull Map<TextRange, Boolean> rangeToExpandStatusMap, @NotNull Map<FoldRegion, Boolean> shouldExpand, @NotNull Map<FoldingGroup, Boolean> groupExpand) {
List<FoldRegion> newRegions = new ArrayList<>();
SmartPointerManager smartPointerManager = SmartPointerManager.getInstance(myProject);
for (PsiElement element : elementsToFold.keySet()) {
ProgressManager.checkCanceled();
final Collection<FoldingDescriptor> descriptors = elementsToFold.get(element);
for (FoldingDescriptor descriptor : descriptors) {
FoldingGroup group = descriptor.getGroup();
TextRange range = descriptor.getRange();
String placeholder = null;
try {
placeholder = descriptor.getPlaceholderText();
} catch (IndexNotReadyException ignore) {
}
if (range.getEndOffset() > myEditor.getDocument().getTextLength()) {
LOG.error(String.format("Invalid folding descriptor detected (%s). It ends beyond the document range (%d)", descriptor, myEditor.getDocument().getTextLength()));
continue;
}
FoldRegion region = foldingModel.createFoldRegion(range.getStartOffset(), range.getEndOffset(), placeholder == null ? "..." : placeholder, group, descriptor.isNonExpandable());
if (region == null)
continue;
PsiElement psi = descriptor.getElement().getPsi();
if (psi == null || !psi.isValid() || !foldingModel.addFoldRegion(region) || !myFile.isValid()) {
region.dispose();
continue;
}
if (descriptor.canBeRemovedWhenCollapsed())
region.putUserData(CAN_BE_REMOVED_WHEN_COLLAPSED, Boolean.TRUE);
info.addRegion(region, smartPointerManager.createSmartPsiElementPointer(psi));
newRegions.add(region);
boolean expandStatus = !descriptor.isNonExpandable() && shouldExpandNewRegion(element, range, rangeToExpandStatusMap);
if (group == null) {
shouldExpand.put(region, expandStatus);
} else {
final Boolean alreadyExpanded = groupExpand.get(group);
groupExpand.put(group, alreadyExpanded == null ? expandStatus : alreadyExpanded.booleanValue() || expandStatus);
}
}
}
return newRegions;
}
use of com.intellij.openapi.editor.FoldRegion 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();
}
use of com.intellij.openapi.editor.FoldRegion in project intellij-community by JetBrains.
the class FoldingProcessingOnDocumentModificationTest method testUnexpectedClassLevelJavadocExpandingOnClassSignatureChange.
public void testUnexpectedClassLevelJavadocExpandingOnClassSignatureChange() throws IOException {
// Inspired by IDEA-61275
String text = "/**\n" + " * This is a test comment\n" + " */\n" + "public <caret>class Test {\n" + "}";
init(text, TestFileType.JAVA);
CaretModel caretModel = myEditor.getCaretModel();
int caretOffset = caretModel.getOffset();
assertEquals(caretOffset, caretModel.getOffset());
updateFoldRegions();
toggleFoldRegionState(getFoldRegion(0), false);
type('a');
updateFoldRegions();
assertEquals(caretOffset + 1, caretModel.getOffset());
assertEquals(1, myEditor.getFoldingModel().getAllFoldRegions().length);
FoldRegion foldRegion = getFoldRegion(0);
assertFalse(foldRegion.isExpanded());
}
use of com.intellij.openapi.editor.FoldRegion in project intellij-community by JetBrains.
the class JavaFileEditorManagerTest method testFoldingIsNotBlinkingOnNavigationToSingleLineMethod.
public void testFoldingIsNotBlinkingOnNavigationToSingleLineMethod() {
VirtualFile file = getFile("/src/Bar.java");
PsiJavaFile psiFile = (PsiJavaFile) getPsiManager().findFile(file);
assertNotNull(psiFile);
PsiMethod method = psiFile.getClasses()[0].getMethods()[0];
method.navigate(true);
FileEditor[] editors = myManager.getEditors(file);
assertEquals(1, editors.length);
Editor editor = ((TextEditor) editors[0]).getEditor();
EditorTestUtil.waitForLoading(editor);
FoldRegion[] regions = editor.getFoldingModel().getAllFoldRegions();
assertEquals(2, regions.length);
assertTrue(regions[0].isExpanded());
assertTrue(regions[1].isExpanded());
CodeInsightTestFixtureImpl.instantiateAndRun(psiFile, editor, new int[] { Pass.UPDATE_ALL, Pass.LOCAL_INSPECTIONS }, false);
regions = editor.getFoldingModel().getAllFoldRegions();
assertEquals(2, regions.length);
assertTrue(regions[0].isExpanded());
assertTrue(regions[1].isExpanded());
}
use of com.intellij.openapi.editor.FoldRegion in project intellij-community by JetBrains.
the class FoldingModelSupport method addFolding.
@Nullable
public static FoldRegion addFolding(@NotNull EditorEx editor, int start, int end, boolean expanded) {
DocumentEx document = editor.getDocument();
final int startOffset = document.getLineStartOffset(start);
final int endOffset = document.getLineEndOffset(end - 1);
FoldRegion value = editor.getFoldingModel().addFoldRegion(startOffset, endOffset, PLACEHOLDER);
if (value != null)
value.setExpanded(expanded);
return value;
}
Aggregations