diff options
author | Tor Norbye <tnorbye@google.com> | 2011-08-03 11:25:15 -0700 |
---|---|---|
committer | Android Code Review <code-review@android.com> | 2011-08-03 11:25:15 -0700 |
commit | e6836b9ded9013725500aa2c9cd113545628a693 (patch) | |
tree | 041fc006384310b88c08dc3dc47148455d6c3d6f | |
parent | 494ff8c4edbf7aa97dd95bf8427a8e218a8b87ce (diff) | |
parent | df1ac98030fa7c31e42921cf4011d8faf97cef17 (diff) | |
download | sdk-e6836b9ded9013725500aa2c9cd113545628a693.zip sdk-e6836b9ded9013725500aa2c9cd113545628a693.tar.gz sdk-e6836b9ded9013725500aa2c9cd113545628a693.tar.bz2 |
Merge "Use Windows line delimiters on Windows"
3 files changed, 51 insertions, 21 deletions
diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/formatting/AndroidXmlFormattingStrategy.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/formatting/AndroidXmlFormattingStrategy.java index 55ca025..d28a6d3 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/formatting/AndroidXmlFormattingStrategy.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/formatting/AndroidXmlFormattingStrategy.java @@ -28,6 +28,7 @@ import com.android.sdklib.SdkConstants; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IRegion; +import org.eclipse.jface.text.TextUtilities; import org.eclipse.jface.text.TypedPosition; import org.eclipse.jface.text.formatter.ContextBasedFormattingStrategy; import org.eclipse.jface.text.formatter.IFormattingContext; @@ -189,7 +190,8 @@ public class AndroidXmlFormattingStrategy extends ContextBasedFormattingStrategy XmlFormatStyle style = guessStyle(model, domDocument); XmlFormatPreferences prefs = XmlFormatPreferences.create(); - XmlPrettyPrinter printer = new XmlPrettyPrinter(prefs, style); + String delimiter = TextUtilities.getDefaultLineDelimiter(document); + XmlPrettyPrinter printer = new XmlPrettyPrinter(prefs, style, delimiter); StringBuilder sb = new StringBuilder(length); printer.prettyPrint(initialDepth, root, startNode, endNode, sb); diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinter.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinter.java index 34dbe34..9b97dde 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinter.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinter.java @@ -56,16 +56,20 @@ class XmlPrettyPrinter { private StringBuilder mOut; /** String to insert for a single indentation level */ private String mIndentString; + /** Line separator to use */ + private String mLineSeparator; /** * Creates a new {@link XmlPrettyPrinter} * * @param prefs the preferences to format with * @param style the style to format with + * @param lineSeparator the line separator to use, such as "\n" */ - XmlPrettyPrinter(XmlFormatPreferences prefs, XmlFormatStyle style) { + XmlPrettyPrinter(XmlFormatPreferences prefs, XmlFormatStyle style, String lineSeparator) { mPrefs = prefs; mStyle = style; + mLineSeparator = lineSeparator; } /** @@ -179,7 +183,7 @@ class XmlPrettyPrinter { private void printProcessingInstruction(Node node) { mOut.append("<?xml "); //$NON-NLS-1$ mOut.append(node.getNodeValue().trim()); - mOut.append('?').append('>').append('\n'); + mOut.append('?').append('>').append(mLineSeparator); } private void printDocType(Node node) { @@ -187,7 +191,7 @@ class XmlPrettyPrinter { if (node instanceof DocumentTypeImpl) { String content = ((DocumentTypeImpl) node).getSource(); mOut.append(content); - mOut.append('\n'); + mOut.append(mLineSeparator); } } @@ -195,7 +199,8 @@ class XmlPrettyPrinter { indent(depth); mOut.append("<![CDATA["); //$NON-NLS-1$ mOut.append(node.getNodeValue()); - mOut.append("]]>\n"); //$NON-NLS-1$ + mOut.append("]]>"); //$NON-NLS-1$ + mOut.append(mLineSeparator); } private void printText(Node node) { @@ -212,7 +217,7 @@ class XmlPrettyPrinter { DomUtilities.appendXmlTextValue(mOut, text); if (mStyle != XmlFormatStyle.RESOURCE) { - mOut.append('\n'); + mOut.append(mLineSeparator); } } } @@ -228,8 +233,8 @@ class XmlPrettyPrinter { || (curr.getNodeType() == Node.TEXT_NODE && curr.getNodeValue().trim().length() == 0 && (curr.getPreviousSibling() == null - || curr.getPreviousSibling().getNodeType() == Node.ELEMENT_NODE))) { - mOut.append('\n'); + || curr.getPreviousSibling().getNodeType() == Node.ELEMENT_NODE))) { + mOut.append(mLineSeparator); } } @@ -241,10 +246,12 @@ class XmlPrettyPrinter { indent(depth); mOut.append("<!-- "); //$NON-NLS-1$ mOut.append(trimmed); - mOut.append(" -->\n"); //$NON-NLS-1$ + mOut.append(" -->"); //$NON-NLS-1$ + mOut.append(mLineSeparator); } else { indent(depth); - mOut.append("<!--\n"); //$NON-NLS-1$ + mOut.append("<!--"); //$NON-NLS-1$ + mOut.append(mLineSeparator); int index = 0; int end = comment.length(); int recentNewline = 0; @@ -277,16 +284,17 @@ class XmlPrettyPrinter { if (start < end) { mOut.append(comment.substring(start, end)); } - mOut.append('\n'); + mOut.append(mLineSeparator); indent(depth); - mOut.append("-->\n"); //$NON-NLS-1$ + mOut.append("-->"); //$NON-NLS-1$ + mOut.append(mLineSeparator); } } private void printOpenElementTag(int depth, Node node) { Element element = (Element) node; if (newlineBeforeElementOpen(element, depth)) { - mOut.append('\n'); + mOut.append(mLineSeparator); } if (indentBeforeElementOpen(element, depth)) { indent(depth); @@ -304,7 +312,7 @@ class XmlPrettyPrinter { if (singleLine) { mOut.append(' '); } else { - mOut.append('\n'); + mOut.append(mLineSeparator); } // Sort the attributes @@ -328,7 +336,7 @@ class XmlPrettyPrinter { // Don't add a newline at the last attribute line; the > should // immediately follow the last attribute if (attribute != last) { - mOut.append(singleLine ? ' ' : '\n'); + mOut.append(singleLine ? " " : mLineSeparator); //$NON-NLS-1$ } } } @@ -348,7 +356,7 @@ class XmlPrettyPrinter { mOut.append('>'); if (newlineAfterElementOpen(element, depth, isClosed)) { - mOut.append('\n'); + mOut.append(mLineSeparator); } } @@ -363,7 +371,7 @@ class XmlPrettyPrinter { // resource file format // If the element had element children, separate the end tag from them if (newlineBeforeElementClose(element, depth)) { - mOut.append('\n'); + mOut.append(mLineSeparator); } if (indentBeforeElementClose(element, depth)) { indent(depth); @@ -373,7 +381,7 @@ class XmlPrettyPrinter { mOut.append('>'); if (newlineAfterElementClose(element, depth)) { - mOut.append('\n'); + mOut.append(mLineSeparator); } } @@ -452,7 +460,9 @@ class XmlPrettyPrinter { } private boolean indentBeforeElementClose(Element element, int depth) { - return mOut.charAt(mOut.length() - 1) == '\n'; + char lastOutChar = mOut.charAt(mOut.length() - 1); + char lastDelimiterChar = mLineSeparator.charAt(mLineSeparator.length() - 1); + return lastOutChar == lastDelimiterChar; } private boolean newlineAfterElementOpen(Element element, int depth, boolean isClosed) { diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinterTest.java b/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinterTest.java index d1a2ace..ab4cc2f 100644 --- a/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinterTest.java +++ b/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinterTest.java @@ -30,7 +30,7 @@ import junit.framework.TestCase; public class XmlPrettyPrinterTest extends TestCase { private void checkFormat(XmlFormatPreferences prefs, XmlFormatStyle style, - String xml, String expected) throws Exception { + String xml, String expected, String delimiter) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); InputSource is = new InputSource(new StringReader(xml)); @@ -50,7 +50,7 @@ public class XmlPrettyPrinterTest extends TestCase { }); Document document = builder.parse(is); - XmlPrettyPrinter printer = new XmlPrettyPrinter(prefs, style); + XmlPrettyPrinter printer = new XmlPrettyPrinter(prefs, style, delimiter); StringBuilder sb = new StringBuilder(1000); printer.prettyPrint(-1, document, document, document, sb); @@ -61,6 +61,10 @@ public class XmlPrettyPrinterTest extends TestCase { assertEquals(expected, formatted); } + private void checkFormat(XmlFormatPreferences prefs, XmlFormatStyle style, + String xml, String expected) throws Exception { + checkFormat(prefs, style, xml, expected, "\n"); //$NON-NLS-1$ + } private void checkFormat(XmlFormatStyle style, String xml, String expected) throws Exception { XmlFormatPreferences prefs = XmlFormatPreferences.create(); @@ -209,4 +213,18 @@ public class XmlPrettyPrinterTest extends TestCase { "\n" + "</LinearLayout>"); } + + public void testWindowsDelimiters() throws Exception { + checkFormat( + XmlFormatPreferences.create(), XmlFormatStyle.LAYOUT, + "<LinearLayout><Button foo=\"bar\"></Button></LinearLayout>", + "<LinearLayout>\r\n" + + "\r\n" + + " <Button\r\n" + + " foo=\"bar\">\r\n" + + " </Button>\r\n" + + "\r\n" + + "</LinearLayout>", + "\r\n"); + } } |