about summary refs log tree commit diff
diff options
context:
space:
mode:
authorChenyu Huang <itschenyu@gmail.com>2025-07-24 15:58:24 -0700
committerChenyu Huang <itschenyu@gmail.com>2025-07-24 15:58:24 -0700
commitead6862fc64597fdf811e0fb88e12ef0409b8d11 (patch)
tree7f2f0a42b540e105234e2df6cfc773fd014a7e7c
parentfc2fae42bd8d90fd15b06c14a781efb79bc14e1d (diff)
downloadvoidsky-ead6862fc64597fdf811e0fb88e12ef0409b8d11.tar.zst
fix some auto complete issues
-rw-r--r--__tests__/lib/strings/mention-manip.test.ts2
-rw-r--r--src/lib/strings/mention-manip.ts2
-rw-r--r--src/view/com/composer/text-input/TextInput.tsx9
3 files changed, 8 insertions, 5 deletions
diff --git a/__tests__/lib/strings/mention-manip.test.ts b/__tests__/lib/strings/mention-manip.test.ts
index f9075763e..ece69e195 100644
--- a/__tests__/lib/strings/mention-manip.test.ts
+++ b/__tests__/lib/strings/mention-manip.test.ts
@@ -32,6 +32,7 @@ describe('getMentionAt', () => {
     ['@alice hello', 7, undefined],
     ['alice@alice', 0, undefined],
     ['alice@alice', 6, undefined],
+    ['hello @alice-com goodbye', 8, 'alice-com'],
   ]
 
   it.each(cases)(
@@ -72,6 +73,7 @@ describe('insertMentionAt', () => {
     ['@alice hello', 7, '@alice hello'],
     ['alice@alice', 0, 'alice@alice'],
     ['alice@alice', 6, 'alice@alice'],
+    ['hello @alice-com goodbye', 10, 'hello @alice.com  goodbye'],
   ]
 
   it.each(cases)(
diff --git a/src/lib/strings/mention-manip.ts b/src/lib/strings/mention-manip.ts
index 1f7cbe434..7b52f745b 100644
--- a/src/lib/strings/mention-manip.ts
+++ b/src/lib/strings/mention-manip.ts
@@ -7,7 +7,7 @@ export function getMentionAt(
   text: string,
   cursorPos: number,
 ): FoundMention | undefined {
-  let re = /(^|\s)@([a-z0-9.]*)/gi
+  let re = /(^|\s)@([a-z0-9.-]*)/gi
   let match
   while ((match = re.exec(text))) {
     const spaceOffset = match[1].length
diff --git a/src/view/com/composer/text-input/TextInput.tsx b/src/view/com/composer/text-input/TextInput.tsx
index e9c5d86b9..ea92d0b91 100644
--- a/src/view/com/composer/text-input/TextInput.tsx
+++ b/src/view/com/composer/text-input/TextInput.tsx
@@ -96,10 +96,11 @@ export const TextInput = forwardRef(function TextInputImpl(
       newRt.detectFacetsWithoutResolution()
       setRichText(newRt)
 
-      const prefix = getMentionAt(
-        newText,
-        textInputSelection.current?.start || 0,
-      )
+      // NOTE: BinaryFiddler
+      // onChangeText happens before onSelectionChange, cursorPos is out of bound if the user deletes characters,
+      const cursorPos = textInputSelection.current?.start ?? 0
+      const prefix = getMentionAt(newText, Math.min(cursorPos, newText.length))
+
       if (prefix) {
         setAutocompletePrefix(prefix.value)
       } else if (autocompletePrefix) {