Class IndentManipulation

java.lang.Object
org.eclipse.jdt.core.formatter.IndentManipulation

public final class IndentManipulation extends Object
Helper class to provide String manipulation functions dealing with indentations.
Since:
3.2
Restriction:
This class is not intended to be instantiated by clients.
  • Method Details

    • isIndentChar

      public static boolean isIndentChar(char ch)
      Returns true if the given character is an indentation character. Indentation character are all whitespace characters except the line delimiter characters.
      Parameters:
      ch - the given character
      Returns:
      Returns true if this the character is a indent character, false otherwise
    • isLineDelimiterChar

      public static boolean isLineDelimiterChar(char ch)
      Returns true if the given character is a line delimiter character.
      Parameters:
      ch - the given character
      Returns:
      Returns true if this the character is a line delimiter character, false otherwise
    • measureIndentUnits

      public static int measureIndentUnits(CharSequence line, int tabWidth, int indentWidth)
      Returns the indentation of the given line in indentation units. Odd spaces are not counted. This method only analyzes the content of line up to the first non-whitespace character.
      Parameters:
      line - the string to measure the indent of
      tabWidth - the width of one tab character in space equivalents
      indentWidth - the width of one indentation unit in space equivalents
      Returns:
      the number of indentation units that line is indented by
      Throws:
      IllegalArgumentException - if:
      • the given indentWidth is lower than zero
      • the given tabWidth is lower than zero
      • the given line is null
    • measureIndentInSpaces

      public static int measureIndentInSpaces(CharSequence line, int tabWidth)
      Returns the indentation of the given line in space equivalents.

      Tab characters are counted using the given tabWidth and every other indent character as one. This method analyzes the content of line up to the first non-whitespace character.

      Parameters:
      line - the string to measure the indent of
      tabWidth - the width of one tab in space equivalents
      Returns:
      the measured indent width in space equivalents
      Throws:
      IllegalArgumentException - if:
      • the given line is null
      • the given tabWidth is lower than zero
    • extractIndentString

      public static String extractIndentString(String line, int tabWidth, int indentWidth)
      Returns the leading indentation string of the given line. Note that the returned string need not be equal to the leading whitespace as odd spaces are not considered part of the indentation.
      Parameters:
      line - the line to scan
      tabWidth - the size of one tab in space equivalents
      indentWidth - the width of one indentation unit in space equivalents
      Returns:
      the indent part of line, but no odd spaces
      Throws:
      IllegalArgumentException - if:
      • the given indentWidth is lower than zero
      • the given tabWidth is lower than zero
      • the given line is null
    • trimIndent

      public static String trimIndent(String line, int indentUnitsToRemove, int tabWidth, int indentWidth)
      Removes the given number of indentation units from a given line. If the line has less indent than the given indentUnitsToRemove, all the available indentation is removed. If indentsToRemove <= 0 or indent == 0 the line is returned.
      Parameters:
      line - the line to trim
      tabWidth - the width of one tab in space equivalents
      indentWidth - the width of one indentation unit in space equivalents
      Returns:
      the trimmed string
      Throws:
      IllegalArgumentException - if:
      • the given indentWidth is lower than zero
      • the given tabWidth is lower than zero
      • the given line is null
    • changeIndent

      public static String changeIndent(String code, int indentUnitsToRemove, int tabWidth, int indentWidth, String newIndentString, String lineDelim)
      Change the indent of a, possible multiple line, code string. The given number of indent units is removed, and a new indent string is added.

      The first line of the code will not be changed (It is considered to have no indent as it might start in the middle of a line).

      Parameters:
      code - the code to change the indent of
      indentUnitsToRemove - the number of indent units to remove from each line (except the first) of the given code
      tabWidth - the size of one tab in space equivalents
      indentWidth - the width of one indentation unit in space equivalents
      newIndentString - the new indent string to be added to all lines (except the first)
      lineDelim - the new line delimiter to be used. The returned code will contain only this line delimiter.
      Returns:
      the newly indent code, containing only the given line delimiters.
      Throws:
      IllegalArgumentException - if:
      • the given indentWidth is lower than zero
      • the given tabWidth is lower than zero
      • the given code is null
      • the given indentUnitsToRemove is lower than zero
      • the given newIndentString is null
      • the given lineDelim is null
    • getChangeIndentEdits

      public static org.eclipse.text.edits.ReplaceEdit[] getChangeIndentEdits(String source, int indentUnitsToRemove, int tabWidth, int indentWidth, String newIndentString)
      Returns the text edits retrieved after changing the indentation of a, possible multi-line, code string.

      The given number of indent units is removed, and a new indent string is added.

      The first line of the code will not be changed (It is considered to have no indent as it might start in the middle of a line).

      Parameters:
      source - The code to change the indent of
      indentUnitsToRemove - the number of indent units to remove from each line (except the first) of the given code
      tabWidth - the size of one tab in space equivalents
      indentWidth - the width of one indentation unit in space equivalents
      newIndentString - the new indent string to be added to all lines (except the first)
      Returns:
      returns the resulting text edits
      Throws:
      IllegalArgumentException - if:
      • the given indentWidth is lower than zero
      • the given tabWidth is lower than zero
      • the given source is null
      • the given indentUnitsToRemove is lower than zero
      • the given newIndentString is null
    • getTabWidth

      public static int getTabWidth(Map<String,String> options)
      Returns the tab width as configured in the given map.

      Use IJavaProject.getOptions(boolean) to get the most current project options.

      Parameters:
      options - the map to get the formatter settings from.
      Returns:
      the tab width
      Throws:
      IllegalArgumentException - if the given options is null
    • getIndentWidth

      public static int getIndentWidth(Map<String,String> options)
      Returns the tab width as configured in the given map.

      Use IJavaProject.getOptions(boolean) to get the most current project options.

      Parameters:
      options - the map to get the formatter settings from
      Returns:
      the indent width
      Throws:
      IllegalArgumentException - if the given options is null