类com.intellij.psi.formatter.FormatterUtil源码实例Demo

下面列出了怎么用com.intellij.psi.formatter.FormatterUtil的API类实例代码及写法,或者点击链接到github查看源代码。

@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;
}
 
源代码2 项目: intellij-haxe   文件: HaxeWrappingProcessor.java
private static Wrap createChildWrap(ASTNode child, int parentWrap, boolean newLineAfterLBrace, boolean newLineBeforeRBrace) {
  IElementType childType = child.getElementType();
  if (childType != PLPAREN && childType != PRPAREN) {
    if (FormatterUtil.isPrecededBy(child, PLBRACK)) {
      if (newLineAfterLBrace) {
        return Wrap.createChildWrap(Wrap.createWrap(parentWrap, true), WrapType.ALWAYS, true);
      }
      else {
        return Wrap.createWrap(WrapType.NONE, true);
      }
    }
    return Wrap.createWrap(WrappingUtil.getWrapType(parentWrap), true);
  }
  if (childType == PRBRACK && newLineBeforeRBrace) {
    return Wrap.createWrap(WrapType.ALWAYS, true);
  }
  return Wrap.createWrap(WrapType.NONE, true);
}
 
源代码3 项目: consulo   文件: TemplateLanguageBlock.java
@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);
}
 
@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;
}
 
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();
    }
}
 
源代码6 项目: protobuf-jetbrains-plugin   文件: ParentBlock.java
@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;
}
 
源代码7 项目: intellij-haxe   文件: HaxeBlock.java
@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;
}
 
源代码8 项目: consulo   文件: EnterHandler.java
@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;
}
 
源代码9 项目: consulo   文件: AbstractBlock.java
@Override
public boolean isIncomplete() {
  if (myIncomplete == null) {
    myIncomplete = FormatterUtil.isIncomplete(getNode());
  }
  return myIncomplete;
}
 
源代码10 项目: consulo   文件: CodeStyleManagerImpl.java
private void reformatText(@Nonnull PsiFile file, @Nonnull FormatTextRanges ranges, @Nullable Editor editor) throws IncorrectOperationException {
  if (ranges.isEmpty()) {
    return;
  }
  ApplicationManager.getApplication().assertWriteAccessAllowed();
  PsiDocumentManager.getInstance(getProject()).commitAllDocuments();

  CheckUtil.checkWritable(file);
  if (!SourceTreeToPsiMap.hasTreeElement(file)) {
    return;
  }

  ASTNode treeElement = SourceTreeToPsiMap.psiElementToTree(file);
  transformAllChildren(treeElement);

  LOG.assertTrue(file.isValid(), "File name: " + file.getName() + " , class: " + file.getClass().getSimpleName());

  if (editor == null) {
    editor = PsiUtilBase.findEditor(file);
  }

  CaretPositionKeeper caretKeeper = null;
  if (editor != null) {
    caretKeeper = new CaretPositionKeeper(editor, getSettings(file), file.getLanguage());
  }

  if (FormatterUtil.isFormatterCalledExplicitly()) {
    removeEndingWhiteSpaceFromEachRange(file, ranges);
  }

  formatRanges(file, ranges, ExternalFormatProcessor.useExternalFormatter(file) ? null  // do nothing, delegate the external formatting activity to post-processor
                                                                                : () -> {
                                                                                  final CodeFormatterFacade codeFormatter = new CodeFormatterFacade(getSettings(file), file.getLanguage());
                                                                                  codeFormatter.processText(file, ranges, true);
                                                                                });

  if (caretKeeper != null) {
    caretKeeper.restoreCaretPosition();
  }
}
 
源代码11 项目: consulo   文件: FormatterImpl.java
@Override
public void setProgressTask(@Nonnull FormattingProgressTask progressIndicator) {
  if (!FormatterUtil.isFormatterCalledExplicitly()) {
    return;
  }
  myProgressTask.set(progressIndicator);
}
 
private static boolean isReturnBodyKeywords(ASTNode node) {
    return FormatterUtil.isOneOf(node,
            CypherTypes.LIMIT,
            CypherTypes.SKIP,
            CypherTypes.ORDER);
}
 
源代码13 项目: intellij-haxe   文件: HaxeWrappingProcessor.java
Wrap createChildWrap(ASTNode child, Wrap defaultWrap, Wrap childWrap) {
  final IElementType childType = child.getElementType();
  final IElementType elementType = myNode.getElementType();
  if (childType == OCOMMA || childType == OSEMI) return defaultWrap;

  //
  // Function definition/call
  //
  if (elementType == PARAMETER_LIST || elementType == EXPRESSION_LIST) {
    final ASTNode parent = myNode.getTreeParent();
    if (parent == null) {
      return defaultWrap;
    }
    final IElementType parentType = parent.getElementType();
    if (parentType == CALL_EXPRESSION &&
        mySettings.CALL_PARAMETERS_WRAP != CommonCodeStyleSettings.DO_NOT_WRAP) {
      if (myNode.getFirstChildNode() == child) {
        return createWrap(mySettings.CALL_PARAMETERS_LPAREN_ON_NEXT_LINE);
      }
      if (!mySettings.PREFER_PARAMETERS_WRAP && childWrap != null) {
        return Wrap.createChildWrap(childWrap, WrappingUtil.getWrapType(mySettings.CALL_PARAMETERS_WRAP), true);
      }
      return Wrap.createWrap(WrappingUtil.getWrapType(mySettings.CALL_PARAMETERS_WRAP), true);
    }
    if (FUNCTION_DEFINITION.contains(parentType) &&
        mySettings.METHOD_PARAMETERS_WRAP != CommonCodeStyleSettings.DO_NOT_WRAP) {
      if (myNode.getFirstChildNode() == child) {
        return createWrap(mySettings.METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE);
      }
      if (childType == PRPAREN) {
        return createWrap(mySettings.METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE);
      }
      return Wrap.createWrap(WrappingUtil.getWrapType(mySettings.METHOD_PARAMETERS_WRAP), true);
    }
  }

  if (elementType == CALL_EXPRESSION) {
    if (mySettings.CALL_PARAMETERS_WRAP != CommonCodeStyleSettings.DO_NOT_WRAP) {
      if (childType == PRPAREN) {
        return createWrap(mySettings.CALL_PARAMETERS_RPAREN_ON_NEXT_LINE);
      }
    }
  }

  //
  // If
  //
  if (elementType == IF_STATEMENT && childType == KELSE) {
    return createWrap(mySettings.ELSE_ON_NEW_LINE);
  }

  //
  //Binary expressions
  //
  if (BINARY_EXPRESSIONS.contains(elementType) && mySettings.BINARY_OPERATION_WRAP != CommonCodeStyleSettings.DO_NOT_WRAP) {
    if ((mySettings.BINARY_OPERATION_SIGN_ON_NEXT_LINE && BINARY_OPERATORS.contains(childType)) ||
        (!mySettings.BINARY_OPERATION_SIGN_ON_NEXT_LINE && isRightOperand(child))) {
      return Wrap.createWrap(WrappingUtil.getWrapType(mySettings.BINARY_OPERATION_WRAP), true);
    }
  }

  //
  // Assignment
  //
  if (elementType == ASSIGN_EXPRESSION && mySettings.ASSIGNMENT_WRAP != CommonCodeStyleSettings.DO_NOT_WRAP) {
    if (childType != ASSIGN_OPERATION) {
      if (FormatterUtil.isPrecededBy(child, ASSIGN_OPERATION) &&
          mySettings.PLACE_ASSIGNMENT_SIGN_ON_NEXT_LINE) {
        return Wrap.createWrap(WrapType.NONE, true);
      }
      return Wrap.createWrap(WrappingUtil.getWrapType(mySettings.ASSIGNMENT_WRAP), true);
    }
    else if (mySettings.PLACE_ASSIGNMENT_SIGN_ON_NEXT_LINE) {
      return Wrap.createWrap(WrapType.NORMAL, true);
    }
  }

  //
  // Ternary expressions
  //
  if (elementType == TERNARY_EXPRESSION) {
    if (myNode.getFirstChildNode() != child) {
      if (mySettings.TERNARY_OPERATION_SIGNS_ON_NEXT_LINE) {
        if (!FormatterUtil.isPrecededBy(child, OQUEST) &&
            !FormatterUtil.isPrecededBy(child, OCOLON)) {
          return Wrap.createWrap(WrappingUtil.getWrapType(mySettings.TERNARY_OPERATION_WRAP), true);
        }
      }
      else if (childType != OQUEST && childType != OCOLON) {
        return Wrap.createWrap(WrappingUtil.getWrapType(mySettings.TERNARY_OPERATION_WRAP), true);
      }
    }
    return Wrap.createWrap(WrapType.NONE, true);
  }
  return defaultWrap;
}
 
源代码14 项目: intellij-xquery   文件: XQueryFormattingBlock.java
private IElementType getTypeOfPreviousElement(ASTNode myNode) {
    ASTNode prevSibling = FormatterUtil.getPreviousNonWhitespaceSibling(myNode);
    IElementType prevType = prevSibling != null ? prevSibling.getElementType() : null;
    return prevType;
}
 
 类所在包
 同包方法