use of com.intellij.openapi.editor.FoldRegion in project intellij-community by JetBrains.
the class CollapseBlockHandler method invoke.
@Override
public void invoke(@NotNull final Project project, @NotNull final Editor editor, @NotNull final PsiFile file) {
int[] targetCaretOffset = { -1 };
editor.getFoldingModel().runBatchFoldingOperation(() -> {
final EditorFoldingInfo info = EditorFoldingInfo.get(editor);
FoldingModelEx model = (FoldingModelEx) editor.getFoldingModel();
PsiElement element = file.findElementAt(editor.getCaretModel().getOffset() - 1);
if (!(element instanceof PsiJavaToken) || ((PsiJavaToken) element).getTokenType() != JavaTokenType.RBRACE) {
element = file.findElementAt(editor.getCaretModel().getOffset());
}
if (element == null)
return;
PsiCodeBlock block = PsiTreeUtil.getParentOfType(element, PsiCodeBlock.class);
FoldRegion previous = null;
FoldRegion myPrevious = null;
while (block != null) {
int start = block.getTextRange().getStartOffset();
int end = block.getTextRange().getEndOffset();
FoldRegion existing = FoldingUtil.findFoldRegion(editor, start, end);
if (existing != null) {
if (existing.isExpanded()) {
existing.setExpanded(false);
targetCaretOffset[0] = existing.getEndOffset();
return;
}
previous = existing;
if (info.getPsiElement(existing) == null)
myPrevious = existing;
block = PsiTreeUtil.getParentOfType(block, PsiCodeBlock.class);
continue;
}
if (!model.intersectsRegion(start, end)) {
FoldRegion region = model.addFoldRegion(start, end, ourPlaceHolderText);
LOG.assertTrue(region != null);
region.setExpanded(false);
if (myPrevious != null && info.getPsiElement(region) == null) {
info.removeRegion(myPrevious);
model.removeFoldRegion(myPrevious);
}
targetCaretOffset[0] = block.getTextRange().getEndOffset() < editor.getCaretModel().getOffset() ? start : end;
return;
} else
break;
}
if (previous != null) {
previous.setExpanded(false);
if (myPrevious != null) {
info.removeRegion(myPrevious);
model.removeFoldRegion(myPrevious);
}
targetCaretOffset[0] = previous.getEndOffset();
}
});
if (targetCaretOffset[0] >= 0)
editor.getCaretModel().moveToOffset(targetCaretOffset[0]);
}
use of com.intellij.openapi.editor.FoldRegion in project intellij-community by JetBrains.
the class DuplicatesImpl method expandAllRegionsCoveringRange.
private static void expandAllRegionsCoveringRange(final Project project, Editor editor, final TextRange textRange) {
final FoldRegion[] foldRegions = CodeFoldingManager.getInstance(project).getFoldRegionsAtOffset(editor, textRange.getStartOffset());
boolean anyCollapsed = false;
for (final FoldRegion foldRegion : foldRegions) {
if (!foldRegion.isExpanded()) {
anyCollapsed = true;
break;
}
}
if (anyCollapsed) {
editor.getFoldingModel().runBatchFoldingOperation(() -> {
for (final FoldRegion foldRegion : foldRegions) {
if (!foldRegion.isExpanded()) {
foldRegion.setExpanded(true);
}
}
});
}
}
use of com.intellij.openapi.editor.FoldRegion in project intellij-community by JetBrains.
the class ExpandRegionAction method expandRegionAtOffset.
public static void expandRegionAtOffset(@NotNull Project project, @NotNull final Editor editor, final int offset) {
CodeFoldingManager foldingManager = CodeFoldingManager.getInstance(project);
foldingManager.updateFoldRegions(editor);
final int line = editor.getDocument().getLineNumber(offset);
Runnable processor = () -> {
FoldRegion region = FoldingUtil.findFoldRegionStartingAtLine(editor, line);
if (region != null && !region.isExpanded()) {
region.setExpanded(true);
} else {
FoldRegion[] regions = FoldingUtil.getFoldRegionsAtOffset(editor, offset);
for (int i = regions.length - 1; i >= 0; i--) {
region = regions[i];
if (!region.isExpanded()) {
region.setExpanded(true);
break;
}
}
}
};
editor.getFoldingModel().runBatchFoldingOperation(processor);
}
use of com.intellij.openapi.editor.FoldRegion in project intellij-community by JetBrains.
the class FoldingUtil method getFoldRegionsAtOffset.
public static FoldRegion[] getFoldRegionsAtOffset(Editor editor, int offset) {
List<FoldRegion> list = new ArrayList<>();
FoldRegion[] allRegions = editor.getFoldingModel().getAllFoldRegions();
for (FoldRegion region : allRegions) {
if (region.getStartOffset() <= offset && offset <= region.getEndOffset()) {
list.add(region);
}
}
FoldRegion[] regions = list.toArray(new FoldRegion[list.size()]);
Arrays.sort(regions, Collections.reverseOrder(RangeMarker.BY_START_OFFSET));
return regions;
}
use of com.intellij.openapi.editor.FoldRegion in project intellij-community by JetBrains.
the class UpdateFoldRegionsOperation method regionOrGroupCanBeRemovedWhenCollapsed.
private boolean regionOrGroupCanBeRemovedWhenCollapsed(FoldRegion region) {
FoldingGroup group = region.getGroup();
List<FoldRegion> affectedRegions = group != null && myEditor instanceof EditorEx ? ((EditorEx) myEditor).getFoldingModel().getGroupedRegions(group) : Collections.singletonList(region);
for (FoldRegion affectedRegion : affectedRegions) {
if (regionCanBeRemovedWhenCollapsed(affectedRegion))
return true;
}
return false;
}
Aggregations