Skip to content

Commit

Permalink
Stops autoClose of end tag if it already exists
Browse files Browse the repository at this point in the history
Fixes eclipse-lemminx#314

Signed-off-by: Nikolas Komonen <[email protected]>
  • Loading branch information
NikolasKomonen committed Mar 8, 2019
1 parent 4797365 commit f169217
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ public String doTagComplete(DOMDocument xmlDocument, Position position) {
int offset;
try {
offset = xmlDocument.offsetAt(position);
if(offset - 2 < 0) { //There is not enough content for autoClose
return null;
}
} catch (BadLocationException e) {
LOGGER.log(Level.SEVERE, "doTagComplete failed", e);
return null;
Expand All @@ -313,34 +316,27 @@ public String doTagComplete(DOMDocument xmlDocument, Position position) {
return null;
}
char c = xmlDocument.getText().charAt(offset - 1);
char cBefore = xmlDocument.getText().charAt(offset - 2);
if (c == '>') {
DOMNode node = xmlDocument.findNodeBefore(offset);
if (node != null && node.isElement() && ((DOMElement) node).getTagName() != null
&& !isEmptyElement(((DOMElement) node).getTagName()) && node.getStart() < offset
&& (!((DOMElement) node).hasEndTag() || ((DOMElement) node).getEndTagOpenOffset() > offset)) {
Scanner scanner = XMLScanner.createScanner(xmlDocument.getText(), node.getStart());
TokenType token = scanner.scan();
while (token != TokenType.EOS && scanner.getTokenEnd() <= offset) {
if (token == TokenType.StartTagClose && scanner.getTokenEnd() == offset) {
return "$0</" + ((DOMElement) node).getTagName() + ">";
}
token = scanner.scan();
}
DOMElement element = ((DOMElement) node);
if (node != null
&& node.isElement()
&& !element.isSelfClosed()
&& element.getTagName() != null
&& !isEmptyElement(((DOMElement) node).getTagName())
&& node.getStart() < offset
&& (!((DOMElement) node).hasEndTag())) {

return "$0</" + ((DOMElement) node).getTagName() + ">";
}
} else if (c == '/') {
} else if (cBefore == '<' && c == '/') {
DOMNode node = xmlDocument.findNodeBefore(offset);
while (node != null && node.isClosed()) {
node = node.getParentNode();
}
if (node != null && node.isElement() && ((DOMElement) node).getTagName() != null) {
Scanner scanner = XMLScanner.createScanner(xmlDocument.getText(), node.getStart());
TokenType token = scanner.scan();
while (token != TokenType.EOS && scanner.getTokenEnd() <= offset) {
if (token == TokenType.EndTagOpen && scanner.getTokenEnd() == offset) {
return ((DOMElement) node).getTagName() + ">";
}
token = scanner.scan();
}
return ((DOMElement) node).getTagName() + ">$0";
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ public void doTagComplete() throws BadLocationException {
testTagCompletion("<div>|</div>", null);
testTagCompletion("<div class=\"\">|", "$0</div>");
testTagCompletion("<img />|", null);
testTagCompletion("<div><br /></|", "div>");
testTagCompletion("<div><br /><span></span></|", "div>");
testTagCompletion("<div><br /></|", "div>$0");
testTagCompletion("<div><br /><span></span></|", "div>$0");
// testTagCompletion("<div><h1><br /><span></span><img /></| </h1></div>",
// "h1>");
}
Expand All @@ -133,6 +133,7 @@ public void testAutoCloseTagCompletion() {
assertAutoCloseEndTagCompletion("<a><b>|</a>", "$0</b>");
assertAutoCloseEndTagCompletion("<a> <b>|</a>", "$0</b>");
assertAutoCloseEndTagCompletion("<a><b>|", "$0</b>");
assertAutoCloseEndTagCompletion("<a></|", "a>$0");
}

@Test
Expand Down

0 comments on commit f169217

Please sign in to comment.