Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions TextEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ TextEditor::TextEditor()
, mIgnoreImGuiChild(false)
, mShowWhitespaces(true)
, mStartTime(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count())
, mExternalUndoBuffer(nullptr)
{
SetPalette(GetDarkPalette());
SetLanguageDefinition(LanguageDefinition::HLSL());
Expand Down Expand Up @@ -319,6 +320,16 @@ void TextEditor::AddUndo(UndoRecord& aValue)
mUndoBuffer.resize((size_t)(mUndoIndex + 1));
mUndoBuffer.back() = aValue;
++mUndoIndex;

// If a custom undo buffer is set we send the undo value on it
if ( mExternalUndoBuffer != nullptr) {
mExternalUndoBuffer->AddUndo(aValue);
}
}

void TextEditor::SetExternalUndoBuffer(ExternalUndoBufferInterface* aExternalUndoBuffer)
{
this->mExternalUndoBuffer = aExternalUndoBuffer;
}

TextEditor::Coordinates TextEditor::ScreenPosToCoordinates(const ImVec2& aPosition) const
Expand Down Expand Up @@ -1471,6 +1482,22 @@ void TextEditor::InsertText(const char * aValue)
if (aValue == nullptr)
return;

// Prepare the undo record
UndoRecord u;
u.mBefore = mState;

if (HasSelection())
{
u.mRemoved = GetSelectedText();
u.mRemovedStart = mState.mSelectionStart;
u.mRemovedEnd = mState.mSelectionEnd;
DeleteSelection();
}

u.mAdded = aValue;
u.mAddedStart = GetActualCursorCoordinates();

// Insert text
auto pos = GetActualCursorCoordinates();
auto start = std::min(pos, mState.mSelectionStart);
int totalLines = pos.mLine - start.mLine;
Expand All @@ -1480,6 +1507,11 @@ void TextEditor::InsertText(const char * aValue)
SetSelection(pos, pos);
SetCursorPosition(pos);
Colorize(start.mLine - 1, totalLines + 2);

// Finish the undorecord and add it to the buffer
u.mAddedEnd = GetActualCursorCoordinates();
u.mAfter = mState;
AddUndo(u);
}

void TextEditor::DeleteSelection()
Expand Down Expand Up @@ -2479,6 +2511,18 @@ TextEditor::UndoRecord::UndoRecord(
assert(mRemovedStart <= mRemovedEnd);
}

TextEditor::UndoRecord::UndoRecord(UndoRecord& undoRecord)
: mAdded(undoRecord.mAdded)
, mAddedStart(undoRecord.mAddedStart)
, mAddedEnd(undoRecord.mAddedEnd)
, mRemoved(undoRecord.mRemoved)
, mRemovedStart(undoRecord.mRemovedStart)
, mRemovedEnd(undoRecord.mRemovedEnd)
, mBefore(undoRecord.mBefore)
, mAfter(undoRecord.mAfter) {

}

void TextEditor::UndoRecord::Undo(TextEditor * aEditor)
{
if (!mAdded.empty())
Expand Down
94 changes: 52 additions & 42 deletions TextEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,56 @@ class TextEditor
static const LanguageDefinition& Lua();
};

struct EditorState
{
Coordinates mSelectionStart;
Coordinates mSelectionEnd;
Coordinates mCursorPosition;
};

class UndoRecord
{
public:
UndoRecord() {}
~UndoRecord() {}

UndoRecord(
const std::string& aAdded,
const TextEditor::Coordinates aAddedStart,
const TextEditor::Coordinates aAddedEnd,

const std::string& aRemoved,
const TextEditor::Coordinates aRemovedStart,
const TextEditor::Coordinates aRemovedEnd,

TextEditor::EditorState& aBefore,
TextEditor::EditorState& aAfter);

UndoRecord(UndoRecord& undoRecord);

void Undo(TextEditor* aEditor);
void Redo(TextEditor* aEditor);

std::string mAdded;
Coordinates mAddedStart;
Coordinates mAddedEnd;

std::string mRemoved;
Coordinates mRemovedStart;
Coordinates mRemovedEnd;

EditorState mBefore;
EditorState mAfter;
};

class ExternalUndoBufferInterface
{
public:
virtual void AddUndo(UndoRecord&) = 0;
};

typedef std::vector<UndoRecord> UndoBuffer;

TextEditor();
~TextEditor();

Expand Down Expand Up @@ -261,6 +311,7 @@ class TextEditor
bool CanRedo() const;
void Undo(int aSteps = 1);
void Redo(int aSteps = 1);
void SetExternalUndoBuffer(ExternalUndoBufferInterface*);

static const Palette& GetDarkPalette();
static const Palette& GetLightPalette();
Expand All @@ -269,48 +320,6 @@ class TextEditor
private:
typedef std::vector<std::pair<std::regex, PaletteIndex>> RegexList;

struct EditorState
{
Coordinates mSelectionStart;
Coordinates mSelectionEnd;
Coordinates mCursorPosition;
};

class UndoRecord
{
public:
UndoRecord() {}
~UndoRecord() {}

UndoRecord(
const std::string& aAdded,
const TextEditor::Coordinates aAddedStart,
const TextEditor::Coordinates aAddedEnd,

const std::string& aRemoved,
const TextEditor::Coordinates aRemovedStart,
const TextEditor::Coordinates aRemovedEnd,

TextEditor::EditorState& aBefore,
TextEditor::EditorState& aAfter);

void Undo(TextEditor* aEditor);
void Redo(TextEditor* aEditor);

std::string mAdded;
Coordinates mAddedStart;
Coordinates mAddedEnd;

std::string mRemoved;
Coordinates mRemovedStart;
Coordinates mRemovedEnd;

EditorState mBefore;
EditorState mAfter;
};

typedef std::vector<UndoRecord> UndoBuffer;

void ProcessInputs();
void Colorize(int aFromLine = 0, int aCount = -1);
void ColorizeRange(int aFromLine = 0, int aToLine = 0);
Expand Down Expand Up @@ -353,6 +362,7 @@ class TextEditor
EditorState mState;
UndoBuffer mUndoBuffer;
int mUndoIndex;
ExternalUndoBufferInterface* mExternalUndoBuffer;

int mTabSize;
bool mOverwrite;
Expand Down