Text: toggle comment operator

This commit is contained in:
Campbell Barton 2019-08-01 20:31:57 +10:00
parent 3a47fbfac5
commit 433eb3f35d
6 changed files with 59 additions and 9 deletions

View File

@ -288,6 +288,7 @@ class TEXT_MT_format(Menu):
layout.operator("text.comment")
layout.operator("text.uncomment")
layout.operator("text.toggle_comment")
layout.separator()

View File

@ -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);

View File

@ -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)

View File

@ -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);

View File

@ -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);

View File

@ -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
* \{ */