From d45340dc7d92ec2e0c15dbbac3c51ae086d1b131 Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Sun, 27 Mar 2011 04:46:30 +0000 Subject: [PATCH] Code streamlining. --- .../corext/util/CodeFormatterUtil.java | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/util/CodeFormatterUtil.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/util/CodeFormatterUtil.java index c7021879c3e..9e2065e69da 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/util/CodeFormatterUtil.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/util/CodeFormatterUtil.java @@ -21,7 +21,6 @@ import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants; import org.eclipse.cdt.core.model.ICProject; public class CodeFormatterUtil { - /** * Creates a string that represents the given number of indentation units. * 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 null if the code could not be formatted for the given kind. + * Creates edits that describe how to format the given string. Returns null + * if the code could not be formatted for the given kind. + * * @throws IllegalArgumentException If the offset and length are not inside the string, a * IllegalArgumentException is thrown. */ - public static TextEdit format(int kind, String source, int offset, int length, int indentationLevel, String lineSeparator, Map options) { + public static TextEdit format(int kind, String source, int offset, int length, + int indentationLevel, String lineSeparator, Map options) { 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); if (formatter != null) { @@ -128,7 +131,8 @@ public class CodeFormatterUtil { return null; } - public static TextEdit format(int kind, String source, int indentationLevel, String lineSeparator, Map options) { + public static TextEdit format(int kind, String source, int indentationLevel, + String lineSeparator, Map options) { String prefix= ""; //$NON-NLS-1$ String suffix= ""; //$NON-NLS-1$ switch (kind) { @@ -143,9 +147,16 @@ public class CodeFormatterUtil { break; } String tuSource= prefix + source + suffix; - TextEdit edit= format(CodeFormatter.K_TRANSLATION_UNIT, tuSource, prefix.length(), source.length(), indentationLevel, lineSeparator, options); - if (edit != null && prefix.length() > 0) { - edit.moveTree(-prefix.length()); + return format(tuSource, prefix.length(), source.length(), indentationLevel, lineSeparator, + options); + } + + private static TextEdit format(String source, int offset, int length, int indentationLevel, + String lineSeparator, Map options) { + TextEdit edit= format(CodeFormatter.K_TRANSLATION_UNIT, source, offset, length, + indentationLevel, lineSeparator, options); + if (edit != null && offset > 0) { + edit.moveTree(-offset); } return edit; }