@@ -13,6 +13,7 @@ import { useData } from "../../context/data"
1313import { getScrollAcceleration } from "../../util/scroll"
1414import { useTuiPaths } from "../../context/runtime"
1515import { useTuiConfig } from "../../config"
16+ import { useLocation } from "../../context/location"
1617import { useTheme , selectedForeground } from "../../context/theme"
1718import { SplitBorder } from "../../ui/border"
1819import { useTerminalDimensions } from "@opentui/solid"
@@ -21,6 +22,7 @@ import type { PromptInfo } from "../../prompt/history"
2122import { useFrecency } from "../../prompt/frecency"
2223import { useBindings , useCommandSlashes , useOpencodeModeStack } from "../../keymap"
2324import { displayCharAt , mentionTriggerIndex } from "../../prompt/display"
25+ import type { FileSystemEntry } from "@opencode-ai/sdk/v2"
2426
2527function removeLineRange ( input : string ) {
2628 const hashIndex = input . lastIndexOf ( "#" )
@@ -94,6 +96,7 @@ export function Autocomplete(props: {
9496 const frecency = useFrecency ( )
9597 const tuiConfig = useTuiConfig ( )
9698 const paths = useTuiPaths ( )
99+ const location = useLocation ( )
97100 const [ store , setStore ] = createStore ( {
98101 index : 0 ,
99102 selected : 0 ,
@@ -236,16 +239,18 @@ export function Autocomplete(props: {
236239 }
237240 }
238241
239- function createFilePart ( item : string , lineRange ?: { startLine : number ; endLine ?: number } ) {
240- const baseDir = ( sync . path . directory || paths . cwd ) . replace ( / \/ + $ / , "" )
241- const fullPath = path . isAbsolute ( item ) ? item : path . join ( baseDir , item )
242- const urlObj = pathToFileURL ( fullPath )
242+ function createFilePart (
243+ item : FileSystemEntry ,
244+ filePath : string ,
245+ lineRange ?: { startLine : number ; endLine ?: number } ,
246+ ) {
247+ const urlObj = pathToFileURL ( filePath )
243248 const filename =
244- lineRange && ! item . endsWith ( "/" )
245- ? `${ item } #${ lineRange . startLine } ${ lineRange . endLine ? `-${ lineRange . endLine } ` : "" } `
246- : item
249+ lineRange && item . type !== "directory"
250+ ? `${ item . path } #${ lineRange . startLine } ${ lineRange . endLine ? `-${ lineRange . endLine } ` : "" } `
251+ : item . path
247252
248- if ( lineRange && ! item . endsWith ( "/" ) ) {
253+ if ( lineRange && item . type !== "directory" ) {
249254 urlObj . searchParams . set ( "start" , String ( lineRange . startLine ) )
250255 if ( lineRange . endLine !== undefined ) {
251256 urlObj . searchParams . set ( "end" , String ( lineRange . endLine ) )
@@ -254,10 +259,9 @@ export function Autocomplete(props: {
254259
255260 return {
256261 filename,
257- url : urlObj . href ,
258262 part : {
259263 type : "file" as const ,
260- mime : "text/plain" ,
264+ mime : item . mime ,
261265 filename,
262266 url : urlObj . href ,
263267 source : {
@@ -267,7 +271,7 @@ export function Autocomplete(props: {
267271 end : 0 ,
268272 value : "" ,
269273 } ,
270- path : item ,
274+ path : item . path ,
271275 } ,
272276 } ,
273277 }
@@ -284,7 +288,7 @@ export function Autocomplete(props: {
284288 } )
285289
286290 function normalizeMentionPath ( filePath : string ) {
287- const baseDir = sync . path . directory || paths . cwd
291+ const baseDir = location ( ) ?. directory || sync . path . directory || paths . cwd
288292 const absolute = path . resolve ( filePath )
289293 const relative = path . relative ( baseDir , absolute )
290294
@@ -301,7 +305,11 @@ export function Autocomplete(props: {
301305 startLine : input . lineStart ,
302306 endLine : input . lineEnd > input . lineStart ? input . lineEnd : undefined ,
303307 }
304- const { filename, part } = createFilePart ( item , lineRange )
308+ const { filename, part } = createFilePart (
309+ { path : item , type : "file" , mime : "text/plain" } ,
310+ input . filePath ,
311+ lineRange ,
312+ )
305313 const index = store . visible === "@" ? store . index : props . input ( ) . cursorOffset
306314
307315 setStore ( "visible" , false )
@@ -310,17 +318,20 @@ export function Autocomplete(props: {
310318 }
311319
312320 const [ files ] = createResource (
313- ( ) => search ( ) ,
314- async ( query ) => {
321+ ( ) => ( { query : search ( ) , location : location ( ) } ) ,
322+ async ( input ) => {
315323 if ( ! store . visible || store . visible === "/" ) return [ ]
316324 if ( referenceMatch ( ) ) return [ ]
317- const { lineRange, baseQuery } = extractLineRange ( query ?? "" )
325+ const { lineRange, baseQuery } = extractLineRange ( input . query ?? "" )
318326
319327 // Get files from SDK
320328 const result = await sdk . client . v2 . fs . find ( {
321329 query : baseQuery ,
322330 limit : "20" ,
323- location : { workspace : project . workspace . current ( ) } ,
331+ location : {
332+ directory : input . location ?. directory ,
333+ workspace : input . location ?. workspaceID ?? project . workspace . current ( ) ,
334+ } ,
324335 } )
325336
326337 const options : AutocompleteOption [ ] = [ ]
@@ -331,7 +342,11 @@ export function Autocomplete(props: {
331342 const width = props . anchor ( ) . width - 4
332343 options . push (
333344 ...result . data . data . map ( ( item ) : AutocompleteOption => {
334- const { filename, url, part } = createFilePart ( item . path , lineRange )
345+ const { filename, part } = createFilePart (
346+ item ,
347+ path . join ( result . data . location . directory , item . path ) ,
348+ lineRange ,
349+ )
335350 return {
336351 display : Locale . truncateMiddle ( filename , width ) ,
337352 value : filename ,
0 commit comments