@@ -14,7 +14,10 @@ import { createEffect, createMemo, createResource, createSignal, onCleanup, onMo
1414import * as Locale from "@/util/locale"
1515import {
1616 createPromptHistory ,
17+ displayCharAt ,
18+ displaySlice ,
1719 isExitCommand ,
20+ mentionTriggerIndex ,
1821 isNewCommand ,
1922 movePromptHistory ,
2023 promptCycle ,
@@ -537,7 +540,7 @@ export function createPromptState(input: PromptInput): PromptState {
537540 } )
538541 }
539542
540- const restore = ( value : RunPrompt , cursor = value . text . length ) => {
543+ const restore = ( value : RunPrompt , cursor = Bun . stringWidth ( value . text ) ) => {
541544 draft = clonePrompt ( value )
542545 if ( ! area || area . isDestroyed ) {
543546 return
@@ -546,7 +549,7 @@ export function createPromptState(input: PromptInput): PromptState {
546549 hide ( )
547550 area . setText ( value . text )
548551 restoreParts ( value . parts )
549- area . cursorOffset = Math . min ( cursor , area . plainText . length )
552+ area . cursorOffset = Math . min ( cursor , Bun . stringWidth ( area . plainText ) )
550553 scheduleRows ( )
551554 area . focus ( )
552555 }
@@ -577,7 +580,7 @@ export function createPromptState(input: PromptInput): PromptState {
577580 area . setText ( text )
578581 clearParts ( )
579582 draft = { text : area . plainText , parts : [ ] }
580- area . cursorOffset = Math . min ( text . length , area . plainText . length )
583+ area . cursorOffset = Math . min ( Bun . stringWidth ( text ) , Bun . stringWidth ( area . plainText ) )
581584 scheduleRows ( )
582585 area . focus ( )
583586 }
@@ -610,32 +613,26 @@ export function createPromptState(input: PromptInput): PromptState {
610613 }
611614
612615 if ( visible ( ) && mode ( ) === "mention" ) {
613- if ( cursor <= at ( ) || / \s / . test ( text . slice ( at ( ) , cursor ) ) ) {
616+ const query = displaySlice ( text , at ( ) , cursor )
617+ if ( cursor <= at ( ) || / \s / . test ( query ) ) {
614618 hide ( )
615619 return
616620 }
617621
618- setQuery ( text . slice ( at ( ) + 1 , cursor ) )
622+ setQuery ( displaySlice ( text , at ( ) + 1 , cursor ) )
619623 return
620624 }
621625
622626 if ( cursor === 0 ) {
623627 return
624628 }
625629
626- const head = text . slice ( 0 , cursor )
627- const idx = head . lastIndexOf ( "@" )
628- if ( idx === - 1 ) {
629- return
630- }
631-
632- const before = idx === 0 ? undefined : head [ idx - 1 ]
633- const tail = head . slice ( idx )
634- if ( ( before === undefined || / \s / . test ( before ) ) && ! / \s / . test ( tail ) ) {
630+ const idx = mentionTriggerIndex ( text , cursor )
631+ if ( idx !== undefined ) {
635632 setAt ( idx )
636633 menu . reset ( )
637634 setMode ( "mention" )
638- setQuery ( head . slice ( idx + 1 ) )
635+ setQuery ( displaySlice ( text , idx + 1 , cursor ) )
639636 }
640637 }
641638
@@ -782,7 +779,7 @@ export function createPromptState(input: PromptInput): PromptState {
782779 }
783780
784781 const cursor = area . cursorOffset
785- const tail = area . plainText . at ( cursor )
782+ const tail = displayCharAt ( area . plainText , cursor )
786783 const append = "@" + next . value + ( tail === " " ? "" : " " )
787784 area . cursorOffset = at ( )
788785 const start = area . logicalCursor
@@ -941,7 +938,8 @@ export function createPromptState(input: PromptInput): PromptState {
941938 }
942939
943940 const dir = up ? - 1 : 1
944- if ( ( dir === - 1 && area . cursorOffset === 0 ) || ( dir === 1 && area . cursorOffset === area . plainText . length ) ) {
941+ const endOffset = Bun . stringWidth ( area . plainText )
942+ if ( ( dir === - 1 && area . cursorOffset === 0 ) || ( dir === 1 && area . cursorOffset === endOffset ) ) {
945943 move ( dir , event )
946944 return
947945 }
@@ -955,7 +953,7 @@ export function createPromptState(input: PromptInput): PromptState {
955953 ? area . height - 1
956954 : Math . max ( 0 , ( area . virtualLineCount ?? 1 ) - 1 )
957955 if ( dir === 1 && area . visualCursor . visualRow === end ) {
958- area . cursorOffset = area . plainText . length
956+ area . cursorOffset = endOffset
959957 }
960958 }
961959
0 commit comments