use of com.intellij.openapi.editor.FoldRegion in project intellij-community by JetBrains.
the class CollapseExpandDocCommentsHandler method invoke.
@Override
public void invoke(@NotNull Project project, @NotNull final Editor editor, @NotNull PsiFile file) {
CodeFoldingManager foldingManager = CodeFoldingManager.getInstance(project);
foldingManager.updateFoldRegions(editor);
final FoldRegion[] allFoldRegions = editor.getFoldingModel().getAllFoldRegions();
Runnable processor = () -> {
for (FoldRegion region : allFoldRegions) {
PsiElement element = EditorFoldingInfo.get(editor).getPsiElement(region);
if (element instanceof PsiDocCommentBase) {
region.setExpanded(myExpand);
}
}
};
editor.getFoldingModel().runBatchFoldingOperation(processor);
}
use of com.intellij.openapi.editor.FoldRegion in project intellij-community by JetBrains.
the class CollapseSelectionHandler method invoke.
@Override
public void invoke(@NotNull Project project, @NotNull final Editor editor, @NotNull PsiFile file) {
editor.getFoldingModel().runBatchFoldingOperation(() -> {
final EditorFoldingInfo info = EditorFoldingInfo.get(editor);
FoldingModelEx foldingModel = (FoldingModelEx) editor.getFoldingModel();
if (editor.getSelectionModel().hasSelection()) {
int start = editor.getSelectionModel().getSelectionStart();
int end = editor.getSelectionModel().getSelectionEnd();
if (start + 1 >= end) {
return;
}
Document doc = editor.getDocument();
if (start < end && doc.getCharsSequence().charAt(end - 1) == '\n')
end--;
FoldRegion region;
if ((region = FoldingUtil.findFoldRegion(editor, start, end)) != null) {
if (info.getPsiElement(region) == null) {
editor.getFoldingModel().removeFoldRegion(region);
info.removeRegion(region);
}
} else if (!foldingModel.intersectsRegion(start, end)) {
region = foldingModel.addFoldRegion(start, end, ourPlaceHolderText);
LOG.assertTrue(region != null, "Fold region is not created. Folding model: " + foldingModel);
region.setExpanded(false);
int offset = Math.min(start + ourPlaceHolderText.length(), doc.getTextLength());
editor.getCaretModel().moveToOffset(offset);
}
} else {
FoldRegion[] regions = FoldingUtil.getFoldRegionsAtOffset(editor, editor.getCaretModel().getOffset());
if (regions.length > 0) {
FoldRegion region = regions[0];
if (info.getPsiElement(region) == null) {
editor.getFoldingModel().removeFoldRegion(region);
info.removeRegion(region);
} else {
region.setExpanded(!region.isExpanded());
}
}
}
});
}
use of com.intellij.openapi.editor.FoldRegion in project intellij-community by JetBrains.
the class DocumentFoldingInfo method loadFromEditor.
void loadFromEditor(@NotNull Editor editor) {
assertDispatchThread();
LOG.assertTrue(!editor.isDisposed());
clear();
PsiFile file = PsiDocumentManager.getInstance(myProject).getPsiFile(editor.getDocument());
SmartPointerManager smartPointerManager = SmartPointerManager.getInstance(myProject);
EditorFoldingInfo info = EditorFoldingInfo.get(editor);
FoldRegion[] foldRegions = editor.getFoldingModel().getAllFoldRegions();
for (FoldRegion region : foldRegions) {
if (!region.isValid())
continue;
PsiElement element = info.getPsiElement(region);
boolean expanded = region.isExpanded();
boolean collapseByDefault = element != null && FoldingPolicy.isCollapseByDefault(element) && !FoldingUtil.caretInsideRange(editor, TextRange.create(region));
if (collapseByDefault == expanded || element == null) {
FoldingInfo fi = new FoldingInfo(region.getPlaceholderText(), expanded);
if (element != null) {
myPsiElements.add(smartPointerManager.createSmartPsiElementPointer(element, file));
element.putUserData(FOLDING_INFO_KEY, fi);
} else {
RangeMarker marker = editor.getDocument().createRangeMarker(region.getStartOffset(), region.getEndOffset());
myRangeMarkers.add(marker);
marker.putUserData(FOLDING_INFO_KEY, fi);
}
}
}
}
use of com.intellij.openapi.editor.FoldRegion in project intellij-community by JetBrains.
the class CopyPasteFoldingProcessor method collectTransferableData.
@NotNull
@Override
public List<FoldingTransferableData> collectTransferableData(final PsiFile file, final Editor editor, final int[] startOffsets, final int[] endOffsets) {
// might be slow
//CodeFoldingManager.getInstance(file.getManager().getProject()).updateFoldRegions(editor);
final ArrayList<FoldingData> list = new ArrayList<>();
final FoldRegion[] regions = editor.getFoldingModel().getAllFoldRegions();
for (final FoldRegion region : regions) {
if (!region.isValid())
continue;
int refOffset = 0;
for (int j = 0; j < startOffsets.length; j++) {
refOffset += startOffsets[j];
if (startOffsets[j] <= region.getStartOffset() && region.getEndOffset() <= endOffsets[j]) {
list.add(new FoldingData(// offsets should be relative to clipboard contents start
region.getStartOffset() - refOffset, region.getEndOffset() - refOffset, region.isExpanded()));
break;
}
// 1 accounts for line break inserted between contents corresponding to different carets
refOffset -= endOffsets[j] + 1;
}
}
return Collections.singletonList(new FoldingTransferableData(list.toArray(new FoldingData[list.size()])));
}
use of com.intellij.openapi.editor.FoldRegion in project intellij-community by JetBrains.
the class UnSelectWordHandler method doAction.
private static void doAction(final Editor editor, PsiFile file) {
if (!editor.getSelectionModel().hasSelection()) {
return;
}
CharSequence text = editor.getDocument().getCharsSequence();
int cursorOffset = editor.getCaretModel().getOffset();
if (cursorOffset > 0 && cursorOffset < text.length() && !Character.isJavaIdentifierPart(text.charAt(cursorOffset)) && Character.isJavaIdentifierPart(text.charAt(cursorOffset - 1))) {
cursorOffset--;
}
PsiElement element = file.findElementAt(cursorOffset);
if (element instanceof PsiWhiteSpace && cursorOffset > 0) {
PsiElement anotherElement = file.findElementAt(cursorOffset - 1);
if (!(anotherElement instanceof PsiWhiteSpace)) {
element = anotherElement;
}
}
if (element instanceof PsiWhiteSpace) {
PsiElement nextSibling = element.getNextSibling();
if (nextSibling == null) {
element = element.getParent();
if (element == null || element instanceof PsiFile) {
return;
}
nextSibling = element.getNextSibling();
if (nextSibling == null) {
return;
}
}
element = nextSibling;
cursorOffset = element.getTextRange().getStartOffset();
}
final TextRange selectionRange = new TextRange(editor.getSelectionModel().getSelectionStart(), editor.getSelectionModel().getSelectionEnd());
final Ref<TextRange> maximumRange = new Ref<>();
final int finalCursorOffset = cursorOffset;
SelectWordUtil.processRanges(element, text, cursorOffset, editor, new Processor<TextRange>() {
@Override
public boolean process(TextRange range) {
range = expandToFoldingBoundaries(range);
if (selectionRange.contains(range) && !range.equals(selectionRange) && (range.contains(finalCursorOffset) || finalCursorOffset == range.getEndOffset())) {
if (maximumRange.get() == null || range.contains(maximumRange.get())) {
maximumRange.set(range);
}
}
return false;
}
private TextRange expandToFoldingBoundaries(TextRange range) {
int startOffset = range.getStartOffset();
FoldRegion region = editor.getFoldingModel().getCollapsedRegionAtOffset(startOffset);
if (region != null)
startOffset = region.getStartOffset();
int endOffset = range.getEndOffset();
region = editor.getFoldingModel().getCollapsedRegionAtOffset(endOffset);
if (region != null && endOffset > region.getStartOffset())
endOffset = region.getEndOffset();
return new TextRange(startOffset, endOffset);
}
});
TextRange range = maximumRange.get();
if (range == null) {
editor.getSelectionModel().setSelection(cursorOffset, cursorOffset);
} else {
editor.getSelectionModel().setSelection(range.getStartOffset(), range.getEndOffset());
}
}
Aggregations