From 87ab4720a4ce0a6415b5bf755489d2f65501c76a Mon Sep 17 00:00:00 2001
From: Jack Lilhammers <jack.lilhammers@gmail.com>
Date: Fri, 15 May 2020 15:03:28 +0200
Subject: [PATCH] C++: Commenting defaults to single line comments

---
 src/libs/utils/uncommentselection.cpp | 21 ++++++++++++---------
 src/libs/utils/uncommentselection.h   |  3 ++-
 2 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/src/libs/utils/uncommentselection.cpp b/src/libs/utils/uncommentselection.cpp
index 6771f3f4ea..8c172f20d1 100644
--- a/src/libs/utils/uncommentselection.cpp
+++ b/src/libs/utils/uncommentselection.cpp
@@ -74,7 +74,8 @@ static bool isComment(const QString &text, int index,
 }
 
 
-void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &definition)
+
+void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &definition, bool singleLineOnly)
 {
     if (!definition.isValid())
         return;
@@ -103,7 +104,7 @@ void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &de
 
     bool hasSelection = cursor.hasSelection();
 
-    if (hasSelection && definition.hasMultiLineStyle()) {
+    if (hasSelection && definition.hasMultiLineStyle() && !singleLineOnly) {
 
         QString startText = startBlock.text();
         int startPos = start - startBlock.position();
@@ -119,14 +120,14 @@ void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &de
         }
 
         bool hasSelStart = startPos <= startText.length() - multiLineStartLength
-            && isComment(startText, startPos, definition.multiLineStart);
+                           && isComment(startText, startPos, definition.multiLineStart);
 
         QString endText = endBlock.text();
         int endPos = end - endBlock.position();
         const int multiLineEndLength = definition.multiLineEnd.length();
         bool hasTrailingCharacters =
-                !endText.left(endPos).remove(definition.singleLine).trimmed().isEmpty()
-                && !endText.mid(endPos).trimmed().isEmpty();
+            !endText.left(endPos).remove(definition.singleLine).trimmed().isEmpty()
+            && !endText.mid(endPos).trimmed().isEmpty();
 
         if (endPos <= endText.length() - multiLineEndLength
             && isComment(endText, endPos, definition.multiLineEnd)) {
@@ -135,7 +136,7 @@ void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &de
         }
 
         bool hasSelEnd = endPos >= multiLineEndLength
-            && isComment(endText, endPos - multiLineEndLength, definition.multiLineEnd);
+                         && isComment(endText, endPos - multiLineEndLength, definition.multiLineEnd);
 
         doMultiLineStyleUncomment = hasSelStart && hasSelEnd;
         doMultiLineStyleComment = !doMultiLineStyleUncomment
@@ -145,10 +146,12 @@ void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &de
     } else if (!hasSelection && !definition.hasSingleLineStyle()) {
 
         QString text = startBlock.text().trimmed();
-        doMultiLineStyleUncomment = text.startsWith(definition.multiLineStart)
-                                    && text.endsWith(definition.multiLineEnd);
-        doMultiLineStyleComment = !doMultiLineStyleUncomment && !text.isEmpty();
 
+        if (!singleLineOnly) {
+            doMultiLineStyleUncomment = text.startsWith(definition.multiLineStart)
+                                        && text.endsWith(definition.multiLineEnd);
+            doMultiLineStyleComment = !doMultiLineStyleUncomment && !text.isEmpty();
+        }
         start = startBlock.position();
         end = endBlock.position() + endBlock.length() - 1;
 
diff --git a/src/libs/utils/uncommentselection.h b/src/libs/utils/uncommentselection.h
index b61fce48c9..68dd71dbb3 100644
--- a/src/libs/utils/uncommentselection.h
+++ b/src/libs/utils/uncommentselection.h
@@ -58,6 +58,7 @@ public:
 
 QTCREATOR_UTILS_EXPORT
 void unCommentSelection(QPlainTextEdit *edit,
-                        const CommentDefinition &definiton = CommentDefinition());
+                        const CommentDefinition &definiton = CommentDefinition(),
+                        bool singleLineOnly = true);
 
 } // namespace Utils
-- 
2.17.1.windows.2

