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

项目:protobuf-jetbrains-plugin    文件:ProtoFileBlock.java   
@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;
}
项目:intellij-ce-playground    文件:JavaFormattingModelBuilder.java   
/**
 * Checks if previous non-white space leaf of the given node is error element and combines formatting range relevant for it
 * with the range of the given node.
 * 
 * @param node  target node
 * @return      given node range if there is no error-element before it; combined range otherwise
 */
@Nullable
private static TextRange combineWithErrorElementIfPossible(@NotNull ASTNode node) {
  if (node.getElementType() == TokenType.ERROR_ELEMENT) {
    return node.getTextRange();
  }
  final ASTNode prevLeaf = FormatterUtil.getPreviousLeaf(node, TokenType.WHITE_SPACE);
  if (prevLeaf == null || prevLeaf.getElementType() != TokenType.ERROR_ELEMENT) {
    return node.getTextRange();
  }

  final TextRange range = doGetRangeAffectingIndent(prevLeaf);
  if (range == null) {
    return node.getTextRange();
  }
  else {
    return new TextRange(range.getStartOffset(), node.getTextRange().getEndOffset());
  } 
}
项目:intellij-ce-playground    文件:LabeledJavaBlock.java   
@Override
protected List<Block> buildChildren() {
  final ArrayList<Block> result = new ArrayList<Block>();
  ASTNode child = myNode.getFirstChildNode();
  Indent currentIndent = getLabelIndent();
  Wrap currentWrap = null;
  while (child != null) {
    if (!FormatterUtil.containsWhiteSpacesOnly(child) && child.getTextLength() > 0){
      result.add(createJavaBlock(child, mySettings, myJavaSettings, currentIndent, currentWrap, AlignmentStrategy.getNullStrategy()));
      if (child.getElementType() == JavaTokenType.COLON) {
        currentIndent = Indent.getNoneIndent();
        currentWrap =Wrap.createWrap(WrapType.ALWAYS, true);
      }
    }
    child = child.getTreeNext();
  }
  return result;
}
项目:intellij-ce-playground    文件:SimpleJavaBlock.java   
private void processHeadCommentsAndWhiteSpaces(@NotNull List<Block> result) {
  while (myCurrentChild != null) {
    if (StdTokenSets.COMMENT_BIT_SET.contains(myCurrentChild.getElementType()) || myCurrentChild.getElementType() == JavaDocElementType.DOC_COMMENT) {
      Block commentBlock = createJavaBlock(
        myCurrentChild,
        mySettings, myJavaSettings,
        Indent.getNoneIndent(), null, AlignmentStrategy.getNullStrategy()
      );
      result.add(commentBlock);
      myCurrentIndent = Indent.getNoneIndent();
    }
    else if (!FormatterUtil.containsWhiteSpacesOnly(myCurrentChild)) {
      break;
    }

    myCurrentOffset += myCurrentChild.getTextLength();
    myCurrentChild = myCurrentChild.getTreeNext();
  }
}
项目:intellij-ce-playground    文件:AbstractJavaBlock.java   
private ASTNode processField(@NotNull final List<Block> result,
                             ASTNode child,
                             @NotNull final AlignmentStrategy alignmentStrategy,
                             final Wrap defaultWrap,
                             final Indent childIndent) {
  ASTNode lastFieldInGroup = findLastFieldInGroup(child);
  if (lastFieldInGroup == child) {
    result.add(createJavaBlock(child, getSettings(), myJavaSettings, childIndent, arrangeChildWrap(child, defaultWrap), alignmentStrategy));
    return child;
  }
  else {
    final ArrayList<Block> localResult = new ArrayList<Block>();
    while (child != null) {
      if (!FormatterUtil.containsWhiteSpacesOnly(child)) {
        localResult.add(createJavaBlock(
            child, getSettings(), myJavaSettings,
            Indent.getContinuationWithoutFirstIndent(myIndentSettings.USE_RELATIVE_INDENTS),
            arrangeChildWrap(child, defaultWrap),
            alignmentStrategy
          )
        );
      }
      if (child == lastFieldInGroup) break;

      child = child.getTreeNext();
    }
    if (!localResult.isEmpty()) {
      result.add(new SyntheticCodeBlock(localResult, null, getSettings(), myJavaSettings, childIndent, null));
    }
    return lastFieldInGroup;
  }
}
项目:intellij-ce-playground    文件:AbstractJavaBlock.java   
private static void collectNodes(@NotNull List<ASTNode> nodes, @NotNull ASTNode node) {
  ASTNode child = node.getFirstChildNode();
  while (child != null) {
    if (!FormatterUtil.containsWhiteSpacesOnly(child)) {
      IElementType type = child.getElementType();
      if (type == JavaElementType.METHOD_CALL_EXPRESSION ||
          type == JavaElementType.REFERENCE_EXPRESSION) {
        collectNodes(nodes, child);
      }
      else {
        nodes.add(child);
      }
    }
    child = child.getTreeNext();
  }
}
项目:intellij-ce-playground    文件:AbstractJavaBlock.java   
/**
 * Encapsulates alignment retrieval logic for variable declaration use-case assuming that given node is a child node
 * of basic variable declaration node.
 *
 * @param child   variable declaration child node which alignment is to be defined
 * @return        alignment to use for the given node
 * @see CodeStyleSettings#ALIGN_GROUP_FIELD_DECLARATIONS
 */
@Nullable
private boolean shouldAlignFieldInColumns(@NotNull ASTNode child) {
  // The whole idea of variable declarations alignment is that complete declaration blocks which children are to be aligned hold
  // reference to the same AlignmentStrategy object, hence, reuse the same Alignment objects. So, there is no point in checking
  // if it's necessary to align sub-blocks if shared strategy is not defined.
  if (!mySettings.ALIGN_GROUP_FIELD_DECLARATIONS) {
    return false;
  }

  IElementType childType = child.getElementType();

  // We don't want to align subsequent identifiers in single-line declarations like 'int i1, i2, i3'. I.e. only 'i1'
  // should be aligned then.
  ASTNode previousNode = FormatterUtil.getPreviousNonWhitespaceSibling(child);
  if (childType == JavaTokenType.IDENTIFIER && (previousNode == null || previousNode.getElementType() == JavaTokenType.COMMA)) {
    return false;
  }

  return true;
}
项目:intellij-ce-playground    文件:AbstractJavaBlock.java   
@Nullable
private ASTNode processEnumBlock(@NotNull List<Block> result,
                                 @Nullable ASTNode child,
                                 ASTNode last)
{
  final WrappingStrategy wrappingStrategy = WrappingStrategy.createDoNotWrapCommaStrategy(Wrap
    .createWrap(getWrapType(mySettings.ENUM_CONSTANTS_WRAP), true));
  while (child != null) {
    if (!FormatterUtil.containsWhiteSpacesOnly(child) && child.getTextLength() > 0) {
      result.add(createJavaBlock(child, mySettings, myJavaSettings, Indent.getNormalIndent(),
                                 wrappingStrategy.getWrap(child.getElementType()), AlignmentStrategy.getNullStrategy()));
      if (child == last) return child;
    }
    child = child.getTreeNext();
  }
  return null;
}
项目:intellij-ce-playground    文件:DocCommentBlock.java   
@Override
protected List<Block> buildChildren() {
  final ArrayList<Block> result = new ArrayList<Block>();

  ASTNode child = myNode.getFirstChildNode();
  while (child != null) {
    if (child.getElementType() == JavaDocTokenType.DOC_COMMENT_START) {
      result.add(createJavaBlock(child, mySettings, myJavaSettings, Indent.getNoneIndent(), null, AlignmentStrategy.getNullStrategy()));
    } else if (!FormatterUtil.containsWhiteSpacesOnly(child) && !child.getText().trim().isEmpty()){
      result.add(createJavaBlock(child, mySettings, myJavaSettings, Indent.getSpaceIndent(1), null, AlignmentStrategy.getNullStrategy()));
    }
    child = child.getTreeNext();
  }
  return result;

}
项目:intellij-ce-playground    文件:CodeBlockBlock.java   
/**
 * There is a possible case that 'implements' section is incomplete (e.g. ends with comma). We may want to align lbrace
 * to the comma then.
 *
 * @param alignment     block alignment
 * @param baseNode      base AST node
 * @return              alignment strategy to use for the given node
 */
private static AlignmentStrategy getAlignmentStrategy(Alignment alignment, ASTNode baseNode, @NotNull CommonCodeStyleSettings settings) {
  if (baseNode.getElementType() != JavaElementType.CLASS || !settings.ALIGN_MULTILINE_EXTENDS_LIST) {
    return AlignmentStrategy.wrap(alignment);
  }
  for (ASTNode node = baseNode.getLastChildNode(); node != null; node = FormatterUtil.getPreviousNonWhitespaceSibling(node)) {
    if (node.getElementType() != JavaElementType.IMPLEMENTS_LIST) {
      continue;
    }
    ASTNode lastChildNode = node.getLastChildNode();
    if (lastChildNode != null && lastChildNode.getElementType() == TokenType.ERROR_ELEMENT) {
      Alignment alignmentToUse = alignment;
      if (alignment == null) {
        alignmentToUse = Alignment.createAlignment();
      }
      return AlignmentStrategy.wrap(
        alignmentToUse, false, JavaTokenType.LBRACE, JavaElementType.JAVA_CODE_REFERENCE, node.getElementType()
      );
    }
    break;
  }
  return AlignmentStrategy.wrap(alignment);
}
项目:intellij-ce-playground    文件:ChildrenBlocksBuilder.java   
public List<Block> buildNodeChildBlocks(ASTNode node, BlockFactory factory) {
  List<Block> blocks = ContainerUtil.newArrayList();

  for (ASTNode child : node.getChildren(null)) {
    if (FormatterUtil.isWhitespaceOrEmpty(child) || child.getTextLength() == 0) {
        continue;
    }

    Alignment alignment = myConfig.getAlignment(child);

    IElementType type = child.getElementType();
    Indent indent = myConfig.getIndent(type);
    Wrap wrap = myConfig.getWrap(type);

    blocks.add(factory.createBlock(child, indent, alignment, wrap));
  }

  return blocks;
}
项目:intellij-ce-playground    文件:ReformatBeforeCheckinHandler.java   
@Override
public void runCheckinHandlers(@NotNull final Runnable finishAction) {
  final VcsConfiguration configuration = VcsConfiguration.getInstance(myProject);
  final Collection<VirtualFile> files = myPanel.getVirtualFiles();

  final Runnable performCheckoutAction = new Runnable() {
    @Override
    public void run() {
      FileDocumentManager.getInstance().saveAllDocuments();
      finishAction.run();
    }
  };

  if (reformat(configuration, true)) {
    new ReformatCodeProcessor(
      myProject, CheckinHandlerUtil.getPsiFiles(myProject, files), FormatterUtil.REFORMAT_BEFORE_COMMIT_COMMAND_NAME, performCheckoutAction, true
    ).run();
  }
  else {
    performCheckoutAction.run();
  }

}
项目:intellij-ce-playground    文件:TemplateLanguageBlock.java   
@Override
protected List<Block> buildChildren() {
  myChildrenBuilt = true;
  if (isLeaf()) {
    return EMPTY;
  }
  final ArrayList<TemplateLanguageBlock> tlChildren = new ArrayList<TemplateLanguageBlock>(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);
}
项目:intellij-ce-playground    文件:GroovySpacingProcessor.java   
private boolean manageComments() {
  if (mySettings.KEEP_FIRST_COLUMN_COMMENT && TokenSets.COMMENT_SET.contains(myType2)) {
    if (!isAfterElementOrSemi(GroovyElementTypes.IMPORT_STATEMENT)) {
      myResult = Spacing.createKeepingFirstColumnSpacing(0, Integer.MAX_VALUE, true, 1);
      return true;
    }
    return false;
  }

  ASTNode prev = FormatterUtil.getPreviousNonWhitespaceLeaf(myChild2);
  if (prev != null && prev.getElementType() == GroovyTokenTypes.mNLS) {
    prev = FormatterUtil.getPreviousNonWhitespaceLeaf(prev);
  }
  if (prev != null && prev.getElementType() == GroovyTokenTypes.mSL_COMMENT) {
    myResult = Spacing.createSpacing(0, Integer.MAX_VALUE, 1, mySettings.KEEP_LINE_BREAKS, keepBlankLines());
    return true;
  }
  return false;
}
项目:tools-idea    文件:JavaFormattingModelBuilder.java   
/**
 * Checks if previous non-white space leaf of the given node is error element and combines formatting range relevant for it
 * with the range of the given node.
 * 
 * @param node  target node
 * @return      given node range if there is no error-element before it; combined range otherwise
 */
@Nullable
private static TextRange combineWithErrorElementIfPossible(@NotNull ASTNode node) {
  if (node.getElementType() == TokenType.ERROR_ELEMENT) {
    return node.getTextRange();
  }
  final ASTNode prevLeaf = FormatterUtil.getPreviousLeaf(node, TokenType.WHITE_SPACE);
  if (prevLeaf == null || prevLeaf.getElementType() != TokenType.ERROR_ELEMENT) {
    return node.getTextRange();
  }

  final TextRange range = doGetRangeAffectingIndent(prevLeaf);
  if (range == null) {
    return node.getTextRange();
  }
  else {
    return new TextRange(range.getStartOffset(), node.getTextRange().getEndOffset());
  } 
}
项目:tools-idea    文件:LabeledJavaBlock.java   
@Override
protected List<Block> buildChildren() {
  final ArrayList<Block> result = new ArrayList<Block>();
  ASTNode child = myNode.getFirstChildNode();
  Indent currentIndent = getLabelIndent();
  Wrap currentWrap = null;
  while (child != null) {
    if (!FormatterUtil.containsWhiteSpacesOnly(child) && child.getTextLength() > 0){
      result.add(createJavaBlock(child, mySettings, currentIndent, currentWrap, AlignmentStrategy.getNullStrategy()));
      if (child.getElementType() == ElementType.COLON) {
        currentIndent = Indent.getNoneIndent();
        currentWrap =Wrap.createWrap(WrapType.ALWAYS, true);
      }
    }
    child = child.getTreeNext();
  }
  return result;
}
项目:tools-idea    文件:AbstractJavaBlock.java   
@Nullable
private static Indent getDefaultSubtreeIndent(@NotNull ASTNode child, @NotNull CommonCodeStyleSettings.IndentOptions indentOptions) {
  final ASTNode parent = child.getTreeParent();
  final IElementType childNodeType = child.getElementType();
  if (childNodeType == JavaElementType.ANNOTATION) {
    if (parent.getPsi() instanceof PsiArrayInitializerMemberValue) {
      return Indent.getNormalIndent();
    }
    return Indent.getNoneIndent();
  }

  final ASTNode prevElement = FormatterUtil.getPreviousNonWhitespaceSibling(child);
  if (prevElement != null && prevElement.getElementType() == JavaElementType.MODIFIER_LIST) {
    return Indent.getNoneIndent();
  }

  if (childNodeType == JavaDocElementType.DOC_TAG) return Indent.getNoneIndent();
  if (childNodeType == JavaDocTokenType.DOC_COMMENT_LEADING_ASTERISKS) return Indent.getSpaceIndent(1);
  if (child.getPsi() instanceof PsiFile) return Indent.getNoneIndent();
  if (parent != null) {
    final Indent defaultChildIndent = getChildIndent(parent, indentOptions);
    if (defaultChildIndent != null) return defaultChildIndent;
  }

  return null;
}
项目:tools-idea    文件:AbstractJavaBlock.java   
private static void collectNodes(@NotNull List<ASTNode> nodes, @NotNull ASTNode node) {
  ASTNode child = node.getFirstChildNode();
  while (child != null) {
    if (!FormatterUtil.containsWhiteSpacesOnly(child)) {
      if (child.getElementType() == JavaElementType.METHOD_CALL_EXPRESSION || child.getElementType() ==
                                                                              JavaElementType
                                                                                .REFERENCE_EXPRESSION) {
        collectNodes(nodes, child);
      }
      else {
        nodes.add(child);
      }
    }
    child = child.getTreeNext();
  }
}
项目:tools-idea    文件:AbstractJavaBlock.java   
/**
 * Encapsulates alignment retrieval logic for variable declaration use-case assuming that given node is a child node
 * of basic variable declaration node.
 *
 * @param child   variable declaration child node which alignment is to be defined
 * @return        alignment to use for the given node
 * @see CodeStyleSettings#ALIGN_GROUP_FIELD_DECLARATIONS
 */
@Nullable
private Alignment getVariableDeclarationSubElementAlignment(@NotNull ASTNode child) {
  // The whole idea of variable declarations alignment is that complete declaration blocks which children are to be aligned hold
  // reference to the same AlignmentStrategy object, hence, reuse the same Alignment objects. So, there is no point in checking
  // if it's necessary to align sub-blocks if shared strategy is not defined.
  if (!mySettings.ALIGN_GROUP_FIELD_DECLARATIONS) {
    return null;
  }

  IElementType childType = child.getElementType();

  // We don't want to align subsequent identifiers in single-line declarations like 'int i1, i2, i3'. I.e. only 'i1'
  // should be aligned then.
  ASTNode previousNode = FormatterUtil.getPreviousNonWhitespaceSibling(child);
  if (childType == JavaTokenType.IDENTIFIER && (previousNode == null || previousNode.getElementType() == JavaTokenType.COMMA)) {
    return null;
  }

  return myAlignmentStrategy.getAlignment(childType);
}
项目:tools-idea    文件:AbstractJavaBlock.java   
@Nullable
private ASTNode processEnumBlock(@NotNull List<Block> result,
                                 @Nullable ASTNode child,
                                 ASTNode last)
{
  final WrappingStrategy wrappingStrategy = WrappingStrategy.createDoNotWrapCommaStrategy(Wrap
    .createWrap(getWrapType(mySettings.ENUM_CONSTANTS_WRAP), true));
  while (child != null) {
    if (!FormatterUtil.containsWhiteSpacesOnly(child) && child.getTextLength() > 0) {
      result.add(createJavaBlock(child, mySettings, Indent.getNormalIndent(),
                                 wrappingStrategy.getWrap(child.getElementType()), AlignmentStrategy.getNullStrategy()));
      if (child == last) return child;
    }
    child = child.getTreeNext();
  }
  return null;
}
项目:tools-idea    文件:DocCommentBlock.java   
@Override
protected List<Block> buildChildren() {
  final ArrayList<Block> result = new ArrayList<Block>();

  ASTNode child = myNode.getFirstChildNode();
  while (child != null) {
    if (child.getElementType() == JavaDocTokenType.DOC_COMMENT_START) {
      result.add(createJavaBlock(child, mySettings, Indent.getNoneIndent(), null, AlignmentStrategy.getNullStrategy()));
    } else if (!FormatterUtil.containsWhiteSpacesOnly(child) && child.getText().trim().length() > 0){
      result.add(createJavaBlock(child, mySettings, Indent.getSpaceIndent(1), null, AlignmentStrategy.getNullStrategy()));
    }
    child = child.getTreeNext();
  }
  return result;

}
项目:tools-idea    文件:CodeBlockBlock.java   
/**
 * There is a possible case that 'implements' section is incomplete (e.g. ends with comma). We may want to align lbrace
 * to the comma then.
 * 
 * @param alignment     block alignment
 * @param baseNode      base AST node
 * @return              alignment strategy to use for the given node
 */
private static AlignmentStrategy getAlignmentStrategy(Alignment alignment, ASTNode baseNode, @NotNull CommonCodeStyleSettings settings) {
  if (baseNode.getElementType() != JavaElementType.CLASS || !settings.ALIGN_MULTILINE_EXTENDS_LIST) {
    return AlignmentStrategy.wrap(alignment);
  }
  for (ASTNode node = baseNode.getLastChildNode(); node != null; node = FormatterUtil.getPreviousNonWhitespaceSibling(node)) {
    if (node.getElementType() != JavaElementType.IMPLEMENTS_LIST) {
      continue;
    }
    ASTNode lastChildNode = node.getLastChildNode();
    if (lastChildNode != null && lastChildNode.getElementType() == TokenType.ERROR_ELEMENT) {
      Alignment alignmentToUse = alignment;
      if (alignment == null) {
        alignmentToUse = Alignment.createAlignment();
      }
      return AlignmentStrategy.wrap(
        alignmentToUse, false, JavaTokenType.LBRACE, JavaElementType.JAVA_CODE_REFERENCE, node.getElementType()
      );
    }
    break;
  }
  return AlignmentStrategy.wrap(alignment);
}
项目:tools-idea    文件:TemplateLanguageBlock.java   
@Override
protected List<Block> buildChildren() {
  myChildrenBuilt = true;
  if (isLeaf()) {
    return EMPTY;
  }
  final ArrayList<TemplateLanguageBlock> tlChildren = new ArrayList<TemplateLanguageBlock>(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);
}
项目:tools-idea    文件:GroovySpacingProcessor.java   
private boolean manageComments() {
  if (mySettings.KEEP_FIRST_COLUMN_COMMENT && COMMENT_SET.contains(myType2)) {
    if (myType1 != IMPORT_STATEMENT) {
      myResult = Spacing.createKeepingFirstColumnSpacing(0, Integer.MAX_VALUE, true, 1);
    }
    return true;
  }

  ASTNode prev = FormatterUtil.getPreviousNonWhitespaceLeaf(myChild2);
  if (prev != null && prev.getElementType() == mNLS) {
    prev = FormatterUtil.getPreviousNonWhitespaceLeaf(prev);
  }
  if (prev != null && prev.getElementType() == mSL_COMMENT) {
    myResult = Spacing.createSpacing(0, Integer.MAX_VALUE, 1, mySettings.KEEP_LINE_BREAKS, keepBlankLines());
    return true;
  }
  return false;
}
项目: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);
}
项目:consulo-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);
}
项目: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);
}
项目:consulo-java    文件:JavaFormattingModelBuilder.java   
/**
 * Checks if previous non-white space leaf of the given node is error element and combines formatting range relevant for it
 * with the range of the given node.
 *
 * @param node target node
 * @return given node range if there is no error-element before it; combined range otherwise
 */
@Nullable
@RequiredReadAction
private static TextRange combineWithErrorElementIfPossible(@NotNull ASTNode node)
{
    if(node.getElementType() == TokenType.ERROR_ELEMENT)
    {
        return node.getTextRange();
    }
    final ASTNode prevLeaf = FormatterUtil.getPreviousLeaf(node, TokenType.WHITE_SPACE);
    if(prevLeaf == null || prevLeaf.getElementType() != TokenType.ERROR_ELEMENT)
    {
        return node.getTextRange();
    }

    final TextRange range = doGetRangeAffectingIndent(prevLeaf);
    if(range == null)
    {
        return node.getTextRange();
    }
    else
    {
        return new TextRange(range.getStartOffset(), node.getTextRange().getEndOffset());
    }
}
项目:consulo-java    文件:LabeledJavaBlock.java   
@Override
protected List<Block> buildChildren()
{
    final ArrayList<Block> result = new ArrayList<>();
    ASTNode child = myNode.getFirstChildNode();
    Indent currentIndent = getLabelIndent();
    Wrap currentWrap = null;
    while(child != null)
    {
        if(!FormatterUtil.containsWhiteSpacesOnly(child) && child.getTextLength() > 0)
        {
            result.add(createJavaBlock(child, mySettings, myJavaSettings, currentIndent, currentWrap, AlignmentStrategy.getNullStrategy()));
            if(child.getElementType() == JavaTokenType.COLON)
            {
                currentIndent = Indent.getNoneIndent();
                currentWrap = Wrap.createWrap(WrapType.ALWAYS, true);
            }
        }
        child = child.getTreeNext();
    }
    return result;
}
项目:consulo-java    文件:SimpleJavaBlock.java   
private void processHeadCommentsAndWhiteSpaces(@NotNull List<Block> result)
{
    while(myCurrentChild != null)
    {
        if(StdTokenSets.COMMENT_BIT_SET.contains(myCurrentChild.getElementType()) || myCurrentChild.getElementType() == JavaDocElementType.DOC_COMMENT)
        {
            Block commentBlock = createJavaBlock(myCurrentChild, mySettings, myJavaSettings, Indent.getNoneIndent(), null, AlignmentStrategy.getNullStrategy());
            result.add(commentBlock);
            myCurrentIndent = Indent.getNoneIndent();
        }
        else if(!FormatterUtil.containsWhiteSpacesOnly(myCurrentChild))
        {
            break;
        }

        myCurrentOffset += myCurrentChild.getTextLength();
        myCurrentChild = myCurrentChild.getTreeNext();
    }
}
项目:consulo-java    文件:AbstractJavaBlock.java   
private static void collectNodes(@NotNull List<ASTNode> nodes, @NotNull ASTNode node)
{
    ASTNode child = node.getFirstChildNode();
    while(child != null)
    {
        if(!FormatterUtil.containsWhiteSpacesOnly(child))
        {
            IElementType type = child.getElementType();
            if(type == JavaElementType.METHOD_CALL_EXPRESSION || type == JavaElementType.REFERENCE_EXPRESSION)
            {
                collectNodes(nodes, child);
            }
            else
            {
                nodes.add(child);
            }
        }
        child = child.getTreeNext();
    }
}
项目:consulo-java    文件:AbstractJavaBlock.java   
/**
 * Encapsulates alignment retrieval logic for variable declaration use-case assuming that given node is a child node
 * of basic variable declaration node.
 *
 * @param child variable declaration child node which alignment is to be defined
 * @return alignment to use for the given node
 * @see CodeStyleSettings#ALIGN_GROUP_FIELD_DECLARATIONS
 */
private boolean shouldAlignFieldInColumns(@NotNull ASTNode child)
{
    // The whole idea of variable declarations alignment is that complete declaration blocks which children are to be aligned hold
    // reference to the same AlignmentStrategy object, hence, reuse the same Alignment objects. So, there is no point in checking
    // if it's necessary to align sub-blocks if shared strategy is not defined.
    if(!mySettings.ALIGN_GROUP_FIELD_DECLARATIONS)
    {
        return false;
    }

    IElementType childType = child.getElementType();

    // We don't want to align subsequent identifiers in single-line declarations like 'int i1, i2, i3'. I.e. only 'i1'
    // should be aligned then.
    ASTNode previousNode = FormatterUtil.getPreviousNonWhitespaceSibling(child);
    if(childType == JavaTokenType.IDENTIFIER && (previousNode == null || previousNode.getElementType() == JavaTokenType.COMMA))
    {
        return false;
    }

    return true;
}
项目:consulo-java    文件:AbstractJavaBlock.java   
@Nullable
private ASTNode processEnumBlock(@NotNull List<Block> result, @Nullable ASTNode child, ASTNode last)
{
    final WrappingStrategy wrappingStrategy = WrappingStrategy.createDoNotWrapCommaStrategy(Wrap.createWrap(getWrapType(mySettings.ENUM_CONSTANTS_WRAP), true));
    while(child != null)
    {
        if(!FormatterUtil.containsWhiteSpacesOnly(child) && child.getTextLength() > 0)
        {
            result.add(createJavaBlock(child, mySettings, myJavaSettings, Indent.getNormalIndent(), wrappingStrategy.getWrap(child.getElementType()), AlignmentStrategy.getNullStrategy()));
            if(child == last)
            {
                return child;
            }
        }
        child = child.getTreeNext();
    }
    return null;
}
项目:consulo-java    文件:DocCommentBlock.java   
@Override
protected List<Block> buildChildren()
{
    final ArrayList<Block> result = new ArrayList<>();

    ASTNode child = myNode.getFirstChildNode();
    while(child != null)
    {
        if(child.getElementType() == JavaDocTokenType.DOC_COMMENT_START)
        {
            result.add(createJavaBlock(child, mySettings, myJavaSettings, Indent.getNoneIndent(), null, AlignmentStrategy.getNullStrategy()));
        }
        else if(!FormatterUtil.containsWhiteSpacesOnly(child) && !child.getText().trim().isEmpty())
        {
            result.add(createJavaBlock(child, mySettings, myJavaSettings, Indent.getSpaceIndent(1), null, AlignmentStrategy.getNullStrategy()));
        }
        child = child.getTreeNext();
    }
    return result;

}
项目:consulo-java    文件:BlockContainingJavaBlock.java   
private static boolean isPartOfCodeBlock(final ASTNode child)
{
    if(child == null)
    {
        return false;
    }
    if(child.getElementType() == JavaElementType.BLOCK_STATEMENT)
    {
        return true;
    }
    if(child.getElementType() == JavaElementType.CODE_BLOCK)
    {
        return true;
    }

    if(FormatterUtil.containsWhiteSpacesOnly(child))
    {
        return isPartOfCodeBlock(child.getTreeNext());
    }
    if(child.getElementType() == JavaTokenType.END_OF_LINE_COMMENT)
    {
        return isPartOfCodeBlock(child.getTreeNext());
    }
    return child.getElementType() == JavaDocElementType.DOC_COMMENT;
}
项目:consulo-java    文件:ChildrenBlocksBuilder.java   
public List<Block> buildNodeChildBlocks(ASTNode node, BlockFactory factory)
{
    List<Block> blocks = ContainerUtil.newArrayList();

    for(ASTNode child : node.getChildren(null))
    {
        if(FormatterUtil.isWhitespaceOrEmpty(child) || child.getTextLength() == 0)
        {
            continue;
        }

        Alignment alignment = myConfig.getAlignment(child);

        IElementType type = child.getElementType();
        Indent indent = myConfig.getIndent(type);
        Wrap wrap = myConfig.getWrap(type);

        blocks.add(factory.createBlock(child, indent, alignment, wrap));
    }

    return blocks;
}
项目:AppleScript-IDEA    文件:AppleScriptFormattingModelBuilder.java   
@Override
protected String replaceWithPsiInLeaf(TextRange textRange, String whiteSpace, ASTNode leafElement) {
  if (!myCanModifyAllWhiteSpaces) {
    if (AppleScriptPsiImplUtil.isWhiteSpaceOrNls(leafElement)) return null;
  }

  IElementType elementTypeToUse = TokenType.WHITE_SPACE;
  ASTNode prevNode = TreeUtil.prevLeaf(leafElement);
  if (prevNode != null && AppleScriptPsiImplUtil.isWhiteSpaceOrNls(prevNode)) {
    elementTypeToUse = prevNode.getElementType();
  }
  FormatterUtil.replaceWhiteSpace(whiteSpace, leafElement, elementTypeToUse, textRange);
  return whiteSpace;
}
项目:protobuf-jetbrains-plugin    文件:StatementBlock.java   
@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;
}
项目:protobuf-jetbrains-plugin    文件:ProtoFileBlock.java   
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();
    }
}
项目: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;
}