fix(ui): completions: simplify completions popup message handling

Ayman Bagabas created

Change summary

internal/ui/completions/completions.go | 33 +++++++++------------------
internal/ui/model/ui.go                | 32 +++++++++++---------------
2 files changed, 25 insertions(+), 40 deletions(-)

Detailed changes

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,
 	}
 }
 

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)
 					}
 				}