From 7d8eccf2e8cc8d56cc2bf0878cbff3ef83740053 Mon Sep 17 00:00:00 2001 From: David Kwon Date: Tue, 11 Jun 2019 16:06:26 -0400 Subject: [PATCH] Fix selection formatting ignoring split attribute indentation preference Signed-off-by: David Kwon --- .../lsp4xml/services/XMLFormatter.java | 26 ++++++++-- .../lsp4xml/services/XMLFormatterTest.java | 47 +++++++++++++++++++ 2 files changed, 69 insertions(+), 4 deletions(-) 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 c8b44ab0c..03e0f7059 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 @@ -214,7 +214,20 @@ private int getFullOffsetFromRangeOffset(int rangeOffset) { } private DOMElement getFullDocElemFromRangeElem(DOMElement elemFromRangeDoc) { - int fullOffset = getFullOffsetFromRangeOffset(elemFromRangeDoc.getEnd()); + int fullOffset = -1; + + if (elemFromRangeDoc.hasStartTag()) { + fullOffset = getFullOffsetFromRangeOffset(elemFromRangeDoc.getStartTagOpenOffset()) + 1; + // +1 because offset must be here: <|root + // for DOMNode.findNodeAt() to find the correct element + } else if (elemFromRangeDoc.hasEndTag()) { + fullOffset = getFullOffsetFromRangeOffset(elemFromRangeDoc.getEndTagCloseOffset()) - 1; + // -1 because offset must be here: root|> + // for DOMNode.findNodeAt() to find the correct element + } else { + return null; + } + DOMElement elemFromFullDoc = (DOMElement) this.fullDomDocument.findNodeAt(fullOffset); return elemFromFullDoc; } @@ -224,7 +237,7 @@ private boolean startTagExistsInRangeDocument(DOMNode node) { return false; } - return ((DOMElement) node).getStartTagOpenOffset() != DOMNode.NULL_VALUE; + return ((DOMElement) node).hasStartTag(); } private boolean startTagExistsInFullDocument(DOMNode node) { @@ -238,7 +251,7 @@ private boolean startTagExistsInFullDocument(DOMNode node) { return false; } - return elemFromFullDoc.getStartTagOpenOffset() != DOMNode.NULL_VALUE; + return elemFromFullDoc.hasStartTag(); } private void format(DOMNode node) throws BadLocationException { @@ -279,7 +292,7 @@ private void format(DOMNode node) throws BadLocationException { if (element.hasAttributes()) { // generate attributes List attributes = element.getAttributeNodes(); - if (attributes.size() == 1) { + if (hasSingleAttributeInFullDoc(element)) { DOMAttr singleAttribute = attributes.get(0); xmlBuilder.addSingleAttribute(singleAttribute.getName(), singleAttribute.getOriginalValue()); @@ -482,6 +495,11 @@ private static boolean formatDTD(DOMDocumentType doctype, int level, int end, XM return true; } + private boolean hasSingleAttributeInFullDoc(DOMElement element) { + DOMElement fullElement = getFullDocElemFromRangeElem(element); + return fullElement.getAttributeNodes().size() == 1; + } + private List getFormatTextEdit() throws BadLocationException { Position startPosition = this.textDocument.positionAt(this.startOffset); Position endPosition = this.textDocument.positionAt(this.endOffset); 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 c42cf73fd..f17f36b01 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 @@ -437,6 +437,51 @@ public void testSplitAttributesProlog() throws BadLocationException { format(content, expected, formattingOptions); } + @Test + public void testSplitAttributesRangeOneLine() throws BadLocationException { + String content = + "\r\n" + + " sss\r\n" + + ""; + + String expected = + "\r\n" + + " sss\r\n" + + ""; + + XMLFormattingOptions formattingOptions = createDefaultFormattingOptions(); + formattingOptions.setSplitAttributes(true); + format(content, expected, formattingOptions); + } + + public void testSplitAttributesRangeMultipleLines() throws BadLocationException { + String content = + "\r\n" + + " sss\r\n" + + ""; + + String expected = + "\r\n" + + " sss\r\n" + + "";; + + XMLFormattingOptions formattingOptions = createDefaultFormattingOptions(); + formattingOptions.setSplitAttributes(true); + format(content, expected, formattingOptions); + } + @Test public void testUnclosedEndTagBracketTrailingElement() throws BadLocationException { String content = @@ -1990,6 +2035,8 @@ public void testAttributeNameValueMultipleLinesWithChildrenSiblings() throws Bad format(content, expected); } + + @Test public void testPreserveNewlines() throws BadLocationException { XMLFormattingOptions formattingOptions = createDefaultFormattingOptions();