mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Code streamlining.
This commit is contained in:
parent
df89c8daab
commit
d45340dc7d
1 changed files with 19 additions and 8 deletions
|
@ -21,7 +21,6 @@ import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants;
|
||||||
import org.eclipse.cdt.core.model.ICProject;
|
import org.eclipse.cdt.core.model.ICProject;
|
||||||
|
|
||||||
public class CodeFormatterUtil {
|
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
|
||||||
|
@ -113,13 +112,17 @@ public class CodeFormatterUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates edits that describe how to format the given string. Returns <code>null</code> if the code could not be formatted for the given kind.
|
* Creates edits that describe how to format the given string. Returns <code>null</code>
|
||||||
|
* 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, a
|
||||||
* IllegalArgumentException is thrown.
|
* IllegalArgumentException is thrown.
|
||||||
*/
|
*/
|
||||||
public static TextEdit format(int kind, String source, int offset, int length, int indentationLevel, String lineSeparator, Map<String, ?> options) {
|
public static TextEdit format(int kind, String source, int offset, int length,
|
||||||
|
int indentationLevel, 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: " + offset + ", length: " + length + ", string size: " + source.length()); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
|
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$
|
||||||
}
|
}
|
||||||
CodeFormatter formatter = ToolFactory.createCodeFormatter(options);
|
CodeFormatter formatter = ToolFactory.createCodeFormatter(options);
|
||||||
if (formatter != null) {
|
if (formatter != null) {
|
||||||
|
@ -128,7 +131,8 @@ public class CodeFormatterUtil {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TextEdit format(int kind, String source, int indentationLevel, String lineSeparator, Map<String, ?> options) {
|
public static TextEdit format(int kind, String source, int indentationLevel,
|
||||||
|
String lineSeparator, 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) {
|
||||||
|
@ -143,9 +147,16 @@ public class CodeFormatterUtil {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
String tuSource= prefix + source + suffix;
|
String tuSource= prefix + source + suffix;
|
||||||
TextEdit edit= format(CodeFormatter.K_TRANSLATION_UNIT, tuSource, prefix.length(), source.length(), indentationLevel, lineSeparator, options);
|
return format(tuSource, prefix.length(), source.length(), indentationLevel, lineSeparator,
|
||||||
if (edit != null && prefix.length() > 0) {
|
options);
|
||||||
edit.moveTree(-prefix.length());
|
}
|
||||||
|
|
||||||
|
private static TextEdit format(String source, int offset, int length, int indentationLevel,
|
||||||
|
String lineSeparator, Map<String, ?> options) {
|
||||||
|
TextEdit edit= format(CodeFormatter.K_TRANSLATION_UNIT, source, offset, length,
|
||||||
|
indentationLevel, lineSeparator, options);
|
||||||
|
if (edit != null && offset > 0) {
|
||||||
|
edit.moveTree(-offset);
|
||||||
}
|
}
|
||||||
return edit;
|
return edit;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue