chore
This commit is contained in:
@@ -320,9 +320,6 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
|
|||||||
.doOnNext(s -> {
|
.doOnNext(s -> {
|
||||||
mEditor.markTextAsSaved();
|
mEditor.markTextAsSaved();
|
||||||
setMenuItemStatus(R.id.save, false);
|
setMenuItemStatus(R.id.save, false);
|
||||||
setMenuItemStatus(R.id.undo, false);
|
|
||||||
setMenuItemStatus(R.id.redo, false);
|
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ import android.view.Gravity;
|
|||||||
|
|
||||||
import com.stardust.scriptdroid.BuildConfig;
|
import com.stardust.scriptdroid.BuildConfig;
|
||||||
import com.stardust.scriptdroid.ui.edit.theme.Theme;
|
import com.stardust.scriptdroid.ui.edit.theme.Theme;
|
||||||
|
import com.stardust.util.TextUtils;
|
||||||
|
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@@ -261,25 +262,35 @@ public class CodeEditText extends AppCompatEditText {
|
|||||||
if (mCallback == null || selStart != selEnd) {
|
if (mCallback == null || selStart != selEnd) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String text = getText().toString();
|
callCursorChangeCallback(getText(), selStart);
|
||||||
if (text.isEmpty()) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
int lineStart = text.lastIndexOf("\n", selStart) + 1;
|
if (mCallback == null)
|
||||||
|
return;
|
||||||
|
int lineStart = TextUtils.lastIndexOf(text, '\n', sel) + 1;
|
||||||
if (lineStart < 0) {
|
if (lineStart < 0) {
|
||||||
lineStart = 0;
|
lineStart = 0;
|
||||||
}
|
}
|
||||||
if (lineStart > text.length() - 1) {
|
if (lineStart > text.length() - 1) {
|
||||||
lineStart = text.length() - 1;
|
lineStart = text.length() - 1;
|
||||||
}
|
}
|
||||||
int lineEnd = text.indexOf("\n", selStart);
|
int lineEnd = TextUtils.indexOf(text, '\n', sel);
|
||||||
if (lineEnd < 0) {
|
if (lineEnd < 0) {
|
||||||
lineEnd = text.length();
|
lineEnd = text.length();
|
||||||
}
|
}
|
||||||
if (lineEnd < lineStart || lineStart < 0 || lineEnd > text.length())
|
if (lineEnd < lineStart || lineStart < 0 || lineEnd > text.length())
|
||||||
return;
|
return;
|
||||||
String line = text.substring(lineStart, lineEnd);
|
String line = text.subSequence(lineStart, lineEnd).toString();
|
||||||
int cursor = selStart - lineStart;
|
int cursor = sel - lineStart;
|
||||||
mCallback.onCursorChange(line, cursor);
|
mCallback.onCursorChange(line, cursor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -297,7 +297,7 @@ public class CodeEditor extends HVScrollView {
|
|||||||
|
|
||||||
|
|
||||||
public void markTextAsSaved() {
|
public void markTextAsSaved() {
|
||||||
mTextViewRedoUndo.clearHistory();
|
mTextViewRedoUndo.markTextAsUnchanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ public class JavaScriptHighlighter implements SimpleTextWatcher.AfterTextChanged
|
|||||||
|
|
||||||
public static class HighlightTokens {
|
public static class HighlightTokens {
|
||||||
|
|
||||||
private int[] mColors;
|
private final int[] mColors;
|
||||||
private String mText;
|
private String mText;
|
||||||
private int mCount;
|
private int mCount;
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import android.text.Editable;
|
|||||||
import android.text.TextWatcher;
|
import android.text.TextWatcher;
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Stack;
|
import java.util.Stack;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -40,8 +41,9 @@ public class TextViewRedoUndo {
|
|||||||
//自动操作标志,防止重复回调,导致无限撤销
|
//自动操作标志,防止重复回调,导致无限撤销
|
||||||
private boolean flag = false;
|
private boolean flag = false;
|
||||||
|
|
||||||
|
private int mInitialHistoryStackSize;
|
||||||
|
|
||||||
public TextViewRedoUndo(@NonNull EditText editText) {
|
public TextViewRedoUndo(@NonNull EditText editText) {
|
||||||
CheckNull(editText, "EditText不能为空");
|
|
||||||
this.editable = editText.getText();
|
this.editable = editText.getText();
|
||||||
this.editText = editText;
|
this.editText = editText;
|
||||||
editText.addTextChangedListener(new Watcher());
|
editText.addTextChangedListener(new Watcher());
|
||||||
@@ -52,7 +54,9 @@ public class TextViewRedoUndo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void onTextChanged(Editable s) {
|
protected void onTextChanged(Editable s) {
|
||||||
|
if (history.size() < mInitialHistoryStackSize) {
|
||||||
|
mInitialHistoryStackSize = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -63,6 +67,7 @@ public class TextViewRedoUndo {
|
|||||||
public final void clearHistory() {
|
public final void clearHistory() {
|
||||||
history.clear();
|
history.clear();
|
||||||
historyBack.clear();
|
historyBack.clear();
|
||||||
|
mInitialHistoryStackSize = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean canUndo() {
|
public boolean canUndo() {
|
||||||
@@ -144,7 +149,11 @@ public class TextViewRedoUndo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean isTextChanged() {
|
public boolean isTextChanged() {
|
||||||
return !history.empty();
|
return history.size() != mInitialHistoryStackSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void markTextAsUnchanged() {
|
||||||
|
mInitialHistoryStackSize = history.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
private class Watcher implements TextWatcher {
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -19,4 +19,40 @@ public class TextUtils {
|
|||||||
return "";
|
return "";
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static int lastIndexOf(CharSequence text, char ch, int fromIndex) {
|
||||||
|
if (text instanceof String) {
|
||||||
|
return ((String) text).lastIndexOf(ch, fromIndex);
|
||||||
|
}
|
||||||
|
int i = Math.min(fromIndex, text.length() - 1);
|
||||||
|
for (; i >= 0; i--) {
|
||||||
|
if (text.charAt(i) == ch) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int indexOf(CharSequence text, char ch, int fromIndex) {
|
||||||
|
if (text instanceof String) {
|
||||||
|
return ((String) text).indexOf(ch, fromIndex);
|
||||||
|
}
|
||||||
|
final int max = text.length();
|
||||||
|
if (fromIndex < 0) {
|
||||||
|
fromIndex = 0;
|
||||||
|
} else if (fromIndex >= max) {
|
||||||
|
// Note: fromIndex might be near -1>>>1.
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// handle most cases here (ch is a BMP code point or a
|
||||||
|
// negative value (invalid code point))
|
||||||
|
for (int i = fromIndex; i < max; i++) {
|
||||||
|
if (text.charAt(i) == ch) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user