on main help sign in
Merge pull request #134 from charmbracelet/comp-padding

fix(tui): editor: position completions correctly and account for padding

commit8ff604f908113224a0956558b6b5983a8c185cc9 authorKujtim Hoxha <kujtimii.h@gmail.com> dateMon 14 Jul 2025, 14:56 (1 year ago) parentfae5ac88807737
2 files changed+19−9
internal/tui/components/chat/editor/editor.go+3−2 internal/tui/components/completions/completions.go+16−7
internal/tui/components/chat/editor/editor.go+3−2
@@ -361,8 +361,9 @@ func (m *editorCmp) startCompletions() tea.Msg {
361361 })
362362 }
363363
364− x := m.textarea.Cursor().X + m.x + 1
365− y := m.textarea.Cursor().Y + m.y + 1
364+ cur := m.textarea.Cursor()
365+ x := cur.X + m.x // adjust for padding
366+ y := cur.Y + m.y + 1
366367 return completions.OpenCompletionsMsg{
367368 Completions: completionItems,
368369 X: x,
internal/tui/components/completions/completions.go+16−7
@@ -9,6 +9,8 @@ import (
99 "github.com/charmbracelet/lipgloss/v2"
1010)
1111
12+const maxCompletionsHeight = 10
13+
1214type Completion struct {
1315 Title string // The title of the completion item
1416 Value any // The value of the completion item
@@ -43,7 +45,7 @@ type Completions interface {
4345type completionsCmp struct {
4446 width int
4547 height int // Height of the completions component`
46− x int // X position for the completions popup\
48+ x int // X position for the completions popup
4749 y int // Y position for the completions popup
4850 open bool // Indicates if the completions are open
4951 keyMap KeyMap
@@ -150,18 +152,25 @@ func (c *completionsCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
150152 if !c.open {
151153 return c, nil // If completions are not open, do nothing
152154 }
153− cmd := c.list.Filter(msg.Query)
154− c.height = max(min(10, len(c.list.Items())), 1)
155− return c, tea.Batch(
156− cmd,
157− c.list.SetSize(c.width, c.height),
158− )
155+ var cmds []tea.Cmd
156+ cmds = append(cmds, c.list.Filter(msg.Query))
157+ itemsLen := len(c.list.Items())
158+ c.height = max(min(maxCompletionsHeight, itemsLen), 1)
159+ cmds = append(cmds, c.list.SetSize(c.width, c.height))
160+ if itemsLen == 0 {
161+ // Close completions if no items match the query
162+ cmds = append(cmds, util.CmdHandler(CloseCompletionsMsg{}))
163+ }
164+ return c, tea.Batch(cmds...)
159165 }
160166 return c, nil
161167}
162168
163169// View implements Completions.
164170func (c *completionsCmp) View() string {
171+ if !c.open {
172+ return ""
173+ }
165174 if len(c.list.Items()) == 0 {
166175 return c.style().Render("No completions found")
167176 }