Limit undo stack size

The stack size of the undo history of a TextEdit was not limited leading
to potential memory leaks when doing lots of operations on a TextEdit.

This commit adds the option gui/common/text_edit_undo_stack_max_size
to the project settings. The first element of the undo stack is popped
if the stack's size exceeds this value ensuring limited memory usage.

The default stack size setting is 1024.

Fixes #37838.

(cherry picked from commit 4a82390aaf)
This commit is contained in:
John Wigg 2020-04-13 14:30:56 +02:00 committed by Rémi Verschelde
parent e88cde0415
commit ee8b3bef2d
2 changed files with 8 additions and 0 deletions

View File

@ -6212,6 +6212,10 @@ void TextEdit::_push_current_op() {
current_op.type = TextOperation::TYPE_NONE; current_op.type = TextOperation::TYPE_NONE;
current_op.text = ""; current_op.text = "";
current_op.chain_forward = false; current_op.chain_forward = false;
if (undo_stack.size() > undo_stack_max_size) {
undo_stack.pop_front();
}
} }
void TextEdit::set_indent_using_spaces(const bool p_use_spaces) { void TextEdit::set_indent_using_spaces(const bool p_use_spaces) {
@ -7189,6 +7193,8 @@ void TextEdit::_bind_methods() {
GLOBAL_DEF("gui/timers/text_edit_idle_detect_sec", 3); GLOBAL_DEF("gui/timers/text_edit_idle_detect_sec", 3);
ProjectSettings::get_singleton()->set_custom_property_info("gui/timers/text_edit_idle_detect_sec", PropertyInfo(Variant::REAL, "gui/timers/text_edit_idle_detect_sec", PROPERTY_HINT_RANGE, "0,10,0.01,or_greater")); // No negative numbers. ProjectSettings::get_singleton()->set_custom_property_info("gui/timers/text_edit_idle_detect_sec", PropertyInfo(Variant::REAL, "gui/timers/text_edit_idle_detect_sec", PROPERTY_HINT_RANGE, "0,10,0.01,or_greater")); // No negative numbers.
GLOBAL_DEF("gui/common/text_edit_undo_stack_max_size", 1024);
ProjectSettings::get_singleton()->set_custom_property_info("gui/common/text_edit_undo_stack_max_size", PropertyInfo(Variant::INT, "gui/common/text_edit_undo_stack_max_size", PROPERTY_HINT_RANGE, "0,10000,1,or_greater")); // No negative numbers.
} }
TextEdit::TextEdit() { TextEdit::TextEdit() {
@ -7268,6 +7274,7 @@ TextEdit::TextEdit() {
current_op.type = TextOperation::TYPE_NONE; current_op.type = TextOperation::TYPE_NONE;
undo_enabled = true; undo_enabled = true;
undo_stack_max_size = GLOBAL_GET("gui/common/text_edit_undo_stack_max_size");
undo_stack_pos = NULL; undo_stack_pos = NULL;
setting_text = false; setting_text = false;
last_dblclk = 0; last_dblclk = 0;

View File

@ -306,6 +306,7 @@ private:
List<TextOperation> undo_stack; List<TextOperation> undo_stack;
List<TextOperation>::Element *undo_stack_pos; List<TextOperation>::Element *undo_stack_pos;
int undo_stack_max_size;
void _clear_redo(); void _clear_redo();
void _do_text_op(const TextOperation &p_op, bool p_reverse); void _do_text_op(const TextOperation &p_op, bool p_reverse);