Skip to content

Commit bfab7f0

Browse files
committed
Improve Auto indenting implementation and able to support python indentation easily
1 parent f0b8636 commit bfab7f0

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

codeview/src/main/java/com/amrdeveloper/codeview/CodeView.java

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import android.text.style.ForegroundColorSpan;
4545
import android.text.style.ReplacementSpan;
4646
import android.util.AttributeSet;
47+
import android.view.KeyEvent;
48+
import android.view.View;
4749
import android.widget.MultiAutoCompleteTextView;
4850

4951
import androidx.annotation.ColorInt;
@@ -130,6 +132,7 @@ private void initEditorView() {
130132
setHorizontallyScrolling(true);
131133
setFilters(new InputFilter[]{mInputFilter});
132134
addTextChangedListener(mEditorTextWatcher);
135+
setOnKeyListener(mOnKeyListener);
133136

134137
lineNumberRect = new Rect();
135138
lineNumberPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
@@ -680,6 +683,26 @@ public void run() {
680683
}
681684
};
682685

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+
683706
private final TextWatcher mEditorTextWatcher = new TextWatcher() {
684707

685708
private int start;
@@ -707,6 +730,13 @@ public void onTextChanged(CharSequence charSequence, int start, int before, int
707730
if (count == 1 && (enableAutoIndentation || enablePairComplete)) {
708731
char currentChar = charSequence.charAt(start);
709732

733+
if (enableAutoIndentation) {
734+
if (indentationStarts.contains(currentChar))
735+
currentIndentation += tabLength;
736+
else if (indentationEnds.contains(currentChar))
737+
currentIndentation -= tabLength;
738+
}
739+
710740
if (enablePairComplete) {
711741
Character pairValue = mPairCompleteMap.get(currentChar);
712742
if (pairValue != null) {
@@ -721,14 +751,6 @@ else if (indentationEnds.contains(pairValue))
721751
modified = true;
722752
}
723753
}
724-
725-
if (enableAutoIndentation) {
726-
if (indentationStarts.contains(currentChar))
727-
currentIndentation += tabLength;
728-
else if (indentationEnds.contains(currentChar))
729-
currentIndentation -= tabLength;
730-
}
731-
732754
}
733755
}
734756

@@ -770,9 +792,15 @@ public CharSequence filter(CharSequence source, int start, int end,
770792
if (modified && enableAutoIndentation && start < source.length()) {
771793
boolean isInsertedAtEnd = dest.length() == dEnd;
772794
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+
}
776804
return applyIndentation(source, indentation);
777805
}
778806
}

0 commit comments

Comments
 (0)