From 33ae6c57a252679fc53b562789325b961dbbdde1 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Fri, 24 Jan 2025 21:53:59 +0100 Subject: [PATCH] Manager: Implement XML comments in selectively commenting feature --- src/dotfile.cpp | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/dotfile.cpp b/src/dotfile.cpp index 1a34b46..aabbc67 100644 --- a/src/dotfile.cpp +++ b/src/dotfile.cpp @@ -391,25 +391,32 @@ void Dotfile::selectivelyCommentOrUncomment(const std::string& path) size_t positionInFile = 0; auto commentOrUncommentLine = [&](std::string& line, bool addComment) { - size_t lineLength = line.size(); - 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)); + size_t indentation = line.find_first_not_of(" \t"); - // If there was no comment, grab whitespace correctly - if (whiteSpaceBeforeComment == std::string::npos) { - whiteSpaceBeforeComment = contentAfterComment; + // Empty lines are skipped + if (line.empty() || indentation == std::string::npos) { + return; } - if (!addComment) { - line = line.substr(0, whiteSpaceBeforeComment) - + line.substr(contentAfterComment, contentLength); + size_t lineLength = line.size(); + size_t commentStart = line.find(commentCharacter, indentation); + 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 { - line = line.substr(0, whiteSpaceBeforeComment) + else if (!hasComment && addComment) { + size_t contentStart = line.find_first_not_of(" \t"); + size_t contentLength = line.size() - contentStart; + + line = line.substr(0, indentation) + commentCharacter + ' ' - + line.substr(contentAfterComment, contentLength) + + line.substr(contentStart, contentLength) + (!commentTerminationCharacter.empty() ? ' ' + commentTerminationCharacter : ""); } @@ -437,6 +444,10 @@ void Dotfile::selectivelyCommentOrUncomment(const std::string& path) if (i > 0 && commentCharacter.at(i - 1) == '/' && commentCharacter.at(i) == '*') { commentTerminationCharacter = "*/"; } + // Support for + 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 if (commentCharacter.at(i) == ' ' || commentCharacter.at(i) == '\t') { commentCharacter.erase(i, 1);