private static boolean hasAtLeastCountChildren(@NotNull CompositeElement parent, @NotNull IElementType childElementType, int atLeastCount) { int count = 0; for (ASTNode child = parent.getFirstChildNode(); child != null && count < atLeastCount; child = child.getTreeNext()) { if (child.getElementType() == childElementType) { count++; } } return count >= atLeastCount; }
@NotNull static List<com.intellij.formatting.Block> buildChildren( @NotNull ASTNode node, @NotNull BlockListReducer blockListReducer) { List<com.intellij.formatting.Block> blockList = new ArrayList<>(); ASTNode child = node.getFirstChildNode(); while (child != null) { if (shouldBuildBlock(child)) { blockList = blockListReducer.reduce(child, child.getElementType(), blockList); } child = child.getTreeNext(); } return blockList; }
private static void printAST(ASTNode node, int indent) { ASTNode sibling = node; while (sibling != null) { for (int i = 0; i < indent; i++) { System.out.print(" "); } System.out.println(sibling.toString() + " " + sibling.getTextRange().toString()); printAST(sibling.getFirstChildNode(), indent + 2); sibling = sibling.getTreeNext(); } }
@Override public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement start, @NotNull PsiElement end) { if (start.isValid() && end.isValid()) { PsiElement parent = start.getParent(); if (parent != null && parent.equals(end.getParent())) { parent.getNode().removeRange(start.getNode(), end.getNode().getTreeNext()); } } } }
private boolean lastArgument(@NotNull ASTNode node) { boolean lastArgument = true; ASTNode currentNode = node; while (currentNode != null) { IElementType elementType = currentNode.getElementType(); if (elementType == COMMA) { lastArgument = false; break; } else if (elementType == NO_PARENTHESES_ONE_ARGUMENT) { break; } else { ASTNode nextNode = currentNode.getTreeNext(); if (nextNode == null) { currentNode = currentNode.getTreeParent(); } else { currentNode = nextNode; } } } return lastArgument; }
@Nullable private static IElementType getFirstChild(ASTNode parent) { ASTNode child = parent.getFirstChildNode(); if (child == null) { return null; } while (child.getElementType() == BallerinaTypes.LINE_COMMENT || child.getElementType() == TokenType.WHITE_SPACE) { child = child.getTreeNext(); if (child == null) { return null; } } return child.getElementType(); }
@NotNull private List<Block> buildSubBlocks() { List<Block> blocks = ContainerUtil.newArrayList(); for (ASTNode child = myNode.getFirstChildNode(); child != null; child = child.getTreeNext()) { IElementType childType = child.getElementType(); if (child.getTextRange().getLength() == 0) { continue; } if (childType == TokenType.WHITE_SPACE) { continue; } Alignment alignment = getAlignment(child); Indent indent = calculateIndent(child); Wrap wrap = createWrap(child); blocks.add( new BallerinaBlock(child, alignment, indent, wrap, mySettings, mySpacingBuilder, myAlignmentMap)); } return blocks; }
@NotNull private List<Block> buildSubBlocks() { AlignmentStrategy.AlignmentPerTypeStrategy strategy = null; boolean isStruct = getNode().getElementType() == STRUCT_TYPE; Alignment forType = null; if (isStruct) { strategy = AlignmentStrategy.createAlignmentPerTypeStrategy(ContainerUtil.list(FIELD_DECLARATION, LINE_COMMENT), STRUCT_TYPE, true); forType = Alignment.createAlignment(true); } List<Block> blocks = ContainerUtil.newArrayList(); for (ASTNode child = myNode.getFirstChildNode(); child != null; child = child.getTreeNext()) { IElementType childType = child.getElementType(); if (child.getTextRange().getLength() == 0) continue; if (childType == TokenType.WHITE_SPACE) continue; IElementType substitutor = childType == MULTILINE_COMMENT ? LINE_COMMENT : childType; Alignment alignment = strategy != null ? strategy.getAlignment(substitutor) : null; GoFormattingBlock e = buildSubBlock(child, alignment); if (isStruct) { e.putUserDataIfAbsent(TYPE_ALIGNMENT_INSIDE_STRUCT, forType); } blocks.add(e); } return Collections.unmodifiableList(blocks); }
@Deprecated // this one is bad. What about comments and so on? @Nullable public static ASTNode getNextSignificantSibling(ASTNode node) { ASTNode result = node.getTreeNext(); while (result != null && result.getElementType() == TokenType.WHITE_SPACE) { result = result.getTreeNext(); } return result; }
public static boolean isSingleChildParent(PsiElement psi) { if (psi == null) { return false; } ASTNode child = psi.getNode(); return child.getTreePrev() == null && child.getTreeNext() == null; }
public static void collectAstNodesByType(List<ASTNode> nodes, ASTNode rootNode, IElementType type) { if (rootNode.getElementType().equals(type)) { nodes.add(rootNode); } for (ASTNode element = rootNode.getFirstChildNode(); element != null; element = element.getTreeNext()) { collectAstNodesByType(nodes, element, type); } } }
@NotNull protected List<Block> buildSubBlocks() { final List<Block> blocks = new ArrayList<>(); for (ASTNode child = myNode.getFirstChildNode(); child != null; child = child.getTreeNext()) { if (shouldCreateBlockFor(child)) { blocks.add(createBlock(child)); } } return processSubBlocks(blocks); }
public static ASTNode findFirstDeepChildByType(ASTNode root, IElementType type) { if (root.getElementType().equals(type)) { return root; } for (ASTNode element = root.getFirstChildNode(); element != null; element = element.getTreeNext()) { ASTNode child = findFirstDeepChildByType(element, type); if (child != null) { return child; } } return null; }
public static boolean isSingleChildParent(PsiElement psi, @NotNull IElementType childType) { if (psi == null) { return false; } ASTNode child = getDeepestEquivalent(psi.getNode()); return child.getTreePrev() == null && child.getTreeNext() == null && (child.getElementType() == childType); }
private static TextRange adjustFoldingRange(ASTNode node) { if (node.getElementType() == HEREDOC_CONTENT_ELEMENT) { int startOffset = node.getStartOffset(); //walk to the last content element before the close marker for (ASTNode next = node.getTreeNext(); next != null; next = next.getTreeNext()) { IElementType elementType = next.getElementType(); if (elementType == BashElementTypes.HEREDOC_END_ELEMENT || elementType == BashElementTypes.HEREDOC_END_IGNORING_TABS_ELEMENT) { return TextRange.create(startOffset, next.getStartOffset() - 1); } } } return node.getTextRange(); }
@Nullable @Override public PsiElement getNextSibling(@NotNull ASTNode node) { ASTNode sibling = node.getTreeNext(); return sibling == null ? null : sibling.getPsi(); }
private void appendProtoBlocks(ASTNode protoRootNode, List<Block> blocks) { ASTNode child = protoRootNode.getFirstChildNode(); Alignment alignment = Alignment.createAlignment(); while (child != null) { if (!FormatterUtil.containsWhiteSpacesOnly(child)) { Block block = createBlock(child, alignment, Indent.getNoneIndent(), settings); blocks.add(block); } child = child.getTreeNext(); } }
@Override protected List<Block> buildChildren() { ASTNode child = getNode().getFirstChildNode(); List<Block> result = new ArrayList<>(); while (child != null) { if (!FormatterUtil.containsWhiteSpacesOnly(child)) { Block block = BlockFactory.createBlock(child, Alignment.createAlignment(), Indent.getNoneIndent(), settings); result.add(block); } child = child.getTreeNext(); } return result; }
public boolean isWrappable() { if (isSingleChildParent()) { return false; } ASTNode node = getNode(); for (ASTNode child = node.getFirstChildNode(); child != null; child = child.getTreeNext()) { if (nonWrappableChilds.contains(child.getElementType())) { return false; } } return true; }
@Override public void addChildren(ASTNode firstChild, ASTNode firstChildToNotAdd, ASTNode anchorBefore) { List<MockTreeNode> nodes = new ArrayList<MockTreeNode>(); int i = indexOfChild(anchorBefore); for (ASTNode child = firstChild; child != firstChildToNotAdd; child = child.getTreeNext()) { nodes.add(coerce(child)); } for (MockTreeNode node : nodes) { children.add(i++, makeMeTheParent(node)); } relinkChildren(); }