From e4803a89f9779c7cfb81d15f0e6ec9cd65df7b4d Mon Sep 17 00:00:00 2001 From: Nikolas Komonen Date: Fri, 5 Oct 2018 09:20:56 -0400 Subject: [PATCH] Formatted node content doesnt get touched Fixes #152 Signed-off-by: Nikolas Komonen --- extensions/deleteThis.xml | 0 .../eclipse/lsp4xml/dom/CharacterData.java | 21 ++++++++++++++ .../java/org/eclipse/lsp4xml/dom/Node.java | 14 ++++++++++ .../lsp4xml/services/XMLFormatter.java | 18 ++++-------- .../org/eclipse/lsp4xml/utils/XMLBuilder.java | 2 +- .../lsp4xml/services/XMLFormatterTest.java | 28 +++++++++++++++++-- 6 files changed, 68 insertions(+), 15 deletions(-) create mode 100644 extensions/deleteThis.xml diff --git a/extensions/deleteThis.xml b/extensions/deleteThis.xml new file mode 100644 index 000000000..e69de29bb diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/dom/CharacterData.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/dom/CharacterData.java index 44b6dbd36..9e626c1a3 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/dom/CharacterData.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/dom/CharacterData.java @@ -45,6 +45,27 @@ public boolean hasMultiLine() { } } + /** + * If data ends with a new line character + * + * Returns false if a character is found before a new line, + * but will ignore whitespace while searching + * @return + */ + public boolean endsWithNewLine() { + for(int i = data.length() - 1; i >= 0; i--) { + char c = data.charAt(i); + if(!Character.isWhitespace(c)) { + return false; + } + if(c == '\n') { + return true; + } + + } + return false; + } + public String getNormalizedData() { if (normalizedData == null) { normalizedData = StringUtils.normalizeSpace(getData()); diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/dom/Node.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/dom/Node.java index 21f3db877..6b6aa7c58 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/dom/Node.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/dom/Node.java @@ -361,6 +361,20 @@ public Element getParentElement() { return null; } + /** + * Checks if previous sibling node is of 'type' + * @param type + * @return + */ + public boolean isPreviousNodeType(Short type) { + int currentIndex = this.getParent().getChildren().indexOf(this); + return currentIndex > 0 ? this.getParent().getChild(currentIndex - 1).isText() : false; + } + + public boolean isFirstChildNode() { + return this.getParent().getChildren().indexOf(this) == 0; + } + public boolean isComment() { return getNodeType() == Node.COMMENT_NODE; } diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/services/XMLFormatter.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/services/XMLFormatter.java index 139197736..838ce7ba4 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/services/XMLFormatter.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/services/XMLFormatter.java @@ -87,7 +87,10 @@ public List format(TextDocument document, Range range, XMLFo private void format(Node node, int level, int end, XMLBuilder xml) { if (node.getNodeType() != Node.DOCUMENT_NODE) { - boolean doLineFeed = !(node.isComment() && ((Comment) node).isCommentSameLineEndTag()) && !node.isText(); + boolean doLineFeed = !(node.isComment() && ((Comment) node).isCommentSameLineEndTag()) + && (!node.isPreviousNodeType(Node.TEXT_NODE) || xml.isJoinContentLines()) + && (!node.isText() || ((xml.isJoinContentLines() && !node.isFirstChildNode()))); + if (level > 0 && doLineFeed) { // add new line + indent xml.linefeed(); @@ -146,13 +149,11 @@ private void format(Node node, int level, int end, XMLBuilder xml) { Text text = (Text) node; if (text.hasData()) { // Generate content - if (text.hasMultiLine() && !xml.isJoinContentLines()) { - xml.linefeed(); - } String content = text.getData(); if (!content.isEmpty()) { xml.addContent(content); } + } return; } else if (node.isElement()) { @@ -180,14 +181,7 @@ private void format(Node node, int level, int end, XMLBuilder xml) { startElementClosed = true; level++; for (Node child : node.getChildren()) { - boolean textElement = !child.isText() - || (child.isCharacterData() && ((CharacterData) child).hasMultiLine()) - || node.getChildren().size() > 1; - if (child.isCharacterData()) { - if (xml.isJoinContentLines()) { - textElement = false; - } - } + boolean textElement = !child.isText(); hasElements = hasElements | textElement; diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/utils/XMLBuilder.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/utils/XMLBuilder.java index d4b767232..913ffac25 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/utils/XMLBuilder.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/utils/XMLBuilder.java @@ -118,7 +118,7 @@ public XMLBuilder addContent(String text) { if (isJoinContentLines()) { normalizeSpace(text, xml); } else { - trimNewLines(text, xml); + xml.append(text); } return this; } diff --git a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/services/XMLFormatterTest.java b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/services/XMLFormatterTest.java index 5043da7b7..365fa2f35 100644 --- a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/services/XMLFormatterTest.java +++ b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/services/XMLFormatterTest.java @@ -389,10 +389,32 @@ public void testContentFormatting2() throws BadLocationException { String expected = "\r" + " Content\r" + - " \r" + + " \r" + " Content2\r" + " Content3\r" + - " \r" + + " \r" + + ""; + + XMLFormattingOptions formattingOptions = createDefaultFormattingOptions(); + formattingOptions.setJoinContentLines(false); + format(content, expected, formattingOptions); + } + + @Test + public void testContentFormattingDontMoveEndTag() throws BadLocationException { + String content = + "\r" + + " Content\r" + + " \r" + + " Content2\r" + + " Content3 \r" + + ""; + String expected = + "\r" + + " Content\r" + + " \r" + + " Content2\r" + + " Content3 \r" + ""; XMLFormattingOptions formattingOptions = createDefaultFormattingOptions(); @@ -448,6 +470,7 @@ public void testContentFormatting6() throws BadLocationException { " Content\r" + // ""; String expected = "\r" + // + "\r" + // " Content\r" + // ""; format(content, expected, formattingOptions); @@ -457,6 +480,7 @@ public void testContentFormatting6() throws BadLocationException { " Content\r\n" + // ""; expected = "\r\n" + // + "\r\n" + // " Content\r\n" + // ""; format(content, expected, formattingOptions);