Skip to content

Add TextEditor::IsSaved(...) #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
58 changes: 58 additions & 0 deletions TextEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ TextEditor::TextEditor()
SetPalette(GetDarkPalette());
SetLanguageDefinition(LanguageDefinition::HLSL());
mLines.push_back(Line());

MarkSaved();
}

TextEditor::~TextEditor()
Expand Down Expand Up @@ -318,7 +320,13 @@ void TextEditor::AddUndo(UndoRecord& aValue)

mUndoBuffer.resize((size_t)(mUndoIndex + 1));
mUndoBuffer.back() = aValue;

if(mUndoIndex < mUndoIndexFirstEdit)
mUndoIndexFirstEdit = mUndoIndex;

++mUndoIndex;

mSaveState = false;
}

TextEditor::Coordinates TextEditor::ScreenPosToCoordinates(const ImVec2& aPosition) const
Expand Down Expand Up @@ -1172,6 +1180,7 @@ void TextEditor::SetText(const std::string & aText)
}

mTextChanged = true;
MarkSaved();
mScrollToTop = true;

mUndoBuffer.clear();
Expand Down Expand Up @@ -1203,6 +1212,7 @@ void TextEditor::SetTextLines(const std::vector<std::string> & aLines)
}

mTextChanged = true;
MarkSaved();
mScrollToTop = true;

mUndoBuffer.clear();
Expand Down Expand Up @@ -1985,6 +1995,14 @@ bool TextEditor::CanUndo() const
{
return !mReadOnly && mUndoIndex > 0;
}
int TextEditor::GetUndoIndex() const
{
return mUndoIndex;
}
size_t TextEditor::GetUndoBufferSize() const
{
return mUndoBuffer.size();
}

bool TextEditor::CanRedo() const
{
Expand All @@ -1995,12 +2013,52 @@ void TextEditor::Undo(int aSteps)
{
while (CanUndo() && aSteps-- > 0)
mUndoBuffer[--mUndoIndex].Undo(this);

if(mUndoIndex == mUndoIndexLastSave){
if(mUndoIndexFirstEdit >= mUndoIndexLastSave && !mMarkedUnsaved)
mSaveState = true;
} else
mSaveState = false;
}

void TextEditor::Redo(int aSteps)
{
while (CanRedo() && aSteps-- > 0)
mUndoBuffer[mUndoIndex++].Redo(this);

if(mUndoIndex == mUndoIndexLastSave){
if(mUndoIndexFirstEdit >= mUndoIndexLastSave && !mMarkedUnsaved)
mSaveState = true;
} else
mSaveState = false;
}

void TextEditor::MarkSaved()
{
mSaveState = true;
mMarkedUnsaved = false;
mUndoIndexLastSave = mUndoIndex;
mUndoIndexFirstEdit = mUndoIndex;
}

// Marks the file changed in a way that can't be reverted with undo/redo
void TextEditor::MarkUnsaved()
{
mSaveState = false;
mMarkedUnsaved = true;
}
void TextEditor::MarkSaved(bool saveState)
{
if(saveState){
mUndoIndexLastSave = mUndoIndex;
mUndoIndexFirstEdit = mUndoIndex;
} else
mMarkedUnsaved = true;
mSaveState = saveState;
mMarkedUnsaved = !saveState;
}
bool TextEditor::IsSaved() const{
return mSaveState;
}

const TextEditor::Palette & TextEditor::GetDarkPalette()
Expand Down
13 changes: 13 additions & 0 deletions TextEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,14 @@ class TextEditor
void Undo(int aSteps = 1);
void Redo(int aSteps = 1);

int GetUndoIndex() const;
size_t GetUndoBufferSize() const;

void MarkSaved();
void MarkUnsaved();
void MarkSaved(bool saveState);
bool IsSaved() const;

static const Palette& GetDarkPalette();
static const Palette& GetLightPalette();
static const Palette& GetRetroBluePalette();
Expand Down Expand Up @@ -386,4 +394,9 @@ class TextEditor
uint64_t mStartTime;

float mLastClick;

bool mSaveState;
bool mMarkedUnsaved;
int mUndoIndexFirstEdit;
int mUndoIndexLastSave;
};