Skip to content

Commit

Permalink
Fixed tag rename for XML tags containing colons
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Chen <[email protected]>
  • Loading branch information
Alexander Chen committed Dec 8, 2021
1 parent ce526f8 commit bc21ab0
Showing 1 changed file with 31 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ private List<TextEdit> 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);
Expand All @@ -191,23 +194,35 @@ private List<TextEdit> 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<TextEdit> tagTextEdits = new ArrayList<TextEdit>();
// Tag prefix rename
if (!prefix.equals(tagParts[0])) {
tagTextEdits.addAll(renameElementNamespace(xmlDocument, element, prefix.length(), tagParts[0]));
}
// Tag suffix/local name rename
if (!prefix.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);
}
}
Expand Down

0 comments on commit bc21ab0

Please sign in to comment.