use of com.intellij.openapi.editor.ex.FoldingModelEx in project intellij-community by JetBrains.
the class FindManagerImpl method expandFoldRegionsIfNecessary.
private static void expandFoldRegionsIfNecessary(@NotNull Editor editor, final int startOffset, int endOffset) {
final FoldingModel foldingModel = editor.getFoldingModel();
final FoldRegion[] regions;
if (foldingModel instanceof FoldingModelEx) {
regions = ((FoldingModelEx) foldingModel).fetchTopLevel();
} else {
regions = foldingModel.getAllFoldRegions();
}
if (regions == null) {
return;
}
int i = Arrays.binarySearch(regions, null, (o1, o2) -> {
// Find the first region that ends after the given start offset
if (o1 == null) {
return startOffset - o2.getEndOffset();
} else {
return o1.getEndOffset() - startOffset;
}
});
if (i < 0) {
i = -i - 1;
} else {
// Don't expand fold region that ends at the start offset.
i++;
}
if (i >= regions.length) {
return;
}
final List<FoldRegion> toExpand = new ArrayList<>();
for (; i < regions.length; i++) {
final FoldRegion region = regions[i];
if (region.getStartOffset() >= endOffset) {
break;
}
if (!region.isExpanded()) {
toExpand.add(region);
}
}
if (toExpand.isEmpty()) {
return;
}
foldingModel.runBatchFoldingOperation(() -> {
for (FoldRegion region : toExpand) {
region.setExpanded(true);
}
});
}
use of com.intellij.openapi.editor.ex.FoldingModelEx in project intellij-community by JetBrains.
the class FoldingModelSupport method expandAll.
//
// Synchronized toggling of ranges
//
public void expandAll(final boolean expanded) {
if (myDuringSynchronize)
return;
myDuringSynchronize = true;
try {
for (int i = 0; i < myCount; i++) {
final int index = i;
final FoldingModelEx model = myEditors[index].getFoldingModel();
model.runBatchFoldingOperation(() -> {
for (FoldedBlock folding : getFoldedBlocks()) {
FoldRegion region = folding.getRegion(index);
if (region != null)
region.setExpanded(expanded);
}
});
}
} finally {
myDuringSynchronize = false;
}
}
use of com.intellij.openapi.editor.ex.FoldingModelEx in project intellij-community by JetBrains.
the class EditorFragmentComponent method doInit.
private void doInit(Component anchorComponent, EditorEx editor, int startLine, int endLine, boolean showFolding, boolean showGutter) {
Document doc = editor.getDocument();
final int endOffset = endLine < doc.getLineCount() ? doc.getLineEndOffset(endLine) : doc.getTextLength();
boolean newRendering = editor instanceof EditorImpl;
int widthAdjustment = newRendering ? EditorUtil.getSpaceWidth(Font.PLAIN, editor) : 0;
final int textImageWidth = Math.min(editor.getMaxWidthInRange(doc.getLineStartOffset(startLine), endOffset) + widthAdjustment, getWidthLimit(editor));
FoldingModelEx foldingModel = editor.getFoldingModel();
boolean isFoldingEnabled = foldingModel.isFoldingEnabled();
if (!showFolding) {
foldingModel.setFoldingEnabled(false);
}
Point p1 = editor.logicalPositionToXY(new LogicalPosition(startLine, 0));
Point p2 = editor.logicalPositionToXY(new LogicalPosition(Math.max(endLine, startLine + 1), 0));
int y1 = p1.y;
int y2 = p2.y;
final int textImageHeight = y2 - y1 == 0 ? editor.getLineHeight() : y2 - y1;
LOG.assertTrue(textImageHeight > 0, "Height: " + textImageHeight + "; startLine:" + startLine + "; endLine:" + endLine + "; p1:" + p1 + "; p2:" + p2);
int savedScrollOffset = newRendering ? 0 : editor.getScrollingModel().getHorizontalScrollOffset();
if (savedScrollOffset > 0) {
editor.getScrollingModel().scrollHorizontally(0);
}
final BufferedImage textImage = UIUtil.createImage(anchorComponent == null ? editor.getContentComponent() : anchorComponent, textImageWidth, textImageHeight, BufferedImage.TYPE_INT_RGB);
Graphics textGraphics = textImage.getGraphics();
EditorUIUtil.setupAntialiasing(textGraphics);
final JComponent rowHeader;
final BufferedImage markersImage;
final int markersImageWidth;
if (showGutter) {
rowHeader = editor.getGutterComponentEx();
markersImageWidth = Math.max(1, rowHeader.getWidth());
markersImage = UIUtil.createImage(editor.getComponent(), markersImageWidth, textImageHeight, BufferedImage.TYPE_INT_RGB);
Graphics markerGraphics = markersImage.getGraphics();
EditorUIUtil.setupAntialiasing(markerGraphics);
markerGraphics.translate(0, -y1);
markerGraphics.setClip(0, y1, rowHeader.getWidth(), textImageHeight);
markerGraphics.setColor(getBackgroundColor(editor));
markerGraphics.fillRect(0, y1, rowHeader.getWidth(), textImageHeight);
rowHeader.paint(markerGraphics);
} else {
markersImageWidth = 0;
rowHeader = null;
markersImage = null;
}
textGraphics.translate(0, -y1);
textGraphics.setClip(0, y1, textImageWidth, textImageHeight);
final boolean wasVisible = editor.setCaretVisible(false);
editor.getContentComponent().paint(textGraphics);
if (wasVisible) {
editor.setCaretVisible(true);
}
if (!showFolding) {
foldingModel.setFoldingEnabled(isFoldingEnabled);
}
if (savedScrollOffset > 0) {
editor.getScrollingModel().scrollHorizontally(savedScrollOffset);
}
JComponent component = new JComponent() {
@Override
public Dimension getPreferredSize() {
return new Dimension(textImageWidth + markersImageWidth, textImageHeight);
}
@Override
protected void paintComponent(Graphics graphics) {
if (markersImage != null) {
UIUtil.drawImage(graphics, markersImage, 0, 0, null);
UIUtil.drawImage(graphics, textImage, rowHeader.getWidth(), 0, null);
} else {
UIUtil.drawImage(graphics, textImage, 0, 0, null);
}
}
};
setLayout(new BorderLayout());
add(component);
final Color borderColor = editor.getColorsScheme().getColor(EditorColors.SELECTED_TEARLINE_COLOR);
Border outsideBorder = JBUI.Borders.customLine(borderColor, LINE_BORDER_THICKNESS);
Border insideBorder = JBUI.Borders.empty(EMPTY_BORDER_THICKNESS, EMPTY_BORDER_THICKNESS);
setBorder(BorderFactory.createCompoundBorder(outsideBorder, insideBorder));
}
use of com.intellij.openapi.editor.ex.FoldingModelEx in project intellij-community by JetBrains.
the class CaretImpl method getLeadSelectionOffset.
@Override
public int getLeadSelectionOffset() {
validateContext(false);
int caretOffset = getOffset();
if (hasSelection()) {
RangeMarker marker = mySelectionMarker;
if (marker != null && marker.isValid()) {
int startOffset = marker.getStartOffset();
int endOffset = marker.getEndOffset();
if (caretOffset != startOffset && caretOffset != endOffset) {
// Try to check if current selection is tweaked by fold region.
FoldingModelEx foldingModel = myEditor.getFoldingModel();
FoldRegion foldRegion = foldingModel.getCollapsedRegionAtOffset(caretOffset);
if (foldRegion != null) {
if (foldRegion.getStartOffset() == startOffset) {
return endOffset;
} else if (foldRegion.getEndOffset() == endOffset) {
return startOffset;
}
}
}
if (caretOffset == endOffset) {
return startOffset;
} else {
return endOffset;
}
}
}
return caretOffset;
}
use of com.intellij.openapi.editor.ex.FoldingModelEx 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]);
}
Aggregations