feat(editor): auto indent

This commit is contained in:
hyb1996
2018-02-25 18:02:44 +08:00
parent 451960008a
commit 445bcfa462
6 changed files with 110 additions and 7 deletions

View File

@@ -8,8 +8,8 @@ android {
applicationId "com.stardust.scriptdroid" applicationId "com.stardust.scriptdroid"
minSdkVersion 17 minSdkVersion 17
targetSdkVersion 23 targetSdkVersion 23
versionCode 253 versionCode 254
versionName "3.1.0 Alpha3" versionName "3.1.0 Alpha4"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true multiDexEnabled true
ndk { ndk {

View File

@@ -2828,7 +2828,7 @@ if (typeof define === "function" && define.amd) {
function beautify(source){ function beautify(source){
return js_beautify(source, { return js_beautify(source, {
'indent_size': 2, 'indent_size': 4,
'e4x': true 'e4x': true
}); });
} }

View File

@@ -0,0 +1,101 @@
package com.stardust.scriptdroid.ui.edit.editor;
import android.text.Editable;
import android.text.TextWatcher;
/**
* Created by Stardust on 2018/2/25.
*/
public class AutoIndent implements TextWatcher {
private String mIndent = " ";
private CodeEditText mEditText;
private boolean mInsertingIndent = false;
private boolean mExtraIndent = false;
private boolean mAutoIndent = false;
private int mCursor;
public AutoIndent(CodeEditText editText) {
mEditText = editText;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
/**
* 判断是否是在光标处插入一个换行符的情况是的话在下一个afterTextChanged回调中将调整缩进
*/
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (mInsertingIndent)
return;
//不是插入一个字符的情况
if (count != 1 || before != 0) {
return;
}
//边界检查,虽然可能并非必要
if (start - 1 < 0 || start >= s.length()) {
return;
}
char charInserted = s.charAt(start);
//不是插入换行符的情况
if (charInserted != '\n') {
return;
}
mCursor = mEditText.getSelectionStart();
//不是在光标处插入字符的情况
if (mCursor != mEditText.getSelectionEnd() || mCursor != start + 1) {
return;
}
//到这里已经可以判断为当前的字符变化为"在光标处插入一个换行符"的情况
mAutoIndent = true;
//我们再做一点额外判断。判断换行符之前的字符是否是括号,是的话下行将额外增加空格用于缩进
char charBefore = s.charAt(start - 1);
if (charBefore == '{' || charBefore == '(') {
mExtraIndent = true;
}
}
@Override
public void afterTextChanged(Editable s) {
if (mInsertingIndent || !mAutoIndent) {
return;
}
CharSequence indent = getLastLineIndent();
if (mExtraIndent) {
indent = mIndent + indent;
}
mInsertingIndent = true;
s.insert(mCursor, indent);
mInsertingIndent = false;
mExtraIndent = mAutoIndent = false;
mCursor = -1;
}
private CharSequence getLastLineIndent() {
if (mCursor < 0 || mCursor > mEditText.length()) {
return "";
}
int line = LayoutHelper.getLineOfChar(mEditText.getLayout(), mCursor);
if (line == 0) {
return "";
}
int lastLineStart = mEditText.getLayout().getLineStart(line - 1);
int lastLineEnd = mEditText.getLayout().getLineEnd(line);
for (int i = lastLineStart; i < lastLineEnd; i++) {
if (mEditText.getText().charAt(i) != ' ') {
if (i == lastLineStart) {
return "";
} else {
return mEditText.getText().subSequence(lastLineStart, i);
}
}
}
return mEditText.getText().subSequence(lastLineStart, lastLineEnd);
}
}

View File

@@ -22,6 +22,7 @@ import android.graphics.Canvas;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.speech.tts.TextToSpeech;
import android.support.v7.widget.AppCompatEditText; import android.support.v7.widget.AppCompatEditText;
import android.text.Layout; import android.text.Layout;
import android.util.AttributeSet; import android.util.AttributeSet;

View File

@@ -68,6 +68,7 @@ public class CodeEditor extends HVScrollView {
//setFillViewport(true); //setFillViewport(true);
inflate(getContext(), R.layout.code_editor, this); inflate(getContext(), R.layout.code_editor, this);
mCodeEditText = (CodeEditText) findViewById(R.id.code_edit_text); mCodeEditText = (CodeEditText) findViewById(R.id.code_edit_text);
mCodeEditText.addTextChangedListener(new AutoIndent(mCodeEditText));
mTextViewRedoUndo = new TextViewRedoUndo(mCodeEditText); mTextViewRedoUndo = new TextViewRedoUndo(mCodeEditText);
mJavaScriptHighlighter = new JavaScriptHighlighter(mTheme, mCodeEditText); mJavaScriptHighlighter = new JavaScriptHighlighter(mTheme, mCodeEditText);
setTheme(Theme.getDefault(getContext())); setTheme(Theme.getDefault(getContext()));

View File

@@ -167,10 +167,10 @@ public class DeveloperUtils {
a.finish(); a.finish();
return; return;
} }
long[] crc = readCrc(a.getString(crcRes)); //long[] crc = readCrc(a.getString(crcRes));
if (!checkDexFile(a, crc)) { //if (!checkDexFile(a, crc)) {
a.finish(); // a.finish();
} //}
} }
}); });