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 PythonFoldingBuilder method foldCollectionLiteral.
private static void foldCollectionLiteral(ASTNode node, List<FoldingDescriptor> descriptors) {
if (StringUtil.countNewLines(node.getChars()) > 0) {
TextRange range = node.getTextRange();
int delta = node.getElementType() == PyElementTypes.TUPLE_EXPRESSION ? 0 : 1;
descriptors.add(new FoldingDescriptor(node, TextRange.create(range.getStartOffset() + delta, range.getEndOffset() - delta)));
}
}
use of com.intellij.lang.folding.FoldingDescriptor in project intellij-community by JetBrains.
the class PythonFoldingBuilder method appendDescriptors.
private static void appendDescriptors(ASTNode node, List<FoldingDescriptor> descriptors) {
IElementType elementType = node.getElementType();
if (elementType instanceof PyFileElementType) {
final List<PyImportStatementBase> imports = ((PyFile) node.getPsi()).getImportBlock();
if (imports.size() > 1) {
final PyImportStatementBase firstImport = imports.get(0);
final PyImportStatementBase lastImport = imports.get(imports.size() - 1);
descriptors.add(new FoldingDescriptor(firstImport, new TextRange(firstImport.getTextRange().getStartOffset(), lastImport.getTextRange().getEndOffset())));
}
} else if (elementType == PyElementTypes.STATEMENT_LIST) {
foldStatementList(node, descriptors);
} else if (elementType == PyElementTypes.STRING_LITERAL_EXPRESSION) {
foldLongStrings(node, descriptors);
} else if (FOLDABLE_COLLECTIONS_LITERALS.contains(elementType)) {
foldCollectionLiteral(node, descriptors);
} else if (elementType == PyTokenTypes.END_OF_LINE_COMMENT) {
foldSequentialComments(node, descriptors);
}
ASTNode child = node.getFirstChildNode();
while (child != null) {
appendDescriptors(child, descriptors);
child = child.getTreeNext();
}
}
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 RncFoldingBuilder method process.
private static void process(@Nullable ASTNode node, Document document, ArrayList<FoldingDescriptor> regions) {
if (node == null) {
return;
}
final ASTNode[] braces = node.getChildren(RncTokenTypes.BRACES);
if (braces.length == 2) {
final ASTNode lbrace = braces[0];
final ASTNode rbrace = braces[1];
if (shouldFold(lbrace, rbrace, document)) {
final TextRange range = new TextRange(lbrace.getStartOffset(), rbrace.getTextRange().getEndOffset());
regions.add(new FoldingDescriptor(lbrace, range));
}
} else if (isAnnotation(node.getElementType())) {
if (isOnDifferentLine(node.getFirstChildNode(), node.getLastChildNode(), document)) {
regions.add(new FoldingDescriptor(node, node.getTextRange()));
}
}
node = node.getFirstChildNode();
while (node != null) {
node = checkNodeAndSiblings(node, RncTokenTypes.DOC_TOKENS, regions, document);
node = checkNodeAndSiblings(node, RncTokenTypes.COMMENTS, regions, document);
process(node, document, regions);
if (node != null) {
node = node.getTreeNext();
}
}
}
Aggregations