@Override public boolean isLeaf() { return myNode.getFirstChildNode() == null; } }
@Override public boolean isLeaf() { return myNode.getFirstChildNode() == null; } }
/** * Returns true if the specified block may not contain child blocks. Used as an optimization * to avoid building the complete formatting model through calls to {@link #getSubBlocks()}. * * @return true if the block is a leaf block and may not contain child blocks, false otherwise. */ @Override public boolean isLeaf() { return myNode.getFirstChildNode() == null; }
@NotNull public static GoStringLiteralImpl updateText(@NotNull GoStringLiteral o, @NotNull String text) { if (text.length() > 2) { if (o.getString() != null) { StringBuilder outChars = new StringBuilder(); GoStringLiteralEscaper.escapeString(text.substring(1, text.length() - 1), outChars); outChars.insert(0, '"'); outChars.append('"'); text = outChars.toString(); } } ASTNode valueNode = o.getNode().getFirstChildNode(); assert valueNode instanceof LeafElement; ((LeafElement)valueNode).replaceWithText(text); return (GoStringLiteralImpl)o; }
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(); } }
@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; }
@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(); }
@Nullable @Override protected ASTNode doParseContents(@NotNull ASTNode chameleon, @NotNull PsiElement psi) { Project project = psi.getProject(); Language languageForParser = getLanguageForParser(psi); PsiBuilder builder = PsiBuilderFactory.getInstance().createBuilder(project, chameleon, null, languageForParser, chameleon.getChars()); PsiParser parser = LanguageParserDefinitions.INSTANCE.forLanguage(languageForParser).createParser(project); builder.putUserData(VIRTUAL_FILE, psi.getContainingFile().getVirtualFile()); ASTNode node = parser.parse(this, builder); return node.getFirstChildNode(); } }
@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); }
@Override public ASTNode parseContents(ASTNode chameleon) { PsiElement psi = chameleon.getPsi(); assert psi != null : "Bad chameleon: " + chameleon; Project project = psi.getProject(); Language languageForParser = this.getLanguageForParser(psi); PsiBuilder builder = PsiBuilderFactory.getInstance().createBuilder( project, chameleon, new org.elixir_lang.eex.lexer.EmbeddedElixir(project), languageForParser, chameleon.getChars() ); PsiParser parser = LanguageParserDefinitions.INSTANCE.forLanguage(languageForParser).createParser(project); builder.putUserData(VIRTUAL_FILE, psi.getContainingFile().getVirtualFile()); ASTNode node = parser.parse(this, builder); return node.getFirstChildNode(); } }
@Override public PsiLanguageInjectionHost updateText(@NotNull String text) { ASTNode valueNode = getNode().getFirstChildNode(); assert valueNode instanceof LeafElement; ((LeafElement) valueNode).replaceWithText(text); return this; }
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; }
@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); }
@Override public String createEquallyWrappedString(String newContent) { ASTNode node = getNode(); ASTNode firstChild = node.getFirstChildNode(); ASTNode lastChild = node.getLastChildNode(); StringBuilder result = new StringBuilder(firstChild.getTextLength() + newContent.length() + lastChild.getTextLength()); return result.append(firstChild.getText()).append(newContent).append(lastChild.getText()).toString(); }
@Override public String createEquallyWrappedString(String newContent) { if (!isWrapped()) { return newContent; } String firstText = getNode().getFirstChildNode().getText(); if (firstText.startsWith("$'")) { return "$'" + newContent + "'"; } else { return "'" + newContent + "'"; } }
@Nullable private static BnfQuantified getQuantifiedOptExpression(PsiElement element) { BnfQuantified quantified = getParentOfType(element, BnfQuantified.class); return quantified != null && quantified.getQuantifier().getNode().getFirstChildNode().getElementType() == BnfTypes.BNF_OP_OPT ? quantified : null; }
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(); } }
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 ASTNode parseContents(@NotNull ASTNode chameleon) { PsiElement parentElement = chameleon.getTreeParent().getPsi(); Project project = parentElement.getProject(); PsiBuilder builder = PsiBuilderFactory.getInstance().createBuilder( project, chameleon, getLexer(parentElement.getProject()), getLanguage(), chameleon.getText()); return getParser().parse(this, builder).getFirstChildNode(); }