Package org.eclipse.jdt.core.formatter
Class IndentManipulation
java.lang.Object
org.eclipse.jdt.core.formatter.IndentManipulation
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 Summary
Modifier and TypeMethodDescriptionstatic String
changeIndent
(String code, int indentUnitsToRemove, int tabWidth, int indentWidth, String newIndentString, String lineDelim) Change the indent of a, possible multiple line, code string.static String
extractIndentString
(String line, int tabWidth, int indentWidth) Returns the leading indentation string of the given line.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.static int
getIndentWidth
(Map<String, String> options) Returns the tab width as configured in the given map.static int
getTabWidth
(Map<String, String> options) Returns the tab width as configured in the given map.static boolean
isIndentChar
(char ch) Returnstrue
if the given character is an indentation character.static boolean
isLineDelimiterChar
(char ch) Returnstrue
if the given character is a line delimiter character.static int
measureIndentInSpaces
(CharSequence line, int tabWidth) Returns the indentation of the given line in space equivalents.static int
measureIndentUnits
(CharSequence line, int tabWidth, int indentWidth) Returns the indentation of the given line in indentation units.static String
trimIndent
(String line, int indentUnitsToRemove, int tabWidth, int indentWidth) Removes the given number of indentation units from a given line.
-
Method Details
-
isIndentChar
public static boolean isIndentChar(char ch) Returnstrue
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) Returnstrue
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
Returns the indentation of the given line in indentation units. Odd spaces are not counted. This method only analyzes the content ofline
up to the first non-whitespace character.- Parameters:
line
- the string to measure the indent oftabWidth
- the width of one tab character in space equivalentsindentWidth
- 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
- the given
-
measureIndentInSpaces
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 ofline
up to the first non-whitespace character.- Parameters:
line
- the string to measure the indent oftabWidth
- 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
- the given
-
extractIndentString
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 scantabWidth
- the size of one tab in space equivalentsindentWidth
- 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
- the given
-
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. IfindentsToRemove <= 0 or indent == 0
the line is returned.- Parameters:
line
- the line to trimtabWidth
- the width of one tab in space equivalentsindentWidth
- 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
- the given
-
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 ofindentUnitsToRemove
- the number of indent units to remove from each line (except the first) of the given codetabWidth
- the size of one tab in space equivalentsindentWidth
- the width of one indentation unit in space equivalentsnewIndentString
- 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
- the given
-
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 ofindentUnitsToRemove
- the number of indent units to remove from each line (except the first) of the given codetabWidth
- the size of one tab in space equivalentsindentWidth
- the width of one indentation unit in space equivalentsnewIndentString
- 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
- the given
-
getTabWidth
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 givenoptions
is null
-
getIndentWidth
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 givenoptions
is null
-