44
44
import android .text .style .ForegroundColorSpan ;
45
45
import android .text .style .ReplacementSpan ;
46
46
import android .util .AttributeSet ;
47
+ import android .view .KeyEvent ;
48
+ import android .view .View ;
47
49
import android .widget .MultiAutoCompleteTextView ;
48
50
49
51
import androidx .annotation .ColorInt ;
@@ -130,6 +132,7 @@ private void initEditorView() {
130
132
setHorizontallyScrolling (true );
131
133
setFilters (new InputFilter []{mInputFilter });
132
134
addTextChangedListener (mEditorTextWatcher );
135
+ setOnKeyListener (mOnKeyListener );
133
136
134
137
lineNumberRect = new Rect ();
135
138
lineNumberPaint = new Paint (Paint .ANTI_ALIAS_FLAG );
@@ -680,6 +683,26 @@ public void run() {
680
683
}
681
684
};
682
685
686
+ private final OnKeyListener mOnKeyListener = new OnKeyListener () {
687
+
688
+ @ Override
689
+ public boolean onKey (View v , int keyCode , KeyEvent event ) {
690
+ if (!enableAutoIndentation ) return false ;
691
+ if (event .getAction () != KeyEvent .ACTION_DOWN )
692
+ return true ;
693
+ switch (keyCode ) {
694
+ case KeyEvent .KEYCODE_SPACE :
695
+ currentIndentation ++;
696
+ break ;
697
+ case KeyEvent .KEYCODE_DEL :
698
+ if (currentIndentation > 0 )
699
+ currentIndentation --;
700
+ break ;
701
+ }
702
+ return false ;
703
+ }
704
+ };
705
+
683
706
private final TextWatcher mEditorTextWatcher = new TextWatcher () {
684
707
685
708
private int start ;
@@ -707,6 +730,13 @@ public void onTextChanged(CharSequence charSequence, int start, int before, int
707
730
if (count == 1 && (enableAutoIndentation || enablePairComplete )) {
708
731
char currentChar = charSequence .charAt (start );
709
732
733
+ if (enableAutoIndentation ) {
734
+ if (indentationStarts .contains (currentChar ))
735
+ currentIndentation += tabLength ;
736
+ else if (indentationEnds .contains (currentChar ))
737
+ currentIndentation -= tabLength ;
738
+ }
739
+
710
740
if (enablePairComplete ) {
711
741
Character pairValue = mPairCompleteMap .get (currentChar );
712
742
if (pairValue != null ) {
@@ -721,14 +751,6 @@ else if (indentationEnds.contains(pairValue))
721
751
modified = true ;
722
752
}
723
753
}
724
-
725
- if (enableAutoIndentation ) {
726
- if (indentationStarts .contains (currentChar ))
727
- currentIndentation += tabLength ;
728
- else if (indentationEnds .contains (currentChar ))
729
- currentIndentation -= tabLength ;
730
- }
731
-
732
754
}
733
755
}
734
756
@@ -770,9 +792,15 @@ public CharSequence filter(CharSequence source, int start, int end,
770
792
if (modified && enableAutoIndentation && start < source .length ()) {
771
793
boolean isInsertedAtEnd = dest .length () == dEnd ;
772
794
if (source .charAt (start ) == '\n' ) {
773
- int indentation = isInsertedAtEnd
774
- ? currentIndentation
775
- : calculateSourceIndentation (dest .subSequence (0 , dStart ));
795
+ char nextChar = dest .charAt (dEnd );
796
+ if (isInsertedAtEnd ) {
797
+ return applyIndentation (source , currentIndentation );
798
+ }
799
+
800
+ int indentation = calculateSourceIndentation (dest .subSequence (0 , dStart ));
801
+ if (indentationEnds .contains (nextChar )) {
802
+ indentation -= tabLength ;
803
+ }
776
804
return applyIndentation (source , indentation );
777
805
}
778
806
}
0 commit comments