Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
Rosemoe committed Oct 12, 2024
2 parents efcf4bc + 2faca82 commit 4400e32
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 36 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 @@ -1888,18 +1888,16 @@ public void commitText(CharSequence text, boolean applyAutoIndent) {
}

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

var cur = cursor;
var editorText = this.text;
var quoteHandler = editorLanguage.getQuickQuoteHandler();

if (pair != null && pair != SymbolPairMatch.SymbolPair.EMPTY_SYMBOL_PAIR
&& (pair.shouldReplace(this))
) {
if (pair != null && pair != SymbolPairMatch.SymbolPair.EMPTY_SYMBOL_PAIR) {

// QuickQuoteHandler can easily implement the feature of AutoSurround
// and is at a higher level (customizable),
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 @@ -89,7 +91,7 @@ public void putPair(char[] charArray, SymbolPair symbolPair) {
}

list.add(symbolPair);
multipleCharByEndPairMaps.put(charArray[charArray.length - 1], list);
multipleCharByEndPairMaps.put(endChar, list);
}

/**
Expand Down Expand Up @@ -125,9 +127,10 @@ public final List<SymbolPair> matchBestPairList(char editChar) {
}

@Nullable
public final SymbolPair matchBestPair(Content content, 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 @@ -136,17 +139,20 @@ public final SymbolPair matchBestPair(Content content, CharPosition cursorPositi
}

// 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) {
if (!pair.shouldReplace(editor)) {
continue;
}
var openCharArray = pair.open.toCharArray();

// if flag is not 1, no match
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 @@ -174,19 +180,17 @@ public final SymbolPair matchBestPair(Content content, CharPosition cursorPositi
matchFlag &= inputCharArray[charIndex] == openCharArray[pairIndex] ? 1 : 0;
}

// input text and symbol pair text equal
if (pairIndex == 0) {
continue;
}

// 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--;
// 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--;

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
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,6 @@ public void updatePair() {
var newPair = new AutoClosingPairConditional(surroundingPair.open, surroundingPair.close,
surroundingPairFlagWithList);

var mergePairIndex = mergePairs.indexOf(newPair);

if (mergePairIndex >= 0) {
var mergePair = mergePairs.get(mergePairIndex);

if (mergePair.notIn == null || mergePair.notIn.isEmpty()) {
mergePairs.add(newPair);
continue;
}

mergePair.notIn.add(surroundingPairFlag);

}
mergePairs.add(newPair);
}
}
Expand Down Expand Up @@ -172,6 +159,10 @@ public boolean shouldReplace(CodeEditor editor, ContentLine contentLine, int lef
if (editor.getCursor().isSelected()) {
return isSurroundingPair;
}
// No text was selected,so should not complete surrounding pair
if (isSurroundingPair) {
return false;
}

if (notInTokenTypeArray == null) {
return true;
Expand Down

0 comments on commit 4400e32

Please sign in to comment.