All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vertx.codetrans.CodeWriter Maven / Gradle / Ivy

There is a newer version: 4.1.0.Beta1
Show newest version
package io.vertx.codetrans;

/**
 * @author Julien Viet
 */
public class CodeWriter implements Appendable {

  private final Lang lang;
  private int indent = 0;
  private boolean first = true;
  private StringBuilder buffer = new StringBuilder();

  public CodeWriter(Lang lang) {
    this.lang = lang;
  }

  public Lang getLang() {
    return lang;
  }

  public void indent() {
    indent += 2;
  }

  public void unindent() {
    if (indent < 2) {
      throw new IllegalStateException();
    }
    indent -= 2;
  }

  public StringBuilder getBuffer() {
    return buffer;
  }

  @Override
  public CodeWriter append(CharSequence csq) {
    return append(csq, 0, csq.length());
  }

  @Override
  public CodeWriter append(CharSequence csq, int start, int end) {
    while (start < end) {
      append(csq.charAt(start++));
    }
    return this;
  }

  @Override
  public CodeWriter append(char c) {
    if (c == '\n') {
      first = true;
    } else if (first) {
      first = false;
      for (int i = 0;i < indent;i++) {
        buffer.append(' ');
      }
    }
    buffer.append(c);
    return this;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy