use of com.intellij.openapi.editor.FoldRegion in project intellij-community by JetBrains.
the class UpdateFoldRegionsOperation method applyExpandStatus.
private static void applyExpandStatus(@NotNull List<FoldRegion> newRegions, @NotNull Map<FoldRegion, Boolean> shouldExpand, @NotNull Map<FoldingGroup, Boolean> groupExpand) {
for (final FoldRegion region : newRegions) {
final FoldingGroup group = region.getGroup();
final Boolean expanded = group == null ? shouldExpand.get(region) : groupExpand.get(group);
if (expanded != null) {
region.setExpanded(expanded.booleanValue());
}
}
}
use of com.intellij.openapi.editor.FoldRegion in project intellij-community by JetBrains.
the class UpdateFoldRegionsOperation method removeInvalidRegions.
private void removeInvalidRegions(@NotNull EditorFoldingInfo info, @NotNull FoldingModelEx foldingModel, FoldingUpdate.FoldingMap elementsToFold, @NotNull Map<TextRange, Boolean> rangeToExpandStatusMap) {
List<FoldRegion> toRemove = new ArrayList<>();
InjectedLanguageManager injectedManager = InjectedLanguageManager.getInstance(myProject);
for (FoldRegion region : foldingModel.getAllFoldRegions()) {
if (myKeepCollapsedRegions && !region.isExpanded() && !regionOrGroupCanBeRemovedWhenCollapsed(region))
continue;
PsiElement element = info.getPsiElement(region);
if (element != null) {
PsiFile containingFile = element.getContainingFile();
boolean isInjected = injectedManager.isInjectedFragment(containingFile);
if (isInjected != myForInjected)
continue;
}
final Collection<FoldingDescriptor> descriptors;
if (element != null && !(descriptors = elementsToFold.get(element)).isEmpty()) {
boolean matchingDescriptorFound = false;
FoldingDescriptor[] array = descriptors.toArray(new FoldingDescriptor[descriptors.size()]);
for (FoldingDescriptor descriptor : array) {
TextRange range = descriptor.getRange();
if (TextRange.areSegmentsEqual(region, range)) {
matchingDescriptorFound = true;
if (!region.isValid() || region.getGroup() != null || descriptor.getGroup() != null || !region.getPlaceholderText().equals(descriptor.getPlaceholderText()) || range.getLength() < 2) {
rangeToExpandStatusMap.put(range, region.isExpanded());
toRemove.add(region);
break;
} else {
elementsToFold.remove(element, descriptor);
}
}
}
if (!matchingDescriptorFound) {
if (Registry.is("editor.durable.folding.state")) {
for (FoldingDescriptor descriptor : descriptors) {
rangeToExpandStatusMap.put(descriptor.getRange(), region.isExpanded());
}
}
toRemove.add(region);
}
} else if (region.isValid() && info.isLightRegion(region)) {
boolean isExpanded = region.isExpanded();
rangeToExpandStatusMap.put(TextRange.create(region), isExpanded);
} else {
toRemove.add(region);
}
}
for (final FoldRegion region : toRemove) {
foldingModel.removeFoldRegion(region);
info.removeRegion(region);
}
}
use of com.intellij.openapi.editor.FoldRegion in project intellij-community by JetBrains.
the class BaseFoldingHandler method getFoldRegionsForCaret.
/**
* Returns a region corresponding to current caret position, and all regions contained in it.
*/
protected List<FoldRegion> getFoldRegionsForCaret(@NotNull Editor editor, @Nullable Caret caret, boolean toCollapse) {
if (caret == null) {
caret = editor.getCaretModel().getPrimaryCaret();
}
int offset = caret.getOffset();
FoldRegion rootRegion = FoldingUtil.findFoldRegionStartingAtLine(editor, editor.getDocument().getLineNumber(offset));
if (rootRegion == null || toCollapse && !rootRegion.isExpanded()) {
rootRegion = null;
FoldRegion[] regions = FoldingUtil.getFoldRegionsAtOffset(editor, offset);
for (FoldRegion region : regions) {
if (region.isExpanded() == toCollapse) {
rootRegion = region;
break;
}
}
}
List<FoldRegion> result = new ArrayList<>();
if (rootRegion != null) {
FoldRegion[] allRegions = editor.getFoldingModel().getAllFoldRegions();
for (FoldRegion region : allRegions) {
if (region.getStartOffset() >= rootRegion.getStartOffset() && region.getEndOffset() <= rootRegion.getEndOffset()) {
result.add(region);
}
}
}
return result;
}
use of com.intellij.openapi.editor.FoldRegion in project intellij-community by JetBrains.
the class CollapseSelectionHandler method isEnabled.
public static boolean isEnabled(@NotNull Editor editor) {
if (editor.getSelectionModel().hasSelection()) {
int start = editor.getSelectionModel().getSelectionStart();
int end = editor.getSelectionModel().getSelectionEnd();
if (start + 1 >= end) {
return false;
}
if (start < end && editor.getDocument().getCharsSequence().charAt(end - 1) == '\n')
end--;
FoldRegion region = FoldingUtil.findFoldRegion(editor, start, end);
if (region == null) {
return !((FoldingModelEx) editor.getFoldingModel()).intersectsRegion(start, end);
} else {
return EditorFoldingInfo.get(editor).getPsiElement(region) == null;
}
} else {
return FoldingUtil.getFoldRegionsAtOffset(editor, editor.getCaretModel().getOffset()).length > 0;
}
}
use of com.intellij.openapi.editor.FoldRegion in project intellij-community by JetBrains.
the class DocumentFoldingInfo method setToEditor.
@Override
public void setToEditor(@NotNull final Editor editor) {
assertDispatchThread();
final PsiManager psiManager = PsiManager.getInstance(myProject);
if (psiManager.isDisposed())
return;
if (!myFile.isValid())
return;
final PsiFile psiFile = psiManager.findFile(myFile);
if (psiFile == null)
return;
if (!mySerializedElements.isEmpty()) {
// Restore postponed state
assert myPsiElements.isEmpty() : "Sequential deserialization";
for (SerializedPsiElement entry : mySerializedElements) {
PsiElement restoredElement = FoldingPolicy.restoreBySignature(psiFile, entry.mySerializedElement);
if (restoredElement != null && restoredElement.isValid()) {
myPsiElements.add(SmartPointerManager.getInstance(myProject).createSmartPsiElementPointer(restoredElement));
restoredElement.putUserData(FOLDING_INFO_KEY, entry.myFoldingInfo);
}
}
mySerializedElements.clear();
}
Map<PsiElement, FoldingDescriptor> ranges = null;
for (SmartPsiElementPointer<PsiElement> ptr : myPsiElements) {
PsiElement element = ptr.getElement();
if (element == null || !element.isValid()) {
continue;
}
if (ranges == null) {
ranges = buildRanges(editor, psiFile);
}
FoldingDescriptor descriptor = ranges.get(element);
if (descriptor == null) {
continue;
}
TextRange range = descriptor.getRange();
FoldRegion region = FoldingUtil.findFoldRegion(editor, range.getStartOffset(), range.getEndOffset());
if (region != null) {
FoldingInfo fi = element.getUserData(FOLDING_INFO_KEY);
boolean state = fi != null && fi.expanded;
region.setExpanded(state);
}
}
for (RangeMarker marker : myRangeMarkers) {
if (!marker.isValid() || marker.getStartOffset() == marker.getEndOffset()) {
continue;
}
FoldRegion region = FoldingUtil.findFoldRegion(editor, marker.getStartOffset(), marker.getEndOffset());
FoldingInfo info = marker.getUserData(FOLDING_INFO_KEY);
if (region == null) {
if (info != null) {
region = editor.getFoldingModel().addFoldRegion(marker.getStartOffset(), marker.getEndOffset(), info.placeHolder);
}
if (region == null) {
return;
}
}
boolean state = info != null && info.expanded;
region.setExpanded(state);
}
}
Aggregations