This commit is contained in:
hyb1996
2018-02-24 23:11:54 +08:00
parent 011008df93
commit 451960008a
6 changed files with 68 additions and 18 deletions

View File

@@ -320,9 +320,6 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
.doOnNext(s -> {
mEditor.markTextAsSaved();
setMenuItemStatus(R.id.save, false);
setMenuItemStatus(R.id.undo, false);
setMenuItemStatus(R.id.redo, false);
});
}

View File

@@ -31,6 +31,7 @@ import android.view.Gravity;
import com.stardust.scriptdroid.BuildConfig;
import com.stardust.scriptdroid.ui.edit.theme.Theme;
import com.stardust.util.TextUtils;
import java.lang.reflect.Array;
import java.util.Arrays;
@@ -261,25 +262,35 @@ public class CodeEditText extends AppCompatEditText {
if (mCallback == null || selStart != selEnd) {
return;
}
String text = getText().toString();
if (text.isEmpty()) {
callCursorChangeCallback(getText(), selStart);
checkParenthesesPairs(getText(), selStart);
}
private void checkParenthesesPairs(CharSequence text, int sel) {
// TODO: 2018/2/24
}
private void callCursorChangeCallback(CharSequence text, int sel) {
if (getText().length() == 0) {
return;
}
int lineStart = text.lastIndexOf("\n", selStart) + 1;
if (mCallback == null)
return;
int lineStart = TextUtils.lastIndexOf(text, '\n', sel) + 1;
if (lineStart < 0) {
lineStart = 0;
}
if (lineStart > text.length() - 1) {
lineStart = text.length() - 1;
}
int lineEnd = text.indexOf("\n", selStart);
int lineEnd = TextUtils.indexOf(text, '\n', sel);
if (lineEnd < 0) {
lineEnd = text.length();
}
if (lineEnd < lineStart || lineStart < 0 || lineEnd > text.length())
return;
String line = text.substring(lineStart, lineEnd);
int cursor = selStart - lineStart;
String line = text.subSequence(lineStart, lineEnd).toString();
int cursor = sel - lineStart;
mCallback.onCursorChange(line, cursor);
}

View File

@@ -297,7 +297,7 @@ public class CodeEditor extends HVScrollView {
public void markTextAsSaved() {
mTextViewRedoUndo.clearHistory();
mTextViewRedoUndo.markTextAsUnchanged();
}
@Override

View File

@@ -23,7 +23,7 @@ public class JavaScriptHighlighter implements SimpleTextWatcher.AfterTextChanged
public static class HighlightTokens {
private int[] mColors;
private final int[] mColors;
private String mText;
private int mCount;

View File

@@ -21,6 +21,7 @@ import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import java.util.Objects;
import java.util.Stack;
/**
@@ -40,8 +41,9 @@ public class TextViewRedoUndo {
//自动操作标志,防止重复回调,导致无限撤销
private boolean flag = false;
private int mInitialHistoryStackSize;
public TextViewRedoUndo(@NonNull EditText editText) {
CheckNull(editText, "EditText不能为空");
this.editable = editText.getText();
this.editText = editText;
editText.addTextChangedListener(new Watcher());
@@ -52,7 +54,9 @@ public class TextViewRedoUndo {
}
protected void onTextChanged(Editable s) {
if (history.size() < mInitialHistoryStackSize) {
mInitialHistoryStackSize = 0;
}
}
@@ -63,6 +67,7 @@ public class TextViewRedoUndo {
public final void clearHistory() {
history.clear();
historyBack.clear();
mInitialHistoryStackSize = 0;
}
public boolean canUndo() {
@@ -144,7 +149,11 @@ public class TextViewRedoUndo {
}
public boolean isTextChanged() {
return !history.empty();
return history.size() != mInitialHistoryStackSize;
}
public void markTextAsUnchanged() {
mInitialHistoryStackSize = history.size();
}
private class Watcher implements TextWatcher {
@@ -261,7 +270,4 @@ public class TextViewRedoUndo {
}
private static void CheckNull(Object o, String message) {
if (o == null) throw new IllegalStateException(message);
}
}