Skip to content

Commit 6531739

Browse files
authored
feat: Make undo work when importing pattern (#2160)
A user complained that they imported a file by accident when they meant to export it and as a result had trouble recovering the changes they were trying to save. Auto-save saved the day but there is no reason for not being able to undo changes after importing a pattern. In fact, the previous implementation treated importing a pattern as a reset on the editor instance which actually erased all previous undo entries. Importing now is treated as a normal editing operation where the entire file is replaced with the imported pattern. Since all imports use AddText it was easy to add an undo entry to that function while removing the part where the previous undo records were being deleted. Care is taken to add the preprocessed version of the imported file to the undo buffer so that unwanted chars don't sneak in. A bug was found in the handling of a tab char as well but hopefully it wont need to be used anymore.
1 parent 7d09cc6 commit 6531739

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

lib/third_party/imgui/ColorTextEditor/source/TextEditor.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,6 @@ int TextEditor::InsertTextAt(Coordinates & /* inout */ aWhere, const char *aValu
263263
while (i-- > 0)
264264
line.insert(line.begin() + cindex++, Glyph(' ', PaletteIndex::Default));
265265

266-
cindex += d;
267266
aWhere.mColumn += d;
268267
aValue++;
269268
} else if (*aValue == '\n') {
@@ -1288,24 +1287,28 @@ void TextEditor::Render(const char *aTitle, const ImVec2 &aSize, bool aBorder) {
12881287
}
12891288

12901289
void TextEditor::SetText(const std::string &aText) {
1290+
UndoRecord u;
1291+
u.mBefore = mState;
1292+
u.mRemoved = GetText();
1293+
u.mRemovedStart = Coordinates(0, 0);
1294+
u.mRemovedEnd = Coordinates((int)mLines.size()-1, GetLineMaxColumn((int)mLines.size()-1));
12911295
mLines.resize(1);
12921296
mLines[0].clear();
12931297
std::string text = PreprocessText(aText);
12941298
for (auto chr : text) {
1295-
if (chr == '\r') {
1296-
// ignore the carriage return character
1297-
} else if (chr == '\n')
1299+
if (chr == '\n')
12981300
mLines.push_back(Line());
1299-
else {
1301+
else
13001302
mLines.back().push_back(Glyph(chr, PaletteIndex::Default));
1301-
}
13021303
}
1303-
1304+
u.mAdded = text;
1305+
u.mAddedStart = Coordinates(0, 0);
1306+
u.mAddedEnd = Coordinates((int)mLines.size()-1, GetLineMaxColumn((int)mLines.size()-1));
13041307
mTextChanged = true;
13051308
mScrollToTop = true;
1306-
1307-
mUndoBuffer.clear();
1308-
mUndoIndex = 0;
1309+
u.mAfter = mState;
1310+
if (!mReadOnly)
1311+
AddUndo(u);
13091312

13101313
Colorize();
13111314
}

0 commit comments

Comments
 (0)