Package org.eclipse.jdt.internal.formatter.align

Examples of org.eclipse.jdt.internal.formatter.align.Alignment


    try {
      this.lastBinaryExpressionAlignmentBreakIndentation = 0;
      if ((builder.realFragmentsSize() > 1 || fragmentsSize > 4) && numberOfParens == 0) {
        int scribeLine = this.scribe.line;
        this.scribe.printComment();
        Alignment binaryExpressionAlignment = this.scribe.createAlignment(
            Alignment.BINARY_EXPRESSION,
            this.preferences.alignment_for_binary_expression,
            Alignment.R_OUTERMOST,
            fragmentsSize,
            this.scribe.scanner.currentPosition);
View Full Code Here


      final int newLinesBeforeField = this.preferences.blank_lines_before_field;
      if (newLinesBeforeField > 0) {
        this.scribe.printEmptyLines(newLinesBeforeField);
      }
    }
    Alignment memberAlignment = this.scribe.getMemberAlignment();

        this.scribe.printComment();
    this.scribe.printModifiers(fieldDeclaration.annotations, this, ICodeFormatterConstants.ANNOTATION_ON_FIELD);
    this.scribe.space();
    /*
     * Field type
     */
    fieldDeclaration.type.traverse(this, scope);

    /*
     * Field name
     */
    this.scribe.alignFragment(memberAlignment, 0);

    this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, true);

    /*
     * Check for extra dimensions
     */
    int extraDimensions = getDimensions();
    if (extraDimensions != 0) {
       for (int i = 0; i < extraDimensions; i++) {
         this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET);
         this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET);
       }
    }

    /*
     * Field initialization
     */
    final Expression initialization = fieldDeclaration.initialization;
    if (initialization != null) {
      this.scribe.alignFragment(memberAlignment, 1);
      this.scribe.printNextToken(TerminalTokens.TokenNameEQUAL, this.preferences.insert_space_before_assignment_operator);
      if (this.preferences.insert_space_after_assignment_operator) {
        this.scribe.space();
      }
      Alignment assignmentAlignment = this.scribe.createAlignment(
          Alignment.FIELD_DECLARATION_ASSIGNMENT,
          this.preferences.alignment_for_assignment,
          Alignment.R_INNERMOST,
          1,
          this.scribe.scanner.currentPosition);
View Full Code Here

      final int newLinesBeforeField = this.preferences.blank_lines_before_field;
      if (newLinesBeforeField > 0) {
        this.scribe.printEmptyLines(newLinesBeforeField);
      }
    }
    Alignment fieldAlignment = this.scribe.getMemberAlignment();

        this.scribe.printComment();
    this.scribe.printModifiers(multiFieldDeclaration.annotations, this, ICodeFormatterConstants.ANNOTATION_ON_FIELD);
    this.scribe.space();

    multiFieldDeclaration.declarations[0].type.traverse(this, scope);

    final int multipleFieldDeclarationsLength = multiFieldDeclaration.declarations.length;

    Alignment multiFieldDeclarationsAlignment =this.scribe.createAlignment(
        Alignment.MULTIPLE_FIELD,
        this.preferences.alignment_for_multiple_fields,
        multipleFieldDeclarationsLength - 1,
        this.scribe.scanner.currentPosition);
    this.scribe.enterAlignment(multiFieldDeclarationsAlignment);
View Full Code Here

    /*
     * Superclass
     */
    final TypeReference superclass = typeDeclaration.superclass;
    if (superclass != null) {
      Alignment superclassAlignment =this.scribe.createAlignment(
          Alignment.SUPER_CLASS,
          this.preferences.alignment_for_superclass_in_type_declaration,
          2,
          this.scribe.scanner.currentPosition);
      this.scribe.enterAlignment(superclassAlignment);
      boolean ok = false;
      do {
        try {
          this.scribe.alignFragment(superclassAlignment, 0);
          this.scribe.printNextToken(TerminalTokens.TokenNameextends, true);
          this.scribe.alignFragment(superclassAlignment, 1);
          this.scribe.space();
          superclass.traverse(this, typeDeclaration.scope);
          ok = true;
        } catch (AlignmentException e) {
          this.scribe.redoAlignment(e);
        }
      } while (!ok);
      this.scribe.exitAlignment(superclassAlignment, true);
    }

    /*
     * Super Interfaces
     */
    final TypeReference[] superInterfaces = typeDeclaration.superInterfaces;
    if (superInterfaces != null) {
      int alignment_for_superinterfaces;
      int kind = TypeDeclaration.kind(typeDeclaration.modifiers);
      switch(kind) {
        case TypeDeclaration.ENUM_DECL :
          alignment_for_superinterfaces = this.preferences.alignment_for_superinterfaces_in_enum_declaration;
          break;
        default:
          alignment_for_superinterfaces = this.preferences.alignment_for_superinterfaces_in_type_declaration;
          break;
      }
      int superInterfaceLength = superInterfaces.length;
      Alignment interfaceAlignment =this.scribe.createAlignment(
          Alignment.SUPER_INTERFACES,
          alignment_for_superinterfaces,
          superInterfaceLength+1// implements token is first fragment
          this.scribe.scanner.currentPosition);
      this.scribe.enterAlignment(interfaceAlignment);
      boolean ok = false;
      do {
        try {
          this.scribe.alignFragment(interfaceAlignment, 0);
          if (kind == TypeDeclaration.INTERFACE_DECL) {
            this.scribe.printNextToken(TerminalTokens.TokenNameextends, true);
          } else  {
            this.scribe.printNextToken(TerminalTokens.TokenNameimplements, true);
          }
          for (int i = 0; i < superInterfaceLength; i++) {
            if (i > 0) {
              this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_superinterfaces);
              this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
              this.scribe.alignFragment(interfaceAlignment, i+1);
              if (this.preferences.insert_space_after_comma_in_superinterfaces) {
                this.scribe.space();
              }
              superInterfaces[i].traverse(this, typeDeclaration.scope);
            } else {
              this.scribe.alignFragment(interfaceAlignment, i+1);
              this.scribe.space();
              superInterfaces[i].traverse(this, typeDeclaration.scope);
            }
          }
          ok = true;
        } catch (AlignmentException e) {
          this.scribe.redoAlignment(e);
        }
      } while (!ok);
      this.scribe.exitAlignment(interfaceAlignment, true);
    }

    /*
     * Type body
     */
    String class_declaration_brace;
    boolean space_before_opening_brace;
    int kind = TypeDeclaration.kind(typeDeclaration.modifiers);
    switch(kind) {
      case TypeDeclaration.ENUM_DECL :
        class_declaration_brace = this.preferences.brace_position_for_enum_declaration;
        space_before_opening_brace = this.preferences.insert_space_before_opening_brace_in_enum_declaration;
        break;
      case TypeDeclaration.ANNOTATION_TYPE_DECL :
        class_declaration_brace = this.preferences.brace_position_for_annotation_type_declaration;
        space_before_opening_brace =  this.preferences.insert_space_before_opening_brace_in_annotation_type_declaration;
        break;
      default:
        class_declaration_brace = this.preferences.brace_position_for_type_declaration;
        space_before_opening_brace = this.preferences.insert_space_before_opening_brace_in_type_declaration;
        break;
    }
    formatLeftCurlyBrace(line, class_declaration_brace);
    formatTypeOpeningBrace(class_declaration_brace, space_before_opening_brace, typeDeclaration);

    boolean indent_body_declarations_compare_to_header;
    switch(kind) {
      case TypeDeclaration.ENUM_DECL :
        indent_body_declarations_compare_to_header = this.preferences.indent_body_declarations_compare_to_enum_declaration_header;
        break;
      case TypeDeclaration.ANNOTATION_TYPE_DECL :
        indent_body_declarations_compare_to_header = this.preferences.indent_body_declarations_compare_to_annotation_declaration_header;
        break;
      default:
        indent_body_declarations_compare_to_header = this.preferences.indent_body_declarations_compare_to_type_header;
        break;
    }
    if (indent_body_declarations_compare_to_header) {
      this.scribe.indent();
    }

    if (kind == TypeDeclaration.ENUM_DECL) {
      FieldDeclaration[] fieldDeclarations = typeDeclaration.fields;
      boolean hasConstants = false;
      int length = fieldDeclarations != null ? fieldDeclarations.length : 0;
      int enumConstantsLength = 0;
      if (fieldDeclarations != null) {
        for (int i = 0; i < length; i++) {
          FieldDeclaration fieldDeclaration = fieldDeclarations[i];
          if (fieldDeclaration.getKind() == AbstractVariableDeclaration.ENUM_CONSTANT) {
            enumConstantsLength++;
          } else {
            break;
          }
        }
        hasConstants = enumConstantsLength != 0;
        if (enumConstantsLength > 1) {
          Alignment enumConstantsAlignment = this.scribe.createAlignment(
              Alignment.ENUM_CONSTANTS,
              this.preferences.alignment_for_enum_constants,
              enumConstantsLength,
              this.scribe.scanner.currentPosition,
              0, // we don't want to indent enum constants when splitting to a new line
View Full Code Here

      if (arguments != null) {
        if (this.preferences.insert_space_after_opening_paren_in_method_invocation) {
          this.scribe.space();
        }
        int argumentLength = arguments.length;
        Alignment argumentsAlignment = this.scribe.createAlignment(
            Alignment.MESSAGE_ARGUMENTS,
            this.preferences.alignment_for_arguments_in_method_invocation,
            Alignment.R_OUTERMOST,
            argumentLength,
            this.scribe.scanner.currentPosition);
        this.scribe.enterAlignment(argumentsAlignment);
        boolean okForArguments = false;
        do {
          try {
            for (int j = 0; j < argumentLength; j++) {
              if (j > 0) {
                this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_method_invocation_arguments);
                this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
              }
              this.scribe.alignFragment(argumentsAlignment, j);
              if (j > 0 && this.preferences.insert_space_after_comma_in_method_invocation_arguments) {
                this.scribe.space();
              }
              arguments[j].traverse(this, scope);
            }
            okForArguments = true;
          } catch (AlignmentException e) {
            this.scribe.redoAlignment(e);
          }
        } while (!okForArguments);
        this.scribe.exitAlignment(argumentsAlignment, true);
        this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_method_invocation);
      } else {
        this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_between_empty_parens_in_method_invocation);
      }
      if (numberOfParens > 0) {
        manageClosingParenthesizedExpression(currentMessageSend, numberOfParens);
      }
      startingPositionInCascade = 2;
    }
    int tieBreakRule = this.preferences.wrap_outer_expressions_when_nested && size-startingPositionInCascade > 2
      ? Alignment.R_OUTERMOST
      : Alignment.R_INNERMOST;
    Alignment cascadingMessageSendAlignment =
      this.scribe.createAlignment(
        Alignment.CASCADING_MESSAGE_SEND,
        this.preferences.alignment_for_selector_in_method_invocation,
        tieBreakRule,
        size,
        this.scribe.scanner.currentPosition);
    this.scribe.enterAlignment(cascadingMessageSendAlignment);
    boolean ok = false;
    boolean setStartingColumn = true;
    switch (this.preferences.alignment_for_arguments_in_method_invocation & Alignment.SPLIT_MASK) {
      case Alignment.M_COMPACT_FIRST_BREAK_SPLIT:
      case Alignment.M_NEXT_SHIFTED_SPLIT:
      case Alignment.M_ONE_PER_LINE_SPLIT:
        setStartingColumn = false;
        break;
    }
    do {
      if (setStartingColumn) {
        cascadingMessageSendAlignment.startingColumn = this.scribe.column;
      }
      try {
        this.scribe.alignFragment(cascadingMessageSendAlignment, 0);
        this.scribe.printNextToken(TerminalTokens.TokenNameDOT);
        for (int i = startingPositionInCascade; i < size; i++) {
          MessageSend currentMessageSend = fragments[i];
          final int numberOfParens = (currentMessageSend.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT;
          if (numberOfParens > 0) {
            manageOpeningParenthesizedExpression(currentMessageSend, numberOfParens);
          }
          TypeReference[] typeArguments = currentMessageSend.typeArguments;
          if (typeArguments != null) {
              this.scribe.printNextToken(TerminalTokens.TokenNameLESS, this.preferences.insert_space_before_opening_angle_bracket_in_type_arguments);
              if (this.preferences.insert_space_after_opening_angle_bracket_in_type_arguments) {
                this.scribe.space();
              }
              int length = typeArguments.length;
              for (int j = 0; j < length - 1; j++) {
                typeArguments[j].traverse(this, scope);
                this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_type_arguments);
                if (this.preferences.insert_space_after_comma_in_type_arguments) {
                  this.scribe.space();
                }
              }
              typeArguments[length - 1].traverse(this, scope);
              if (isClosingGenericToken()) {
                this.scribe.printNextToken(CLOSING_GENERICS_EXPECTEDTOKENS, this.preferences.insert_space_before_closing_angle_bracket_in_type_arguments);
              }
              if (this.preferences.insert_space_after_closing_angle_bracket_in_type_arguments) {
                this.scribe.space();
              }
          }
          ASTNode[] arguments = currentMessageSend.arguments;
          this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier); // selector
          this.scribe.printNextToken(TerminalTokens.TokenNameLPAREN, this.preferences.insert_space_before_opening_paren_in_method_invocation);
          if (arguments != null) {
            if (this.preferences.insert_space_after_opening_paren_in_method_invocation) {
              this.scribe.space();
            }
            int argumentLength = arguments.length;
            int alignmentMode = this.preferences.alignment_for_arguments_in_method_invocation;
            Alignment argumentsAlignment = this.scribe.createAlignment(
                Alignment.MESSAGE_ARGUMENTS,
                alignmentMode,
                Alignment.R_OUTERMOST,
                argumentLength,
                this.scribe.scanner.currentPosition);
View Full Code Here

   */
  private void formatClassBodyDeclarations(ASTNode[] nodes) {
    final int FIELD = 1, METHOD = 2, TYPE = 3;
    this.scribe.lastNumberOfNewLines = 1;
    ASTNode[] mergedNodes = computeMergedMemberDeclarations(nodes);
    Alignment memberAlignment = this.scribe.createMemberAlignment(
        Alignment.TYPE_MEMBERS,
        this.preferences.align_type_members_on_columns ? Alignment.M_MULTICOLUMN : Alignment.M_NO_ALIGNMENT,
        4,
        this.scribe.scanner.currentPosition);
    this.scribe.enterMemberAlignment(memberAlignment);
    boolean isChunkStart = false;
    boolean ok = false;
    int startIndex = 0;
    do {
      try {
        for (int i = startIndex, max = mergedNodes.length; i < max; i++) {
          ASTNode member = mergedNodes[i];
          if (member instanceof FieldDeclaration) {
            isChunkStart = memberAlignment.checkChunkStart(FIELD, i, this.scribe.scanner.currentPosition);
            if (member instanceof MultiFieldDeclaration){
              MultiFieldDeclaration multiField = (MultiFieldDeclaration) member;
              format(multiField, this, null, isChunkStart, i == 0);
            } else if (member instanceof Initializer) {
              int newLineBeforeChunk = isChunkStart ? this.preferences.blank_lines_before_new_chunk : 0;
              if (newLineBeforeChunk > 0 && i != 0) {
                this.scribe.printEmptyLines(newLineBeforeChunk);
              } else if (i == 0) {
                int newLinesBeforeFirstClassBodyDeclaration = this.preferences.blank_lines_before_first_class_body_declaration;
                if (newLinesBeforeFirstClassBodyDeclaration > 0) {
                  this.scribe.printEmptyLines(newLinesBeforeFirstClassBodyDeclaration);
                }
              }
              Initializer initializer = (Initializer) member;
              initializer.traverse(this, null);
            } else {
              FieldDeclaration field = (FieldDeclaration) member;
              format(field, this, null, isChunkStart, i == 0);
            }
          } else if (member instanceof AbstractMethodDeclaration) {
            isChunkStart = memberAlignment.checkChunkStart(METHOD, i, this.scribe.scanner.currentPosition);
            format((AbstractMethodDeclaration) member, null, isChunkStart, i == 0);
          } else {
            isChunkStart = memberAlignment.checkChunkStart(TYPE, i, this.scribe.scanner.currentPosition);
            format((TypeDeclaration)member, null, isChunkStart, i == 0);
          }
          while (isNextToken(TerminalTokens.TokenNameSEMICOLON)) {
            this.scribe.printNextToken(TerminalTokens.TokenNameSEMICOLON, this.preferences.insert_space_before_semicolon);
            this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
View Full Code Here

       */
      this.scribe.printNextToken(TerminalTokens.TokenNameEQUAL, this.preferences.insert_space_before_assignment_operator);
      if (this.preferences.insert_space_after_assignment_operator) {
        this.scribe.space();
      }
      Alignment assignmentAlignment = this.scribe.createAlignment(
          Alignment.LOCAL_DECLARATION_ASSIGNMENT,
          this.preferences.alignment_for_assignment,
          Alignment.R_OUTERMOST,
          1,
          this.scribe.scanner.currentPosition);
View Full Code Here

        this.scribe.space();
      }
      int argumentsLength = arguments.length;
      if (argumentsLength > 1) {
        int alignmentMode = this.preferences.alignment_for_arguments_in_method_invocation;
        Alignment argumentsAlignment = this.scribe.createAlignment(
            Alignment.MESSAGE_ARGUMENTS,
            alignmentMode,
            argumentsLength,
            this.scribe.scanner.currentPosition);
        this.scribe.enterAlignment(argumentsAlignment);
        boolean ok = false;
        do {
          switch (alignmentMode & Alignment.SPLIT_MASK) {
            case Alignment.M_COMPACT_SPLIT:
            case Alignment.M_NEXT_PER_LINE_SPLIT:
              argumentsAlignment.startingColumn = this.scribe.column;
              break;
          }
          try {
            for (int i = 0; i < argumentsLength; i++) {
              if (i > 0) {
                this.scribe.printNextToken(TerminalTokens.TokenNameCOMMA, this.preferences.insert_space_before_comma_in_method_invocation_arguments);
                this.scribe.printComment(CodeFormatter.K_UNKNOWN, Scribe.BASIC_TRAILING_COMMENT);
                if (this.scribe.lastNumberOfNewLines == 1) {
                  // a new line has been inserted while printing the comment
                  // hence we need to use the break indentation level before printing next token...
                  this.scribe.indentationLevel = argumentsAlignment.breakIndentationLevel;
                }
              }
              this.scribe.alignFragment(argumentsAlignment, i);
              if (i > 0 && this.preferences.insert_space_after_comma_in_method_invocation_arguments) {
                this.scribe.space();
              }
              int fragmentIndentation = 0;
              if (i == 0) {
                int wrappedIndex = argumentsAlignment.wrappedIndex();
                if (wrappedIndex >= 0) {
                  fragmentIndentation = argumentsAlignment.fragmentIndentations[wrappedIndex];
                  if ((argumentsAlignment.mode & Alignment.M_INDENT_ON_COLUMN) != 0 && fragmentIndentation > 0) {
                    this.scribe.indentationLevel = fragmentIndentation;
                  }
View Full Code Here

    if (length > 0) {
      this.scribe.printNextToken(TerminalTokens.TokenNameLPAREN, spaceBeforeOpenParen);
      if (spaceBeforeFirstResource) {
        this.scribe.space();
      }
      Alignment resourcesAlignment = this.scribe.createAlignment(
          Alignment.TRY_RESOURCES,
          tryResourcesAligment,
          // make sure alignment options for try with resources takes precedence
          Alignment.R_OUTERMOST,
          length,
View Full Code Here

    if (arguments != null) {
      if (spaceBeforeFirstParameter) {
        this.scribe.space();
      }
      int argumentLength = arguments.length;
      Alignment argumentsAlignment = this.scribe.createAlignment(
          Alignment.METHOD_ARGUMENTS,
          methodDeclarationParametersAlignment,
          argumentLength,
          this.scribe.scanner.currentPosition);
      this.scribe.enterAlignment(argumentsAlignment);
View Full Code Here

TOP

Related Classes of org.eclipse.jdt.internal.formatter.align.Alignment

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.