Browse Source

Manager: Implement XML comments in selectively commenting feature

master
Riyyi 2 weeks ago
parent
commit
33ae6c57a2
  1. 39
      src/dotfile.cpp

39
src/dotfile.cpp

@ -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 lineLength = line.size(); size_t indentation = line.find_first_not_of(" \t");
size_t whiteSpaceBeforeComment = line.find_first_not_of(" \t");
size_t contentAfterComment = line.find_first_not_of(" \t" + commentCharacter);
// NOTE: The +1 is needed to take the newline into account
size_t contentLength = lineLength + 1 - contentAfterComment - (lineLength - line.find_last_not_of(" \t" + commentTerminationCharacter));
// If there was no comment, grab whitespace correctly // Empty lines are skipped
if (whiteSpaceBeforeComment == std::string::npos) { if (line.empty() || indentation == std::string::npos) {
whiteSpaceBeforeComment = contentAfterComment; return;
} }
if (!addComment) { size_t lineLength = line.size();
line = line.substr(0, whiteSpaceBeforeComment) size_t commentStart = line.find(commentCharacter, indentation);
+ line.substr(contentAfterComment, contentLength); size_t commentEnd = line.find(commentTerminationCharacter, commentStart);
bool hasComment = commentStart != std::string::npos && commentEnd != std::string::npos;
if (hasComment && !addComment) {
size_t contentStart = line.find_first_not_of(" \t", commentStart + commentCharacter.size());
size_t contentLength = commentEnd - contentStart;
line = line.substr(0, indentation)
+ line.substr(contentStart, 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);

Loading…
Cancel
Save