diff --git a/editor/src/main/java/io/github/rosemoe/sora/widget/CodeEditor.java b/editor/src/main/java/io/github/rosemoe/sora/widget/CodeEditor.java index c1afe4d3..70f0f46a 100644 --- a/editor/src/main/java/io/github/rosemoe/sora/widget/CodeEditor.java +++ b/editor/src/main/java/io/github/rosemoe/sora/widget/CodeEditor.java @@ -1878,7 +1878,7 @@ public void commitText(CharSequence text, boolean applyAutoIndent) { // replace text SymbolPairMatch.SymbolPair pair = null; if (getProps().symbolPairAutoCompletion && text.length() > 0) { - var firstCharFromText = text.charAt(0); + var endCharFromText = text.charAt(text.length() - 1); char[] inputText = null; @@ -1889,7 +1889,7 @@ public void commitText(CharSequence text, boolean applyAutoIndent) { pair = languageSymbolPairs.matchBestPair( this, cursor.left(), - inputText, firstCharFromText + inputText, endCharFromText ); } diff --git a/editor/src/main/java/io/github/rosemoe/sora/widget/SymbolPairMatch.java b/editor/src/main/java/io/github/rosemoe/sora/widget/SymbolPairMatch.java index 8d354fab..3b85fa95 100644 --- a/editor/src/main/java/io/github/rosemoe/sora/widget/SymbolPairMatch.java +++ b/editor/src/main/java/io/github/rosemoe/sora/widget/SymbolPairMatch.java @@ -23,6 +23,8 @@ */ package io.github.rosemoe.sora.widget; +import android.text.TextUtils; + import androidx.annotation.Nullable; import java.util.ArrayList; @@ -125,10 +127,10 @@ public final List matchBestPairList(char editChar) { } @Nullable - public final SymbolPair matchBestPair(CodeEditor editor, CharPosition cursorPosition, char[] inputCharArray, char firstChar) { + public final SymbolPair matchBestPair(CodeEditor editor, CharPosition cursorPosition, char[] inputCharArray, char endChar) { final Content content = editor.getText(); // do not apply single character pairs for text with length > 1 - var singleCharPair = inputCharArray == null ? matchBestPairBySingleChar(firstChar) : null; + var singleCharPair = inputCharArray == null ? matchBestPairBySingleChar(endChar) : null; // matches single character symbol pair first if (singleCharPair != null) { @@ -137,7 +139,7 @@ public final SymbolPair matchBestPair(CodeEditor editor, CharPosition cursorPosi } // find all possible lists, with a single character for fast search - var matchList = matchBestPairList(firstChar); + var matchList = matchBestPairList(endChar); SymbolPair matchPair = null; for (var pair : matchList) { @@ -150,7 +152,7 @@ public final SymbolPair matchBestPair(CodeEditor editor, CharPosition cursorPosi var matchFlag = 1; var insertIndex = cursorPosition.index; - // the size = 1 + // the size = 1, we need compare characters before cursor, ensure it match the whole open char array if (inputCharArray == null) { var arrayIndex = openCharArray.length - 2; while (arrayIndex >= 0) { @@ -178,19 +180,17 @@ public final SymbolPair matchBestPair(CodeEditor editor, CharPosition cursorPosi matchFlag &= inputCharArray[charIndex] == openCharArray[pairIndex] ? 1 : 0; } - // input text and symbol pair text equal - if (pairIndex == 0) { - continue; - } + // input text and symbol pair text not equal fully, continue compare characters before cursor + if (matchFlag == 1 && pairIndex > 0) { + // When the loop is stopped the character position + // is still in the first position of the matched characters, + // we need to replace this character, + // so we need to subtract a character position + insertIndex--; - // When the loop is stopped the character position - // is still in the first position of the matched characters, - // we need to replace this character, - // so we need to subtract a character position - insertIndex--; - - for (; pairIndex >= 0; insertIndex--, pairIndex--) { - matchFlag &= content.charAt(insertIndex) == openCharArray[pairIndex] ? 1 : 0; + for (; pairIndex >= 0; insertIndex--, pairIndex--) { + matchFlag &= content.charAt(insertIndex) == openCharArray[pairIndex] ? 1 : 0; + } } }