Manager: Implement XML comments in selectively commenting feature
This commit is contained in:
+25
-14
@@ -391,25 +391,32 @@ void Dotfile::selectivelyCommentOrUncomment(const std::string& path)
|
|||||||
size_t positionInFile = 0;
|
size_t positionInFile = 0;
|
||||||
|
|
||||||
auto commentOrUncommentLine = [&](std::string& line, bool addComment) {
|
auto commentOrUncommentLine = [&](std::string& line, bool addComment) {
|
||||||
|
size_t indentation = line.find_first_not_of(" \t");
|
||||||
|
|
||||||
|
// Empty lines are skipped
|
||||||
|
if (line.empty() || indentation == std::string::npos) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
size_t lineLength = line.size();
|
size_t lineLength = line.size();
|
||||||
size_t whiteSpaceBeforeComment = line.find_first_not_of(" \t");
|
size_t commentStart = line.find(commentCharacter, indentation);
|
||||||
size_t contentAfterComment = line.find_first_not_of(" \t" + commentCharacter);
|
size_t commentEnd = line.find(commentTerminationCharacter, commentStart);
|
||||||
// NOTE: The +1 is needed to take the newline into account
|
bool hasComment = commentStart != std::string::npos && commentEnd != std::string::npos;
|
||||||
size_t contentLength = lineLength + 1 - contentAfterComment - (lineLength - line.find_last_not_of(" \t" + commentTerminationCharacter));
|
|
||||||
|
|
||||||
// If there was no comment, grab whitespace correctly
|
if (hasComment && !addComment) {
|
||||||
if (whiteSpaceBeforeComment == std::string::npos) {
|
size_t contentStart = line.find_first_not_of(" \t", commentStart + commentCharacter.size());
|
||||||
whiteSpaceBeforeComment = contentAfterComment;
|
size_t contentLength = commentEnd - contentStart;
|
||||||
}
|
|
||||||
|
|
||||||
if (!addComment) {
|
line = line.substr(0, indentation)
|
||||||
line = line.substr(0, whiteSpaceBeforeComment)
|
+ line.substr(contentStart, contentLength);
|
||||||
+ line.substr(contentAfterComment, contentLength);
|
|
||||||
}
|
}
|
||||||
else {
|
else if (!hasComment && addComment) {
|
||||||
line = line.substr(0, whiteSpaceBeforeComment)
|
size_t contentStart = line.find_first_not_of(" \t");
|
||||||
|
size_t contentLength = line.size() - contentStart;
|
||||||
|
|
||||||
|
line = line.substr(0, indentation)
|
||||||
+ commentCharacter + ' '
|
+ commentCharacter + ' '
|
||||||
+ line.substr(contentAfterComment, contentLength)
|
+ line.substr(contentStart, contentLength)
|
||||||
+ (!commentTerminationCharacter.empty() ? ' ' + commentTerminationCharacter : "");
|
+ (!commentTerminationCharacter.empty() ? ' ' + commentTerminationCharacter : "");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -437,6 +444,10 @@ void Dotfile::selectivelyCommentOrUncomment(const std::string& path)
|
|||||||
if (i > 0 && commentCharacter.at(i - 1) == '/' && commentCharacter.at(i) == '*') {
|
if (i > 0 && commentCharacter.at(i - 1) == '/' && commentCharacter.at(i) == '*') {
|
||||||
commentTerminationCharacter = "*/";
|
commentTerminationCharacter = "*/";
|
||||||
}
|
}
|
||||||
|
// Support for <!-- XMl comments -->
|
||||||
|
if (i > 0 && commentCharacter.at(i - 1) == '<' && commentCharacter.at(i) == '!' && commentCharacter.at(i + 1) == '-' && commentCharacter.at(i + 2) == '-') {
|
||||||
|
commentTerminationCharacter = "-->";
|
||||||
|
}
|
||||||
// NOTE: Modification of the string should be at the end of the iteration to prevent 'out of range' errors
|
// NOTE: Modification of the string should be at the end of the iteration to prevent 'out of range' errors
|
||||||
if (commentCharacter.at(i) == ' ' || commentCharacter.at(i) == '\t') {
|
if (commentCharacter.at(i) == ' ' || commentCharacter.at(i) == '\t') {
|
||||||
commentCharacter.erase(i, 1);
|
commentCharacter.erase(i, 1);
|
||||||
|
|||||||
Reference in New Issue
Block a user