[Bf-blender-cvs] [94dce826a9a] master: Text: only un-comment blocks which are completely commented

Campbell Barton noreply at git.blender.org
Mon Aug 5 08:04:05 CEST 2019


Commit: 94dce826a9a7b77dd99fa7cd8d3b7147913ba591
Author: Campbell Barton
Date:   Mon Aug 5 14:10:36 2019 +1000
Branches: master
https://developer.blender.org/rB94dce826a9a7b77dd99fa7cd8d3b7147913ba591

Text: only un-comment blocks which are completely commented

It's common to select a block of code and comment it
which may already contains some comments.

Now only un-comment blocks which are completely commented
(ignoring white-space).

Makes toggle comments behave more usefully, resolves T68060.

===================================================================

M	source/blender/blenkernel/intern/text.c

===================================================================

diff --git a/source/blender/blenkernel/intern/text.c b/source/blender/blenkernel/intern/text.c
index 29aa3ca1659..b2ea5b12603 100644
--- a/source/blender/blenkernel/intern/text.c
+++ b/source/blender/blenkernel/intern/text.c
@@ -2042,10 +2042,12 @@ static void txt_select_prefix(Text *text, const char *add)
  *
  * \param r_line_index_mask: List of lines that are already at indent level 0,
  * to store them later into the undo buffer.
+ * \param require_all: When true, all non-empty lines must have this prefix.
+ * Needed for comments where we might want to un-comment a block which contains some comments.
  *
  * \note caller must handle undo.
  */
-static bool txt_select_unprefix(Text *text, const char *remove)
+static bool txt_select_unprefix(Text *text, const char *remove, const bool require_all)
 {
   int num = 0;
   const int indentlen = strlen(remove);
@@ -2054,6 +2056,29 @@ static bool txt_select_unprefix(Text *text, const char *remove)
 
   BLI_assert(!ELEM(NULL, text->curl, text->sell));
 
+  if (require_all) {
+    /* Check all non-empty lines use this 'remove',
+     * so the operation is applied equally or not at all. */
+    TextLine *l = text->curl;
+    while (true) {
+      if (STREQLEN(l->line, remove, indentlen)) {
+        /* pass */
+      }
+      else {
+        /* Blank lines or whitespace can be skipped. */
+        for (int i = 0; i < l->len; i++) {
+          if (!ELEM(l->line[i], '\t', ' ')) {
+            return false;
+          }
+        }
+      }
+      if (l == text->sell) {
+        break;
+      }
+      l = l->next;
+    }
+  }
+
   while (true) {
     bool changed = false;
     if (STREQLEN(text->curl->line, remove, indentlen)) {
@@ -2113,7 +2138,7 @@ bool txt_uncomment(Text *text)
     return false;
   }
 
-  return txt_select_unprefix(text, prefix);
+  return txt_select_unprefix(text, prefix, true);
 }
 
 void txt_indent(Text *text)
@@ -2135,7 +2160,7 @@ bool txt_unindent(Text *text)
     return false;
   }
 
-  return txt_select_unprefix(text, prefix);
+  return txt_select_unprefix(text, prefix, false);
 }
 
 void txt_move_lines(struct Text *text, const int direction)



More information about the Bf-blender-cvs mailing list