on main help sign in
fix(ui): completions: load files asynchronously
6a57463Ayman Bagabas
internal/ui/completions/completions.go+11−4
@@ -29,6 +29,11 @@ type SelectionMsg struct {
29// ClosedMsg is sent when the completions are closed.
29// ClosedMsg is sent when the completions are closed.
30type ClosedMsg struct{}
30type ClosedMsg struct{}
31
31
32+// FilesLoadedMsg is sent when files have been loaded for completions.
33+type FilesLoadedMsg struct {
34+ Files []string
35+}
36+
32// Completions represents the completions popup component.
37// Completions represents the completions popup component.
33type Completions struct {
38type Completions struct {
34 // Popup dimensions
39 // Popup dimensions
@@ -88,10 +93,12 @@ func (c *Completions) KeyMap() KeyMap {
88}
93}
89
94
90// OpenWithFiles opens the completions with file items from the filesystem.
95// OpenWithFiles opens the completions with file items from the filesystem.
91−func (c *Completions) OpenWithFiles(depth, limit int) {
96+func (c *Completions) OpenWithFiles(depth, limit int) tea.Cmd {
97+ return func() tea.Msg {
92− files, _, _ := fsext.ListDirectory(".", nil, depth, limit)
98+ files, _, _ := fsext.ListDirectory(".", nil, depth, limit)
93− slices.Sort(files)
99+ slices.Sort(files)
94− c.SetFiles(files)
100+ return FilesLoadedMsg{Files: files}
101+ }
95}
102}
96
103
97// SetFiles sets the file items on the completions popup.
104// SetFiles sets the file items on the completions popup.
internal/ui/model/ui.go+6−1
@@ -371,6 +371,11 @@ func (m *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
371 cmds = append(cmds, clearInfoMsgCmd(ttl))
371 cmds = append(cmds, clearInfoMsgCmd(ttl))
372 case uiutil.ClearStatusMsg:
372 case uiutil.ClearStatusMsg:
373 m.status.ClearInfoMsg()
373 m.status.ClearInfoMsg()
374+ case completions.FilesLoadedMsg:
375+ // Handle async file loading for completions.
376+ if m.completionsOpen {
377+ m.completions.SetFiles(msg.Files)
378+ }
374 }
379 }
375
380
376 // This logic gets triggered on any message type, but should it?
381 // This logic gets triggered on any message type, but should it?
@@ -878,7 +883,7 @@ func (m *UI) handleKeyPressMsg(msg tea.KeyPressMsg) tea.Cmd {
878 m.completionsStartIndex = curIdx
883 m.completionsStartIndex = curIdx
879 m.completionsPositionStart = m.completionsPosition()
884 m.completionsPositionStart = m.completionsPosition()
880 depth, limit := m.com.Config().Options.TUI.Completions.Limits()
885 depth, limit := m.com.Config().Options.TUI.Completions.Limits()
881− m.completions.OpenWithFiles(depth, limit)
886+ cmds = append(cmds, m.completions.OpenWithFiles(depth, limit))
882 }
887 }
883 }
888 }
884
889