on main help sign in
fix(ui): completions: simplify completions popup message handling
6c05739Ayman Bagabas
internal/ui/completions/completions.go+11−22
@@ -29,11 +29,6 @@ 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−
37// Completions represents the completions popup component.
32// Completions represents the completions popup component.
38type Completions struct {
33type Completions struct {
39 // Popup dimensions
34 // Popup dimensions
@@ -93,12 +88,10 @@ func (c *Completions) KeyMap() KeyMap {
93}
88}
94
89
95// OpenWithFiles opens the completions with file items from the filesystem.
90// OpenWithFiles opens the completions with file items from the filesystem.
96−func (c *Completions) OpenWithFiles(depth, limit int) tea.Cmd {
91+func (c *Completions) OpenWithFiles(depth, limit int) {
97− return func() tea.Msg {
98− files, _, _ := fsext.ListDirectory(".", nil, depth, limit)
92+ files, _, _ := fsext.ListDirectory(".", nil, depth, limit)
99− slices.Sort(files)
93+ slices.Sort(files)
100− return FilesLoadedMsg{Files: files}
94+ c.SetFiles(files)
101− }
102}
95}
103
96
104// SetFiles sets the file items on the completions popup.
97// SetFiles sets the file items on the completions popup.
@@ -133,11 +126,9 @@ func (c *Completions) SetFiles(files []string) {
133}
126}
134
127
135// Close closes the completions popup.
128// Close closes the completions popup.
136−func (c *Completions) Close() tea.Cmd {
129+func (c *Completions) Close() tea.Msg {
137 c.open = false
130 c.open = false
138− return func() tea.Msg {
139− return ClosedMsg{}
131+ return ClosedMsg{}
140− }
141}
132}
142
133
143// Filter filters the completions with the given query.
134// Filter filters the completions with the given query.
@@ -171,7 +162,7 @@ func (c *Completions) HasItems() bool {
171}
162}
172
163
173// Update handles key events for the completions.
164// 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) {
175 if !c.open {
166 if !c.open {
176 return nil, false
167 return nil, false
177 }
168 }
@@ -228,7 +219,7 @@ func (c *Completions) selectNext() {
228}
219}
229
220
230// selectCurrent returns a command with the currently selected item.
221// 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 {
232 items := c.list.VisibleItems()
223 items := c.list.VisibleItems()
233 if len(items) == 0 {
224 if len(items) == 0 {
234 return nil
225 return nil
@@ -248,11 +239,9 @@ func (c *Completions) selectCurrent(insert bool) tea.Cmd {
248 c.open = false
239 c.open = false
249 }
240 }
250
241
251− return func() tea.Msg {
252− return SelectionMsg{
242+ return SelectionMsg{
253− Value: item.Value(),
243+ Value: item.Value(),
254− Insert: insert,
244+ Insert: insert,
255− }
256 }
245 }
257}
246}
258
247
internal/ui/model/ui.go+14−18
@@ -351,21 +351,6 @@ func (m *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
351 cmds = append(cmds, cmd)
351 cmds = append(cmds, cmd)
352 }
352 }
353 }
353 }
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
369 case tea.KeyPressMsg:
354 case tea.KeyPressMsg:
370 if cmd := m.handleKeyPressMsg(msg); cmd != nil {
355 if cmd := m.handleKeyPressMsg(msg); cmd != nil {
371 cmds = append(cmds, cmd)
356 cmds = append(cmds, cmd)
@@ -808,8 +793,19 @@ func (m *UI) handleKeyPressMsg(msg tea.KeyPressMsg) tea.Cmd {
808 case uiFocusEditor:
793 case uiFocusEditor:
809 // Handle completions if open.
794 // Handle completions if open.
810 if m.completionsOpen {
795 if m.completionsOpen {
811− if cmd, ok := m.completions.Update(msg); ok {
796+ if msg, ok := m.completions.Update(msg); ok {
812− cmds = append(cmds, cmd)
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+ }
813 return tea.Batch(cmds...)
809 return tea.Batch(cmds...)
814 }
810 }
815 }
811 }
@@ -882,7 +878,7 @@ func (m *UI) handleKeyPressMsg(msg tea.KeyPressMsg) tea.Cmd {
882 m.completionsStartIndex = curIdx
878 m.completionsStartIndex = curIdx
883 m.completionsPositionStart = m.completionsPosition()
879 m.completionsPositionStart = m.completionsPosition()
884 depth, limit := m.com.Config().Options.TUI.Completions.Limits()
880 depth, limit := m.com.Config().Options.TUI.Completions.Limits()
885− cmds = append(cmds, m.completions.OpenWithFiles(depth, limit))
881+ m.completions.OpenWithFiles(depth, limit)
886 }
882 }
887 }
883 }
888
884