use of com.intellij.openapi.editor.FoldingGroup in project intellij-community by JetBrains.
the class ClosureFolding method createDescriptors.
@Nullable
private List<NamedFoldingDescriptor> createDescriptors(PsiElement classRBrace, int rangeStart, int rangeEnd, String header, String footer) {
if (rangeStart >= rangeEnd)
return null;
FoldingGroup group = FoldingGroup.newGroup("lambda");
List<NamedFoldingDescriptor> foldElements = new ArrayList<>();
foldElements.add(new NamedFoldingDescriptor(myNewExpression, getClosureStartOffset(), rangeStart, group, header));
if (rangeEnd + 1 < getClosureEndOffset()) {
foldElements.add(new NamedFoldingDescriptor(classRBrace, rangeEnd, getClosureEndOffset(), group, footer));
}
return foldElements;
}
use of com.intellij.openapi.editor.FoldingGroup in project intellij-community by JetBrains.
the class JavaFoldingBuilderBase method addOneLineMethodFolding.
private boolean addOneLineMethodFolding(@NotNull List<FoldingDescriptor> descriptorList, @NotNull PsiMethod method) {
if (!JavaCodeFoldingSettings.getInstance().isCollapseOneLineMethods()) {
return false;
}
Document document = method.getContainingFile().getViewProvider().getDocument();
PsiCodeBlock body = method.getBody();
PsiIdentifier nameIdentifier = method.getNameIdentifier();
if (body == null || document == null || nameIdentifier == null) {
return false;
}
if (document.getLineNumber(nameIdentifier.getTextRange().getStartOffset()) != document.getLineNumber(method.getParameterList().getTextRange().getEndOffset())) {
return false;
}
PsiJavaToken lBrace = body.getLBrace();
PsiJavaToken rBrace = body.getRBrace();
PsiStatement[] statements = body.getStatements();
if (lBrace == null || rBrace == null || statements.length != 1) {
return false;
}
PsiStatement statement = statements[0];
if (statement.textContains('\n')) {
return false;
}
if (!areOnAdjacentLines(lBrace, statement, document) || !areOnAdjacentLines(statement, rBrace, document)) {
//the user might intend to type at an empty line
return false;
}
int leftStart = method.getParameterList().getTextRange().getEndOffset();
int bodyStart = body.getTextRange().getStartOffset();
if (bodyStart > leftStart && !StringUtil.isEmptyOrSpaces(document.getCharsSequence().subSequence(leftStart + 1, bodyStart))) {
return false;
}
int leftEnd = statement.getTextRange().getStartOffset();
int rightStart = statement.getTextRange().getEndOffset();
int rightEnd = body.getTextRange().getEndOffset();
if (leftEnd <= leftStart + 1 || rightEnd <= rightStart + 1) {
return false;
}
String leftText = " { ";
String rightText = " }";
if (!fitsRightMargin(method, document, leftStart, rightEnd, rightStart - leftEnd + leftText.length() + rightText.length())) {
return false;
}
FoldingGroup group = FoldingGroup.newGroup("one-liner");
descriptorList.add(new NamedFoldingDescriptor(lBrace, leftStart, leftEnd, group, leftText));
descriptorList.add(new NamedFoldingDescriptor(rBrace, rightStart, rightEnd, group, rightText));
return true;
}
use of com.intellij.openapi.editor.FoldingGroup in project intellij-community by JetBrains.
the class FoldingAnchorsOverlayStrategy method getAnchorsToDisplay.
@NotNull
Collection<DisplayedFoldingAnchor> getAnchorsToDisplay(int firstVisibleOffset, int lastVisibleOffset, FoldRegion activeFoldRegion) {
Map<Integer, DisplayedFoldingAnchor> result = new HashMap<>();
FoldRegion[] visibleFoldRegions = myEditor.getFoldingModel().fetchVisible();
for (FoldRegion region : visibleFoldRegions) {
if (!region.isValid())
continue;
final int startOffset = region.getStartOffset();
if (startOffset > lastVisibleOffset)
continue;
final int endOffset = getEndOffset(region);
if (endOffset < firstVisibleOffset)
continue;
if (!isFoldingPossible(startOffset, endOffset))
continue;
final FoldingGroup group = region.getGroup();
if (group != null && myEditor.getFoldingModel().getFirstRegion(group, region) != region)
continue;
//offset = Math.min(myEditor.getDocument().getTextLength() - 1, offset);
int foldStart = myEditor.offsetToVisualLine(startOffset);
if (!region.isExpanded()) {
tryAdding(result, region, foldStart, 0, DisplayedFoldingAnchor.Type.COLLAPSED, activeFoldRegion);
} else {
//offset = Math.min(myEditor.getDocument().getTextLength() - 1, offset);
int foldEnd = myEditor.offsetToVisualLine(endOffset);
tryAdding(result, region, foldStart, foldEnd - foldStart, DisplayedFoldingAnchor.Type.EXPANDED_TOP, activeFoldRegion);
tryAdding(result, region, foldEnd, foldEnd - foldStart, DisplayedFoldingAnchor.Type.EXPANDED_BOTTOM, activeFoldRegion);
}
}
return result.values();
}
use of com.intellij.openapi.editor.FoldingGroup 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;
}
use of com.intellij.openapi.editor.FoldingGroup 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;
}
Aggregations