use of com.intellij.lang.folding.FoldingDescriptor in project intellij-community by JetBrains.
the class PythonFoldingBuilder method foldSequentialComments.
private static void foldSequentialComments(ASTNode node, List<FoldingDescriptor> descriptors) {
//do not start folded comments from custom region
if (isCustomRegionElement(node.getPsi())) {
return;
}
//need to skip previous comments in sequence
ASTNode curNode = node.getTreePrev();
while (curNode != null) {
if (curNode.getElementType() == PyTokenTypes.END_OF_LINE_COMMENT) {
if (isCustomRegionElement(curNode.getPsi())) {
break;
}
return;
}
curNode = curNode.getPsi() instanceof PsiWhiteSpace ? curNode.getTreePrev() : null;
}
//fold sequence comments in one block
curNode = node.getTreeNext();
ASTNode lastCommentNode = node;
while (curNode != null) {
if (curNode.getElementType() == PyTokenTypes.END_OF_LINE_COMMENT) {
//do not end folded comments with custom region
if (isCustomRegionElement(curNode.getPsi())) {
break;
}
lastCommentNode = curNode;
curNode = curNode.getTreeNext();
continue;
}
curNode = curNode.getPsi() instanceof PsiWhiteSpace ? curNode.getTreeNext() : null;
}
if (lastCommentNode != node) {
descriptors.add(new FoldingDescriptor(node, TextRange.create(node.getStartOffset(), lastCommentNode.getTextRange().getEndOffset())));
}
}
use of com.intellij.lang.folding.FoldingDescriptor in project intellij-community by JetBrains.
the class PropertyFoldingBuilder method checkLiteral.
private static void checkLiteral(PsiLiteralExpression expression, List<FoldingDescriptor> result) {
if (isI18nProperty(expression)) {
final IProperty property = getI18nProperty(expression);
final HashSet<Object> set = new HashSet<>();
set.add(property != null ? property : PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT);
final String msg = formatI18nProperty(expression, property);
final PsiElement parent = expression.getParent();
if (!msg.equals(expression.getText()) && parent instanceof PsiExpressionList && ((PsiExpressionList) parent).getExpressions()[0] == expression) {
final PsiExpressionList expressions = (PsiExpressionList) parent;
final int count = JavaI18nUtil.getPropertyValueParamsMaxCount(expression);
final PsiExpression[] args = expressions.getExpressions();
if (args.length == 1 + count && parent.getParent() instanceof PsiMethodCallExpression) {
boolean ok = true;
for (int i = 1; i < count + 1; i++) {
Object value = JavaConstantExpressionEvaluator.computeConstantExpression(args[i], false);
if (value == null) {
if (!(args[i] instanceof PsiReferenceExpression)) {
ok = false;
break;
}
}
}
if (ok) {
result.add(new FoldingDescriptor(ObjectUtils.assertNotNull(parent.getParent().getNode()), parent.getParent().getTextRange(), null, set));
return;
}
}
}
result.add(new FoldingDescriptor(ObjectUtils.assertNotNull(expression.getNode()), expression.getTextRange(), null, set));
}
}
use of com.intellij.lang.folding.FoldingDescriptor in project intellij-community by JetBrains.
the class FoldingUpdate method getUpdateResult.
private static CachedValueProvider.Result<Runnable> getUpdateResult(PsiFile file, @NotNull Document document, boolean quick, final Project project, final Editor editor, final boolean applyDefaultState) {
final FoldingMap elementsToFoldMap = getFoldingsFor(file, document, quick);
final UpdateFoldRegionsOperation operation = new UpdateFoldRegionsOperation(project, editor, file, elementsToFoldMap, applyDefaultState ? EXCEPT_CARET_REGION : NO, !applyDefaultState, false);
Runnable runnable = () -> editor.getFoldingModel().runBatchFoldingOperationDoNotCollapseCaret(operation);
Set<Object> dependencies = new HashSet<>();
dependencies.add(document);
dependencies.add(editor.getFoldingModel());
for (FoldingDescriptor descriptor : elementsToFoldMap.values()) {
dependencies.addAll(descriptor.getDependencies());
}
return CachedValueProvider.Result.create(runnable, ArrayUtil.toObjectArray(dependencies));
}
use of com.intellij.lang.folding.FoldingDescriptor in project intellij-community by JetBrains.
the class FoldingUpdate method getFoldingsFor.
private static void getFoldingsFor(@NotNull PsiFile file, @NotNull Document document, @NotNull FoldingMap elementsToFoldMap, boolean quick) {
final FileViewProvider viewProvider = file.getViewProvider();
TextRange docRange = TextRange.from(0, document.getTextLength());
for (final Language language : viewProvider.getLanguages()) {
final PsiFile psi = viewProvider.getPsi(language);
final FoldingBuilder foldingBuilder = LanguageFolding.INSTANCE.forLanguage(language);
if (psi != null && foldingBuilder != null) {
for (FoldingDescriptor descriptor : LanguageFolding.buildFoldingDescriptors(foldingBuilder, psi, document, quick)) {
PsiElement psiElement = descriptor.getElement().getPsi();
if (psiElement == null) {
LOG.error("No PSI for folding descriptor " + descriptor);
continue;
}
if (!docRange.contains(descriptor.getRange())) {
diagnoseIncorrectRange(psi, document, language, foldingBuilder, descriptor, psiElement);
continue;
}
elementsToFoldMap.putValue(psiElement, descriptor);
}
}
}
}
use of com.intellij.lang.folding.FoldingDescriptor 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);
}
}
Aggregations