chore: sort agent /list-directory output (#17218)

This sorts the `contents` list alphabetically, but with directories before everything else.
This is purely for UX on the Coder Desktop side, where the user only really cares about directories, and files are just for providing context in the file picker.
This commit is contained in:
Ethan
2025-04-03 12:31:46 +11:00
committed by GitHub
parent 5979c3224d
commit 998724de91
2 changed files with 19 additions and 6 deletions
+12
View File
@@ -7,6 +7,7 @@ import (
"path/filepath"
"regexp"
"runtime"
"slices"
"strings"
"github.com/shirou/gopsutil/v4/disk"
@@ -103,6 +104,17 @@ func listFiles(query LSRequest) (LSResponse, error) {
})
}
// Sort alphabetically: directories then files
slices.SortFunc(respContents, func(a, b LSFile) int {
if a.IsDir && !b.IsDir {
return -1
}
if !a.IsDir && b.IsDir {
return 1
}
return strings.Compare(a.Name, b.Name)
})
absolutePath := pathToArray(absolutePathString)
return LSResponse{
+7 -6
View File
@@ -137,17 +137,18 @@ func TestListFilesSuccess(t *testing.T) {
require.NoError(t, err)
require.Equal(t, tmpDir, resp.AbsolutePathString)
require.ElementsMatch(t, []LSFile{
{
Name: "repos",
AbsolutePathString: reposDir,
IsDir: true,
},
// Output is sorted
require.Equal(t, []LSFile{
{
Name: "Downloads",
AbsolutePathString: downloadsDir,
IsDir: true,
},
{
Name: "repos",
AbsolutePathString: reposDir,
IsDir: true,
},
{
Name: "file.txt",
AbsolutePathString: textFile,