|
25 | 25 | import android.support.annotation.NonNull; |
26 | 26 | import android.support.annotation.Nullable; |
27 | 27 | import android.support.v7.widget.AppCompatEditText; |
| 28 | +import android.text.InputFilter; |
28 | 29 | import android.text.Layout; |
| 30 | +import android.text.Spanned; |
29 | 31 | import android.text.TextPaint; |
30 | 32 | import android.util.AttributeSet; |
31 | 33 | import android.util.TypedValue; |
@@ -83,7 +85,6 @@ public EditAreaView2(Context context, AttributeSet attrs) { |
83 | 85 | public EditAreaView2(Context context, AttributeSet attrs, int defStyleAttr) { |
84 | 86 | super(context, attrs, defStyleAttr); |
85 | 87 | init(context); |
86 | | - |
87 | 88 | } |
88 | 89 |
|
89 | 90 | private void init(Context context) { |
@@ -119,6 +120,48 @@ private void init(Context context) { |
119 | 120 | onSharedPreferenceChanged(null, Preferences.KEY_TAB_SIZE); |
120 | 121 | onSharedPreferenceChanged(null, Preferences.KEY_AUTO_INDENT); |
121 | 122 | 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 | + }); |
122 | 165 | } |
123 | 166 |
|
124 | 167 | @Override |
|
0 commit comments