[Bf-blender-cvs] [433eb3f35d5] master: Text: toggle comment operator

Campbell Barton noreply at git.blender.org
Thu Aug 1 12:33:46 CEST 2019


Commit: 433eb3f35d5cf3933801027e33399923609c6539
Author: Campbell Barton
Date:   Thu Aug 1 20:31:57 2019 +1000
Branches: master
https://developer.blender.org/rB433eb3f35d5cf3933801027e33399923609c6539

Text: toggle comment operator

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

M	release/scripts/startup/bl_ui/space_text.py
M	source/blender/blenkernel/BKE_text.h
M	source/blender/blenkernel/intern/text.c
M	source/blender/editors/space_text/space_text.c
M	source/blender/editors/space_text/text_intern.h
M	source/blender/editors/space_text/text_ops.c

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

diff --git a/release/scripts/startup/bl_ui/space_text.py b/release/scripts/startup/bl_ui/space_text.py
index d1976894653..1c37e028199 100644
--- a/release/scripts/startup/bl_ui/space_text.py
+++ b/release/scripts/startup/bl_ui/space_text.py
@@ -288,6 +288,7 @@ class TEXT_MT_format(Menu):
 
         layout.operator("text.comment")
         layout.operator("text.uncomment")
+        layout.operator("text.toggle_comment")
 
         layout.separator()
 
diff --git a/source/blender/blenkernel/BKE_text.h b/source/blender/blenkernel/BKE_text.h
index 6509788932c..f018e912945 100644
--- a/source/blender/blenkernel/BKE_text.h
+++ b/source/blender/blenkernel/BKE_text.h
@@ -90,10 +90,10 @@ void txt_backspace_word(struct Text *text);
 bool txt_add_char(struct Text *text, unsigned int add);
 bool txt_add_raw_char(struct Text *text, unsigned int add);
 bool txt_replace_char(struct Text *text, unsigned int add);
-void txt_unindent(struct Text *text);
+bool txt_unindent(struct Text *text);
 void txt_comment(struct Text *text);
 void txt_indent(struct Text *text);
-void txt_uncomment(struct Text *text);
+bool txt_uncomment(struct Text *text);
 void txt_move_lines(struct Text *text, const int direction);
 void txt_duplicate_line(struct Text *text);
 int txt_setcurr_tab_spaces(struct Text *text, int space);
diff --git a/source/blender/blenkernel/intern/text.c b/source/blender/blenkernel/intern/text.c
index 056229ceb1c..29aa3ca1659 100644
--- a/source/blender/blenkernel/intern/text.c
+++ b/source/blender/blenkernel/intern/text.c
@@ -2045,11 +2045,12 @@ static void txt_select_prefix(Text *text, const char *add)
  *
  * \note caller must handle undo.
  */
-static void txt_select_unprefix(Text *text, const char *remove)
+static bool txt_select_unprefix(Text *text, const char *remove)
 {
   int num = 0;
   const int indentlen = strlen(remove);
   bool unindented_first = false;
+  bool changed_any = false;
 
   BLI_assert(!ELEM(NULL, text->curl, text->sell));
 
@@ -2062,6 +2063,7 @@ static void txt_select_unprefix(Text *text, const char *remove)
       text->curl->len -= indentlen;
       memmove(text->curl->line, text->curl->line + indentlen, text->curl->len + 1);
       changed = true;
+      changed_any = true;
     }
 
     txt_make_dirty(text);
@@ -2089,6 +2091,7 @@ static void txt_select_unprefix(Text *text, const char *remove)
   }
 
   /* caller must handle undo */
+  return changed_any;
 }
 
 void txt_comment(Text *text)
@@ -2102,15 +2105,15 @@ void txt_comment(Text *text)
   txt_select_prefix(text, prefix);
 }
 
-void txt_uncomment(Text *text)
+bool txt_uncomment(Text *text)
 {
   const char *prefix = "#";
 
   if (ELEM(NULL, text->curl, text->sell)) {
-    return;
+    return false;
   }
 
-  txt_select_unprefix(text, prefix);
+  return txt_select_unprefix(text, prefix);
 }
 
 void txt_indent(Text *text)
@@ -2124,15 +2127,15 @@ void txt_indent(Text *text)
   txt_select_prefix(text, prefix);
 }
 
-void txt_unindent(Text *text)
+bool txt_unindent(Text *text)
 {
   const char *prefix = (text->flags & TXT_TABSTOSPACES) ? tab_to_spaces : "\t";
 
   if (ELEM(NULL, text->curl, text->sell)) {
-    return;
+    return false;
   }
 
-  txt_select_unprefix(text, prefix);
+  return txt_select_unprefix(text, prefix);
 }
 
 void txt_move_lines(struct Text *text, const int direction)
diff --git a/source/blender/editors/space_text/space_text.c b/source/blender/editors/space_text/space_text.c
index c5e90cf247d..7d8424a5996 100644
--- a/source/blender/editors/space_text/space_text.c
+++ b/source/blender/editors/space_text/space_text.c
@@ -199,6 +199,7 @@ static void text_operatortypes(void)
   WM_operatortype_append(TEXT_OT_convert_whitespace);
   WM_operatortype_append(TEXT_OT_uncomment);
   WM_operatortype_append(TEXT_OT_comment);
+  WM_operatortype_append(TEXT_OT_toggle_comment);
   WM_operatortype_append(TEXT_OT_unindent);
   WM_operatortype_append(TEXT_OT_indent);
 
diff --git a/source/blender/editors/space_text/text_intern.h b/source/blender/editors/space_text/text_intern.h
index aab5069f919..400405155f8 100644
--- a/source/blender/editors/space_text/text_intern.h
+++ b/source/blender/editors/space_text/text_intern.h
@@ -121,6 +121,7 @@ void TEXT_OT_duplicate_line(struct wmOperatorType *ot);
 void TEXT_OT_convert_whitespace(struct wmOperatorType *ot);
 void TEXT_OT_uncomment(struct wmOperatorType *ot);
 void TEXT_OT_comment(struct wmOperatorType *ot);
+void TEXT_OT_toggle_comment(struct wmOperatorType *ot);
 void TEXT_OT_unindent(struct wmOperatorType *ot);
 void TEXT_OT_indent(struct wmOperatorType *ot);
 
diff --git a/source/blender/editors/space_text/text_ops.c b/source/blender/editors/space_text/text_ops.c
index 0c180dc150c..95e7d906b11 100644
--- a/source/blender/editors/space_text/text_ops.c
+++ b/source/blender/editors/space_text/text_ops.c
@@ -1293,6 +1293,50 @@ void TEXT_OT_uncomment(wmOperatorType *ot)
 
 /** \} */
 
+/* -------------------------------------------------------------------- */
+/** \name Toggle-Comment Operator
+ * \{ */
+
+static int text_toggle_comment_exec(bContext *C, wmOperator *UNUSED(op))
+{
+  Text *text = CTX_data_edit_text(C);
+
+  if (txt_has_sel(text)) {
+    text_drawcache_tag_update(CTX_wm_space_text(C), 0);
+
+    ED_text_undo_push_init(C);
+
+    txt_order_cursors(text, false);
+    if (txt_uncomment(text) == false) {
+      txt_comment(text);
+    }
+    text_update_edited(text);
+
+    text_update_cursor_moved(C);
+    WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text);
+
+    return OPERATOR_FINISHED;
+  }
+
+  return OPERATOR_CANCELLED;
+}
+
+void TEXT_OT_toggle_comment(wmOperatorType *ot)
+{
+  /* identifiers */
+  ot->name = "Toggle Comment";
+  ot->idname = "TEXT_OT_toggle_comment";
+
+  /* api callbacks */
+  ot->exec = text_toggle_comment_exec;
+  ot->poll = text_edit_poll;
+
+  /* flags */
+  ot->flag = OPTYPE_UNDO;
+}
+
+/** \} */
+
 /* -------------------------------------------------------------------- */
 /** \name Convert Whitespace Operator
  * \{ */



More information about the Bf-blender-cvs mailing list