refactor(editor): dep inject directory contents listing

tauraamui created

Change summary

internal/fsext/ls.go                          | 11 +++++++++++
internal/tui/components/chat/editor/editor.go | 10 +++++++---
2 files changed, 18 insertions(+), 3 deletions(-)

Detailed changes

internal/fsext/ls.go 🔗

@@ -198,6 +198,17 @@ func (dl *directoryLister) getIgnore(path string) ignore.IgnoreParser {
 	})
 }
 
+type DirectoryLister func(initialPath string, ignorePatterns []string, limit int) ([]string, bool, error)
+type DirectoryListerResolver func() DirectoryLister
+
+func ResolveDirectoryLister() DirectoryLister {
+	return listDirectory
+}
+
+func listDirectory(initialPath string, ignorePatterns []string, limit int) ([]string, bool, error) {
+	return ListDirectory(initialPath, ignorePatterns, limit)
+}
+
 // ListDirectory lists files and directories in the specified path,
 func ListDirectory(initialPath string, ignorePatterns []string, limit int) ([]string, bool, error) {
 	var results []string

internal/tui/components/chat/editor/editor.go 🔗

@@ -63,6 +63,9 @@ type editorCmp struct {
 
 	keyMap EditorKeyMap
 
+	// injected file dir lister
+	listDirResolver fsext.DirectoryListerResolver
+
 	// File path completions
 	currentQuery          string
 	completionsStartIndex int
@@ -524,7 +527,7 @@ func (m *editorCmp) SetPosition(x, y int) tea.Cmd {
 }
 
 func (m *editorCmp) startCompletions() tea.Msg {
-	files, _, _ := fsext.ListDirectory(".", nil, 0)
+	files, _, _ := m.listDirResolver()(".", nil, 0)
 	slices.Sort(files)
 	completionItems := make([]completions.Completion, 0, len(files))
 	for _, file := range files {
@@ -618,12 +621,13 @@ func newTextArea() *textarea.Model {
 	return ta
 }
 
-func newEditor(app *app.App) *editorCmp {
+func newEditor(app *app.App, resolveDirLister fsext.DirectoryListerResolver) *editorCmp {
 	e := editorCmp{
 		// TODO: remove the app instance from here
 		app:      app,
 		textarea: newTextArea(),
 		keyMap:   DefaultEditorKeyMap(),
+		listDirResolver: resolveDirLister,
 	}
 	e.setEditorPrompt()
 
@@ -634,5 +638,5 @@ func newEditor(app *app.App) *editorCmp {
 }
 
 func New(app *app.App) Editor {
-	return newEditor(app)
+	return newEditor(app, fsext.ResolveDirectoryLister)
 }