From 6c0573915f184d935786ef8ecdfc18571f6dc0d9 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Thu, 8 Jan 2026 10:34:49 -0500 Subject: [PATCH] fix(ui): completions: simplify completions popup message handling --- internal/ui/completions/completions.go | 33 +++++++++----------------- internal/ui/model/ui.go | 32 +++++++++++-------------- 2 files changed, 25 insertions(+), 40 deletions(-) diff --git a/internal/ui/completions/completions.go b/internal/ui/completions/completions.go index 4e5f9f64d46512c29b69ea004a772d3ce89c3777..7e854a6b55ef99691e30ba3566074af3cba2982d 100644 --- a/internal/ui/completions/completions.go +++ b/internal/ui/completions/completions.go @@ -29,11 +29,6 @@ type SelectionMsg struct { // ClosedMsg is sent when the completions are closed. type ClosedMsg struct{} -// FilesLoadedMsg is sent when files have been loaded for completions. -type FilesLoadedMsg struct { - Files []string -} - // Completions represents the completions popup component. type Completions struct { // Popup dimensions @@ -93,12 +88,10 @@ func (c *Completions) KeyMap() KeyMap { } // OpenWithFiles opens the completions with file items from the filesystem. -func (c *Completions) OpenWithFiles(depth, limit int) tea.Cmd { - return func() tea.Msg { - files, _, _ := fsext.ListDirectory(".", nil, depth, limit) - slices.Sort(files) - return FilesLoadedMsg{Files: files} - } +func (c *Completions) OpenWithFiles(depth, limit int) { + files, _, _ := fsext.ListDirectory(".", nil, depth, limit) + slices.Sort(files) + c.SetFiles(files) } // SetFiles sets the file items on the completions popup. @@ -133,11 +126,9 @@ func (c *Completions) SetFiles(files []string) { } // Close closes the completions popup. -func (c *Completions) Close() tea.Cmd { +func (c *Completions) Close() tea.Msg { c.open = false - return func() tea.Msg { - return ClosedMsg{} - } + return ClosedMsg{} } // Filter filters the completions with the given query. @@ -171,7 +162,7 @@ func (c *Completions) HasItems() bool { } // Update handles key events for the completions. -func (c *Completions) Update(msg tea.KeyPressMsg) (tea.Cmd, bool) { +func (c *Completions) Update(msg tea.KeyPressMsg) (tea.Msg, bool) { if !c.open { return nil, false } @@ -228,7 +219,7 @@ func (c *Completions) selectNext() { } // selectCurrent returns a command with the currently selected item. -func (c *Completions) selectCurrent(insert bool) tea.Cmd { +func (c *Completions) selectCurrent(insert bool) tea.Msg { items := c.list.VisibleItems() if len(items) == 0 { return nil @@ -248,11 +239,9 @@ func (c *Completions) selectCurrent(insert bool) tea.Cmd { c.open = false } - return func() tea.Msg { - return SelectionMsg{ - Value: item.Value(), - Insert: insert, - } + return SelectionMsg{ + Value: item.Value(), + Insert: insert, } } diff --git a/internal/ui/model/ui.go b/internal/ui/model/ui.go index 54f453c3ec90b05721bb4c2ab8ab5442d8da6655..f49f8bbdcc455caf6f2228938a1d95e1e24c8c21 100644 --- a/internal/ui/model/ui.go +++ b/internal/ui/model/ui.go @@ -351,21 +351,6 @@ func (m *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) { cmds = append(cmds, cmd) } } - case completions.SelectionMsg: - // Handle file completion selection. - if item, ok := msg.Value.(completions.FileCompletionValue); ok { - m.insertFileCompletion(item.Path) - } - if !msg.Insert { - m.closeCompletions() - } - case completions.FilesLoadedMsg: - // Handle async file loading for completions. - if m.completionsOpen { - m.completions.SetFiles(msg.Files) - } - case completions.ClosedMsg: - m.completionsOpen = false case tea.KeyPressMsg: if cmd := m.handleKeyPressMsg(msg); cmd != nil { cmds = append(cmds, cmd) @@ -808,8 +793,19 @@ func (m *UI) handleKeyPressMsg(msg tea.KeyPressMsg) tea.Cmd { case uiFocusEditor: // Handle completions if open. if m.completionsOpen { - if cmd, ok := m.completions.Update(msg); ok { - cmds = append(cmds, cmd) + if msg, ok := m.completions.Update(msg); ok { + switch msg := msg.(type) { + case completions.SelectionMsg: + // Handle file completion selection. + if item, ok := msg.Value.(completions.FileCompletionValue); ok { + m.insertFileCompletion(item.Path) + } + if !msg.Insert { + m.closeCompletions() + } + case completions.ClosedMsg: + m.completionsOpen = false + } return tea.Batch(cmds...) } } @@ -882,7 +878,7 @@ func (m *UI) handleKeyPressMsg(msg tea.KeyPressMsg) tea.Cmd { m.completionsStartIndex = curIdx m.completionsPositionStart = m.completionsPosition() depth, limit := m.com.Config().Options.TUI.Completions.Limits() - cmds = append(cmds, m.completions.OpenWithFiles(depth, limit)) + m.completions.OpenWithFiles(depth, limit) } }