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.
This commit is contained in:
Campbell Barton
2019-08-05 14:10:36 +10:00
parent 91fa07dfb1
commit 94dce826a9

View File

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