Undo System: replace with simpler binary diffing buffer storage

Applying/undoing incremental changes didn't fit well when
mixed with periodic snapshots from mem-file undo.

This moves to a much simpler undo system.

- Uses array storage with de-duplication from `BLI_array_store`.
- Loads the buffer into existing text data,
  for better performance on large files.
- Has the advantage that Python operators can be supported
  since we don't depend on hard coded undo operations.

Solves T67045, T66695, T65909.
This commit is contained in:
Campbell Barton 2019-07-11 15:25:52 +10:00
parent 9e9fbb39d7
commit 366865dd02
Notes: blender-bot 2023-02-14 19:14:21 +01:00
Referenced by issue #67045, Crash undo/redo in the text editor
Referenced by issue #66695, Text editor becomes out of sync in object mode
Referenced by issue blender/blender-addons#65909, Python API calling: bpy.ops.text.cut +bpy.ops.text.paste will fail when undo.
9 changed files with 299 additions and 1429 deletions

View File

@ -30,7 +30,6 @@ extern "C" {
struct Main;
struct Text;
struct TextLine;
struct TextUndoBuf;
void BKE_text_free_lines(struct Text *text);
void BKE_text_free(struct Text *text);
@ -49,8 +48,8 @@ void BKE_text_copy_data(struct Main *bmain,
const int flag);
struct Text *BKE_text_copy(struct Main *bmain, const struct Text *ta);
void BKE_text_make_local(struct Main *bmain, struct Text *text, const bool lib_local);
void BKE_text_clear(struct Text *text, struct TextUndoBuf *utxt);
void BKE_text_write(struct Text *text, struct TextUndoBuf *utxt, const char *str);
void BKE_text_clear(struct Text *text);
void BKE_text_write(struct Text *text, const char *str);
int BKE_text_file_modified_check(struct Text *text);
void BKE_text_file_modified_ignore(struct Text *text);
@ -77,29 +76,26 @@ void txt_move_eol(struct Text *text, const bool sel);
void txt_move_toline(struct Text *text, unsigned int line, const bool sel);
void txt_move_to(struct Text *text, unsigned int line, unsigned int ch, const bool sel);
void txt_pop_sel(struct Text *text);
void txt_delete_char(struct Text *text, struct TextUndoBuf *utxt);
void txt_delete_word(struct Text *text, struct TextUndoBuf *utxt);
void txt_delete_selected(struct Text *text, struct TextUndoBuf *utxt);
void txt_delete_char(struct Text *text);
void txt_delete_word(struct Text *text);
void txt_delete_selected(struct Text *text);
void txt_sel_all(struct Text *text);
void txt_sel_clear(struct Text *text);
void txt_sel_line(struct Text *text);
char *txt_sel_to_buf(struct Text *text, int *r_buf_strlen);
void txt_insert_buf(struct Text *text, struct TextUndoBuf *utxt, const char *in_buffer);
void txt_undo_add_op(struct Text *text, struct TextUndoBuf *utxt, int op);
void txt_do_undo(struct Text *text, struct TextUndoBuf *utxt);
void txt_do_redo(struct Text *text, struct TextUndoBuf *utxt);
void txt_split_curline(struct Text *text, struct TextUndoBuf *utxt);
void txt_backspace_char(struct Text *text, struct TextUndoBuf *utxt);
void txt_backspace_word(struct Text *text, struct TextUndoBuf *utxt);
bool txt_add_char(struct Text *text, struct TextUndoBuf *utxt, unsigned int add);
bool txt_add_raw_char(struct Text *text, struct TextUndoBuf *utxt, unsigned int add);
bool txt_replace_char(struct Text *text, struct TextUndoBuf *utxt, unsigned int add);
void txt_unindent(struct Text *text, struct TextUndoBuf *utxt);
void txt_comment(struct Text *text, struct TextUndoBuf *utxt);
void txt_indent(struct Text *text, struct TextUndoBuf *utxt);
void txt_uncomment(struct Text *text, struct TextUndoBuf *utxt);
void txt_move_lines(struct Text *text, struct TextUndoBuf *utxt, const int direction);
void txt_duplicate_line(struct Text *text, struct TextUndoBuf *utxt);
void txt_insert_buf(struct Text *text, const char *in_buffer);
void txt_split_curline(struct Text *text);
void txt_backspace_char(struct Text *text);
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);
void txt_comment(struct Text *text);
void txt_indent(struct Text *text);
void 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);
bool txt_cursor_is_line_start(struct Text *text);
bool txt_cursor_is_line_end(struct Text *text);
@ -125,10 +121,9 @@ enum {
TXT_MOVE_LINE_DOWN = 1,
};
typedef struct TextUndoBuf {
char *buf;
int pos, len;
} TextUndoBuf;
/* Fast non-validating buffer conversion for undo. */
char *txt_to_buf_for_undo(struct Text *text, int *r_buf_strlen);
void txt_from_buf_for_undo(struct Text *text, const char *buf, int buf_len);
#ifdef __cplusplus
}

File diff suppressed because it is too large Load Diff

View File

@ -26,7 +26,7 @@
struct ARegion;
struct SpaceText;
struct TextUndoBuf;
struct UndoStep;
struct UndoType;
bool ED_text_region_location_from_cursor(struct SpaceText *st,
@ -37,6 +37,6 @@ bool ED_text_region_location_from_cursor(struct SpaceText *st,
/* text_undo.c */
void ED_text_undosys_type(struct UndoType *ut);
struct TextUndoBuf *ED_text_undo_push_init(struct bContext *C);
struct UndoStep *ED_text_undo_push_init(struct bContext *C);
#endif /* __ED_TEXT_H__ */

View File

@ -1052,8 +1052,7 @@ static int reports_to_text_exec(bContext *C, wmOperator *UNUSED(op))
str = BKE_reports_string(reports, (G.debug & G_DEBUG) ? RPT_DEBUG : RPT_INFO);
if (str) {
TextUndoBuf *utxt = NULL; // FIXME
BKE_text_write(txt, utxt, str);
BKE_text_write(txt, str);
MEM_freeN(str);
return OPERATOR_FINISHED;

View File

@ -260,7 +260,7 @@ static void get_suggest_prefix(Text *text, int offset)
texttool_suggest_prefix(line + i, len);
}
static void confirm_suggestion(Text *text, TextUndoBuf *utxt)
static void confirm_suggestion(Text *text)
{
SuggItem *sel;
int i, over = 0;
@ -285,7 +285,7 @@ static void confirm_suggestion(Text *text, TextUndoBuf *utxt)
// for (i = 0; i < skipleft; i++)
// txt_move_left(text, 0);
BLI_assert(memcmp(sel->name, &line[i], over) == 0);
txt_insert_buf(text, utxt, sel->name + over);
txt_insert_buf(text, sel->name + over);
// for (i = 0; i < skipleft; i++)
// txt_move_right(text, 0);
@ -308,8 +308,8 @@ static int text_autocomplete_invoke(bContext *C, wmOperator *op, const wmEvent *
ED_area_tag_redraw(CTX_wm_area(C));
if (texttool_suggest_first() == texttool_suggest_last()) {
TextUndoBuf *utxt = ED_text_undo_push_init(C);
confirm_suggestion(st->text, utxt);
ED_text_undo_push_init(C);
confirm_suggestion(st->text);
text_update_line_edited(st->text->curl);
text_autocomplete_free(C, op);
ED_undo_push(C, op->type->name);
@ -371,8 +371,8 @@ static int text_autocomplete_modal(bContext *C, wmOperator *op, const wmEvent *e
case MIDDLEMOUSE:
if (event->val == KM_PRESS) {
if (text_do_suggest_select(st, ar)) {
TextUndoBuf *utxt = ED_text_undo_push_init(C);
confirm_suggestion(st->text, utxt);
ED_text_undo_push_init(C);
confirm_suggestion(st->text);
text_update_line_edited(st->text->curl);
ED_undo_push(C, op->type->name);
swallow = 1;
@ -410,8 +410,8 @@ static int text_autocomplete_modal(bContext *C, wmOperator *op, const wmEvent *e
case PADENTER:
if (event->val == KM_PRESS) {
if (tools & TOOL_SUGG_LIST) {
TextUndoBuf *utxt = ED_text_undo_push_init(C);
confirm_suggestion(st->text, utxt);
ED_text_undo_push_init(C);
confirm_suggestion(st->text);
text_update_line_edited(st->text->curl);
ED_undo_push(C, op->type->name);
swallow = 1;

View File

@ -840,7 +840,7 @@ static int text_paste_exec(bContext *C, wmOperator *op)
text_drawcache_tag_update(CTX_wm_space_text(C), 0);
TextUndoBuf *utxt = ED_text_undo_push_init(C);
ED_text_undo_push_init(C);
/* Convert clipboard content indentation to spaces if specified */
if (text->flags & TXT_TABSTOSPACES) {
@ -849,7 +849,7 @@ static int text_paste_exec(bContext *C, wmOperator *op)
buf = new_buf;
}
txt_insert_buf(text, utxt, buf);
txt_insert_buf(text, buf);
text_update_edited(text);
MEM_freeN(buf);
@ -893,9 +893,9 @@ static int text_duplicate_line_exec(bContext *C, wmOperator *UNUSED(op))
{
Text *text = CTX_data_edit_text(C);
TextUndoBuf *utxt = ED_text_undo_push_init(C);
ED_text_undo_push_init(C);
txt_duplicate_line(text, utxt);
txt_duplicate_line(text);
WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text);
@ -971,8 +971,8 @@ static int text_cut_exec(bContext *C, wmOperator *UNUSED(op))
txt_copy_clipboard(text);
TextUndoBuf *utxt = ED_text_undo_push_init(C);
txt_delete_selected(text, utxt);
ED_text_undo_push_init(C);
txt_delete_selected(text);
text_update_cursor_moved(C);
WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text);
@ -1008,14 +1008,14 @@ static int text_indent_exec(bContext *C, wmOperator *UNUSED(op))
text_drawcache_tag_update(CTX_wm_space_text(C), 0);
TextUndoBuf *utxt = ED_text_undo_push_init(C);
ED_text_undo_push_init(C);
if (txt_has_sel(text)) {
txt_order_cursors(text, false);
txt_indent(text, utxt);
txt_indent(text);
}
else {
txt_add_char(text, utxt, '\t');
txt_add_char(text, '\t');
}
text_update_edited(text);
@ -1049,10 +1049,10 @@ static int text_unindent_exec(bContext *C, wmOperator *UNUSED(op))
text_drawcache_tag_update(CTX_wm_space_text(C), 0);
TextUndoBuf *utxt = ED_text_undo_push_init(C);
ED_text_undo_push_init(C);
txt_order_cursors(text, false);
txt_unindent(text, utxt);
txt_unindent(text);
text_update_edited(text);
@ -1090,15 +1090,15 @@ static int text_line_break_exec(bContext *C, wmOperator *UNUSED(op))
// double check tabs/spaces before splitting the line
curts = txt_setcurr_tab_spaces(text, space);
TextUndoBuf *utxt = ED_text_undo_push_init(C);
txt_split_curline(text, utxt);
ED_text_undo_push_init(C);
txt_split_curline(text);
for (a = 0; a < curts; a++) {
if (text->flags & TXT_TABSTOSPACES) {
txt_add_char(text, utxt, ' ');
txt_add_char(text, ' ');
}
else {
txt_add_char(text, utxt, '\t');
txt_add_char(text, '\t');
}
}
@ -1139,10 +1139,10 @@ static int text_comment_exec(bContext *C, wmOperator *UNUSED(op))
if (txt_has_sel(text)) {
text_drawcache_tag_update(CTX_wm_space_text(C), 0);
TextUndoBuf *utxt = ED_text_undo_push_init(C);
ED_text_undo_push_init(C);
txt_order_cursors(text, false);
txt_comment(text, utxt);
txt_comment(text);
text_update_edited(text);
text_update_cursor_moved(C);
@ -1177,10 +1177,10 @@ static int text_uncomment_exec(bContext *C, wmOperator *UNUSED(op))
if (txt_has_sel(text)) {
text_drawcache_tag_update(CTX_wm_space_text(C), 0);
TextUndoBuf *utxt = ED_text_undo_push_init(C);
ED_text_undo_push_init(C);
txt_order_cursors(text, false);
txt_uncomment(text, utxt);
txt_uncomment(text);
text_update_edited(text);
text_update_cursor_moved(C);
@ -1446,9 +1446,9 @@ static int move_lines_exec(bContext *C, wmOperator *op)
Text *text = CTX_data_edit_text(C);
const int direction = RNA_enum_get(op->ptr, "direction");
TextUndoBuf *utxt = ED_text_undo_push_init(C);
ED_text_undo_push_init(C);
txt_move_lines(text, utxt, direction);
txt_move_lines(text, direction);
text_update_cursor_moved(C);
WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text);
@ -2230,13 +2230,13 @@ static int text_delete_exec(bContext *C, wmOperator *op)
}
}
TextUndoBuf *utxt = ED_text_undo_push_init(C);
ED_text_undo_push_init(C);
if (type == DEL_PREV_WORD) {
if (txt_cursor_is_line_start(text)) {
txt_backspace_char(text, utxt);
txt_backspace_char(text);
}
txt_backspace_word(text, utxt);
txt_backspace_word(text);
}
else if (type == DEL_PREV_CHAR) {
@ -2252,13 +2252,13 @@ static int text_delete_exec(bContext *C, wmOperator *op)
}
}
txt_backspace_char(text, utxt);
txt_backspace_char(text);
}
else if (type == DEL_NEXT_WORD) {
if (txt_cursor_is_line_end(text)) {
txt_delete_char(text, utxt);
txt_delete_char(text);
}
txt_delete_word(text, utxt);
txt_delete_word(text);
}
else if (type == DEL_NEXT_CHAR) {
@ -2274,7 +2274,7 @@ static int text_delete_exec(bContext *C, wmOperator *op)
}
}
txt_delete_char(text, utxt);
txt_delete_char(text);
}
text_update_line_edited(text->curl);
@ -3190,18 +3190,18 @@ static int text_insert_exec(bContext *C, wmOperator *op)
str = RNA_string_get_alloc(op->ptr, "text", NULL, 0);
TextUndoBuf *utxt = ED_text_undo_push_init(C);
ED_text_undo_push_init(C);
if (st && st->overwrite) {
while (str[i]) {
code = BLI_str_utf8_as_unicode_step(str, &i);
done |= txt_replace_char(text, utxt, code);
done |= txt_replace_char(text, code);
}
}
else {
while (str[i]) {
code = BLI_str_utf8_as_unicode_step(str, &i);
done |= txt_add_char(text, utxt, code);
done |= txt_add_char(text, code);
}
}
@ -3319,8 +3319,8 @@ static int text_find_and_replace(bContext *C, wmOperator *op, short mode)
if (found) {
if (mode == TEXT_REPLACE) {
TextUndoBuf *utxt = ED_text_undo_push_init(C);
txt_insert_buf(text, utxt, st->replacestr);
ED_text_undo_push_init(C);
txt_insert_buf(text, st->replacestr);
if (text->curl && text->curl->format) {
MEM_freeN(text->curl->format);
text->curl->format = NULL;

View File

@ -25,6 +25,7 @@
#include "DNA_text_types.h"
#include "BLI_array_store.h"
#include "BLI_array_utils.h"
#include "BLT_translation.h"
@ -35,6 +36,7 @@
#include "BKE_report.h"
#include "BKE_text.h"
#include "BKE_undo_system.h"
#include "BKE_main.h"
#include "WM_api.h"
#include "WM_types.h"
@ -53,18 +55,32 @@
#include "text_intern.h"
#include "text_format.h"
/* TODO(campbell): undo_system: move text undo out of text block. */
/* -------------------------------------------------------------------- */
/** \name Implements ED Undo System
* \{ */
#define ARRAY_CHUNK_SIZE 128
typedef struct TextUndoStep {
UndoStep step;
UndoRefID_Text text_ref;
TextUndoBuf data;
struct {
BArrayState *state;
int buf_len;
} data;
struct {
int line, line_select;
int column, column_select;
} cursor;
} TextUndoStep;
static struct {
BArrayStore *buffer_store;
int users;
} g_text_buffers = {NULL};
static bool text_undosys_poll(bContext *UNUSED(C))
{
/* Only use when operators initialized. */
@ -77,12 +93,8 @@ static void text_undosys_step_encode_init(struct bContext *C, UndoStep *us_p)
TextUndoStep *us = (TextUndoStep *)us_p;
BLI_assert(BLI_array_is_zeroed(&us->data, 1));
UNUSED_VARS(C);
UNUSED_VARS(C, us);
/* XXX, use to set the undo type only. */
us->data.buf = NULL;
us->data.len = 0;
us->data.pos = -1;
}
static bool text_undosys_step_encode(struct bContext *C,
@ -93,104 +105,66 @@ static bool text_undosys_step_encode(struct bContext *C,
Text *text = CTX_data_edit_text(C);
/* No undo data was generated. Hint, use global undo here. */
if ((us->data.pos == -1) || (us->data.buf == NULL)) {
return false;
int buf_len = 0;
uchar *buf = (uchar *)txt_to_buf_for_undo(text, &buf_len);
if (g_text_buffers.buffer_store == NULL) {
g_text_buffers.buffer_store = BLI_array_store_create(1, ARRAY_CHUNK_SIZE);
}
g_text_buffers.users += 1;
const size_t total_size_prev = BLI_array_store_calc_size_compacted_get(
g_text_buffers.buffer_store);
us->data.state = BLI_array_store_state_add(g_text_buffers.buffer_store, buf, buf_len, NULL);
MEM_freeN(buf);
us->cursor.line = txt_get_span(text->lines.first, text->curl);
us->cursor.column = text->curc;
if (txt_has_sel(text)) {
us->cursor.line_select = (text->curl == text->sell) ?
us->cursor.line :
txt_get_span(text->lines.first, text->sell);
us->cursor.column_select = text->selc;
}
else {
us->cursor.line_select = us->cursor.line;
us->cursor.column_select = us->cursor.column;
}
us_p->is_applied = true;
us->text_ref.ptr = text;
us->step.data_size = us->data.len;
us->step.data_size = BLI_array_store_calc_size_compacted_get(g_text_buffers.buffer_store) -
total_size_prev;
return true;
}
static void text_undosys_step_decode_undo_impl(Text *text, TextUndoStep *us)
{
BLI_assert(us->step.is_applied == true);
TextUndoBuf data = us->data;
while (data.pos > -1) {
txt_do_undo(text, &data);
}
BLI_assert(data.pos == -1);
us->step.is_applied = false;
}
static void text_undosys_step_decode_redo_impl(Text *text, TextUndoStep *us)
{
BLI_assert(us->step.is_applied == false);
TextUndoBuf data = us->data;
data.pos = -1;
while (data.pos < us->data.pos) {
txt_do_redo(text, &data);
}
BLI_assert(data.pos == us->data.pos);
us->step.is_applied = true;
}
static void text_undosys_step_decode_undo(TextUndoStep *us, bool is_final)
{
TextUndoStep *us_iter = us;
while (us_iter->step.next && (us_iter->step.next->type == us_iter->step.type)) {
if (us_iter->step.next->is_applied == false) {
break;
}
us_iter = (TextUndoStep *)us_iter->step.next;
}
Text *text_prev = NULL;
while ((us_iter != us) || (is_final && us_iter == us)) {
Text *text = us_iter->text_ref.ptr;
text_undosys_step_decode_undo_impl(text, us_iter);
if (text_prev != text) {
text_update_edited(text);
text_prev = text;
}
if (is_final) {
break;
}
us_iter = (TextUndoStep *)us_iter->step.prev;
}
}
static void text_undosys_step_decode_redo(TextUndoStep *us)
{
TextUndoStep *us_iter = us;
while (us_iter->step.prev && (us_iter->step.prev->type == us_iter->step.type)) {
if (us_iter->step.prev->is_applied == true) {
break;
}
us_iter = (TextUndoStep *)us_iter->step.prev;
}
Text *text_prev = NULL;
while (us_iter && (us_iter->step.is_applied == false)) {
Text *text = us_iter->text_ref.ptr;
text_undosys_step_decode_redo_impl(text, us_iter);
if (text_prev != text) {
text_update_edited(text);
text_prev = text;
}
if (us_iter == us) {
break;
}
us_iter = (TextUndoStep *)us_iter->step.next;
}
}
static void text_undosys_step_decode(
struct bContext *C, struct Main *UNUSED(bmain), UndoStep *us_p, int dir, bool is_final)
static void text_undosys_step_decode(struct bContext *C,
struct Main *UNUSED(bmain),
UndoStep *us_p,
int UNUSED(dir),
bool UNUSED(is_final))
{
TextUndoStep *us = (TextUndoStep *)us_p;
if (dir < 0) {
text_undosys_step_decode_undo(us, is_final);
}
else {
text_undosys_step_decode_redo(us);
}
Text *text = us->text_ref.ptr;
size_t buf_len;
{
const uchar *buf = BLI_array_store_state_data_get_alloc(us->data.state, &buf_len);
txt_from_buf_for_undo(text, (const char *)buf, buf_len);
MEM_freeN((void *)buf);
}
const bool has_select = ((us->cursor.line != us->cursor.line_select) ||
(us->cursor.column != us->cursor.column_select));
if (has_select) {
txt_move_to(text, us->cursor.line_select, us->cursor.column_select, false);
}
txt_move_to(text, us->cursor.line, us->cursor.column, has_select);
SpaceText *st = CTX_wm_space_text(C);
if (st) {
/* Not essential, always show text being undo where possible. */
@ -204,7 +178,14 @@ static void text_undosys_step_decode(
static void text_undosys_step_free(UndoStep *us_p)
{
TextUndoStep *us = (TextUndoStep *)us_p;
MEM_SAFE_FREE(us->data.buf);
BLI_array_store_state_remove(g_text_buffers.buffer_store, us->data.state);
g_text_buffers.users -= 1;
if (g_text_buffers.users == 0) {
BLI_array_store_destroy(g_text_buffers.buffer_store);
g_text_buffers.buffer_store = NULL;
}
}
static void text_undosys_foreach_ID_ref(UndoStep *us_p,
@ -240,12 +221,16 @@ void ED_text_undosys_type(UndoType *ut)
* \{ */
/* Use operator system to finish the undo step. */
TextUndoBuf *ED_text_undo_push_init(bContext *C)
UndoStep *ED_text_undo_push_init(bContext *C)
{
UndoStack *ustack = ED_undo_stack_get();
UndoStep *us_p = BKE_undosys_step_push_init_with_type(ustack, C, NULL, BKE_UNDOSYS_TYPE_TEXT);
TextUndoStep *us = (TextUndoStep *)us_p;
return &us->data;
Main *bmain = CTX_data_main(C);
wmWindowManager *wm = bmain->wm.first;
if (wm->op_undo_depth <= 1) {
UndoStep *us_p = BKE_undosys_step_push_init_with_type(ustack, C, NULL, BKE_UNDOSYS_TYPE_TEXT);
return us_p;
}
return NULL;
}
/** \} */

View File

@ -55,8 +55,6 @@ typedef struct Text {
} Text;
#define TXT_TABSIZE 4
#define TXT_INIT_UNDO 1024
#define TXT_MAX_UNDO (TXT_INIT_UNDO * TXT_INIT_UNDO)
/* text flags */
enum {

View File

@ -34,13 +34,13 @@
static void rna_Text_clear(Text *text)
{
BKE_text_clear(text, NULL);
BKE_text_clear(text);
WM_main_add_notifier(NC_TEXT | NA_EDITED, text);
}
static void rna_Text_write(Text *text, const char *str)
{
BKE_text_write(text, NULL, str);
BKE_text_write(text, str);
WM_main_add_notifier(NC_TEXT | NA_EDITED, text);
}