Skip to content

Commit a43c413

Browse files
committed
Implement Auto pair complete feature with support functions
1 parent 4a7793c commit a43c413

1 file changed

Lines changed: 74 additions & 7 deletions

File tree

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

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ public class CodeView extends AppCompatMultiAutoCompleteTextView implements Find
9595
private int maxNumberOfSuggestions = Integer.MAX_VALUE;
9696
private int autoCompleteItemHeightInDp = (int) (50 * Resources.getSystem().getDisplayMetrics().density);
9797

98+
private boolean enablePairComplete = false;
99+
private final Map<Character, Character> mPairCompleteMap = new HashMap<>();
100+
98101
private final Handler mUpdateHandler = new Handler();
99102
private MultiAutoCompleteTextView.Tokenizer mAutoCompleteTokenizer;
100103

@@ -577,7 +580,7 @@ public void setMatchingHighlightColor(int color) {
577580
/**
578581
* Modify the maximum number of suggestions to show, default is Integer.MAX_VALUE
579582
* @param maxSuggestions the maximum number of suggestions
580-
* @since 1.2.3
583+
* @since 1.3.0
581584
*/
582585
public void setMaxSuggestionsSize(int maxSuggestions) {
583586
maxNumberOfSuggestions = maxSuggestions;
@@ -586,12 +589,58 @@ public void setMaxSuggestionsSize(int maxSuggestions) {
586589
/**
587590
* Modify the auto complete item height
588591
* @param height auto complete item height in dp
589-
* @since 1.2.3
592+
* @since 1.3.0
590593
*/
591594
public void setAutoCompleteItemHeightInDp(int height) {
592595
autoCompleteItemHeightInDp = (int) (height * Resources.getSystem().getDisplayMetrics().density);
593596
}
594597

598+
/**
599+
* Enable or disable the auto pairs complete feature
600+
* @param enable Flag to enable or disable auto pair complete
601+
* @since 1.3.0
602+
*/
603+
public void enablePairComplete(boolean enable) {
604+
enablePairComplete = enable;
605+
}
606+
607+
/**
608+
* Set the pairs for auto pairs complete feature
609+
* @param map Map of pairs of characters
610+
* @since 1.3.0
611+
*/
612+
public void setPairCompleteMap(Map<Character, Character> map) {
613+
mPairCompleteMap.clear();
614+
mPairCompleteMap.putAll(map);
615+
}
616+
617+
/**
618+
* Add new pair complete item using key and value
619+
* @param key the pair complete item key
620+
* @param value the pair complete item value
621+
* @since 1.3.0
622+
*/
623+
public void addPairCompleteItem(char key, char value) {
624+
mPairCompleteMap.put(key, value);
625+
}
626+
627+
/**
628+
* Remove single pair complete item by key
629+
* @param key the pair complete item key
630+
* @since 1.3.0
631+
*/
632+
public void removePairCompleteItem(char key) {
633+
mPairCompleteMap.remove(key);
634+
}
635+
636+
/**
637+
* Clear all of pairs
638+
* @since 1.3.0
639+
*/
640+
public void clearPairCompleteMap() {
641+
mPairCompleteMap.clear();
642+
}
643+
595644
@Override
596645
public void showDropDown() {
597646
final int[] screenPoint = new int[2];
@@ -655,11 +704,29 @@ public void onTextChanged(CharSequence charSequence, int start, int before, int
655704

656705
if (mRemoveErrorsWhenTextChanged) removeAllErrorLines();
657706

658-
if (enableAutoIndentation && count == 1) {
659-
if (indentationStarts.contains(charSequence.charAt(start)))
660-
currentIndentation += tabLength;
661-
else if (indentationEnds.contains(charSequence.charAt(start)))
662-
currentIndentation -= tabLength;
707+
if (count == 1 && (enableAutoIndentation || enablePairComplete)) {
708+
char currentChar = charSequence.charAt(start);
709+
if (enableAutoIndentation) {
710+
if (indentationStarts.contains(currentChar))
711+
currentIndentation += tabLength;
712+
else if (indentationEnds.contains(currentChar))
713+
currentIndentation -= tabLength;
714+
}
715+
716+
if (enablePairComplete) {
717+
Character pairValue = mPairCompleteMap.get(currentChar);
718+
if (pairValue != null) {
719+
modified = false;
720+
append(pairValue.toString());
721+
if (enableAutoIndentation) {
722+
if (indentationStarts.contains(pairValue))
723+
currentIndentation += tabLength;
724+
else if (indentationEnds.contains(pairValue))
725+
currentIndentation -= tabLength;
726+
}
727+
modified = true;
728+
}
729+
}
663730
}
664731
}
665732

0 commit comments

Comments
 (0)