@@ -51,18 +51,22 @@ type Completions interface {
5151}
5252
5353type completionsCmp struct {
54− width int
55− height int // Height of the completions component`
56− x int // X position for the completions popup
57− y int // Y position for the completions popup
58− open bool // Indicates if the completions are open
59− keyMap KeyMap
54+ wWidth int // The window width
55+ width int
56+ height int // Height of the completions component`
57+ x, xorig int // X position for the completions popup
58+ y int // Y position for the completions popup
59+ open bool // Indicates if the completions are open
60+ keyMap KeyMap
6061
6162 list list.ListModel
6263 query string // The current filter query
6364}
6465
65−const maxCompletionsWidth = 80 // Maximum width for the completions popup
66+const (
67+ maxCompletionsWidth = 80 // Maximum width for the completions popup
68+ minCompletionsWidth = 20 // Minimum width for the completions popup
69+)
6670
6771func New() Completions {
6872 completionsKeyMap := DefaultKeyMap()
@@ -102,6 +106,7 @@ func (c *completionsCmp) Init() tea.Cmd {
102106func (c *completionsCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
103107 switch msg := msg.(type) {
104108 case tea.WindowSizeMsg:
109+ c.wWidth = msg.Width
105110 c.width = min(listWidth(c.list.Items()), maxCompletionsWidth)
106111 c.height = min(msg.Height-c.y, 15)
107112 return c, nil
@@ -160,7 +165,7 @@ func (c *completionsCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
160165 case OpenCompletionsMsg:
161166 c.open = true
162167 c.query = ""
163− c.x = msg.X
168+ c.x, c.xorig = msg.X, msg.X
164169 c.y = msg.Y
165170 items := []util.Model{}
166171 t := styles.CurrentTheme()
@@ -168,7 +173,14 @@ func (c *completionsCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
168173 item := NewCompletionItem(completion.Title, completion.Value, WithBackgroundColor(t.BgSubtle))
169174 items = append(items, item)
170175 }
171− c.width = listWidth(msg.Completions)
176+ width := listWidth(items)
177+ if len(items) == 0 {
178+ width = listWidth(c.list.Items())
179+ }
180+ if c.x+width >= c.wWidth {
181+ c.x = c.wWidth - width - 1
182+ }
183+ c.width = max(width, c.wWidth-minCompletionsWidth-1)
172184 c.height = max(min(c.height, len(items)), 1) // Ensure at least 1 item height
173185 return c, tea.Batch(
174186 c.list.SetItems(items),
@@ -198,7 +210,13 @@ func (c *completionsCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
198210 cmds = append(cmds, c.list.Filter(msg.Query))
199211 items := c.list.Items()
200212 itemsLen := len(items)
201− c.width = listWidth(items)
213+ width := listWidth(items)
214+ if c.x < 0 {
215+ c.x = c.xorig
216+ } else if c.x+width >= c.wWidth {
217+ c.x = c.wWidth - width - 1
218+ }
219+ c.width = width
202220 c.height = max(min(maxCompletionsHeight, itemsLen), 1)
203221 cmds = append(cmds, c.list.SetSize(c.width, c.height))
204222 if itemsLen == 0 {