org.eclipse.wst.jsdt.core.formatter
Class IndentManipulation

java.lang.Object
  extended by org.eclipse.wst.jsdt.core.formatter.IndentManipulation

public final class IndentManipulation
extends java.lang.Object

Helper class to provide String manipulation functions dealing with indentations. Provisional API: This class/interface is part of an interim API that is still under development and expected to change significantly before reaching stability. It is being made available at this early stage to solicit feedback from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken (repeatedly) as the API evolves.


Method Summary
static java.lang.String changeIndent(java.lang.String code, int indentUnitsToRemove, int tabWidth, int indentWidth, java.lang.String newIndentString, java.lang.String lineDelim)
          Change the indent of a, possible multiple line, code string.
static java.lang.String extractIndentString(java.lang.String line, int tabWidth, int indentWidth)
          Returns the leading indentation string of the given line.
static ReplaceEdit[] getChangeIndentEdits(java.lang.String source, int indentUnitsToRemove, int tabWidth, int indentWidth, java.lang.String newIndentString)
          Returns the text edits retrieved after changing the indentation of a, possible multi-line, code string.
static int getIndentWidth(java.util.Map options)
          Returns the tab width as configured in the given map.
static int getTabWidth(java.util.Map options)
          Returns the tab width as configured in the given map.
static boolean isIndentChar(char ch)
          Returns true if the given character is an indentation character.
static boolean isLineDelimiterChar(char ch)
          Returns true if the given character is a line delimiter character.
static int measureIndentInSpaces(java.lang.CharSequence line, int tabWidth)
          Returns the indentation of the given line in space equivalents.
static int measureIndentUnits(java.lang.CharSequence line, int tabWidth, int indentWidth)
          Returns the indentation of the given line in indentation units.
static java.lang.String trimIndent(java.lang.String line, int indentUnitsToRemove, int tabWidth, int indentWidth)
          Removes the given number of indentation units from a given line.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

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(java.lang.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:
java.lang.IllegalArgumentException - if:
  • the given indentWidth is lower or equals to zero
  • the given tabWidth is lower than zero
  • the given line is null

measureIndentInSpaces

public static int measureIndentInSpaces(java.lang.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:
java.lang.IllegalArgumentException - if:
  • the given line is null
  • the given tabWidth is lower than zero

extractIndentString

public static java.lang.String extractIndentString(java.lang.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:
java.lang.IllegalArgumentException - if:
  • the given indentWidth is lower or equals to zero
  • the given tabWidth is lower than zero
  • the given line is null

trimIndent

public static java.lang.String trimIndent(java.lang.String line,
                                          int indentUnitsToRemove,
                                          int tabWidth,
                                          int indentWidth)
Removes the given number of indentation units from a given line. If the line has less than the given indent, all the available indentation is removed. If indentsToRemove <= 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:
java.lang.IllegalArgumentException - if:
  • the given indentWidth is lower or equals to zero
  • the given tabWidth is lower than zero
  • the given line is null

changeIndent

public static java.lang.String changeIndent(java.lang.String code,
                                            int indentUnitsToRemove,
                                            int tabWidth,
                                            int indentWidth,
                                            java.lang.String newIndentString,
                                            java.lang.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:
java.lang.IllegalArgumentException - if:
  • the given indentWidth is lower or equals to 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 ReplaceEdit[] getChangeIndentEdits(java.lang.String source,
                                                 int indentUnitsToRemove,
                                                 int tabWidth,
                                                 int indentWidth,
                                                 java.lang.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:
java.lang.IllegalArgumentException - if:
  • the given indentWidth is lower or equals to 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(java.util.Map options)
Returns the tab width as configured in the given map.

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

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

getIndentWidth

public static int getIndentWidth(java.util.Map options)
Returns the tab width as configured in the given map.

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

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