Class StringWrapper

java.lang.Object
com.google.googlejavaformat.java.StringWrapper

public final class StringWrapper extends Object
Wraps string literals that exceed the column limit.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private static class 
     
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final com.google.common.base.CharMatcher
     
    private static final Method
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
     
  • Method Summary

    Modifier and Type
    Method
    Description
    private static String
    applyReplacements(String javaInput, com.google.common.collect.TreeRangeMap<Integer,String> replacementMap)
    Applies replacements to the given string.
    private static List<com.sun.source.tree.Tree>
    flatten(String input, com.sun.tools.javac.tree.JCTree.JCCompilationUnit unit, com.sun.source.util.TreePath path, com.sun.source.util.TreePath parent, AtomicBoolean firstInChain)
    Flattens the given binary expression tree, and extracts the subset that contains the given path and any adjacent nodes that are also string literals.
    private static int
    getEndPosition(com.sun.tools.javac.tree.JCTree.JCCompilationUnit unit, com.sun.source.tree.Tree tree)
     
    private static com.google.common.collect.TreeRangeMap<Integer,String>
    getReflowReplacements(int columnLimit, String input)
     
    private static int
    getStartPosition(com.sun.source.tree.Tree tree)
     
    private static @Nullable Method
     
    (package private) static int
    hasEscapedNewlineAt(String input, int idx)
     
    (package private) static int
    hasEscapedWhitespaceAt(String input, int idx)
     
    private static boolean
    needWrapping(int columnLimit, String input)
    Returns true if any lines in the given Java source exceed the column limit, or contain a """ that could indicate a text block.
    private static boolean
    noComments(String input, com.sun.tools.javac.tree.JCTree.JCCompilationUnit unit, com.sun.source.tree.Tree one, com.sun.source.tree.Tree two)
     
    private static com.sun.tools.javac.tree.JCTree.JCCompilationUnit
    parse(String source, boolean allowStringFolding)
    Parses the given Java source.
    private static String
    reflow(String separator, int columnLimit, int startColumn, int trailing, com.google.common.collect.ImmutableList<String> components, boolean first0)
    Reflows the given source text, trying to split on word boundaries.
    private static com.google.common.collect.ImmutableList<String>
    stringComponents(String input, com.sun.tools.javac.tree.JCTree.JCCompilationUnit unit, List<com.sun.source.tree.Tree> flat)
    Returns the source text of the given string literal trees, excluding the leading and trailing double-quotes and the `+` operator.
    private static String
     
    (package private) static String
    wrap(int columnLimit, String input, Formatter formatter)
    Reflows string literals in the given Java source code that extend past the given column limit.
    static String
    wrap(String input, Formatter formatter)
    Reflows long string literals in the given Java source code.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • STRIP_INDENT

      private static final Method STRIP_INDENT
    • STRING_CONCAT_DELIMITER

      public static final com.google.common.base.CharMatcher STRING_CONCAT_DELIMITER
  • Constructor Details

    • StringWrapper

      private StringWrapper()
  • Method Details

    • wrap

      public static String wrap(String input, Formatter formatter) throws FormatterException
      Reflows long string literals in the given Java source code.
      Throws:
      FormatterException
    • wrap

      static String wrap(int columnLimit, String input, Formatter formatter) throws FormatterException
      Reflows string literals in the given Java source code that extend past the given column limit.
      Throws:
      FormatterException
    • getReflowReplacements

      private static com.google.common.collect.TreeRangeMap<Integer,String> getReflowReplacements(int columnLimit, String input) throws FormatterException
      Throws:
      FormatterException
    • getStripIndent

      private static @Nullable Method getStripIndent()
    • stripIndent

      private static String stripIndent(String input)
    • stringComponents

      private static com.google.common.collect.ImmutableList<String> stringComponents(String input, com.sun.tools.javac.tree.JCTree.JCCompilationUnit unit, List<com.sun.source.tree.Tree> flat)
      Returns the source text of the given string literal trees, excluding the leading and trailing double-quotes and the `+` operator.
    • hasEscapedWhitespaceAt

      static int hasEscapedWhitespaceAt(String input, int idx)
    • hasEscapedNewlineAt

      static int hasEscapedNewlineAt(String input, int idx)
    • reflow

      private static String reflow(String separator, int columnLimit, int startColumn, int trailing, com.google.common.collect.ImmutableList<String> components, boolean first0)
      Reflows the given source text, trying to split on word boundaries.
      Parameters:
      separator - the line separator
      columnLimit - the number of columns to wrap at
      startColumn - the column position of the beginning of the original text
      trailing - extra space to leave after the last line, to accommodate a ; or )
      components - the text to reflow. This is a list of “words” of a single literal. Its first and last quotes have been stripped
      first0 - true if the text includes the beginning of its enclosing concat chain
    • flatten

      private static List<com.sun.source.tree.Tree> flatten(String input, com.sun.tools.javac.tree.JCTree.JCCompilationUnit unit, com.sun.source.util.TreePath path, com.sun.source.util.TreePath parent, AtomicBoolean firstInChain)
      Flattens the given binary expression tree, and extracts the subset that contains the given path and any adjacent nodes that are also string literals.
    • noComments

      private static boolean noComments(String input, com.sun.tools.javac.tree.JCTree.JCCompilationUnit unit, com.sun.source.tree.Tree one, com.sun.source.tree.Tree two)
    • getEndPosition

      private static int getEndPosition(com.sun.tools.javac.tree.JCTree.JCCompilationUnit unit, com.sun.source.tree.Tree tree)
    • getStartPosition

      private static int getStartPosition(com.sun.source.tree.Tree tree)
    • needWrapping

      private static boolean needWrapping(int columnLimit, String input)
      Returns true if any lines in the given Java source exceed the column limit, or contain a """ that could indicate a text block.
    • parse

      private static com.sun.tools.javac.tree.JCTree.JCCompilationUnit parse(String source, boolean allowStringFolding) throws FormatterException
      Parses the given Java source.
      Throws:
      FormatterException
    • applyReplacements

      private static String applyReplacements(String javaInput, com.google.common.collect.TreeRangeMap<Integer,String> replacementMap) throws FormatterException
      Applies replacements to the given string.
      Throws:
      FormatterException