Java Code Examples for com.intellij.psi.formatter.FormatterUtil#containsWhiteSpacesOnly()

The following examples show how to use com.intellij.psi.formatter.FormatterUtil#containsWhiteSpacesOnly() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProtoFileBlock.java    From protobuf-jetbrains-plugin with Apache License 2.0 6 votes vote down vote up
@Override
protected List<Block> buildChildren() {
    List<Block> blocks = new ArrayList<>();
    ASTNode child = myNode.getFirstChildNode();
    while (child != null) {
        if (!FormatterUtil.containsWhiteSpacesOnly(child)) {
            IElementType elementType = child.getElementType();
            if (ProtoParserDefinition.rule(ProtoParser.RULE_proto).equals(elementType)) {
                appendProtoBlocks(child, blocks);
            } else {
                // Comments are not part of root rule, we have to append them separately
                blocks.add(new LeafBlock(child, Alignment.createAlignment(), Indent.getNoneIndent(), settings));
            }
        }
        child = child.getTreeNext();
    }
    return blocks;
}
 
Example 2
Source File: TemplateLanguageBlock.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
protected List<Block> buildChildren() {
  myChildrenBuilt = true;
  if (isLeaf()) {
    return EMPTY;
  }
  final ArrayList<TemplateLanguageBlock> tlChildren = new ArrayList<>(5);
  for (ASTNode childNode = getNode().getFirstChildNode(); childNode != null; childNode = childNode.getTreeNext()) {
    if (FormatterUtil.containsWhiteSpacesOnly(childNode)) continue;
    if (shouldBuildBlockFor(childNode)) {
      final TemplateLanguageBlock childBlock = myBlockFactory
              .createTemplateLanguageBlock(childNode, createChildWrap(childNode), createChildAlignment(childNode), null, mySettings);
      childBlock.setParent(this);
      tlChildren.add(childBlock);
    }
  }
  final List<Block> children = (List<Block>)(myForeignChildren == null ? tlChildren : BlockUtil.mergeBlocks(tlChildren, myForeignChildren));
  //BlockUtil.printBlocks(getTextRange(), children);
  return BlockUtil.setParent(children, this);
}
 
Example 3
Source File: StatementBlock.java    From protobuf-jetbrains-plugin with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 4
Source File: ProtoFileBlock.java    From protobuf-jetbrains-plugin with Apache License 2.0 5 votes vote down vote up
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();
    }
}
 
Example 5
Source File: ParentBlock.java    From protobuf-jetbrains-plugin with Apache License 2.0 5 votes vote down vote up
@Override
protected List<Block> buildChildren() {
    ASTNode child = getNode().getFirstChildNode();
    State state = State.BEFORE_LEFT_CURLY_BRACE;
    List<Block> result = new ArrayList<>();
    while (child != null) {
        if (!FormatterUtil.containsWhiteSpacesOnly(child)) {
            IElementType elementType = child.getElementType();
            if (LCURLY.equals(elementType)) {
                state = State.AFTER_LEFT_CURLY_BRACE;
                result.add(BlockFactory.createBlock(child, myAlignment, Indent.getNoneIndent(), settings));
            } else if (RCURLY.equals(elementType)) {
                result.add(BlockFactory.createBlock(child, myAlignment, Indent.getNoneIndent(), settings));
                state = State.AFTER_RIGHT_CURLY_BRACE;
            } else {
                switch (state) {
                    case BEFORE_LEFT_CURLY_BRACE:
                        Block block = BlockFactory.createBlock(child, myAlignment, Indent.getNoneIndent(), settings);
                        headerBlocks.add(block);
                        result.add(block);
                        break;
                    case AFTER_LEFT_CURLY_BRACE:
                        result.add(BlockFactory.createBlock(child, childAlignment, Indent.getNormalIndent(true), settings));
                        break;
                    case AFTER_RIGHT_CURLY_BRACE:
                        result.add(BlockFactory.createBlock(child, myAlignment, Indent.getNoneIndent(), settings));
                        break;
                    default:
                        throw new IllegalStateException(state.toString());
                }
            }
        }
        child = child.getTreeNext();
    }
    return result;
}
 
Example 6
Source File: HaxeBlock.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
@Override
protected List<Block> buildChildren() {
  myChildrenBuilt = true;
  if (isLeaf()) {
    return EMPTY;
  }
  final ArrayList<Block> tlChildren = new ArrayList<Block>();
  for (ASTNode childNode = getNode().getFirstChildNode(); childNode != null; childNode = childNode.getTreeNext()) {
    if (FormatterUtil.containsWhiteSpacesOnly(childNode)) continue;
    final HaxeBlock childBlock = new HaxeBlock(childNode, createChildWrap(childNode), createChildAlignment(childNode), mySettings);
    childBlock.setParent(this);
    tlChildren.add(childBlock);
  }
  return tlChildren;
}
 
Example 7
Source File: EnterHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private PsiComment createComment(final CharSequence buffer, final CodeInsightSettings settings) throws IncorrectOperationException {
  myDocument.insertString(myOffset, buffer);

  PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
  CodeStyleManager.getInstance(getProject()).adjustLineIndent(myFile, myOffset + buffer.length() - 2);

  PsiComment comment = PsiTreeUtil.getNonStrictParentOfType(myFile.findElementAt(myOffset), PsiComment.class);

  comment = createJavaDocStub(settings, comment, getProject());
  if (comment == null) {
    return null;
  }

  CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(getProject());
  final Ref<PsiComment> commentRef = Ref.create(comment);
  codeStyleManager.runWithDocCommentFormattingDisabled(myFile, () -> formatComment(commentRef, codeStyleManager));
  comment = commentRef.get();

  PsiElement next = comment.getNextSibling();
  if (next == null && comment.getParent().getClass() == comment.getClass()) {
    next = comment.getParent().getNextSibling(); // expanding chameleon comment produces comment under comment
  }
  if (next != null) {
    next = myFile.findElementAt(next.getTextRange().getStartOffset()); // maybe switch to another tree
  }
  if (next != null && (!FormatterUtil.containsWhiteSpacesOnly(next.getNode()) || !next.getText().contains(LINE_SEPARATOR))) {
    int lineBreakOffset = comment.getTextRange().getEndOffset();
    myDocument.insertString(lineBreakOffset, LINE_SEPARATOR);
    PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
    codeStyleManager.adjustLineIndent(myFile, lineBreakOffset + 1);
    comment = PsiTreeUtil.getNonStrictParentOfType(myFile.findElementAt(myOffset), PsiComment.class);
  }
  return comment;
}