use of com.intellij.lang.folding.FoldingDescriptor 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;
}
use of com.intellij.lang.folding.FoldingDescriptor in project intellij-community by JetBrains.
the class JavaFoldingBuilderBase method buildLanguageFoldRegions.
@Override
protected void buildLanguageFoldRegions(@NotNull List<FoldingDescriptor> descriptors, @NotNull PsiElement root, @NotNull Document document, boolean quick) {
if (!(root instanceof PsiJavaFile)) {
return;
}
PsiJavaFile file = (PsiJavaFile) root;
PsiImportList importList = file.getImportList();
if (importList != null) {
PsiImportStatementBase[] statements = importList.getAllImportStatements();
if (statements.length > 1) {
final TextRange rangeToFold = getRangeToFold(importList);
if (rangeToFold != null && rangeToFold.getLength() > 1) {
FoldingDescriptor descriptor = new FoldingDescriptor(importList, rangeToFold);
// imports are often added/removed automatically, so we enable autoupdate of folded region for foldings even if it's collapsed
descriptor.setCanBeRemovedWhenCollapsed(true);
descriptors.add(descriptor);
}
}
}
PsiClass[] classes = file.getClasses();
for (PsiClass aClass : classes) {
ProgressManager.checkCanceled();
ProgressIndicatorProvider.checkCanceled();
addElementsToFold(descriptors, aClass, document, true, quick);
}
TextRange range = getFileHeader(file);
if (range != null && range.getLength() > 1 && document.getLineNumber(range.getEndOffset()) > document.getLineNumber(range.getStartOffset())) {
PsiElement anchorElementToUse = file;
PsiElement candidate = file.getFirstChild();
// So, our point is to preserve fold descriptor referencing javadoc PSI element.
if (candidate != null && candidate.getTextRange().equals(range)) {
ASTNode node = candidate.getNode();
if (node != null && node.getElementType() == JavaDocElementType.DOC_COMMENT) {
anchorElementToUse = candidate;
}
}
descriptors.add(new FoldingDescriptor(anchorElementToUse, range));
}
}
use of com.intellij.lang.folding.FoldingDescriptor in project intellij-community by JetBrains.
the class JavaFoldingBuilderBase method addFoldRegion.
private static boolean addFoldRegion(@NotNull List<FoldingDescriptor> list, @NotNull PsiElement elementToFold, @NotNull Document document, boolean allowOneLiners, @NotNull TextRange range) {
final TextRange fileRange = elementToFold.getContainingFile().getTextRange();
if (range.equals(fileRange))
return false;
LOG.assertTrue(range.getStartOffset() >= 0 && range.getEndOffset() <= fileRange.getEndOffset());
// PSI element text ranges may be invalid because of reparse exception (see, for example, IDEA-10617)
if (range.getStartOffset() < 0 || range.getEndOffset() > fileRange.getEndOffset()) {
return false;
}
if (!allowOneLiners) {
int startLine = document.getLineNumber(range.getStartOffset());
int endLine = document.getLineNumber(range.getEndOffset() - 1);
if (startLine < endLine && range.getLength() > 1) {
list.add(new FoldingDescriptor(elementToFold, range));
return true;
}
return false;
} else {
if (range.getLength() > getPlaceholderText(elementToFold).length()) {
list.add(new FoldingDescriptor(elementToFold, range));
return true;
}
return false;
}
}
use of com.intellij.lang.folding.FoldingDescriptor in project intellij-community by JetBrains.
the class DocumentFoldingInfo method buildRanges.
@NotNull
private static Map<PsiElement, FoldingDescriptor> buildRanges(@NotNull Editor editor, @NotNull PsiFile psiFile) {
final FoldingBuilder foldingBuilder = LanguageFolding.INSTANCE.forLanguage(psiFile.getLanguage());
final ASTNode node = psiFile.getNode();
if (node == null)
return Collections.emptyMap();
final FoldingDescriptor[] descriptors = LanguageFolding.buildFoldingDescriptors(foldingBuilder, psiFile, editor.getDocument(), true);
Map<PsiElement, FoldingDescriptor> ranges = new HashMap<>();
for (FoldingDescriptor descriptor : descriptors) {
final ASTNode ast = descriptor.getElement();
final PsiElement psi = ast.getPsi();
if (psi != null) {
ranges.put(psi, descriptor);
}
}
return ranges;
}
use of com.intellij.lang.folding.FoldingDescriptor in project intellij-community by JetBrains.
the class XmlCodeFoldingBuilder method addToFold.
protected boolean addToFold(List<FoldingDescriptor> foldings, PsiElement elementToFold, Document document) {
PsiUtilCore.ensureValid(elementToFold);
TextRange range = getRangeToFold(elementToFold);
if (range == null)
return false;
if (range.getStartOffset() >= 0 && range.getEndOffset() <= elementToFold.getContainingFile().getTextRange().getEndOffset() && // psi and document maybe not in sync after error
range.getEndOffset() <= document.getTextLength()) {
int startLine = document.getLineNumber(range.getStartOffset());
int endLine = document.getLineNumber(range.getEndOffset() - 1);
final boolean entity = isEntity(elementToFold);
if (startLine < endLine || elementToFold instanceof XmlAttribute || entity) {
if (range.getStartOffset() + MIN_TEXT_RANGE_LENGTH < range.getEndOffset() || entity) {
foldings.add(new FoldingDescriptor(elementToFold.getNode(), range));
return true;
}
}
}
return false;
}
Aggregations