Skip to content

Commit

Permalink
Merge pull request #612 from StarkZhidian/main
Browse files Browse the repository at this point in the history
--bugfix=Fix SymbolPairMatch#matchBestPair return incorrect result when commitText is multi characters
  • Loading branch information
Rosemoe authored Sep 13, 2024
2 parents 1703c04 + 05c253e commit 2faca82
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -1889,7 +1889,7 @@ public void commitText(CharSequence text, boolean applyAutoIndent) {

pair = languageSymbolPairs.matchBestPair(
this, cursor.left(),
inputText, firstCharFromText
inputText, endCharFromText
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
package io.github.rosemoe.sora.widget;

import android.text.TextUtils;

import androidx.annotation.Nullable;

import java.util.ArrayList;
Expand Down Expand Up @@ -125,10 +127,10 @@ public final List<SymbolPair> 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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
}
}

Expand Down

0 comments on commit 2faca82

Please sign in to comment.