Skip to content

Commit 8fcf878

Browse files
committed
fix(tui): resolve display name issue for explicit directory paths in autocomplete
1 parent 012eb5b commit 8fcf878

1 file changed

Lines changed: 70 additions & 11 deletions

File tree

‎packages/tui/src/component/prompt/autocomplete.tsx‎

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -320,15 +320,52 @@ export function Autocomplete(props: {
320320
if(referenceMatch())return[]
321321
const{ lineRange, baseQuery }=extractLineRange(input.query??"")
322322

323+
constisExplicitPath=baseQuery.startsWith(".")||baseQuery.startsWith("/")||baseQuery.startsWith("~")
324+
letsearchDir=input.location?.directory||project.data.project.mainDir||project.instance.directory()||process.cwd()
325+
letsearchQuery=baseQuery
326+
327+
if(searchDir&&isExplicitPath){
328+
letexpanded=baseQuery
329+
if(expanded.startsWith("~/")){
330+
expanded=expanded.replace(/^~/,process.env.HOME||"")
331+
}
332+
333+
constresolvedPath=path.resolve(searchDir,expanded)
334+
335+
if(baseQuery.endsWith("/")||baseQuery==="."||baseQuery===".."){
336+
searchDir=resolvedPath
337+
searchQuery=""
338+
}else{
339+
searchDir=path.dirname(resolvedPath)
340+
searchQuery=path.basename(resolvedPath)
341+
}
342+
}
343+
323344
// Get files from SDK
324-
constresult=awaitsdk.client.v2.fs.find({
325-
query: baseQuery,
326-
limit: "20",
327-
location: {
328-
directory: input.location?.directory,
329-
workspace: input.location?.workspaceID??project.workspace.current(),
330-
},
331-
})
345+
letresult
346+
if(isExplicitPath){
347+
result=awaitsdk.client.v2.fs.list({
348+
location: {
349+
directory: searchDir,
350+
workspace: input.location?.workspaceID??project.workspace.current(),
351+
},
352+
})
353+
354+
// v2.fs.list returns all entries, so we filter by searchQuery
355+
if(!result.error&&result.data&&searchQuery){
356+
constlowerQuery=searchQuery.toLowerCase()
357+
result.data.data=result.data.data.filter((item)=>item.path.toLowerCase().includes(lowerQuery))
358+
}
359+
}else{
360+
result=awaitsdk.client.v2.fs.find({
361+
query: searchQuery,
362+
limit: "20",
363+
location: {
364+
directory: searchDir,
365+
workspace: input.location?.workspaceID??project.workspace.current(),
366+
},
367+
})
368+
}
332369

333370
constoptions: AutocompleteOption[]=[]
334371

@@ -338,16 +375,38 @@ export function Autocomplete(props: {
338375
constwidth=props.anchor().width-4
339376
options.push(
340377
...result.data.data.map((item): AutocompleteOption=>{
378+
constabsolutePath=path.join(result.data.location.directory,item.path)
379+
letdisplayPath
380+
if(baseQuery.startsWith("/")){
381+
displayPath=absolutePath
382+
}elseif(baseQuery.startsWith("~")){
383+
consthome=process.env.HOME||""
384+
if(home&&absolutePath.startsWith(home)){
385+
displayPath=`~${absolutePath.slice(home.length)}`
386+
}else{
387+
displayPath=absolutePath
388+
}
389+
}else{
390+
constbaseDir=input.location?.directory||project.data.project.mainDir||project.instance.directory()||process.cwd()
391+
displayPath=path.relative(baseDir,absolutePath)
392+
393+
if(isExplicitPath&&baseQuery.startsWith("./")&&!displayPath.startsWith(".")){
394+
displayPath=`./${displayPath}`
395+
}
396+
}
397+
398+
constdisplayItem={ ...item,path: displayPath||"."}
399+
341400
const{ filename, part }=createFilePart(
342-
item,
343-
path.join(result.data.location.directory,item.path),
401+
displayItem,
402+
absolutePath,
344403
lineRange,
345404
)
346405
return{
347406
display: Locale.truncateMiddle(filename,width),
348407
value: filename,
349408
isDirectory: item.type==="directory",
350-
path: item.path,
409+
path: displayPath||".",
351410
onSelect: ()=>{
352411
insertPart(filename,part)
353412
},

0 commit comments

Comments
 (0)