@@ -83,7 +83,7 @@ func (c *Completions) Query() string {
8383
8484// Size returns the visible size of the popup.
8585func (c *Completions) Size() (width, height int) {
86− visible := len(c.list.VisibleItems())
86+ visible := len(c.list.FilteredItems())
8787 return c.width, min(visible, c.height)
8888}
8989
@@ -104,7 +104,6 @@ func (c *Completions) OpenWithFiles(depth, limit int) tea.Cmd {
104104// SetFiles sets the file items on the completions popup.
105105func (c *Completions) SetFiles(files []string) {
106106 items := make([]list.FilterableItem, 0, len(files))
107− width := 0
108107 for _, file := range files {
109108 file = strings.TrimPrefix(file, "./")
110109 item := NewCompletionItem(
@@ -114,8 +113,6 @@ func (c *Completions) SetFiles(files []string) {
114113 c.focusedStyle,
115114 c.matchStyle,
116115 )
117−
118− width = max(width, ansi.StringWidth(file))
119116 items = append(items, item)
120117 }
121118
@@ -125,11 +122,22 @@ func (c *Completions) SetFiles(files []string) {
125122 c.list.SetFilter("") // Clear any previous filter.
126123 c.list.Focus()
127124
128− c.width = ordered.Clamp(width+2, int(minWidth), int(maxWidth))
125+ c.width = maxWidth
129126 c.height = ordered.Clamp(len(items), int(minHeight), int(maxHeight))
130127 c.list.SetSize(c.width, c.height)
131128 c.list.SelectFirst()
132129 c.list.ScrollToSelected()
130+
131+ // recalculate width by using just the visible items
132+ start, end := c.list.VisibleItemIndices()
133+ width := 0
134+ if end != 0 {
135+ for _, file := range files[start : end+1] {
136+ width = max(width, ansi.StringWidth(file))
137+ }
138+ }
139+ c.width = ordered.Clamp(width+2, int(minWidth), int(maxWidth))
140+ c.list.SetSize(c.width, c.height)
133141}
134142
135143// Close closes the completions popup.
@@ -150,10 +158,14 @@ func (c *Completions) Filter(query string) {
150158 c.query = query
151159 c.list.SetFilter(query)
152160
153− items := c.list.VisibleItems()
161+ // recalculate width by using just the visible items
162+ items := c.list.FilteredItems()
163+ start, end := c.list.VisibleItemIndices()
154164 width := 0
155− for _, item := range items {
156− width = max(width, ansi.StringWidth(item.(interface{ Text() string }).Text()))
165+ if end != 0 {
166+ for _, item := range items[start : end+1] {
167+ width = max(width, ansi.StringWidth(item.(interface{ Text() string }).Text()))
168+ }
157169 }
158170 c.width = ordered.Clamp(width+2, int(minWidth), int(maxWidth))
159171 c.height = ordered.Clamp(len(items), int(minHeight), int(maxHeight))
@@ -164,7 +176,7 @@ func (c *Completions) Filter(query string) {
164176
165177// HasItems returns whether there are visible items.
166178func (c *Completions) HasItems() bool {
167− return len(c.list.VisibleItems()) > 0
179+ return len(c.list.FilteredItems()) > 0
168180}
169181
170182// Update handles key events for the completions.
@@ -203,7 +215,7 @@ func (c *Completions) Update(msg tea.KeyPressMsg) (tea.Msg, bool) {
203215
204216// selectPrev selects the previous item with circular navigation.
205217func (c *Completions) selectPrev() {
206− items := c.list.VisibleItems()
218+ items := c.list.FilteredItems()
207219 if len(items) == 0 {
208220 return
209221 }
@@ -215,7 +227,7 @@ func (c *Completions) selectPrev() {
215227
216228// selectNext selects the next item with circular navigation.
217229func (c *Completions) selectNext() {
218− items := c.list.VisibleItems()
230+ items := c.list.FilteredItems()
219231 if len(items) == 0 {
220232 return
221233 }
@@ -227,7 +239,7 @@ func (c *Completions) selectNext() {
227239
228240// selectCurrent returns a command with the currently selected item.
229241func (c *Completions) selectCurrent(insert bool) tea.Msg {
230− items := c.list.VisibleItems()
242+ items := c.list.FilteredItems()
231243 if len(items) == 0 {
232244 return nil
233245 }
@@ -258,7 +270,7 @@ func (c *Completions) Render() string {
258270 return ""
259271 }
260272
261− items := c.list.VisibleItems()
273+ items := c.list.FilteredItems()
262274 if len(items) == 0 {
263275 return ""
264276 }