mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 14:42:11 +02:00
Cosmetics.
Change-Id: If3fa0aa0c6679cf508492322c6e38e2dc0d1351f
This commit is contained in:
parent
efd6a7263b
commit
ed7f1cea09
1 changed files with 35 additions and 42 deletions
|
@ -25,22 +25,22 @@ public class CodeFormatterUtil {
|
||||||
* Creates a string that represents the given number of indentation units.
|
* Creates a string that represents the given number of indentation units.
|
||||||
* The returned string can contain tabs and/or spaces depending on the core
|
* The returned string can contain tabs and/or spaces depending on the core
|
||||||
* formatter preferences.
|
* formatter preferences.
|
||||||
*
|
*
|
||||||
* @param indentationUnits the number of indentation units to generate
|
* @param indentationUnits the number of indentation units to generate
|
||||||
* @param project the project from which to get the formatter settings,
|
* @param project the project from which to get the formatter settings,
|
||||||
* <code>null</code> if the workspace default should be used
|
* {@code null} if the workspace default should be used
|
||||||
* @return the indent string
|
* @return the indent string
|
||||||
*/
|
*/
|
||||||
public static String createIndentString(int indentationUnits, ICProject project) {
|
public static String createIndentString(int indentationUnits, ICProject project) {
|
||||||
Map<String, String> options= project != null ? project.getOptions(true) : CCorePlugin.getOptions();
|
Map<String, String> options= project != null ? project.getOptions(true) : CCorePlugin.getOptions();
|
||||||
return ToolFactory.createDefaultCodeFormatter(options).createIndentationString(indentationUnits);
|
return ToolFactory.createDefaultCodeFormatter(options).createIndentationString(indentationUnits);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the current tab width.
|
* Gets the current tab width.
|
||||||
*
|
*
|
||||||
* @param project The project where the source is used, used for project
|
* @param project The project where the source is used, used for project
|
||||||
* specific options or <code>null</code> if the project is unknown
|
* specific options or {@code null} if the project is unknown
|
||||||
* and the workspace default should be used
|
* and the workspace default should be used
|
||||||
* @return The tab width
|
* @return The tab width
|
||||||
*/
|
*/
|
||||||
|
@ -52,37 +52,37 @@ public class CodeFormatterUtil {
|
||||||
* that case.
|
* that case.
|
||||||
*/
|
*/
|
||||||
String key;
|
String key;
|
||||||
if (CCorePlugin.SPACE.equals(getCoreOption(project, DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR)))
|
if (CCorePlugin.SPACE.equals(getCoreOption(project, DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR))) {
|
||||||
key= DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE;
|
key= DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE;
|
||||||
else
|
} else {
|
||||||
key= DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE;
|
key= DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
return getCoreOption(project, key, 4);
|
return getCoreOption(project, key, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the current indent width.
|
* Returns the current indent width.
|
||||||
*
|
*
|
||||||
* @param project the project where the source is used or <code>null</code>
|
* @param project the project where the source is used or {@code null}
|
||||||
* if the project is unknown and the workspace default should be used
|
* if the project is unknown and the workspace default should be used
|
||||||
* @return the indent width
|
* @return the indent width
|
||||||
*/
|
*/
|
||||||
public static int getIndentWidth(ICProject project) {
|
public static int getIndentWidth(ICProject project) {
|
||||||
String key;
|
String key;
|
||||||
if (DefaultCodeFormatterConstants.MIXED.equals(getCoreOption(project, DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR)))
|
if (DefaultCodeFormatterConstants.MIXED.equals(getCoreOption(project, DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR))) {
|
||||||
key= DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE;
|
key= DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE;
|
||||||
else
|
} else {
|
||||||
key= DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE;
|
key= DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
return getCoreOption(project, key, 4);
|
return getCoreOption(project, key, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the possibly <code>project</code>-specific core preference
|
* Returns the possibly {@code project}-specific core preference defined under {@code key}.
|
||||||
* defined under <code>key</code>.
|
*
|
||||||
*
|
* @param project the project to get the preference from, or {@code null} to get the global preference
|
||||||
* @param project the project to get the preference from, or
|
|
||||||
* <code>null</code> to get the global preference
|
|
||||||
* @param key the key of the preference
|
* @param key the key of the preference
|
||||||
* @return the value of the preference
|
* @return the value of the preference
|
||||||
*/
|
*/
|
||||||
|
@ -93,12 +93,10 @@ public class CodeFormatterUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the possibly <code>project</code>-specific core preference
|
* Returns the possibly {@code project}-specific core preference defined under {@code key},
|
||||||
* defined under <code>key</code>, or <code>def</code> if the value is
|
* or {@code def} if the value is not a integer.
|
||||||
* not a integer.
|
*
|
||||||
*
|
* @param project the project to get the preference from, or {@code null} to get the global preference
|
||||||
* @param project the project to get the preference from, or
|
|
||||||
* <code>null</code> to get the global preference
|
|
||||||
* @param key the key of the preference
|
* @param key the key of the preference
|
||||||
* @param def the default value
|
* @param def the default value
|
||||||
* @return the value of the preference
|
* @return the value of the preference
|
||||||
|
@ -112,27 +110,23 @@ public class CodeFormatterUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates edits that describe how to format the given string. Returns <code>null</code>
|
* Creates edits that describe how to format the given string. Returns {@code null}
|
||||||
* if the code could not be formatted for the given kind.
|
* if the code could not be formatted for the given kind.
|
||||||
*
|
*
|
||||||
* @throws IllegalArgumentException If the offset and length are not inside the string, a
|
* @throws IllegalArgumentException if the offset and length are not inside the string.
|
||||||
* IllegalArgumentException is thrown.
|
|
||||||
*/
|
*/
|
||||||
public static TextEdit format(int kind, String source, int offset, int length,
|
public static TextEdit format(int kind, String source, int offset, int length, int indentationLevel,
|
||||||
int indentationLevel, String lineSeparator, Map<String, ?> options) {
|
String lineSeparator, Map<String, ?> options) {
|
||||||
if (offset < 0 || length < 0 || offset + length > source.length()) {
|
if (offset < 0 || length < 0 || offset + length > source.length()) {
|
||||||
throw new IllegalArgumentException("offset or length outside of string. offset: " + //$NON-NLS-1$
|
throw new IllegalArgumentException("offset or length outside of string. offset: " + //$NON-NLS-1$
|
||||||
offset + ", length: " + length + ", string size: " + source.length()); //$NON-NLS-1$//$NON-NLS-2$
|
offset + ", length: " + length + ", string size: " + source.length()); //$NON-NLS-1$//$NON-NLS-2$
|
||||||
}
|
}
|
||||||
CodeFormatter formatter = ToolFactory.createCodeFormatter(options);
|
CodeFormatter formatter = ToolFactory.createCodeFormatter(options);
|
||||||
if (formatter != null) {
|
return formatter.format(kind, source, offset, length, indentationLevel, lineSeparator);
|
||||||
return formatter.format(kind, source, offset, length, indentationLevel, lineSeparator);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TextEdit format(int kind, String source, int indentationLevel,
|
public static TextEdit format(int kind, String source, int indentationLevel, String lineSeparator,
|
||||||
String lineSeparator, Map<String, ?> options) {
|
Map<String, ?> options) {
|
||||||
String prefix= ""; //$NON-NLS-1$
|
String prefix= ""; //$NON-NLS-1$
|
||||||
String suffix= ""; //$NON-NLS-1$
|
String suffix= ""; //$NON-NLS-1$
|
||||||
switch (kind) {
|
switch (kind) {
|
||||||
|
@ -147,8 +141,7 @@ public class CodeFormatterUtil {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
String tuSource= prefix + source + suffix;
|
String tuSource= prefix + source + suffix;
|
||||||
return format(tuSource, prefix.length(), source.length(), indentationLevel, lineSeparator,
|
return format(tuSource, prefix.length(), source.length(), indentationLevel, lineSeparator, options);
|
||||||
options);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static TextEdit format(String source, int offset, int length, int indentationLevel,
|
private static TextEdit format(String source, int offset, int length, int indentationLevel,
|
||||||
|
@ -160,7 +153,7 @@ public class CodeFormatterUtil {
|
||||||
}
|
}
|
||||||
return edit;
|
return edit;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return The formatter tab width on workspace level.
|
* @return The formatter tab width on workspace level.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Add table
Reference in a new issue