use of com.intellij.lang.folding.FoldingDescriptor in project intellij-community by JetBrains.
the class GroovyFoldingBuilder method processImports.
private static void processImports(final List<FoldingDescriptor> descriptors, GrImportStatement[] imports) {
if (imports.length < 2)
return;
PsiElement first = imports[0];
while (first != null) {
PsiElement marker = first;
PsiElement next = first.getNextSibling();
while (next instanceof GrImportStatement || next instanceof LeafPsiElement) {
if (next instanceof GrImportStatement)
marker = next;
next = next.getNextSibling();
}
if (marker != first) {
int start = first.getTextRange().getStartOffset();
int end = marker.getTextRange().getEndOffset();
int tail = "import ".length();
if (start + tail < end && !JavaFoldingBuilderBase.hasErrorElementsNearby(first.getContainingFile(), start, end)) {
FoldingDescriptor descriptor = new FoldingDescriptor(first.getNode(), new TextRange(start + tail, end));
// 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);
}
}
while (!(next instanceof GrImportStatement) && next != null) next = next.getNextSibling();
first = next;
}
}
use of com.intellij.lang.folding.FoldingDescriptor in project intellij-community by JetBrains.
the class GroovyFoldingBuilder method collapseBlock.
private static void collapseBlock(List<FoldingDescriptor> descriptors, PsiElement psi) {
if (psi instanceof GrCodeBlock) {
final int lineFeedCount = StringUtil.countChars(psi.getText(), '\n');
if (lineFeedCount <= 2) {
final PsiElement lbrace = ((GrCodeBlock) psi).getLBrace();
final PsiElement rbrace = ((GrCodeBlock) psi).getRBrace();
if (lbrace != null && rbrace != null) {
final PsiElement next = lbrace.getNextSibling();
final PsiElement prev = rbrace.getPrevSibling();
if (next != null && PsiImplUtil.isWhiteSpaceOrNls(next) && prev != null && PsiImplUtil.isWhiteSpaceOrNls(prev)) {
final FoldingGroup group = FoldingGroup.newGroup("block_group");
descriptors.add(new NamedFoldingDescriptor(psi, lbrace.getTextRange().getStartOffset(), next.getTextRange().getEndOffset(), group, "{"));
descriptors.add(new NamedFoldingDescriptor(psi, prev.getTextRange().getStartOffset(), rbrace.getTextRange().getEndOffset(), group, "}"));
return;
}
}
}
}
descriptors.add(new FoldingDescriptor(psi, psi.getTextRange()));
}
use of com.intellij.lang.folding.FoldingDescriptor in project intellij-community by JetBrains.
the class GroovyFoldingBuilder method appendDescriptors.
private void appendDescriptors(PsiElement element, List<FoldingDescriptor> descriptors, Set<PsiElement> usedComments) {
ASTNode node = element.getNode();
if (node == null)
return;
IElementType type = node.getElementType();
if (TokenSets.BLOCK_SET.contains(type) && !isSingleHighLevelClassBody(element) || type == GroovyElementTypes.CLOSABLE_BLOCK) {
if (isMultiline(element)) {
collapseBlock(descriptors, element);
}
}
// comments
if ((type.equals(GroovyTokenTypes.mML_COMMENT) || type.equals(GroovyDocElementTypes.GROOVY_DOC_COMMENT)) && isMultiline(element) && isWellEndedComment(element)) {
descriptors.add(new FoldingDescriptor(node, node.getTextRange()));
}
if (type.equals(GroovyTokenTypes.mSL_COMMENT) && !usedComments.contains(element)) {
boolean containsCustomRegionMarker = isCustomRegionElement(element);
usedComments.add(element);
PsiElement end = null;
for (PsiElement current = element.getNextSibling(); current != null; current = current.getNextSibling()) {
if (PsiImplUtil.isWhiteSpaceOrNls(current))
continue;
IElementType elementType = current.getNode().getElementType();
if (elementType == GroovyTokenTypes.mSL_COMMENT) {
end = current;
usedComments.add(current);
containsCustomRegionMarker |= isCustomRegionElement(current);
continue;
}
break;
}
if (end != null && !containsCustomRegionMarker) {
final TextRange range = new TextRange(element.getTextRange().getStartOffset(), end.getTextRange().getEndOffset());
descriptors.add(new FoldingDescriptor(element, range));
}
}
//multiline strings
addFoldingForStrings(descriptors, node);
Set<PsiElement> newUsedComments = new HashSet<>();
for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
appendDescriptors(child, descriptors, newUsedComments);
}
if (element instanceof GroovyFile) {
processImports(descriptors, ((GroovyFile) element).getImportStatements());
}
}
use of com.intellij.lang.folding.FoldingDescriptor in project android by JetBrains.
the class ResourceFoldingBuilder method createdInlinedResource.
private static InlinedResource createdInlinedResource(@NotNull ResourceType type, @NotNull String name, @NotNull PsiElement foldElement) {
// Not part of a call: just fold the R.string reference itself
LocalResourceRepository appResources = getAppResources(foldElement);
if (appResources != null && appResources.hasResourceItem(type, name)) {
ASTNode node = foldElement.getNode();
if (node != null) {
TextRange textRange = foldElement.getTextRange();
HashSet<Object> dependencies = new HashSet<Object>();
dependencies.add(foldElement);
FoldingDescriptor descriptor = new FoldingDescriptor(node, textRange, null, dependencies);
InlinedResource inlinedResource = new InlinedResource(type, name, appResources, descriptor, foldElement);
dependencies.add(inlinedResource);
return inlinedResource;
}
}
return NONE;
}
use of com.intellij.lang.folding.FoldingDescriptor in project intellij-plugins by JetBrains.
the class CfmlFoldingBuilder method addFoldingDescriptors.
private static void addFoldingDescriptors(final List<FoldingDescriptor> descriptors, final PsiElement tag, @NotNull Document document) {
TextRange elementRange = tag.getTextRange();
final int start = elementRange.getStartOffset();
final int end = elementRange.getEndOffset();
if (start + 1 < end) {
TextRange range = null;
ASTNode astNode = tag.getNode();
IElementType astType = astNode.getElementType();
if (tag instanceof CfmlTag) {
//if (tag instanceof CfmlTagFunctionImpl || tag instanceof CfmlTagComponentImpl || tag instanceof CfmlTagScriptImpl) {
range = buildRangeForBraces(range, astNode, CfmlTokenTypes.R_ANGLEBRACKET, CfmlTokenTypes.LSLASH_ANGLEBRACKET);
//}
} else if (astType == CfmlElementTypes.FUNCTIONBODY || astType == CfmlElementTypes.BLOCK_OF_STATEMENTS) {
range = buildRange(range, start, end);
} else if (astType == CfmlElementTypes.SWITCHEXPRESSION) {
ASTNode lparen = astNode.findChildByType(CfscriptTokenTypes.L_CURLYBRACKET);
ASTNode rparen = astNode.findChildByType(CfscriptTokenTypes.R_CURLYBRACKET);
if (lparen != null && rparen != null) {
range = buildRange(range, lparen.getStartOffset(), rparen.getTextRange().getEndOffset());
}
} else if (tag instanceof PsiComment) {
boolean isColdFusionComment = astNode.getElementType() == CfmlTokenTypes.COMMENT;
int endIndex = astNode.getText().lastIndexOf(isColdFusionComment ? "--->" : "*/");
if (endIndex != -1) {
String commentText = astNode.getText().substring(0, endIndex);
if (commentText.contains("\n")) {
int startOffset = tag.getTextRange().getStartOffset();
range = buildRange(range, startOffset + (isColdFusionComment ? "<!---" : "/*").length(), startOffset + commentText.length());
}
}
}
if (range != null) {
descriptors.add(new FoldingDescriptor(astNode, range));
}
// TODO: insert condition
addFoldingDescriptorsFromChildren(descriptors, tag, document);
}
}
Aggregations