2424import android .graphics .Typeface ;
2525import android .support .annotation .NonNull ;
2626import android .support .annotation .Nullable ;
27+ import android .text .Editable ;
2728import android .text .InputFilter ;
2829import android .text .InputType ;
2930import android .text .Layout ;
31+ import android .text .Spannable ;
3032import android .text .Spanned ;
3133import android .text .TextPaint ;
34+ import android .text .TextWatcher ;
3235import android .util .AttributeSet ;
3336import android .util .TypedValue ;
3437import android .view .inputmethod .EditorInfo ;
3740import com .duy .ide .editor .text .LayoutContext ;
3841import com .duy .ide .editor .text .LineManager ;
3942import com .duy .ide .editor .text .TextLineNumber ;
43+ import com .duy .ide .editor .text .style .TabSpan ;
4044import com .duy .ide .editor .theme .model .EditorTheme ;
4145import com .jecelyin .common .utils .ReflectionUtil ;
4246import com .jecelyin .common .utils .SysUtils ;
4650import java .util .List ;
4751
4852public abstract class HighlightEditorView extends android .support .v7 .widget .AppCompatEditText
49- implements IEditAreaView , SharedPreferences .OnSharedPreferenceChangeListener {
53+ implements IEditAreaView , SharedPreferences .OnSharedPreferenceChangeListener ,
54+ TextWatcher {
5055 private static final String TAG = "EditAreaView2" ;
5156 private final LayoutContext mLayoutContext = new LayoutContext ();
5257 protected Preferences mPreferences ;
@@ -68,6 +73,7 @@ public abstract class HighlightEditorView extends android.support.v7.widget.AppC
6873 */
6974 private boolean mNeedUpdateLineNumber = false ;
7075 private boolean mIsAutoIndent = true ;
76+ private int mTabWidth = 3 ;
7177
7278 public HighlightEditorView (Context context ) {
7379 super (context );
@@ -123,6 +129,7 @@ private void init(Context context) {
123129 onSharedPreferenceChanged (null , Preferences .KEY_AUTO_CAPITALIZE );
124130
125131 initAutoIndent ();
132+ addTextChangedListener (this );
126133 }
127134
128135 private void initAutoIndent () {
@@ -213,7 +220,6 @@ private void setCursorColor(int caretColor) {
213220 }
214221 }
215222
216-
217223 @ Override
218224 public void scrollToLine (int line ) {
219225
@@ -241,7 +247,6 @@ public void setInitLineNumber(int lineNumber) {
241247 updateLineNumberCount (0 );
242248 }
243249
244-
245250 private void drawLineNumber (Canvas canvas ) {
246251 if (!mLayoutContext .getPreferences ().isShowLineNumber ()) {
247252 return ;
@@ -321,7 +326,6 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
321326 }
322327 }
323328
324-
325329 @ Override
326330 public void setTextSize (int unit , float size ) {
327331 super .setTextSize (unit , size );
@@ -330,9 +334,30 @@ public void setTextSize(int unit, float size) {
330334 }
331335
332336 @ Override
333- protected void onTextChanged (CharSequence text , int start , int lengthBefore , int lengthAfter ) {
334- super .onTextChanged (text , start , lengthBefore , lengthAfter );
337+ public void beforeTextChanged (CharSequence s , int start , int count , int after ) {
338+
339+ }
340+
341+ @ Override
342+ public void afterTextChanged (Editable s ) {
343+
344+ }
345+
346+ @ Override
347+ public void onTextChanged (CharSequence s , int start , int before , int count ) {
335348 updateLineNumberCount (start );
349+ if (s instanceof Spannable ) {
350+ applyTabWidth ((Spannable ) s , start , start + count - 1 );
351+ }
352+ }
353+
354+ public void applyTabWidth (Spannable text , int start , int end ) {
355+ for (int index = start ; index <= end ; index ++) {
356+ if (text .charAt (index ) == '\t' ) {
357+ text .setSpan (new TabSpan (getLayoutContext (), mTabWidth ),
358+ index , index + 1 , Spannable .SPAN_EXCLUSIVE_EXCLUSIVE );
359+ }
360+ }
336361 }
337362
338363 /**
@@ -342,6 +367,7 @@ private void updateTabChar() {
342367 if (DLog .DEBUG ) DLog .d (TAG , "updateTabChar() called" );
343368 float spaceWidth = getPaint ().measureText (" " );
344369 float tabWidth = spaceWidth * (mPreferences == null ? 4 : mPreferences .getTabSize ());
370+ mTabWidth = (int ) tabWidth ;
345371 try {
346372 Field tabIncrement = ReflectionUtil .getField (Layout .class ,
347373 "TAB_INCREMENT" , true );
0 commit comments