Skip to content

Commit

Permalink
Formatted node content doesnt get touched
Browse files Browse the repository at this point in the history
Fixes eclipse#152

Signed-off-by: Nikolas Komonen <[email protected]>
  • Loading branch information
NikolasKomonen committed Oct 5, 2018
1 parent ab6d20e commit 5692f64
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 17 deletions.
Empty file added extensions/deleteThis.xml
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,7 @@ private void createPrologCompletionSuggestion(int tagNameEnd, String tag, Comple
Range editRange = getReplaceRange(tagNameEnd, end, request);
item.setTextEdit(new TextEdit(editRange, " version=\"1.0\" encoding=\"UTF-8\"?" + closeTag + "$0"));
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
LOGGER.log(Level.SEVERE, "While performing getReplaceRange for prolog completion.", e);
}
response.addCompletionItem(item);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ public List<? extends TextEdit> 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();
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public XMLBuilder addContent(String text) {
if (isJoinContentLines()) {
normalizeSpace(text, xml);
} else {
trimNewLines(text, xml);
xml.append(text);
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,32 @@ public void testContentFormatting2() throws BadLocationException {
String expected =
"<a>\r" +
" Content\r" +
" <b>\r" +
" <b>\r" +
" Content2\r" +
" Content3\r" +
" </b>\r" +
" </b>\r" +
"</a>";

XMLFormattingOptions formattingOptions = createDefaultFormattingOptions();
formattingOptions.setJoinContentLines(false);
format(content, expected, formattingOptions);
}

@Test
public void testContentFormattingDontMoveEndTag() throws BadLocationException {
String content =
"<a>\r" +
" Content\r" +
" <b>\r" +
" Content2\r" +
" Content3 </b>\r" +
"</a>";
String expected =
"<a>\r" +
" Content\r" +
" <b>\r" +
" Content2\r" +
" Content3 </b>\r" +
"</a>";

XMLFormattingOptions formattingOptions = createDefaultFormattingOptions();
Expand Down Expand Up @@ -448,6 +470,7 @@ public void testContentFormatting6() throws BadLocationException {
" Content\r" + //
"</a>";
String expected = "<a>\r" + //
"\r" + //
" Content\r" + //
"</a>";
format(content, expected, formattingOptions);
Expand All @@ -457,6 +480,7 @@ public void testContentFormatting6() throws BadLocationException {
" Content\r\n" + //
"</a>";
expected = "<a>\r\n" + //
"\r\n" + //
" Content\r\n" + //
"</a>";
format(content, expected, formattingOptions);
Expand Down

0 comments on commit 5692f64

Please sign in to comment.