-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
2,768 additions
and
5 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
class Diff::Impl | ||
{ | ||
public: | ||
diff_match_patch<std::string> dmp; | ||
}; | ||
|
||
Diff::Diff() | ||
{ | ||
impl = std::make_unique<Impl>(); | ||
} | ||
|
||
Diff::~Diff() | ||
{ | ||
} | ||
|
||
juce::String Diff::diff (const juce::String s1, const juce::String s2) | ||
{ | ||
auto diffs = impl->dmp.diff_main (s1.toStdString(), s2.toStdString()); | ||
auto patches = impl->dmp.patch_make (diffs); | ||
return impl->dmp.patch_toText (patches); | ||
} | ||
|
||
juce::String Diff::applyPatch (const juce::String s, const juce::String patchText) | ||
{ | ||
auto patches = impl->dmp.patch_fromText (patchText.toStdString()); | ||
auto [res, status] = impl->dmp.patch_apply (patches, s.toStdString()); | ||
|
||
for (auto b : status) | ||
{ | ||
jassert (b); | ||
if (! b) | ||
return {}; | ||
} | ||
|
||
return res; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#pragma once | ||
|
||
class Diff | ||
{ | ||
public: | ||
Diff(); | ||
~Diff(); | ||
|
||
juce::String diff (const juce::String s1, const juce::String s2); | ||
juce::String applyPatch (const juce::String s, const juce::String patch); | ||
|
||
private: | ||
class Impl; | ||
std::unique_ptr<Impl> impl; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
|
||
void TextHistory::addText (const juce::String& t) | ||
{ | ||
if (t == currentText) | ||
return; | ||
|
||
while (historyStack.size() - 1 > stackPointer) | ||
historyStack.removeLast(); | ||
|
||
auto fordardPatch = diff.diff (currentText, t); | ||
auto backwardPatch = diff.diff (t, currentText); | ||
|
||
historyStack.add ({fordardPatch, backwardPatch}); | ||
currentText = t; | ||
|
||
while (historyStack.size() > limit) | ||
{ | ||
historyStack.remove (0); | ||
stackPointer--; | ||
} | ||
} | ||
|
||
const juce::String& TextHistory::getCurrentText() | ||
{ | ||
return currentText; | ||
} | ||
|
||
void TextHistory::undo() | ||
{ | ||
if (canUndo()) | ||
{ | ||
currentText = diff.applyPatch (currentText, historyStack[stackPointer].backwardPatch); | ||
stackPointer--; | ||
} | ||
} | ||
|
||
void TextHistory::redo() | ||
{ | ||
if (canRedo()) | ||
{ | ||
currentText = diff.applyPatch (currentText, historyStack[stackPointer + 1].forwardPatch); | ||
stackPointer++; | ||
} | ||
} | ||
|
||
bool TextHistory::canUndo() | ||
{ | ||
return stackPointer > 0; | ||
} | ||
|
||
bool TextHistory::canRedo() | ||
{ | ||
return stackPointer < historyStack.size() - 1; | ||
} | ||
|
||
void TextHistory::setHistoryLimit (int limit_) | ||
{ | ||
limit = limit_; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#pragma once | ||
|
||
class TextHistory | ||
{ | ||
public: | ||
void setHistoryLimit (int numItems); | ||
|
||
void undo(); | ||
void redo(); | ||
bool canUndo(); | ||
bool canRedo(); | ||
|
||
void addText (const juce::String&); | ||
const juce::String& getCurrentText(); | ||
|
||
private: | ||
juce::String currentText; | ||
|
||
struct HistoryItem | ||
{ | ||
juce::String forwardPatch; | ||
juce::String backwardPatch; | ||
}; | ||
|
||
Diff diff; | ||
|
||
juce::Array<HistoryItem> historyStack; | ||
int stackPointer = -1; | ||
int limit = 100; | ||
}; | ||
//------------------------------------------------------------------------------------------------- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters