Skip to content

Commit

Permalink
fix: common suffix length calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
aswin-s committed Jan 23, 2024
1 parent 2cc4858 commit f7a19ca
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/libs/ComposerUtils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ function canSkipTriggerHotkeys(isSmallScreenWidth: boolean, isKeyboardShown: boo
* Finds the length of common suffix between two texts
* @param str1 - first string to compare
* @param str2 - next string to compare
* @param cursorPosition - position of cursor
* @returns number - Length of the common suffix
*/
function findCommonSuffixLength(str1: string, str2: string) {
function findCommonSuffixLength(str1: string, str2: string, cursorPosition: number) {
let commonSuffixLength = 0;
const minLength = Math.min(str1.length, str2.length);
const minLength = Math.min(str1.length - cursorPosition, str2.length);

for (let i = 1; i <= minLength; i++) {
if (str1.charAt(str1.length - i) === str2.charAt(str2.length - i)) {
commonSuffixLength++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ function ComposerWithSuggestions({

if (currentIndex < newText.length) {
startIndex = currentIndex;
const commonSuffixLength = ComposerUtils.findCommonSuffixLength(prevText, newText);
const commonSuffixLength = ComposerUtils.findCommonSuffixLength(prevText, newText, selection.end);
// if text is getting pasted over find length of common suffix and subtract it from new text length
if (commonSuffixLength > 0 || selection.end - selection.start > 0) {
endIndex = newText.length - commonSuffixLength;
Expand All @@ -228,7 +228,7 @@ function ComposerWithSuggestions({
diff: newText.substring(startIndex, endIndex),
};
},
[selection.end, selection.start],
[selection.start, selection.end],
);

/**
Expand Down

0 comments on commit f7a19ca

Please sign in to comment.