From 91fa07dfb18f35783177db47d4c721e5a03aad43 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 5 Aug 2019 13:54:27 +1000 Subject: Text: merge toggle comments into a single operator This allows users to map comment/un-comment to be mapped to keys. --- source/blender/editors/space_text/text_ops.c | 122 +++++++-------------------- 1 file changed, 31 insertions(+), 91 deletions(-) (limited to 'source/blender/editors/space_text/text_ops.c') 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); } /** \} */ -- cgit v1.2.3