com.google.googlejavaformat.Input.Token Java Examples

The following examples show how to use com.google.googlejavaformat.Input.Token. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OpsBuilder.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Output any remaining tokens from the input stream (e.g. terminal whitespace).
 */
public final void drain() {
    int inputPosition = input.getText().length() + 1;
    if (inputPosition > this.inputPosition) {
        ImmutableList<? extends Token> tokens = input.getTokens();
        int tokensN = tokens.size();
        while (tokenI < tokensN && inputPosition > tokens.get(tokenI).getTok().getPosition()) {
            Token token = tokens.get(tokenI++);
            add(
                    Doc.Token.make(
                            token, Doc.Token.RealOrImaginary.IMAGINARY, ZERO, Optional.<Indent>absent()));
        }
    }
    this.inputPosition = inputPosition;
    checkClosed(0);
}
 
Example #2
Source File: OpsBuilder.java    From google-java-format with Apache License 2.0 6 votes vote down vote up
/** Output any remaining tokens from the input stream (e.g. terminal whitespace). */
public final void drain() {
  int inputPosition = input.getText().length() + 1;
  if (inputPosition > this.inputPosition) {
    ImmutableList<? extends Input.Token> tokens = input.getTokens();
    int tokensN = tokens.size();
    while (tokenI < tokensN && inputPosition > tokens.get(tokenI).getTok().getPosition()) {
      Input.Token token = tokens.get(tokenI++);
      add(
          Doc.Token.make(
              token,
              Doc.Token.RealOrImaginary.IMAGINARY,
              ZERO,
              /* breakAndIndentTrailingComment= */ Optional.empty()));
    }
  }
  this.inputPosition = inputPosition;
  checkClosed(0);
}
 
Example #3
Source File: OpsBuilder.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Output any remaining tokens from the input stream (e.g. terminal whitespace).
 */
public final void drain() {
    int inputPosition = input.getText().length() + 1;
    if (inputPosition > this.inputPosition) {
        ImmutableList<? extends Token> tokens = input.getTokens();
        int tokensN = tokens.size();
        while (tokenI < tokensN && inputPosition > tokens.get(tokenI).getTok().getPosition()) {
            Token token = tokens.get(tokenI++);
            add(
                    Doc.Token.make(
                            token, Doc.Token.RealOrImaginary.IMAGINARY, ZERO, Optional.<Indent>absent()));
        }
    }
    this.inputPosition = inputPosition;
    checkClosed(0);
}
 
Example #4
Source File: JavaOutput.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * The earliest position of any Tok in the Token, including leading whitespace.
 */
public static int startPosition(Token token) {
    int min = token.getTok().getPosition();
    for (Input.Tok tok : token.getToksBefore()) {
        min = Math.min(min, tok.getPosition());
    }
    return min;
}
 
Example #5
Source File: OpsBuilder.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private static int getI(Token token) {
    for (Tok tok : token.getToksBefore()) {
        if (tok.getIndex() >= 0) {
            return tok.getIndex();
        }
    }
    return token.getTok().getIndex();
}
 
Example #6
Source File: OpsBuilder.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @return the start column of the token at {@code position}, including leading comments.
 */
public Integer actualStartColumn(int position) {
    Token startToken = input.getPositionTokenMap().get(position);
    int start = startToken.getTok().getPosition();
    int line0 = input.getLineNumber(start);
    for (Tok tok : startToken.getToksBefore()) {
        if (line0 != input.getLineNumber(tok.getPosition())) {
            return start;
        }
        if (tok.isComment()) {
            start = Math.min(start, tok.getPosition());
        }
    }
    return start;
}
 
Example #7
Source File: OpsBuilder.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Sync to position in the input. If we've skipped outputting any tokens that were present in the
 * input tokens, output them here and optionally complain.
 *
 * @param inputPosition the {@code 0}-based input position
 */
public final void sync(int inputPosition) {
    if (inputPosition > this.inputPosition) {
        ImmutableList<? extends Token> tokens = input.getTokens();
        int tokensN = tokens.size();
        this.inputPosition = inputPosition;
        if (tokenI < tokensN && inputPosition > tokens.get(tokenI).getTok().getPosition()) {
            // Found a missing input token. Insert it and mark it missing (usually not good).
            Token token = tokens.get(tokenI++);
            throw new FormattingError(
                    diagnostic(String.format("did not generate token \"%s\"", token.getTok().getText())));
        }
    }
}
 
Example #8
Source File: OpsBuilder.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Return the text of the next {@link Token}, or absent if there is none.
 */
public final Optional<String> peekToken() {
    ImmutableList<? extends Token> tokens = input.getTokens();
    return tokenI < tokens.size()
            ? Optional.of(tokens.get(tokenI).getTok().getOriginalText())
            : Optional.<String>absent();
}
 
Example #9
Source File: OpsBuilder.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Make the boundary of a region that can be partially formatted. The boundary will be included in
 * the following region, e.g.: [[boundary0, boundary1), [boundary1, boundary2), ...].
 */
public void markForPartialFormat() {
    if (lastPartialFormatBoundary == -1) {
        lastPartialFormatBoundary = tokenI;
        return;
    }
    if (tokenI == lastPartialFormatBoundary) {
        return;
    }
    Token start = input.getTokens().get(lastPartialFormatBoundary);
    Token end = input.getTokens().get(tokenI - 1);
    output.markForPartialFormat(start, end);
    lastPartialFormatBoundary = tokenI;
}
 
Example #10
Source File: JavaOutput.java    From google-java-format with Apache License 2.0 5 votes vote down vote up
/** The earliest position of any Tok in the Token, including leading whitespace. */
public static int startPosition(Token token) {
  int min = token.getTok().getPosition();
  for (Input.Tok tok : token.getToksBefore()) {
    min = Math.min(min, tok.getPosition());
  }
  return min;
}
 
Example #11
Source File: JavaOutput.java    From google-java-format with Apache License 2.0 5 votes vote down vote up
/** The earliest non-whitespace Tok in the Token. */
public static Input.Tok startTok(Token token) {
  for (Input.Tok tok : token.getToksBefore()) {
    if (tok.getIndex() >= 0) {
      return tok;
    }
  }
  return token.getTok();
}
 
Example #12
Source File: JavaOutput.java    From google-java-format with Apache License 2.0 5 votes vote down vote up
/** The last non-whitespace Tok in the Token. */
public static Input.Tok endTok(Token token) {
  for (int i = token.getToksAfter().size() - 1; i >= 0; i--) {
    Input.Tok tok = token.getToksAfter().get(i);
    if (tok.getIndex() >= 0) {
      return tok;
    }
  }
  return token.getTok();
}
 
Example #13
Source File: OpsBuilder.java    From google-java-format with Apache License 2.0 5 votes vote down vote up
/** @return the start column of the token at {@code position}, including leading comments. */
public Integer actualStartColumn(int position) {
  Token startToken = input.getPositionTokenMap().get(position);
  int start = startToken.getTok().getPosition();
  int line0 = input.getLineNumber(start);
  for (Tok tok : startToken.getToksBefore()) {
    if (line0 != input.getLineNumber(tok.getPosition())) {
      return start;
    }
    if (tok.isComment()) {
      start = Math.min(start, tok.getPosition());
    }
  }
  return start;
}
 
Example #14
Source File: OpsBuilder.java    From google-java-format with Apache License 2.0 5 votes vote down vote up
/**
 * Sync to position in the input. If we've skipped outputting any tokens that were present in the
 * input tokens, output them here and optionally complain.
 *
 * @param inputPosition the {@code 0}-based input position
 */
public final void sync(int inputPosition) {
  if (inputPosition > this.inputPosition) {
    ImmutableList<? extends Input.Token> tokens = input.getTokens();
    int tokensN = tokens.size();
    this.inputPosition = inputPosition;
    if (tokenI < tokensN && inputPosition > tokens.get(tokenI).getTok().getPosition()) {
      // Found a missing input token. Insert it and mark it missing (usually not good).
      Input.Token token = tokens.get(tokenI++);
      throw new FormattingError(
          diagnostic(String.format("did not generate token \"%s\"", token.getTok().getText())));
    }
  }
}
 
Example #15
Source File: OpsBuilder.java    From google-java-format with Apache License 2.0 5 votes vote down vote up
/** Return the text of an upcoming {@link Input.Token}, or absent if there is none. */
public final Optional<String> peekToken(int skip) {
  ImmutableList<? extends Input.Token> tokens = input.getTokens();
  int idx = tokenI + skip;
  return idx < tokens.size()
      ? Optional.of(tokens.get(idx).getTok().getOriginalText())
      : Optional.empty();
}
 
Example #16
Source File: OpsBuilder.java    From google-java-format with Apache License 2.0 5 votes vote down vote up
/**
 * Emit an optional token iff it exists on the input. This is used to emit tokens whose existence
 * has been lost in the AST.
 *
 * @param token the optional token
 */
public final void guessToken(String token) {
  token(
      token,
      Doc.Token.RealOrImaginary.IMAGINARY,
      ZERO,
      /* breakAndIndentTrailingComment=  */ Optional.empty());
}
 
Example #17
Source File: OpsBuilder.java    From google-java-format with Apache License 2.0 5 votes vote down vote up
/**
 * Make the boundary of a region that can be partially formatted. The boundary will be included in
 * the following region, e.g.: [[boundary0, boundary1), [boundary1, boundary2), ...].
 */
public void markForPartialFormat() {
  if (lastPartialFormatBoundary == -1) {
    lastPartialFormatBoundary = tokenI;
    return;
  }
  if (tokenI == lastPartialFormatBoundary) {
    return;
  }
  Token start = input.getTokens().get(lastPartialFormatBoundary);
  Token end = input.getTokens().get(tokenI - 1);
  output.markForPartialFormat(start, end);
  lastPartialFormatBoundary = tokenI;
}
 
Example #18
Source File: OpsBuilder.java    From google-java-format with Apache License 2.0 5 votes vote down vote up
private static int getI(Input.Token token) {
  for (Input.Tok tok : token.getToksBefore()) {
    if (tok.getIndex() >= 0) {
      return tok.getIndex();
    }
  }
  return token.getTok().getIndex();
}
 
Example #19
Source File: OpsBuilder.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Return the text of the next {@link Token}, or absent if there is none.
 */
public final Optional<String> peekToken() {
    ImmutableList<? extends Token> tokens = input.getTokens();
    return tokenI < tokens.size()
            ? Optional.of(tokens.get(tokenI).getTok().getOriginalText())
            : Optional.<String>absent();
}
 
Example #20
Source File: JavaOutput.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * The earliest non-whitespace Tok in the Token.
 */
public static Input.Tok startTok(Token token) {
    for (Input.Tok tok : token.getToksBefore()) {
        if (tok.getIndex() >= 0) {
            return tok;
        }
    }
    return token.getTok();
}
 
Example #21
Source File: JavaOutput.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * The last non-whitespace Tok in the Token.
 */
public static Input.Tok endTok(Token token) {
    for (int i = token.getToksAfter().size() - 1; i >= 0; i--) {
        Input.Tok tok = token.getToksAfter().get(i);
        if (tok.getIndex() >= 0) {
            return tok;
        }
    }
    return token.getTok();
}
 
Example #22
Source File: OpsBuilder.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
private static int getI(Token token) {
    for (Tok tok : token.getToksBefore()) {
        if (tok.getIndex() >= 0) {
            return tok.getIndex();
        }
    }
    return token.getTok().getIndex();
}
 
Example #23
Source File: OpsBuilder.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * @return the start column of the token at {@code position}, including leading comments.
 */
public Integer actualStartColumn(int position) {
    Token startToken = input.getPositionTokenMap().get(position);
    int start = startToken.getTok().getPosition();
    int line0 = input.getLineNumber(start);
    for (Tok tok : startToken.getToksBefore()) {
        if (line0 != input.getLineNumber(tok.getPosition())) {
            return start;
        }
        if (tok.isComment()) {
            start = Math.min(start, tok.getPosition());
        }
    }
    return start;
}
 
Example #24
Source File: OpsBuilder.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Sync to position in the input. If we've skipped outputting any tokens that were present in the
 * input tokens, output them here and optionally complain.
 *
 * @param inputPosition the {@code 0}-based input position
 */
public final void sync(int inputPosition) {
    if (inputPosition > this.inputPosition) {
        ImmutableList<? extends Token> tokens = input.getTokens();
        int tokensN = tokens.size();
        this.inputPosition = inputPosition;
        if (tokenI < tokensN && inputPosition > tokens.get(tokenI).getTok().getPosition()) {
            // Found a missing input token. Insert it and mark it missing (usually not good).
            Token token = tokens.get(tokenI++);
            throw new FormattingError(
                    diagnostic(String.format("did not generate token \"%s\"", token.getTok().getText())));
        }
    }
}
 
Example #25
Source File: OpsBuilder.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Make the boundary of a region that can be partially formatted. The boundary will be included in
 * the following region, e.g.: [[boundary0, boundary1), [boundary1, boundary2), ...].
 */
public void markForPartialFormat() {
    if (lastPartialFormatBoundary == -1) {
        lastPartialFormatBoundary = tokenI;
        return;
    }
    if (tokenI == lastPartialFormatBoundary) {
        return;
    }
    Token start = input.getTokens().get(lastPartialFormatBoundary);
    Token end = input.getTokens().get(tokenI - 1);
    output.markForPartialFormat(start, end);
    lastPartialFormatBoundary = tokenI;
}
 
Example #26
Source File: JavaOutput.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * The earliest position of any Tok in the Token, including leading whitespace.
 */
public static int startPosition(Token token) {
    int min = token.getTok().getPosition();
    for (Input.Tok tok : token.getToksBefore()) {
        min = Math.min(min, tok.getPosition());
    }
    return min;
}
 
Example #27
Source File: JavaOutput.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * The earliest non-whitespace Tok in the Token.
 */
public static Input.Tok startTok(Token token) {
    for (Input.Tok tok : token.getToksBefore()) {
        if (tok.getIndex() >= 0) {
            return tok;
        }
    }
    return token.getTok();
}
 
Example #28
Source File: JavaOutput.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * The last non-whitespace Tok in the Token.
 */
public static Input.Tok endTok(Token token) {
    for (int i = token.getToksAfter().size() - 1; i >= 0; i--) {
        Input.Tok tok = token.getToksAfter().get(i);
        if (tok.getIndex() >= 0) {
            return tok;
        }
    }
    return token.getTok();
}
 
Example #29
Source File: JavaOutput.java    From google-java-format with Apache License 2.0 4 votes vote down vote up
@Override
public void markForPartialFormat(Token start, Token end) {
  int lo = JavaOutput.startTok(start).getIndex();
  int hi = JavaOutput.endTok(end).getIndex();
  partialFormatRanges.add(Range.closed(lo, hi));
}
 
Example #30
Source File: ModifierOrderer.java    From google-java-format with Apache License 2.0 4 votes vote down vote up
/**
 * Reorders all modifiers in the given text and within the given character ranges to be in JLS
 * order.
 */
static JavaInput reorderModifiers(JavaInput javaInput, Collection<Range<Integer>> characterRanges)
    throws FormatterException {
  if (javaInput.getTokens().isEmpty()) {
    // There weren't any tokens, possible because of a lexing error.
    // Errors about invalid input will be reported later after parsing.
    return javaInput;
  }
  RangeSet<Integer> tokenRanges = javaInput.characterRangesToTokenRanges(characterRanges);
  Iterator<? extends Token> it = javaInput.getTokens().iterator();
  TreeRangeMap<Integer, String> replacements = TreeRangeMap.create();
  while (it.hasNext()) {
    Token token = it.next();
    if (!tokenRanges.contains(token.getTok().getIndex())) {
      continue;
    }
    Modifier mod = asModifier(token);
    if (mod == null) {
      continue;
    }

    List<Token> modifierTokens = new ArrayList<>();
    List<Modifier> mods = new ArrayList<>();

    int begin = token.getTok().getPosition();
    mods.add(mod);
    modifierTokens.add(token);

    int end = -1;
    while (it.hasNext()) {
      token = it.next();
      mod = asModifier(token);
      if (mod == null) {
        break;
      }
      mods.add(mod);
      modifierTokens.add(token);
      end = token.getTok().getPosition() + token.getTok().length();
    }

    if (!Ordering.natural().isOrdered(mods)) {
      Collections.sort(mods);
      StringBuilder replacement = new StringBuilder();
      for (int i = 0; i < mods.size(); i++) {
        if (i > 0) {
          addTrivia(replacement, modifierTokens.get(i).getToksBefore());
        }
        replacement.append(mods.get(i).toString());
        if (i < (modifierTokens.size() - 1)) {
          addTrivia(replacement, modifierTokens.get(i).getToksAfter());
        }
      }
      replacements.put(Range.closedOpen(begin, end), replacement.toString());
    }
  }
  return applyReplacements(javaInput, replacements);
}