Skip to content

Commit

Permalink
Fix selection formatting ignoring split attribute indentation
Browse files Browse the repository at this point in the history
preference

Signed-off-by: David Kwon <dakwon@redhat.com>
  • Loading branch information
dkwon17 authored and NikolasKomonen committed Jun 14, 2019
1 parent d4bdf5d commit 7d8eccf
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -279,7 +292,7 @@ private void format(DOMNode node) throws BadLocationException {
if (element.hasAttributes()) {
// generate attributes
List<DOMAttr> attributes = element.getAttributeNodes();
if (attributes.size() == 1) {
if (hasSingleAttributeInFullDoc(element)) {
DOMAttr singleAttribute = attributes.get(0);
xmlBuilder.addSingleAttribute(singleAttribute.getName(),
singleAttribute.getOriginalValue());
Expand Down Expand Up @@ -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<? extends TextEdit> getFormatTextEdit() throws BadLocationException {
Position startPosition = this.textDocument.positionAt(this.startOffset);
Position endPosition = this.textDocument.positionAt(this.endOffset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,51 @@ public void testSplitAttributesProlog() throws BadLocationException {
format(content, expected, formattingOptions);
}

@Test
public void testSplitAttributesRangeOneLine() throws BadLocationException {
String content =
"<note>\r\n" +
" <from\r\n" +
" |foo = \"bar\"|\r\n" +
" bar=\"foo\">sss</from>\r\n" +
"</note>";

String expected =
"<note>\r\n" +
" <from\r\n" +
" foo=\"bar\"\r\n" +
" bar=\"foo\">sss</from>\r\n" +
"</note>";

XMLFormattingOptions formattingOptions = createDefaultFormattingOptions();
formattingOptions.setSplitAttributes(true);
format(content, expected, formattingOptions);
}

public void testSplitAttributesRangeMultipleLines() throws BadLocationException {
String content =
"<note>\r\n" +
" <from\r\n" +
" |foo = \"bar\"\r\n" +
"bar = \"foo\" abc = \r\n" +
" \"def\"\r\n" +
" ghi=\"jkl\"|>sss</from>\r\n" +
"</note>";

String expected =
"<note>\r\n" +
" <from\r\n" +
" foo=\"bar\"\r\n" +
" bar=\"foo\"\r\n" +
" abc=\"def\"\r\n" +
" ghi=\"jkl\">sss</from>\r\n" +
"</note>";;

XMLFormattingOptions formattingOptions = createDefaultFormattingOptions();
formattingOptions.setSplitAttributes(true);
format(content, expected, formattingOptions);
}

@Test
public void testUnclosedEndTagBracketTrailingElement() throws BadLocationException {
String content =
Expand Down Expand Up @@ -1990,6 +2035,8 @@ public void testAttributeNameValueMultipleLinesWithChildrenSiblings() throws Bad
format(content, expected);
}



@Test
public void testPreserveNewlines() throws BadLocationException {
XMLFormattingOptions formattingOptions = createDefaultFormattingOptions();
Expand Down

0 comments on commit 7d8eccf

Please sign in to comment.