Skip to content

Commit

Permalink
Bugfix: undo-redo history at new created files (PR gsantner#2038)
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 authored May 24, 2023
1 parent 6d6d278 commit dc95d28
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
_document.resetChangeTracking(); // force next reload
loadDocument();

// If not set by loadDocument, se the undo-redo helper here
if (_editTextUndoRedoHelper == null) {
_editTextUndoRedoHelper = new TextViewUndoRedo(_hlEditor);
}

// Configure the editor. Doing so after load helps prevent some errors
// ---------------------------------------------------------
_hlEditor.setLineSpacing(0, _appSettings.getEditorLineSpacing());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,15 @@ public void setAutoFormatEnabled(final boolean enable) {

// Run some code with auto formatters and accessibility disabled
public void withAutoFormatDisabled(final GsCallback.a0 callback) {
final boolean enabled = getAutoFormatEnabled();
try {
_accessibilityEnabled = false;
TextViewUtils.withAutoFormatDisabled(getText(), callback);
if (enabled) {
setAutoFormatEnabled(false);
}
callback.callback();
} finally {
setAutoFormatEnabled(enabled);
_accessibilityEnabled = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
import android.text.TextWatcher;
import android.util.Pair;


public class ListHandler implements TextViewUtils.MTextWatcher {
public class ListHandler implements TextWatcher {
private boolean triggerReorder = false;
private Integer beforeLineEnd = null;
private boolean alreadyRunning = false; // Prevent this instance from triggering itself
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -713,39 +713,4 @@ public static Boolean isImeOpen(final View view) {
}
return null; // Uncertain
}

public interface MTextWatcher extends TextWatcher {
// Classes inheriting from this class will be disabled in withAutoFormatDisabled
}

public static void withAutoFormatDisabled(final Editable editable, final GsCallback.a0 callback) {
if (editable instanceof ChunkedEditable) {
// Optimization for chunked editabled, which do not support formatting
callback.callback();
return;
}

final InputFilter[] filters = editable.getFilters();
editable.setFilters(new InputFilter[0]);

// We use MTextWatcher and not TextWatcher as SpannableStringBuilder uses TextWatcher internally
// And we don't want to break that functionality too
final MTextWatcher[] watchers = editable.getSpans(0, editable.length(), MTextWatcher.class);
final int[][] wData = new int[watchers.length][3];
for (int i = 0; i < watchers.length; i++) {
final TextWatcher w = watchers[i];
wData[i][0] = editable.getSpanStart(w);
wData[i][1] = editable.getSpanEnd(w);
wData[i][2] = editable.getSpanFlags(w);
editable.removeSpan(w);
}

callback.callback();

for (int i = 0; i < watchers.length; i++) {
editable.setSpan(watchers[i], wData[i][0], wData[i][1], wData[i][2]);
}

editable.setFilters(filters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ private boolean doRestorePersistentState(SharedPreferences sp, String prefix) {
return true;
}

if (Integer.valueOf(hash) != mTextView.getText().toString().hashCode()) {
if (Integer.parseInt(hash) != mTextView.getText().toString().hashCode()) {
return false;
}

Expand Down Expand Up @@ -404,27 +404,21 @@ private final class EditTextChangeListener implements TextWatcher {
*/
private CharSequence mBeforeChange;

/**
* The text that was inserted by the change event.
*/
private CharSequence mAfterChange;

public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if (mIsUndoOrRedo) {
return;
}

mBeforeChange = TextViewUtils.toString(s, start, start + count);
}

public void onTextChanged(CharSequence s, int start, int before,
int count) {
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (mIsUndoOrRedo) {
return;
}

mAfterChange = TextViewUtils.toString(s, start, start + count);
// The text that was inserted by the change event.
final CharSequence mAfterChange = TextViewUtils.toString(s, start, start + count);
mEditHistory.add(new EditItem(start, mBeforeChange, mAfterChange));
}

Expand Down

0 comments on commit dc95d28

Please sign in to comment.