@@ -4,6 +4,8 @@ export interface AutocompleteState {
44 atPosition : number ;
55}
66
7+ export type ReferenceType = 'file' | 'tool' ;
8+
79export function shouldShowAutocomplete ( text : string , cursorPos : number ) : AutocompleteState {
810 const textBeforeCursor = text . slice ( 0 , cursorPos ) ;
911 const lastAtIndex = textBeforeCursor . lastIndexOf ( '@' ) ;
@@ -34,6 +36,10 @@ export function extractSearchQuery(text: string, cursorPos: number): string {
3436 return state . searchQuery ;
3537}
3638
39+ export function detectReferenceType ( searchQuery : string ) : ReferenceType {
40+ return / [ \/ \. ] / . test ( searchQuery ) ? 'file' : 'tool' ;
41+ }
42+
3743function getTextWidth ( text : string , font : string ) : number {
3844 const canvas = document . createElement ( 'canvas' ) ;
3945 const context = canvas . getContext ( '2d' ) ;
@@ -94,3 +100,22 @@ export function insertToolMention(
94100
95101 return { newText, newCursorPos } ;
96102}
103+
104+ export function insertFileReference (
105+ text : string ,
106+ cursorPos : number ,
107+ filePath : string ,
108+ ) : { newText : string ; newCursorPos : number } {
109+ const state = shouldShowAutocomplete ( text , cursorPos ) ;
110+
111+ if ( ! state . isOpen ) {
112+ return { newText : text , newCursorPos : cursorPos } ;
113+ }
114+
115+ const beforeAt = text . slice ( 0 , state . atPosition ) ;
116+ const afterCursor = text . slice ( cursorPos ) ;
117+ const newText = `${ beforeAt } @${ filePath } ${ afterCursor } ` ;
118+ const newCursorPos = state . atPosition + filePath . length + 2 ;
119+
120+ return { newText, newCursorPos } ;
121+ }
0 commit comments