Undo: split text undo steps out of the data-block

This moves undo storage into a separate struct which is passed
in from the undo system.
This commit is contained in:
2018-04-05 15:22:33 +02:00
parent 79c3c56894
commit ad1ea9a427
11 changed files with 384 additions and 360 deletions

View File

@@ -40,6 +40,7 @@ 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);
@@ -55,8 +56,8 @@ struct Text *BKE_text_load (struct Main *bmain, const char *file, const char
void BKE_text_copy_data(struct Main *bmain, struct Text *ta_dst, const struct Text *ta_src, 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);
void BKE_text_write (struct Text *text, const char *str);
void BKE_text_clear (struct Text *text, struct TextUndoBuf *utxt);
void BKE_text_write (struct Text *text, struct TextUndoBuf *utxt, const char *str);
int BKE_text_file_modified_check(struct Text *text);
void BKE_text_file_modified_ignore(struct Text *text);
@@ -83,29 +84,29 @@ 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);
void txt_delete_word (struct Text *text);
void txt_delete_selected (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_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);
void txt_insert_buf (struct Text *text, const char *in_buffer);
void txt_undo_add_op (struct Text *text, int op);
void txt_do_undo (struct Text *text);
void txt_do_redo (struct Text *text);
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);
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);
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);
@@ -135,6 +136,11 @@ enum {
TXT_MOVE_LINE_DOWN = 1
};
typedef struct TextUndoBuf {
char *buf;
int pos, len;
} TextUndoBuf;
#ifdef __cplusplus
}
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -3712,15 +3712,11 @@ static void lib_link_text(FileData *fd, Main *main)
static void direct_link_text(FileData *fd, Text *text)
{
TextLine *ln;
text->name = newdataadr(fd, text->name);
text->undo_pos = -1;
text->undo_len = TXT_INIT_UNDO;
text->undo_buf = MEM_mallocN(text->undo_len, "undo buf");
text->compiled = NULL;
#if 0
if (text->flags & TXT_ISEXT) {
BKE_text_reload(text);

View File

@@ -33,10 +33,13 @@
struct SpaceText;
struct ARegion;
struct UndoType;
struct TextUndoBuf;
bool ED_text_region_location_from_cursor(struct SpaceText *st, struct ARegion *ar, const int cursor_co[2], int r_pixel_co[2]);
/* text_undo.c */
void ED_text_undosys_type(struct UndoType *ut);
struct TextUndoBuf *ED_text_undo_push_init(struct bContext *C);
#endif /* __ED_TEXT_H__ */

View File

@@ -611,7 +611,8 @@ 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) {
BKE_text_write(txt, str);
TextUndoBuf *utxt = NULL; // FIXME
BKE_text_write(txt, utxt, str);
MEM_freeN(str);
return OPERATOR_FINISHED;

View File

@@ -241,7 +241,7 @@ static void get_suggest_prefix(Text *text, int offset)
texttool_suggest_prefix(line + i, len);
}
static void confirm_suggestion(Text *text)
static void confirm_suggestion(Text *text, TextUndoBuf *utxt)
{
SuggItem *sel;
int i, over = 0;
@@ -260,7 +260,7 @@ static void confirm_suggestion(Text *text)
// for (i = 0; i < skipleft; i++)
// txt_move_left(text, 0);
BLI_assert(memcmp(sel->name, &line[i], over) == 0);
txt_insert_buf(text, sel->name + over);
txt_insert_buf(text, utxt, sel->name + over);
// for (i = 0; i < skipleft; i++)
// txt_move_right(text, 0);
@@ -284,7 +284,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()) {
confirm_suggestion(st->text);
TextUndoBuf *utxt = NULL; // FIXME
confirm_suggestion(st->text, utxt);
text_update_line_edited(st->text->curl);
text_autocomplete_free(C, op);
return OPERATOR_FINISHED;
@@ -314,6 +315,8 @@ static int text_autocomplete_modal(bContext *C, wmOperator *op, const wmEvent *e
(void)text;
TextUndoBuf *utxt = NULL; // FIXME
if (st->doplugins && texttool_text_is_active(st->text)) {
if (texttool_suggest_first()) tools |= TOOL_SUGG_LIST;
if (texttool_docs_get()) tools |= TOOL_DOCUMENT;
@@ -340,7 +343,7 @@ 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)) {
confirm_suggestion(st->text);
confirm_suggestion(st->text, utxt);
text_update_line_edited(st->text->curl);
swallow = 1;
}
@@ -375,7 +378,7 @@ static int text_autocomplete_modal(bContext *C, wmOperator *op, const wmEvent *e
case PADENTER:
if (event->val == KM_PRESS) {
if (tools & TOOL_SUGG_LIST) {
confirm_suggestion(st->text);
confirm_suggestion(st->text, utxt);
text_update_line_edited(st->text->curl);
swallow = 1;
draw = 1;

View File

@@ -731,7 +731,8 @@ static int text_paste_exec(bContext *C, wmOperator *op)
text_drawcache_tag_update(CTX_wm_space_text(C), 0);
txt_insert_buf(text, buf);
TextUndoBuf *utxt = ED_text_undo_push_init(C);
txt_insert_buf(text, utxt, buf);
text_update_edited(text);
MEM_freeN(buf);
@@ -769,9 +770,11 @@ void TEXT_OT_paste(wmOperatorType *ot)
static int text_duplicate_line_exec(bContext *C, wmOperator *UNUSED(op))
{
Text *text = CTX_data_edit_text(C);
txt_duplicate_line(text);
TextUndoBuf *utxt = ED_text_undo_push_init(C);
txt_duplicate_line(text, utxt);
WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text);
/* run the script while editing, evil but useful */
@@ -844,7 +847,9 @@ static int text_cut_exec(bContext *C, wmOperator *UNUSED(op))
text_drawcache_tag_update(CTX_wm_space_text(C), 0);
txt_copy_clipboard(text);
txt_delete_selected(text);
TextUndoBuf *utxt = ED_text_undo_push_init(C);
txt_delete_selected(text, utxt);
text_update_cursor_moved(C);
WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text);
@@ -879,12 +884,15 @@ 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);
if (txt_has_sel(text)) {
txt_order_cursors(text, false);
txt_indent(text);
txt_indent(text, utxt);
}
else {
txt_add_char(text, utxt, '\t');
}
else
txt_add_char(text, '\t');
text_update_edited(text);
@@ -917,8 +925,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);
txt_order_cursors(text, false);
txt_unindent(text);
txt_unindent(text, utxt);
text_update_edited(text);
@@ -956,14 +966,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);
txt_split_curline(text);
TextUndoBuf *utxt = ED_text_undo_push_init(C);
txt_split_curline(text, utxt);
for (a = 0; a < curts; a++) {
if (text->flags & TXT_TABSTOSPACES) {
txt_add_char(text, ' ');
txt_add_char(text, utxt, ' ');
}
else {
txt_add_char(text, '\t');
txt_add_char(text, utxt, '\t');
}
}
@@ -1003,8 +1014,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);
txt_order_cursors(text, false);
txt_comment(text);
txt_comment(text, utxt);
text_update_edited(text);
text_update_cursor_moved(C);
@@ -1039,8 +1052,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);
txt_order_cursors(text, false);
txt_uncomment(text);
txt_uncomment(text, utxt);
text_update_edited(text);
text_update_cursor_moved(C);
@@ -1292,8 +1307,10 @@ 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");
txt_move_lines(text, direction);
TextUndoBuf *utxt = ED_text_undo_push_init(C);
txt_move_lines(text, utxt, direction);
text_update_cursor_moved(C);
WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text);
@@ -1966,6 +1983,7 @@ static int text_delete_exec(bContext *C, wmOperator *op)
Text *text = CTX_data_edit_text(C);
int type = RNA_enum_get(op->ptr, "type");
text_drawcache_tag_update(st, 0);
/* behavior could be changed here,
@@ -1975,11 +1993,13 @@ static int text_delete_exec(bContext *C, wmOperator *op)
else if (type == DEL_NEXT_WORD) type = DEL_NEXT_CHAR;
}
TextUndoBuf *utxt = ED_text_undo_push_init(C);
if (type == DEL_PREV_WORD) {
if (txt_cursor_is_line_start(text)) {
txt_backspace_char(text);
txt_backspace_char(text, utxt);
}
txt_backspace_word(text);
txt_backspace_word(text, utxt);
}
else if (type == DEL_PREV_CHAR) {
@@ -1995,13 +2015,13 @@ static int text_delete_exec(bContext *C, wmOperator *op)
}
}
txt_backspace_char(text);
txt_backspace_char(text, utxt);
}
else if (type == DEL_NEXT_WORD) {
if (txt_cursor_is_line_end(text)) {
txt_delete_char(text);
txt_delete_char(text, utxt);
}
txt_delete_word(text);
txt_delete_word(text, utxt);
}
else if (type == DEL_NEXT_CHAR) {
@@ -2017,7 +2037,7 @@ static int text_delete_exec(bContext *C, wmOperator *op)
}
}
txt_delete_char(text);
txt_delete_char(text, utxt);
}
text_update_line_edited(text->curl);
@@ -2870,16 +2890,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);
if (st && st->overwrite) {
while (str[i]) {
code = BLI_str_utf8_as_unicode_step(str, &i);
done |= txt_replace_char(text, code);
done |= txt_replace_char(text, utxt, code);
}
}
else {
while (str[i]) {
code = BLI_str_utf8_as_unicode_step(str, &i);
done |= txt_add_char(text, code);
done |= txt_add_char(text, utxt, code);
}
}
@@ -2988,7 +3010,8 @@ static int text_find_and_replace(bContext *C, wmOperator *op, short mode)
if (found) {
if (mode == TEXT_REPLACE) {
txt_insert_buf(text, st->replacestr);
TextUndoBuf *utxt = ED_text_undo_push_init(C);
txt_insert_buf(text, utxt, st->replacestr);
if (text->curl && text->curl->format) {
MEM_freeN(text->curl->format);
text->curl->format = NULL;

View File

@@ -48,6 +48,7 @@
#include "ED_text.h"
#include "ED_curve.h"
#include "ED_screen.h"
#include "ED_undo.h"
#include "UI_interface.h"
#include "UI_resources.h"
@@ -63,11 +64,6 @@
/* -------------------------------------------------------------------- */
/** \name Implements ED Undo System
* \{ */
typedef struct TextUndoBuf {
char *buf;
int len;
int pos;
} TextUndoBuf;
typedef struct TextUndoStep {
UndoStep step;
@@ -87,29 +83,33 @@ static bool text_undosys_poll(bContext *C)
return true;
}
static bool text_undosys_step_encode(struct bContext *C, UndoStep *us_p)
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);
/* 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, UndoStep *us_p)
{
TextUndoStep *us = (TextUndoStep *)us_p;
Text *text = CTX_data_edit_text(C);
/* No undo data was generated. Hint, use global undo here. */
if (text->undo_pos == -1) {
if ((us->data.pos == -1) || (us->data.buf == NULL)) {
return false;
}
us->text_ref.ptr = text;
us->data.buf = text->undo_buf;
us->data.pos = text->undo_pos;
us->data.len = text->undo_len;
text->undo_buf = NULL;
text->undo_len = 0;
text->undo_pos = -1;
us->step.data_size = text->undo_len;
us->step.data_size = us->data.len;
return true;
}
@@ -119,20 +119,15 @@ static void text_undosys_step_decode(struct bContext *C, UndoStep *us_p, int dir
TextUndoStep *us = (TextUndoStep *)us_p;
Text *text = us->text_ref.ptr;
/* TODO(campbell): undo_system: move undo out of Text data block. */
text->undo_buf = us->data.buf;
text->undo_len = us->data.len;
if (dir < 0) {
text->undo_pos = us->data.pos;
txt_do_undo(text);
TextUndoBuf data = us->data;
txt_do_undo(text, &data);
}
else {
text->undo_pos = -1;
txt_do_redo(text);
TextUndoBuf data = us->data;
data.pos = -1;
txt_do_redo(text, &data);
}
text->undo_buf = NULL;
text->undo_len = 0;
text->undo_pos = -1;
text_update_edited(text);
text_update_cursor_moved(C);
@@ -159,6 +154,7 @@ void ED_text_undosys_type(UndoType *ut)
{
ut->name = "Text";
ut->poll = text_undosys_poll;
ut->step_encode_init = text_undosys_step_encode_init;
ut->step_encode = text_undosys_step_encode;
ut->step_decode = text_undosys_step_decode;
ut->step_free = text_undosys_step_free;
@@ -166,9 +162,24 @@ void ED_text_undosys_type(UndoType *ut)
ut->step_foreach_ID_ref = text_undosys_foreach_ID_ref;
ut->mode = BKE_UNDOTYPE_MODE_ACCUMULATE;
ut->use_context = true;
ut->use_context = false;
ut->step_size = sizeof(TextUndoStep);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Utilities
* \{ */
/* Use operator system to finish the undo step. */
TextUndoBuf *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;
}
/** \} */

View File

@@ -62,7 +62,7 @@ void ED_undosys_type_init(void)
BKE_UNDOSYS_TYPE_PAINTCURVE = BKE_undosys_type_append(ED_paintcurve_undosys_type);
/* Text editor */
BKE_undosys_type_append(ED_text_undosys_type);
BKE_UNDOSYS_TYPE_TEXT = BKE_undosys_type_append(ED_text_undosys_type);
/* Keep global undo last (as a fallback). */
BKE_UNDOSYS_TYPE_MEMFILE = BKE_undosys_type_append(ED_memfile_undosys_type);

View File

@@ -59,10 +59,6 @@ typedef struct Text {
TextLine *curl, *sell;
int curc, selc;
char *undo_buf;
void *pad;
int undo_pos, undo_len;
double mtime;
} Text;

View File

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