private Indent calcCurrentIndent(final ASTNode child, final int state) {
    IElementType elementType = child.getElementType();
    if (isRBrace(child) || elementType == JavaTokenType.AT) {
      return Indent.getNoneIndent();
    }

    if (state == BEFORE_FIRST) return Indent.getNoneIndent();

    if (elementType == JavaElementType.SWITCH_LABEL_STATEMENT) {
      return getCodeBlockInternalIndent(myChildrenIndent);
    }
    if (state == BEFORE_LBRACE) {
      if (isLBrace(child)
          || elementType == JavaTokenType.CLASS_KEYWORD
          || elementType == JavaTokenType.INTERFACE_KEYWORD
          || elementType == JavaTokenType.IDENTIFIER
          || elementType == JavaTokenType.ENUM_KEYWORD) {
        return Indent.getNoneIndent();
      } else {
        return Indent.getContinuationIndent(myIndentSettings.USE_RELATIVE_INDENTS);
      }
    } else {
      if (isRBrace(child)) {
        return Indent.getNoneIndent();
      } else {
        return getCodeBlockInternalIndent(myChildrenIndent);
      }
    }
  }
示例#2
0
 @Override
 public Indent getIndent() {
   PsiElement element = myNode.getPsi();
   if (isNode()) {
     return Indent.getIndent(Indent.Type.NORMAL, true, true);
   }
   return Indent.getNoneIndent();
 }
示例#3
0
文件: Tag.java 项目: hemantgp/bnd
 public void printComments(Indent indent, PrintWriter pw) {
   for (String comment : this.comments) {
     Indent nextIndent = indent.next();
     nextIndent.print(pw);
     pw.print("<!-- ");
     pw.print(escape(comment));
     pw.print(" -->");
   }
 }
 @Override
 @NotNull
 public ChildAttributes getChildAttributes(final int newChildIndex) {
   if (isAfter(
       newChildIndex,
       new IElementType[] {JavaDocElementType.DOC_COMMENT, JavaElementType.MODIFIER_LIST})) {
     return new ChildAttributes(Indent.getNoneIndent(), null);
   } else {
     if (getSubBlocks().size() == newChildIndex) {
       return new ChildAttributes(Indent.getNoneIndent(), null);
     } else {
       return new ChildAttributes(getCodeBlockInternalIndent(myChildrenIndent), null);
     }
   }
 }
  private SyntheticCodeBlock createCaseSectionBlock(
      final ArrayList<Block> localResult,
      final Alignment childAlignment,
      final Indent indent,
      final Wrap childWrap) {
    final SyntheticCodeBlock result =
        new SyntheticCodeBlock(
            localResult, childAlignment, getSettings(), myJavaSettings, indent, childWrap) {
          @Override
          @NotNull
          public ChildAttributes getChildAttributes(final int newChildIndex) {
            IElementType prevElementType = null;
            if (newChildIndex > 0) {
              final Block previousBlock = getSubBlocks().get(newChildIndex - 1);
              if (previousBlock instanceof AbstractBlock) {
                prevElementType = ((AbstractBlock) previousBlock).getNode().getElementType();
              }
            }

            if (prevElementType == JavaElementType.BLOCK_STATEMENT
                || prevElementType == JavaElementType.BREAK_STATEMENT
                || prevElementType == JavaElementType.RETURN_STATEMENT) {
              return new ChildAttributes(Indent.getNoneIndent(), null);
            } else {
              return super.getChildAttributes(newChildIndex);
            }
          }
        };
    result.setChildAttributes(new ChildAttributes(Indent.getNormalIndent(), null));
    result.setIsIncomplete(true);
    return result;
  }
示例#6
0
文件: Tag.java 项目: hemantgp/bnd
  /** Convenience method to print a string nicely and does character conversion to entities. */
  void formatted(PrintWriter pw, Indent indent, int width, String s) {
    int pos = width + 1;
    s = s.trim();

    for (int i = 0; i < s.length(); i++) {
      char c = s.charAt(i);
      if (i == 0 || (Character.isWhitespace(c) && pos > width - 3)) {
        indent.print(pw);
        pos = 0;
      }
      switch (c) {
        case '<':
          pw.print("&lt;");
          pos += 4;
          break;
        case '>':
          pw.print("&gt;");
          pos += 4;
          break;
        case '&':
          pw.print("&amp;");
          pos += 5;
          break;
        default:
          pw.print(c);
          pos++;
          break;
      }
    }
  }
  private RestBlock buildSubBlock(ASTNode child) {
    IElementType parentType = myNode.getElementType();
    IElementType childType = child.getElementType();
    IElementType grandparentType =
        myNode.getTreeParent() == null ? null : myNode.getTreeParent().getElementType();
    Wrap wrap = null;
    Indent childIndent = Indent.getNoneIndent();
    Alignment childAlignment = null;

    if (grandparentType == RestElementTypes.FIELD_LIST
        && parentType == RestElementTypes.LINE_TEXT
        && childType == RestTokenTypes.LINE) {
      childIndent = Indent.getNormalIndent();
    }
    return new RestBlock(this, child, childAlignment, childIndent, wrap);
  }
 private static Block generateGoFileBlock(ASTNode node, CommonCodeStyleSettings settings) {
   return new GoFileBlock(
       node,
       Alignment.createAlignment(),
       Indent.getAbsoluteNoneIndent(),
       Wrap.createWrap(WrapType.NONE, false),
       settings);
 }
 private void setDefaultIndents(final List<AbstractBlockWrapper> list) {
   if (!list.isEmpty()) {
     for (AbstractBlockWrapper wrapper : list) {
       if (wrapper.getIndent() == null) {
         wrapper.setIndent(
             (IndentImpl)
                 Indent.getContinuationWithoutFirstIndent(myOptions.USE_RELATIVE_INDENTS));
       }
     }
   }
 }
示例#10
0
文件: Tag.java 项目: hemantgp/bnd
 public void printContents(Indent indent, PrintWriter pw) {
   for (Object content : this.content) {
     Indent nextIndent = indent.next();
     if (content instanceof String) {
       formatted(pw, nextIndent, 60, escape((String) content));
     } else if (content instanceof Tag) {
       Tag tag = (Tag) content;
       tag.print(nextIndent, pw);
     }
   }
 }
示例#11
0
 private List<Block> buildSubBlocks() {
   final List<Block> blocks = new ArrayList<Block>();
   Indent prevIndent = Indent.getNoneIndent();
   //        if (myNode.getElementType() == CoqTypes.PROOFPHRASE) return
   // Collections.unmodifiableList(blocks);
   for (ASTNode child = myNode.getFirstChildNode(); child != null; child = child.getTreeNext()) {
     if (!shouldCreateBlockFor(child)) continue;
     Block b = createChildBlock(child, Alignment.createAlignment(), prevIndent);
     prevIndent = b.getIndent();
     blocks.add(b);
   }
   return Collections.unmodifiableList(blocks);
 }
  @Nullable
  private ASTNode processCaseAndStatementAfter(
      final ArrayList<Block> result,
      ASTNode child,
      final Alignment childAlignment,
      final Wrap childWrap,
      final Indent indent) {
    final ArrayList<Block> localResult = new ArrayList<Block>();
    processChild(
        localResult, child, AlignmentStrategy.getNullStrategy(), null, Indent.getNoneIndent());
    child = child.getTreeNext();
    Indent childIndent = Indent.getNormalIndent();
    while (child != null) {
      if (child.getElementType() == JavaElementType.SWITCH_LABEL_STATEMENT || isRBrace(child)) {
        result.add(createCaseSectionBlock(localResult, childAlignment, indent, childWrap));
        return child.getTreePrev();
      }

      if (!FormatterUtil.containsWhiteSpacesOnly(child)) {

        if (child.getElementType() == JavaElementType.BLOCK_STATEMENT) {
          childIndent = Indent.getNoneIndent();
        }

        boolean breakOrReturn = isBreakOrReturn(child);
        processChild(localResult, child, AlignmentStrategy.getNullStrategy(), null, childIndent);
        if (breakOrReturn) {
          result.add(createCaseSectionBlock(localResult, childAlignment, indent, childWrap));
          return child;
        }
      }

      child = child.getTreeNext();
    }
    result.add(createCaseSectionBlock(localResult, childAlignment, indent, childWrap));
    return null;
  }
示例#13
0
文件: Tag.java 项目: hemantgp/bnd
  public void printOpen(Indent indent, PrintWriter pw, boolean andClose) {
    indent.print(pw);
    pw.print('<');
    pw.print(name);

    String quote = "\"";
    for (Map.Entry<String, String> e : attributes.entrySet()) {
      String key = e.getKey();
      String value = escape(e.getValue());
      pw.print(' ');
      pw.print(key);
      pw.print("=");
      pw.print(quote);
      pw.print(value.replaceAll("\"", "&quot;"));
      pw.print(quote);
    }

    if (andClose) pw.print("/>");
    else pw.print('>');
  }
 @NotNull
 @Override
 public FormattingModel createModel(
     @NotNull PsiElement element,
     @NotNull CodeStyleSettings settings,
     @NotNull FormattingMode mode) {
   if (DUMP_FORMATTING_AST) {
     ASTNode fileNode = element.getContainingFile().getNode();
     System.out.println("AST tree for " + element.getContainingFile().getName() + ":");
     printAST(fileNode, 0);
   }
   final PyBlockContext context =
       new PyBlockContext(settings, createSpacingBuilder(settings), mode);
   final PyBlock block =
       new PyBlock(null, element.getNode(), null, Indent.getNoneIndent(), null, context);
   if (DUMP_FORMATTING_AST) {
     FormattingModelDumper.dumpFormattingModel(block, 2, System.out);
   }
   return FormattingModelProvider.createFormattingModelForPsiFile(
       element.getContainingFile(), block, settings);
 }
示例#15
0
 public SOF parse(StringBuffer buf) {
   Indent.clearStats();
   ArrayList<LexicalNode> nodeList = new ArrayList<LexicalNode>();
   char[] content = buf.toString().toCharArray();
   int start = 0;
   int lastMatch = 0;
   int length = 0;
   SOF sof = new SOF(new Interval(0, 0));
   nodeList.add(sof);
   while (start < content.length) {
     if ((length = match(content, start, buf, nodeList, lastMatch)) > 0) {
       start += length;
       lastMatch = start;
     } else start++;
   }
   if (start - lastMatch - 1 > 0) {
     createWater(buf, lastMatch + 1, start, nodeList);
   }
   nodeList.add(
       new EOF(
           nodeList.get(nodeList.size() - 1),
           new Interval(content.length - 1, content.length - 1)));
   return sof;
 }
  private CompositeBlockWrapper buildCompositeBlock(
      final Block rootBlock,
      @Nullable final CompositeBlockWrapper parent,
      final int index,
      @Nullable final WrapImpl currentWrapParent,
      boolean rootBlockIsRightBlock) {
    final CompositeBlockWrapper wrappedRootBlock =
        new CompositeBlockWrapper(rootBlock, myCurrentWhiteSpace, parent);
    if (index == 0) {
      wrappedRootBlock.arrangeParentTextRange();
    }

    if (myRootBlockWrapper == null) {
      myRootBlockWrapper = wrappedRootBlock;
      myRootBlockWrapper.setIndent((IndentImpl) Indent.getNoneIndent());
    }
    boolean blocksMayBeOfInterest = false;

    if (myPositionOfInterest != -1) {
      myResult.put(wrappedRootBlock, rootBlock);
      blocksMayBeOfInterest = true;
    }

    final boolean blocksAreReadOnly =
        rootBlock instanceof ReadOnlyBlockContainer || blocksMayBeOfInterest;

    State state =
        new State(
            rootBlock,
            wrappedRootBlock,
            currentWrapParent,
            blocksAreReadOnly,
            rootBlockIsRightBlock);
    myStates.push(state);
    return wrappedRootBlock;
  }
  public static Block generateBlock(
      ASTNode node, Indent indent, Alignment alignment, CommonCodeStyleSettings styleSettings) {
    PsiElement psi = node.getPsi();
    if (psi instanceof GoBlockStatement)
      return new GoBlockStatementBlock(node, indent, styleSettings);

    if (psi instanceof GoFile) return generateGoFileBlock(node, styleSettings);

    if (psi instanceof GoPackageDeclaration) return generatePackageBlock(node, styleSettings);

    if (psi instanceof GoBinaryExpression) {
      return new GoBinaryExpressionBlock(node, alignment, NO_WRAP, styleSettings);
    }

    if (psi instanceof GoFunctionDeclaration) {
      return new GoFunctionDeclarationBlock(node, alignment, indent, styleSettings);
    }

    IElementType elementType = node.getElementType();
    if (elementType == GoTokenTypes.pLPAREN) {
      return new GoLeafBlock(node, null, indent, NO_WRAP, styleSettings);
    } else if (elementType == GoTokenTypes.pRCURLY) {
      if (node.getTreeParent().getElementType() == GoElementTypes.LITERAL_COMPOSITE_VALUE) {
        boolean inFunctionCall = false;
        ASTNode nodeParent = node;
        while (nodeParent != null) {
          if (nodeParent.getElementType() == GoElementTypes.CALL_OR_CONVERSION_EXPRESSION) {
            int indentTabSize =
                styleSettings.getIndentOptions() == null
                    ? 4
                    : styleSettings.getIndentOptions().INDENT_SIZE;
            return new GoLeafBlock(
                node, null, Indent.getSpaceIndent(indentTabSize * -1), NO_WRAP, styleSettings);
          }

          nodeParent = nodeParent.getTreeParent();
        }
      }
    } else if (elementType == GoTokenTypes.kPACKAGE || elementType == GoTokenTypes.oSEMI) {
      return new GoLeafBlock(
          node,
          null,
          Indent.getAbsoluteNoneIndent(),
          Wrap.createWrap(WrapType.NONE, false),
          styleSettings);
    } else if (GoTokenTypeSets.COMMENTS.contains(elementType)) {
      return new GoLeafBlock(
          node, alignment, indent, Wrap.createWrap(WrapType.NONE, false), styleSettings);
    } else if (ALIGN_LIST_BLOCK_STATEMENTS.contains(elementType)) {
      return new GoAssignListBlock(node, alignment, indent, styleSettings);
    } else if (elementType == GoElementTypes.TYPE_STRUCT) {
      return new GoTypeStructBlock(node, alignment, indent, styleSettings);
      //        } else if (elementType == GoElementTypes.TYPE_INTERFACE) {
      //            return new GoTypeInterfaceBlock(node, alignment, indent, styleSettings);
    } else if (elementType == GoElementTypes.EXPRESSION_LIST) {
      return new GoExpressionListBlock(node, alignment, indent, styleSettings);
    } else if (elementType == GoElementTypes.UNARY_EXPRESSION) {
      return new GoUnaryExpressionBlock(node, alignment, indent, NO_WRAP, styleSettings);
    } else if (GoElementTypes.FUNCTION_CALL_SETS.contains(elementType)) {
      return new GoCallOrConvExpressionBlock(node, alignment, indent, NO_WRAP, styleSettings);
    } else if (elementType == GoElementTypes.PARENTHESISED_EXPRESSION) {
      return new GoParenthesisedExpressionBlock(node, alignment, indent, styleSettings);
    } else if (elementType == GoElementTypes.LABELED_STATEMENT) {
      return new GoLabeledStatmentBlock(node, styleSettings);
    } else if (elementType == GoElementTypes.FUNCTION_PARAMETER_LIST) {
      return new GoFunctionParameterListBlock(node, indent, styleSettings);
    } else if (elementType == GoElementTypes.FUNCTION_PARAMETER) {
      return new GoFunctionParameterBlock(node, indent, styleSettings);
    }

    return new GoBlock(node, alignment, indent, NO_WRAP, styleSettings);
  }
示例#18
0
文件: Tag.java 项目: hemantgp/bnd
 public void printClose(Indent indent, PrintWriter pw) {
   indent.print(pw);
   pw.print("</");
   pw.print(name);
   pw.print('>');
 }
 @NotNull
 public ChildAttributes getChildAttributes(int newChildIndex) {
   return new ChildAttributes(Indent.getNoneIndent(), Alignment.createAlignment());
   // return ChildAttributes.DELEGATE_TO_NEXT_CHILD;
 }
 public static Block generateBlock(
     ASTNode node, Alignment alignment, CommonCodeStyleSettings settings) {
   return generateBlock(node, Indent.getNoneIndent(), alignment, settings);
 }
示例#21
0
 @NotNull
 @Override
 public ChildAttributes getChildAttributes(int newChildIndex) {
   return new ChildAttributes(Indent.getNoneIndent(), null);
 }