fix(ui): CodeEditText rendering
This commit is contained in:
@@ -60,7 +60,7 @@ public class CodeEditText extends AppCompatEditText {
|
||||
protected HVScrollView mParentScrollView;
|
||||
|
||||
private final CopyOnWriteArrayList<CodeEditor.CursorChangeCallback> mCursorChangeCallbacks = new CopyOnWriteArrayList<>();
|
||||
private JavaScriptHighlighter.HighlightTokens mHighlightTokens;
|
||||
private volatile JavaScriptHighlighter.HighlightTokens mHighlightTokens;
|
||||
private Theme mTheme;
|
||||
private TimingLogger mLogger = new TimingLogger(LOG_TAG, "draw");
|
||||
private Paint mLineHighlightPaint = new Paint();
|
||||
@@ -192,10 +192,11 @@ public class CodeEditText extends AppCompatEditText {
|
||||
return;
|
||||
}
|
||||
JavaScriptHighlighter.HighlightTokens highlightTokens = mHighlightTokens;
|
||||
Log.d(LOG_TAG, "drawText: tokens = " + highlightTokens);
|
||||
Layout layout = getLayout();
|
||||
int lineCount = getLineCount();
|
||||
int textLength = highlightTokens == null ? 0 : highlightTokens.getText().length();
|
||||
String text = highlightTokens == null ? "" : highlightTokens.getText();
|
||||
Editable text = getText();
|
||||
int paddingLeft = getPaddingLeft();
|
||||
int scrollX = Math.max(getRealScrollX() - paddingLeft, 0);
|
||||
Paint paint = getPaint();
|
||||
@@ -260,10 +261,10 @@ public class CodeEditText extends AppCompatEditText {
|
||||
}
|
||||
paint.setColor(previousColor);
|
||||
float offsetX = paint.measureText(text, lineStart, previousColorPos);
|
||||
if(previousColorPos < 0 || visibleCharEnd > textLength || previousColorPos >= visibleCharEnd){
|
||||
if (previousColorPos < 0 || visibleCharEnd > textLength || previousColorPos >= visibleCharEnd) {
|
||||
Log.e(LOG_TAG, "IndexOutOfBounds: previousColorPos = " + previousColorPos + ", visibleCharEnd = "
|
||||
+visibleCharEnd + ", textLength = " + textLength);
|
||||
postInvalidate();
|
||||
+ visibleCharEnd + ", textLength = " + textLength);
|
||||
//postInvalidate();
|
||||
return;
|
||||
}
|
||||
canvas.drawText(text, previousColorPos, visibleCharEnd, paddingLeft + offsetX, lineBaseline, paint);
|
||||
@@ -413,10 +414,12 @@ public class CodeEditText extends AppCompatEditText {
|
||||
|
||||
|
||||
public void updateHighlightTokens(JavaScriptHighlighter.HighlightTokens highlightTokens) {
|
||||
post(() -> {
|
||||
mHighlightTokens = highlightTokens;
|
||||
invalidate();
|
||||
});
|
||||
if (mHighlightTokens != null && mHighlightTokens.getId() >= highlightTokens.getId()) {
|
||||
return;
|
||||
}
|
||||
mHighlightTokens = highlightTokens;
|
||||
Log.d(LOG_TAG, "updateHighlightTokens: tokens = " + highlightTokens);
|
||||
postInvalidate();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -12,9 +12,9 @@ import org.autojs.autojs.ui.widget.SimpleTextWatcher;
|
||||
import org.mozilla.javascript.Token;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class JavaScriptHighlighter implements SimpleTextWatcher.AfterTextChangedListener {
|
||||
@@ -25,12 +25,17 @@ public class JavaScriptHighlighter implements SimpleTextWatcher.AfterTextChanged
|
||||
public final int[] colors;
|
||||
private String mText;
|
||||
private int mCount;
|
||||
private final int mId;
|
||||
|
||||
public HighlightTokens(String text) {
|
||||
public HighlightTokens(String text, int id) {
|
||||
colors = new int[text.length()];
|
||||
mText = text;
|
||||
mId = id;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return mId;
|
||||
}
|
||||
|
||||
public void addToken(int tokenStart, int tokenEnd, int color) {
|
||||
for (int i = tokenStart; i < tokenEnd; i++) {
|
||||
@@ -41,9 +46,7 @@ public class JavaScriptHighlighter implements SimpleTextWatcher.AfterTextChanged
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "HighlightTokens{" +
|
||||
"colors=" + Arrays.toString(colors) +
|
||||
'}';
|
||||
return super.toString() + "{count = " + mCount + ", length = " + mText.length() + "}";
|
||||
}
|
||||
|
||||
public int getCharCount() {
|
||||
@@ -57,11 +60,13 @@ public class JavaScriptHighlighter implements SimpleTextWatcher.AfterTextChanged
|
||||
|
||||
private Theme mTheme;
|
||||
private CodeEditText mCodeEditText;
|
||||
private ExecutorService mExecutorService = Executors.newSingleThreadExecutor();
|
||||
private ThreadPoolExecutor mExecutorService = new ThreadPoolExecutor(3, 6,
|
||||
2L, TimeUnit.MINUTES, new LinkedBlockingQueue<>());
|
||||
private AtomicInteger mRunningHighlighterId = new AtomicInteger();
|
||||
private TimingLogger mLogger = new TimingLogger(CodeEditText.LOG_TAG, "highlight");
|
||||
|
||||
public JavaScriptHighlighter(Theme theme, CodeEditText codeEditText) {
|
||||
mExecutorService.allowCoreThreadTimeOut(true);
|
||||
mTheme = theme;
|
||||
mCodeEditText = codeEditText;
|
||||
codeEditText.addTextChangedListener(new SimpleTextWatcher(this));
|
||||
@@ -94,12 +99,10 @@ public class JavaScriptHighlighter implements SimpleTextWatcher.AfterTextChanged
|
||||
|
||||
private void updateTokens(String sourceString, int id) throws IOException {
|
||||
TokenStream ts = new TokenStream(null, sourceString, 0);
|
||||
HighlightTokens highlightTokens = new HighlightTokens(sourceString);
|
||||
HighlightTokens highlightTokens = new HighlightTokens(sourceString, id);
|
||||
int token;
|
||||
int color = mTheme.getColorForToken(Token.NAME);
|
||||
while ((token = ts.getToken()) != Token.EOF) {
|
||||
if (mRunningHighlighterId.get() != id)
|
||||
return;
|
||||
color = mTheme.getColorForToken(token);
|
||||
highlightTokens.addToken(ts.getTokenBeg(), ts.getTokenEnd(), color);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user