diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/XMLRename.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/XMLRename.java index 42907d2d6..18970ae84 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/XMLRename.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/XMLRename.java @@ -180,6 +180,9 @@ private List getTagNameRenameTextEdits(DOMDocument xmlDocument, DOMEle String fullNodeName = element.getNodeName(); int indexOfColon = fullNodeName.indexOf(":"); if (indexOfColon > 0) { + // Take the prefix and local name of the namespace tag + String prefix = element.getPrefix(); + Position startTagStartPosition = startTagRange.getStart(); Position startTagPrefixPosition = new Position(startTagStartPosition.getLine(), startTagStartPosition.getCharacter() + indexOfColon); @@ -191,23 +194,35 @@ private List getTagNameRenameTextEdits(DOMDocument xmlDocument, DOMEle Range startTagPrefixRange = new Range(startTagStartPosition, startTagPrefixPosition); Range endTagPrefixRange = new Range(endTagStartPosition, endTagPrefixPosition); - if (doesTagCoverPosition(startTagPrefixRange, endTagPrefixRange, position)) {// Element prefix rename - String prefix = element.getPrefix(); - return renameElementNamespace(xmlDocument, element, prefix.length(), newText); - } else { // suffix rename without wiping namespace - String suffixName = element.getLocalName(); - int suffixLength = suffixName.length(); - Position startTagEndPosition = startTagRange.getEnd(); - Position suffixStartPositionStart = new Position(startTagEndPosition.getLine(), - startTagEndPosition.getCharacter() - suffixLength); - - Position endTagEndPosition = endTagRange.getEnd(); - Position suffixEndPositionStart = new Position(endTagEndPosition.getLine(), - endTagEndPosition.getCharacter() - suffixLength); - - Range suffixRangeStart = new Range(suffixStartPositionStart, startTagEndPosition); - Range suffixRangeEnd = new Range(suffixEndPositionStart, endTagEndPosition); + String suffixName = element.getLocalName(); + int suffixLength = suffixName.length(); + Position startTagEndPosition = startTagRange.getEnd(); + Position suffixStartPositionStart = new Position(startTagEndPosition.getLine(), + startTagEndPosition.getCharacter() - suffixLength); + + Position endTagEndPosition = endTagRange.getEnd(); + Position suffixEndPositionStart = new Position(endTagEndPosition.getLine(), + endTagEndPosition.getCharacter() - suffixLength); + + Range suffixRangeStart = new Range(suffixStartPositionStart, startTagEndPosition); + Range suffixRangeEnd = new Range(suffixEndPositionStart, endTagEndPosition); + + if (newText.contains(":")) { + String[] tagParts = newText.split(":", 2); + List tagTextEdits = new ArrayList(); + // Tag prefix rename + if (!prefix.equals(tagParts[0])) { + tagTextEdits.addAll(renameElementNamespace(xmlDocument, element, prefix.length(), tagParts[0])); + } + // Tag suffix/local name rename + if (!suffixName.equals(tagParts[1])) { + tagTextEdits.addAll(getRenameList(suffixRangeStart, suffixRangeEnd, tagParts[1])); + } + return tagTextEdits; + } else if (doesTagCoverPosition(startTagPrefixRange, endTagPrefixRange, position)) { + return renameElementNamespace(xmlDocument, element, prefix.length(), newText); + } else { return getRenameList(suffixRangeStart, suffixRangeEnd, newText); } } diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/XMLRenameTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/XMLRenameTest.java index 79d60008d..17a6fa44c 100644 --- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/XMLRenameTest.java +++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/XMLRenameTest.java @@ -81,7 +81,7 @@ public void insideEndTag() throws BadLocationException { @Test public void testNamespaceRename() throws BadLocationException { - String xml = + String xml = "\n" + " \n" + " \n" + @@ -90,15 +90,15 @@ public void testNamespaceRename() throws BadLocationException { " \n" + ""; assertRename(xml, "ns", edits("ns", r(0, 12, 14), r(0, 1, 3), r(6, 2, 4), //root attribute, start tag, end tag - r(1, 3, 5), r(1, 10, 12), - r(2, 3, 5), r(2, 10, 12), + r(1, 3, 5), r(1, 10, 12), + r(2, 3, 5), r(2, 10, 12), r(3, 3, 5), //\n" + " \n" + " \n" + @@ -106,12 +106,12 @@ public void testNamespaceRenameEndTagPrefix() throws BadLocationException { " \n" + " \n" + ""; - assertRename(xml, "ns", edits("ns", r(2, 3, 5), r(2, 10, 12))); + assertRename(xml, "ns", edits("ns", r(2, 3, 5), r(2, 10, 12))); } @Test public void testNamespaceRenameStartTagPrefix() throws BadLocationException { - String xml = + String xml = "\n" + " \n" + " \n" + @@ -119,12 +119,12 @@ public void testNamespaceRenameStartTagPrefix() throws BadLocationException { " \n" + " \n" + ""; - assertRename(xml, "ns", edits("ns", r(2, 3, 5), r(2, 10, 12))); + assertRename(xml, "ns", edits("ns", r(2, 3, 5), r(2, 10, 12))); } @Test public void testNamespaceRenameEndTagSuffix() throws BadLocationException { - String xml = + String xml = "\n" + " \n" + " \n" + @@ -132,12 +132,12 @@ public void testNamespaceRenameEndTagSuffix() throws BadLocationException { " \n" + " \n" + ""; - assertRename(xml, "BB", edits("BB", r(2, 6, 7), r(2, 13, 14))); + assertRename(xml, "BB", edits("BB", r(2, 6, 7), r(2, 13, 14))); } @Test public void testNamespaceRenameStartTagSuffix() throws BadLocationException { - String xml = + String xml = "\n" + " \n" + " \n" + @@ -145,16 +145,48 @@ public void testNamespaceRenameStartTagSuffix() throws BadLocationException { " \n" + " \n" + ""; - assertRename(xml, "BB", edits("BB", r(2, 6, 7), r(2, 13, 14))); + assertRename(xml, "BB", edits("BB", r(2, 6, 7), r(2, 13, 14))); + } + + @Test + public void testNamespaceRenameWholeTagHoverLocalName() throws BadLocationException { + String xml = + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + ""; + + List expectedEdits = edits("ns", r(2, 3, 5), r(2, 10, 12)); + expectedEdits.addAll(edits("tag", r(2, 6, 7), r(2, 13, 14))); + assertRename(xml, "ns:tag", expectedEdits); + } + + @Test + public void testNamespaceRenameWholeTagHoverPrefix() throws BadLocationException { + String xml = + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + ""; + + List expectedEdits = edits("ns", r(2, 3, 5), r(2, 10, 12)); + expectedEdits.addAll(edits("tag", r(2, 6, 7), r(2, 13, 14))); + assertRename(xml, "ns:tag", expectedEdits); } @Test public void testTryToRenameXMLNS() throws BadLocationException { - String xml = + String xml = "\n" + " \n" + ""; - assertRename(xml, "BBBB"); + assertRename(xml, "BBBB"); }