on main help sign in
fix(ui): completions: simplify completions popup message handling
commit6c0573915f184d935786ef8ecdfc18571f6dc0d9 authorAyman Bagabas <ayman.bagabas@gmail.com> dateThu 8 Jan 2026, 10:34 (9 months ago) parentfb78b80
2 files changed+25−40
internal/ui/completions/completions.go+11−22 internal/ui/model/ui.go+14−18
internal/ui/completions/completions.go+11−22
@@ -29,11 +29,6 @@ type SelectionMsg struct {
2929// ClosedMsg is sent when the completions are closed.
3030type ClosedMsg struct{}
3131
32−// FilesLoadedMsg is sent when files have been loaded for completions.
33−type FilesLoadedMsg struct {
34− Files []string
35−}
36−
3732// Completions represents the completions popup component.
3833type Completions struct {
3934 // Popup dimensions
@@ -93,12 +88,10 @@ func (c *Completions) KeyMap() KeyMap {
9388}
9489
9590// OpenWithFiles opens the completions with file items from the filesystem.
96−func (c *Completions) OpenWithFiles(depth, limit int) tea.Cmd {
97− return func() tea.Msg {
98− files, _, _ := fsext.ListDirectory(".", nil, depth, limit)
99− slices.Sort(files)
100− return FilesLoadedMsg{Files: files}
101− }
91+func (c *Completions) OpenWithFiles(depth, limit int) {
92+ files, _, _ := fsext.ListDirectory(".", nil, depth, limit)
93+ slices.Sort(files)
94+ c.SetFiles(files)
10295}
10396
10497// SetFiles sets the file items on the completions popup.
@@ -133,11 +126,9 @@ func (c *Completions) SetFiles(files []string) {
133126}
134127
135128// Close closes the completions popup.
136−func (c *Completions) Close() tea.Cmd {
129+func (c *Completions) Close() tea.Msg {
137130 c.open = false
138− return func() tea.Msg {
139− return ClosedMsg{}
140− }
131+ return ClosedMsg{}
141132}
142133
143134// Filter filters the completions with the given query.
@@ -171,7 +162,7 @@ func (c *Completions) HasItems() bool {
171162}
172163
173164// Update handles key events for the completions.
174−func (c *Completions) Update(msg tea.KeyPressMsg) (tea.Cmd, bool) {
165+func (c *Completions) Update(msg tea.KeyPressMsg) (tea.Msg, bool) {
175166 if !c.open {
176167 return nil, false
177168 }
@@ -228,7 +219,7 @@ func (c *Completions) selectNext() {
228219}
229220
230221// selectCurrent returns a command with the currently selected item.
231−func (c *Completions) selectCurrent(insert bool) tea.Cmd {
222+func (c *Completions) selectCurrent(insert bool) tea.Msg {
232223 items := c.list.VisibleItems()
233224 if len(items) == 0 {
234225 return nil
@@ -248,11 +239,9 @@ func (c *Completions) selectCurrent(insert bool) tea.Cmd {
248239 c.open = false
249240 }
250241
251− return func() tea.Msg {
252− return SelectionMsg{
253− Value: item.Value(),
254− Insert: insert,
255− }
242+ return SelectionMsg{
243+ Value: item.Value(),
244+ Insert: insert,
256245 }
257246}
258247
internal/ui/model/ui.go+14−18
@@ -351,21 +351,6 @@ func (m *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
351351 cmds = append(cmds, cmd)
352352 }
353353 }
354− case completions.SelectionMsg:
355− // Handle file completion selection.
356− if item, ok := msg.Value.(completions.FileCompletionValue); ok {
357− m.insertFileCompletion(item.Path)
358− }
359− if !msg.Insert {
360− m.closeCompletions()
361− }
362− case completions.FilesLoadedMsg:
363− // Handle async file loading for completions.
364− if m.completionsOpen {
365− m.completions.SetFiles(msg.Files)
366− }
367− case completions.ClosedMsg:
368− m.completionsOpen = false
369354 case tea.KeyPressMsg:
370355 if cmd := m.handleKeyPressMsg(msg); cmd != nil {
371356 cmds = append(cmds, cmd)
@@ -808,8 +793,19 @@ func (m *UI) handleKeyPressMsg(msg tea.KeyPressMsg) tea.Cmd {
808793 case uiFocusEditor:
809794 // Handle completions if open.
810795 if m.completionsOpen {
811− if cmd, ok := m.completions.Update(msg); ok {
812− cmds = append(cmds, cmd)
796+ if msg, ok := m.completions.Update(msg); ok {
797+ switch msg := msg.(type) {
798+ case completions.SelectionMsg:
799+ // Handle file completion selection.
800+ if item, ok := msg.Value.(completions.FileCompletionValue); ok {
801+ m.insertFileCompletion(item.Path)
802+ }
803+ if !msg.Insert {
804+ m.closeCompletions()
805+ }
806+ case completions.ClosedMsg:
807+ m.completionsOpen = false
808+ }
813809 return tea.Batch(cmds...)
814810 }
815811 }
@@ -882,7 +878,7 @@ func (m *UI) handleKeyPressMsg(msg tea.KeyPressMsg) tea.Cmd {
882878 m.completionsStartIndex = curIdx
883879 m.completionsPositionStart = m.completionsPosition()
884880 depth, limit := m.com.Config().Options.TUI.Completions.Limits()
885− cmds = append(cmds, m.completions.OpenWithFiles(depth, limit))
881+ m.completions.OpenWithFiles(depth, limit)
886882 }
887883 }
888884