From 94dce826a9a7b77dd99fa7cd8d3b7147913ba591 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 5 Aug 2019 14:10:36 +1000 Subject: 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. --- source/blender/blenkernel/intern/text.c | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) (limited to 'source/blender/blenkernel') 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) -- cgit v1.2.3