[Bf-blender-cvs] [91fa07dfb18] master: Text: merge toggle comments into a single operator

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


Commit: 91fa07dfb18f35783177db47d4c721e5a03aad43
Author: Campbell Barton
Date:   Mon Aug 5 13:54:27 2019 +1000
Branches: master
https://developer.blender.org/rB91fa07dfb18f35783177db47d4c721e5a03aad43

Text: merge toggle comments into a single operator

This allows users to map comment/un-comment to be mapped to keys.

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

M	release/scripts/presets/keyconfig/keymap_data/blender_default.py
M	release/scripts/startup/bl_ui/space_text.py
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/presets/keyconfig/keymap_data/blender_default.py b/release/scripts/presets/keyconfig/keymap_data/blender_default.py
index 78fd2fe8008..8a35a937928 100644
--- a/release/scripts/presets/keyconfig/keymap_data/blender_default.py
+++ b/release/scripts/presets/keyconfig/keymap_data/blender_default.py
@@ -2189,7 +2189,7 @@ def km_text(params):
          {"properties": [("direction", 'DOWN')]}),
         ("text.indent", {"type": 'TAB', "value": 'PRESS'}, None),
         ("text.unindent", {"type": 'TAB', "value": 'PRESS', "shift": True}, None),
-        ("text.toggle_comment", {"type": 'SLASH', "value": 'PRESS', "ctrl": True}, None),
+        ("text.comment_toggle", {"type": 'SLASH', "value": 'PRESS', "ctrl": True}, None),
         ("text.move", {"type": 'HOME', "value": 'PRESS'},
          {"properties": [("type", 'LINE_BEGIN')]}),
         ("text.move", {"type": 'END', "value": 'PRESS'},
diff --git a/release/scripts/startup/bl_ui/space_text.py b/release/scripts/startup/bl_ui/space_text.py
index 7a50b690ad8..91984131464 100644
--- a/release/scripts/startup/bl_ui/space_text.py
+++ b/release/scripts/startup/bl_ui/space_text.py
@@ -291,9 +291,7 @@ class TEXT_MT_format(Menu):
 
         layout.separator()
 
-        layout.operator("text.comment")
-        layout.operator("text.uncomment")
-        layout.operator("text.toggle_comment")
+        layout.operator("text.comment_toggle")
 
         layout.separator()
 
diff --git a/source/blender/editors/space_text/space_text.c b/source/blender/editors/space_text/space_text.c
index 945b9190d26..8bfb6c87625 100644
--- a/source/blender/editors/space_text/space_text.c
+++ b/source/blender/editors/space_text/space_text.c
@@ -197,9 +197,7 @@ static void text_operatortypes(void)
   WM_operatortype_append(TEXT_OT_duplicate_line);
 
   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_comment_toggle);
   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 400405155f8..7a1dd312d02 100644
--- a/source/blender/editors/space_text/text_intern.h
+++ b/source/blender/editors/space_text/text_intern.h
@@ -119,9 +119,7 @@ void TEXT_OT_cut(struct wmOperatorType *ot);
 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_comment_toggle(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 95e7d906b11..7c4a403d43d 100644
--- a/source/blender/editors/space_text/text_ops.c
+++ b/source/blender/editors/space_text/text_ops.c
@@ -1209,12 +1209,13 @@ void TEXT_OT_line_break(wmOperatorType *ot)
 /** \} */
 
 /* -------------------------------------------------------------------- */
-/** \name Comment Operator
+/** \name Toggle-Comment Operator
  * \{ */
 
-static int text_comment_exec(bContext *C, wmOperator *UNUSED(op))
+static int text_comment_exec(bContext *C, wmOperator *op)
 {
   Text *text = CTX_data_edit_text(C);
+  int type = RNA_enum_get(op->ptr, "type");
 
   if (txt_has_sel(text)) {
     text_drawcache_tag_update(CTX_wm_space_text(C), 0);
@@ -1222,49 +1223,21 @@ static int text_comment_exec(bContext *C, wmOperator *UNUSED(op))
     ED_text_undo_push_init(C);
 
     txt_order_cursors(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_comment(wmOperatorType *ot)
-{
-  /* identifiers */
-  ot->name = "Comment";
-  ot->idname = "TEXT_OT_comment";
-  ot->description = "Convert selected text to comment";
-
-  /* api callbacks */
-  ot->exec = text_comment_exec;
-  ot->poll = text_edit_poll;
-
-  /* flags */
-  ot->flag = OPTYPE_UNDO;
-}
-
-/** \} */
-
-/* -------------------------------------------------------------------- */
-/** \name Uncomment Operator
- * \{ */
 
-static int text_uncomment_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);
+    switch (type) {
+      case 1:
+        txt_comment(text);
+        break;
+      case -1:
+        txt_uncomment(text);
+        break;
+      default:
+        if (txt_uncomment(text) == false) {
+          txt_comment(text);
+        }
+        break;
+    }
 
-    txt_order_cursors(text, false);
-    txt_uncomment(text);
     text_update_edited(text);
 
     text_update_cursor_moved(C);
@@ -1276,63 +1249,30 @@ static int text_uncomment_exec(bContext *C, wmOperator *UNUSED(op))
   return OPERATOR_CANCELLED;
 }
 
-void TEXT_OT_uncomment(wmOperatorType *ot)
+void TEXT_OT_comment_toggle(wmOperatorType *ot)
 {
-  /* identifiers */
-  ot->name = "Uncomment";
-  ot->idname = "TEXT_OT_uncomment";
-  ot->description = "Convert selected comment to text";
-
-  /* api callbacks */
-  ot->exec = text_uncomment_exec;
-  ot->poll = text_edit_poll;
-
-  /* flags */
-  ot->flag = OPTYPE_UNDO;
-}
-
-/** \} */
-
-/* -------------------------------------------------------------------- */
-/** \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;
-}
+  static const EnumPropertyItem comment_items[] = {
+      {0, "TOGGLE", 0, "Toggle Comments", NULL},
+      {1, "COMMENT", 0, "Comment", NULL},
+      {-1, "UNCOMMENT", 0, "Un-Comment", NULL},
+      {0, NULL, 0, NULL, NULL},
+  };
 
-void TEXT_OT_toggle_comment(wmOperatorType *ot)
-{
   /* identifiers */
-  ot->name = "Toggle Comment";
-  ot->idname = "TEXT_OT_toggle_comment";
+  ot->name = "Toggle Comments";
+  ot->idname = "TEXT_OT_comment_toggle";
 
   /* api callbacks */
-  ot->exec = text_toggle_comment_exec;
+  ot->exec = text_comment_exec;
   ot->poll = text_edit_poll;
 
   /* flags */
   ot->flag = OPTYPE_UNDO;
+
+  /* properties */
+  PropertyRNA *prop;
+  prop = RNA_def_enum(ot->srna, "type", comment_items, 0, "Type", "Add or remove comments");
+  RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
 }
 
 /** \} */



More information about the Bf-blender-cvs mailing list