Skip to content

Commit b4674bd

Browse files
committed
Support auto indent
1 parent 27c98a3 commit b4674bd

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

libeditor/src/main/java/com/duy/ide/editor/view/EditAreaView2.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
import android.support.annotation.NonNull;
2626
import android.support.annotation.Nullable;
2727
import android.support.v7.widget.AppCompatEditText;
28+
import android.text.InputFilter;
2829
import android.text.Layout;
30+
import android.text.Spanned;
2931
import android.text.TextPaint;
3032
import android.util.AttributeSet;
3133
import android.util.TypedValue;
@@ -83,7 +85,6 @@ public EditAreaView2(Context context, AttributeSet attrs) {
8385
public EditAreaView2(Context context, AttributeSet attrs, int defStyleAttr) {
8486
super(context, attrs, defStyleAttr);
8587
init(context);
86-
8788
}
8889

8990
private void init(Context context) {
@@ -119,6 +120,48 @@ private void init(Context context) {
119120
onSharedPreferenceChanged(null, Preferences.KEY_TAB_SIZE);
120121
onSharedPreferenceChanged(null, Preferences.KEY_AUTO_INDENT);
121122
onSharedPreferenceChanged(null, Preferences.KEY_AUTO_CAPITALIZE);
123+
124+
initAutoIndent();
125+
}
126+
127+
private void initAutoIndent() {
128+
setFilters(new InputFilter[]{new InputFilter() {
129+
@Override
130+
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
131+
if (mPreferences.isAutoIndent()) {
132+
if (!(source.length() == 1 && source.charAt(0) == '\n')) {
133+
return null;
134+
}
135+
dstart = dstart - 1;
136+
if (dstart < 0 || dstart >= dest.length())
137+
return null;
138+
139+
char ch;
140+
for (; dstart >= 0; dstart--) {
141+
ch = dest.charAt(dstart);
142+
if (ch != '\r')
143+
break;
144+
}
145+
146+
StringBuilder sb = new StringBuilder();
147+
for (int i = dstart; i >= 0; i--) {
148+
ch = dest.charAt(i);
149+
if (ch == '\n' || ch == '\r') {
150+
break;
151+
} else if (ch == ' ' || ch == '\t') {
152+
sb.append(ch);
153+
} else {
154+
sb.setLength(0);
155+
}
156+
}
157+
sb.reverse();
158+
159+
return "\n" + sb.toString();
160+
}
161+
return null;
162+
}
163+
}
164+
});
122165
}
123166

124167
@Override

0 commit comments

Comments
 (0)