public boolean visit()

in ruta-ep-ide-ui/src/main/java/org/apache/uima/ruta/ide/formatter/RutaFormattedPrinter.java [159:293]


  public boolean visit(Statement s) throws Exception {

    appendComments(s);
    fillNewLines(s);

    if (s instanceof RutaDeclarationsStatement) {
      // append new lines before LONG DECLARATIONS
      RutaDeclarationsStatement decls = (RutaDeclarationsStatement) s;
      // if (decls.getDeclarationsCount() > NL_DECLS_COUNT) {
      // for (int i = 0; i < lines_before_long_declarations; i++) {
      // appendNewLine();
      // }
      // }
      // format declarations:
      // print type token
      appendIntoNewLine(document.get(decls.getTypeTokenStart(), decls.getTypeTokenEnd()) + " ");
      // print parent if available
      if (s instanceof RutaDeclareDeclarationsStatement) {
        RutaDeclareDeclarationsStatement dds = (RutaDeclareDeclarationsStatement) s;
        ASTNode p = dds.getParent();
        if (p != null) {
          append(p);
          append(" ");
        }
      }
      // print identifiers
      List<RutaAbstractDeclaration> declarations = decls.getDeclarations();
      traverseAstNodes(declarations);
      // print init expr
      if (decls.getInitExpr() != null) {
        append(EQUALS);
        decls.getInitExpr().traverse(this);
      }
      appendStatementEnd();
      return false;
    }
    if (s instanceof RutaRegExpRule) {
      // traverse into container RutaRule to format RuleElements
      if (!inBlockDeclaration) {
        appendNewLine();
      }
      // Rules always just consists of RuleElements: whitespace separation
      if (s.sourceEnd() - s.sourceStart() > 2 * maxLineLength) {
        inLargeRule = 1;
        indentLevel++;
      }
      RutaRule rule = (RutaRule) s;
      List<Expression> expressions = rule.getExpressions();
      if (expressions != null && !expressions.isEmpty()) {
        append(expressions.get(0));
        append(" " + THEN + " ");
        if (expressions.size() > 1) {
          for (int i = 1; i < expressions.size(); i++) {
            Expression expression = expressions.get(i);
            if (expression.getKind() == RutaTypeConstants.RUTA_TYPE_N
                    && i < expressions.size() - 1) {
              append(expression);
              append(EQUALS);
              append(expressions.get(++i));
            } else {
              append(expression);
            }
            if (i < expressions.size() - 1) {
              append(COMMA + " ");
            }
          }
        }
      }
      if (!inBlockDeclaration) {
        appendStatementEnd();
      }
      if (inLargeRule > 0) {
        indentLevel--;
        inLargeRule = 0;
      }
      return false;
    }

    if (s instanceof RutaRule) {
      // traverse into container RutaRule to format RuleElements
      if (!inBlockDeclaration) {
        appendNewLine();
      }
      // Rules always just consists of RuleElements: whitespace separation
      if (s.sourceEnd() - s.sourceStart() > 2 * maxLineLength) {
        inLargeRule = 1;
        indentLevel++;
      }
      RutaRule rule = (RutaRule) s;
      List<Expression> expressions = rule.getExpressions();
      String sep = "";
      traverseAstNodes(expressions, sep);
      if (!inBlockDeclaration) {
        appendStatementEnd();
      }
      if (inLargeRule > 0) {
        indentLevel--;
        inLargeRule = 0;
      }
      return false;
      // return true;
    }
    if (s instanceof RutaTypeDeclaration) {
      RutaTypeDeclaration rtd = (RutaTypeDeclaration) s;
      append(document.get(rtd.getNameStart(), rtd.getNameEnd()));
      List<RutaFeatureDeclaration> features = ((RutaTypeDeclaration) s).getFeatures();
      if (features != null && !features.isEmpty()) {
        append(PAR_OPEN);
        for (RutaFeatureDeclaration each : features) {
          append(each.getType());
          append(" ");
          append(each.getName());
          if (features.indexOf(each) < features.size() - 1) {
            append(COMMA + " ");
          }
        }
        append(PAR_CLOSE);
      }
      return false;
    }

    if (s instanceof Declaration && !(s instanceof RutaPackageDeclaration)) {
      append(s);
      return false;
    }
    if (s instanceof RutaPackageDeclaration) {
      append(s);
      appendStatementEnd();
      return false;
    }
    // append SEMIs for all other statements
    appendIntoNewLine(s);
    appendStatementEnd();
    return false;
  }