use of com.intellij.lang.folding.FoldingDescriptor in project oxy-template-support-plugin by mutant-industries.
the class MatchingTagsAndJsBlock method buildBraceMatcherBasedFolding.
private static void buildBraceMatcherBasedFolding(@NotNull List<FoldingDescriptor> descriptors, @NotNull PsiElement root, @NotNull Document document, @NotNull SyntaxHighlighter highlighter) {
LexerEditorHighlighter editorHighlighter = new LexerEditorHighlighter(highlighter, EditorColorsManager.getInstance().getGlobalScheme());
editorHighlighter.setText(document.getText());
FileType fileType = root.getContainingFile().getFileType();
BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, root.getLanguage());
TextRange totalRange = root.getTextRange();
final HighlighterIterator iterator = editorHighlighter.createIterator(totalRange.getStartOffset());
final LinkedList<Trinity<Integer, Integer, IElementType>> stack = new LinkedList<>();
String editorText = document.getText();
TextRange range;
while (!iterator.atEnd() && iterator.getStart() < totalRange.getEndOffset()) {
final Trinity<Integer, Integer, IElementType> last;
if (OxyTemplateParserDefinition.PARAMETER_QUOTES.contains(iterator.getTokenType())) {
iterator.advance();
continue;
}
if (braceMatcher.isLBraceToken(iterator, editorText, fileType)) {
if (iterator.getTokenType() == OxyTemplateTypes.T_XML_TAG_START) {
IElementType tokenType = iterator.getTokenType();
do {
iterator.advance();
} while (!iterator.atEnd() && iterator.getTokenType() != OxyTemplateTypes.T_XML_OPEN_TAG_END && iterator.getTokenType() != OxyTemplateTypes.T_XML_CLOSE_TAG_END && iterator.getTokenType() != OxyTemplateTypes.T_XML_EMPTY_TAG_END);
if (iterator.atEnd()) {
return;
}
stack.addLast(Trinity.create(iterator.getStart(), iterator.getEnd(), tokenType));
iterator.retreat();
} else {
stack.addLast(Trinity.create(iterator.getStart() + 2, iterator.getEnd(), iterator.getTokenType()));
}
} else if (braceMatcher.isRBraceToken(iterator, editorText, fileType) && !stack.isEmpty() && braceMatcher.isPairBraces((last = stack.getLast()).third, iterator.getTokenType())) {
stack.removeLast();
range = new TextRange(last.first, iterator.getEnd() - (iterator.getTokenType() == OxyTemplateTypes.T_XML_CLOSE_TAG_END ? 1 : 2));
if (last.third != OxyTemplateTypes.T_XML_EMPTY_TAG_END && StringUtil.countChars(document.getText(range), '\n') >= 2) {
descriptors.add(new FoldingDescriptor(root, range));
}
}
iterator.advance();
}
}
use of com.intellij.lang.folding.FoldingDescriptor in project Perl5-IDEA by Camelcade.
the class PerlFoldingBuilder method getCommentsDescriptors.
/**
* Searching for sequential comments (starting from newline or subblock beginning) and making folding descriptors for such blocks of size > 1
*
* @param comments list of collected comments
* @param document document to search in
* @return list of FoldingDescriptros
*/
private List<FoldingDescriptor> getCommentsDescriptors(@NotNull List<PsiComment> comments, @NotNull Document document) {
List<FoldingDescriptor> descriptors = new ArrayList<>();
TokenSet commentExcludedTokens = getCommentExcludedTokens();
int currentOffset = 0;
for (PsiComment comment : comments) {
ASTNode commentNode = comment.getNode();
IElementType commentElementType = commentNode.getElementType();
if (currentOffset <= comment.getTextOffset() && // skips already collapsed blocks
!commentExcludedTokens.contains(commentElementType)) {
if (commentElementType == POD) {
TextRange commentRange = comment.getTextRange();
int startOffset = commentRange.getStartOffset();
int endOffset = commentRange.getEndOffset();
if (comment.getText().endsWith("\n")) {
endOffset--;
}
currentOffset = endOffset;
descriptors.add(new FoldingDescriptor(commentNode, new TextRange(startOffset, endOffset)));
continue;
}
boolean isCollapsable = false;
PsiElement lastComment = comment;
if (commentElementType == COMMENT_BLOCK || // template blocks are always collapsable
commentElementType == getTemplateBlockElementType()) {
isCollapsable = true;
} else {
// checking if this is a first element of block or starts from newline
while (true) {
lastComment = lastComment.getPrevSibling();
if (lastComment == null || lastComment instanceof PsiComment) {
isCollapsable = true;
break;
} else if (lastComment instanceof PsiWhiteSpace) {
// whitespace with newline
if (StringUtil.containsLineBreak(lastComment.getNode().getChars())) {
isCollapsable = true;
break;
}
} else // non-whitespace block
{
break;
}
}
}
if (isCollapsable) {
// looking for an end
int startOffset = comment.getTextOffset();
if (comment.getText().startsWith("\n") && startOffset > 0 && document.getCharsSequence().charAt(startOffset - 1) != '\n') {
startOffset++;
}
int endOffset = comment.getTextRange().getEndOffset();
PsiElement currentComment = comment;
while (currentComment != null) {
if (currentComment instanceof PsiComment && !commentExcludedTokens.contains(currentComment.getNode().getElementType()) && !currentComment.getText().contains("todo") && !currentComment.getText().contains("fixme")) {
endOffset = currentComment.getTextOffset() + currentComment.getTextLength();
if (currentComment.getText().endsWith("\n")) {
endOffset--;
}
} else if (!(currentComment instanceof PsiWhiteSpace)) {
break;
}
currentComment = currentComment.getNextSibling();
}
if (endOffset > startOffset) {
int startLine = document.getLineNumber(startOffset);
int endLine = document.getLineNumber(endOffset);
if (endLine > startLine) {
currentOffset = endOffset;
descriptors.add(new FoldingDescriptor(commentNode, new TextRange(startOffset, endOffset)));
}
}
}
}
}
return descriptors;
}
use of com.intellij.lang.folding.FoldingDescriptor in project Perl5-IDEA by Camelcade.
the class PerlFoldingBuilderBase method addDescriptorFor.
/**
* Finding psi elements of specific types and add Folding descriptor for them if they are more than certain lines lenght
*
* @param element root element for searching
* @param startMargin beginning margin for collapsable block
* @param endMargin end margin for collapsable block
* @return list of folding descriptors
*/
protected static void addDescriptorFor(@NotNull List<FoldingDescriptor> result, @NotNull Document document, @NotNull PsiElement element, int startMargin, int endMargin, int minLines) {
if (!(element.getParent() instanceof PerlNamespaceDefinitionWithIdentifier)) {
TextRange range = element.getTextRange();
int startOffset = range.getStartOffset() + startMargin;
int endOffset = range.getEndOffset() - endMargin;
int startLine = document.getLineNumber(startOffset);
int endLine = document.getLineNumber(endOffset);
if (endLine - startLine > minLines) {
result.add(new FoldingDescriptor(element.getNode(), new TextRange(startOffset, endOffset)));
}
}
}
use of com.intellij.lang.folding.FoldingDescriptor in project intellij by bazelbuild.
the class BuildFileFoldingBuilderTest method testRulesFolded.
@Test
public void testRulesFolded() {
BuildFile file = createBuildFile(new WorkspacePath("java/com/google/BUILD"), "java_library(", " name = 'lib',", " srcs = glob(['*.java']),", ")");
FoldingDescriptor[] foldingRegions = getFoldingRegions(file);
assertThat(foldingRegions).hasLength(1);
assertThat(foldingRegions[0].getElement().getPsi()).isEqualTo(file.findRule("lib"));
}
use of com.intellij.lang.folding.FoldingDescriptor in project intellij by bazelbuild.
the class BuildFileFoldingBuilderTest method testLoadStatementFolded.
@Test
public void testLoadStatementFolded() {
BuildFile file = createBuildFile(new WorkspacePath("java/com/google/BUILD"), "load(", " '//java/com/foo/build_defs.bzl',", " 'function1',", " 'function2',", ")");
FoldingDescriptor[] foldingRegions = getFoldingRegions(file);
assertThat(foldingRegions).hasLength(1);
assertThat(foldingRegions[0].getElement().getPsi()).isEqualTo(file.findChildByClass(LoadStatement.class));
}
Aggregations