src/eric7/QScintilla/Editor.py

branch
eric7
changeset 9331
1f8780a59a0d
parent 9221
bf71ee032bb4
child 9333
b0541ec21e51
--- a/src/eric7/QScintilla/Editor.py	Thu Sep 15 13:19:51 2022 +0200
+++ b/src/eric7/QScintilla/Editor.py	Mon Sep 19 09:41:35 2022 +0200
@@ -3924,9 +3924,10 @@
         """
         Public slot to toggle the comment of a block.
 
-        If the line of the cursor or the selection is not commented, it will
-        be commented. If it is commented, the comment block will be removed.
-        The later works independent of the current selection.
+        If the editor contains selected text and the start line is not commented, it
+        will be commented. Otherwise the selection will be un-commented. In case there
+        is no selected text and the current line is not commented, it will be commented.
+        If is commented, the comment block will be removed.
         """
         if self.lexer_ is None or not self.lexer_.canBlockComment():
             return
@@ -3934,23 +3935,26 @@
         commentStr = self.lexer_.commentStr()
         line, index = self.getCursorPosition()
 
-        # check if line starts with our comment string (i.e. was commented
-        # by our comment...() slots
-        if self.hasSelectedText() and self.__isCommentedLine(
-            self.text(self.getSelection()[0]), commentStr
-        ):
-            self.uncommentLineOrSelection()
+        if self.hasSelectedText():
+            # Check if the selection starts with our comment string (i.e. was commented
+            # by our comment...() slots.
+            if self.__isCommentedLine(self.text(self.getSelection()[0]), commentStr):
+                self.uncommentLineOrSelection()
+            else:
+                self.commentLineOrSelection()
         elif not self.__isCommentedLine(self.text(line), commentStr):
-            # it doesn't, so comment the line or selection
+            # No selected text and the current line does not start with our comment
+            # string, so comment the line.
             self.commentLineOrSelection()
         else:
-            # determine the start of the comment block
+            # Uncomment the comment block containing the current line.
+            # 1. determine the start of the comment block
             begline = line
             while begline > 0 and self.__isCommentedLine(
                 self.text(begline - 1), commentStr
             ):
                 begline -= 1
-            # determine the end of the comment block
+            # 2. determine the end of the comment block
             endline = line
             lines = self.lines()
             while endline < lines and self.__isCommentedLine(
@@ -3958,10 +3962,9 @@
             ):
                 endline += 1
 
+            # 3. uncomment the determined block and reset the cursor position
             self.setSelection(begline, 0, endline, self.lineLength(endline))
             self.uncommentLineOrSelection()
-
-            # reset the cursor
             self.setCursorPosition(line, index - len(commentStr))
 
     def commentLine(self):

eric ide

mercurial