1package completions
2
3import (
4 "cmp"
5 "path/filepath"
6 "slices"
7 "strings"
8 "sync"
9
10 "charm.land/bubbles/v2/key"
11 tea "charm.land/bubbletea/v2"
12 "charm.land/lipgloss/v2"
13 "github.com/charmbracelet/crush/internal/agent/tools/mcp"
14 "github.com/charmbracelet/crush/internal/fsext"
15 "github.com/charmbracelet/crush/internal/ui/list"
16 "github.com/charmbracelet/x/ansi"
17 "github.com/charmbracelet/x/exp/ordered"
18)
19
20const (
21 minHeight = 1
22 maxHeight = 10
23 minWidth = 10
24 maxWidth = 100
25
26 tierExactName = iota
27 tierPrefixName
28 tierPathSegment
29 tierFallback
30)
31
32// SelectionMsg is sent when a completion is selected.
33type SelectionMsg[T any] struct {
34 Value T
35 KeepOpen bool // If true, insert without closing.
36}
37
38// ClosedMsg is sent when the completions are closed.
39type ClosedMsg struct{}
40
41// CompletionItemsLoadedMsg is sent when files have been loaded for completions.
42type CompletionItemsLoadedMsg struct {
43 Files []FileCompletionValue
44 Resources []ResourceCompletionValue
45}
46
47// Completions represents the completions popup component.
48type Completions struct {
49 // Popup dimensions
50 width int
51 height int
52
53 // State
54 open bool
55 query string
56
57 // Key bindings
58 keyMap KeyMap
59
60 // List component
61 list *list.FilterableList
62
63 // Styling
64 normalStyle lipgloss.Style
65 focusedStyle lipgloss.Style
66 matchStyle lipgloss.Style
67
68 allItems []list.FilterableItem
69 filtered []list.FilterableItem
70}
71
72type namePriorityRule struct {
73 tier int
74 match func(pathLower, baseLower, stemLower, queryLower string) bool
75}
76
77var namePriorityRules = []namePriorityRule{
78 {
79 tier: tierExactName,
80 match: func(_ string, baseLower, stemLower, queryLower string) bool {
81 return baseLower == queryLower || stemLower == queryLower
82 },
83 },
84 {
85 tier: tierPrefixName,
86 match: func(_ string, baseLower, _ string, queryLower string) bool {
87 return strings.HasPrefix(baseLower, queryLower)
88 },
89 },
90 {
91 tier: tierPathSegment,
92 match: func(pathLower, _ string, _ string, queryLower string) bool {
93 return hasPathSegment(pathLower, queryLower)
94 },
95 },
96}
97
98// New creates a new completions component.
99func New(normalStyle, focusedStyle, matchStyle lipgloss.Style) *Completions {
100 l := list.NewFilterableList()
101 l.SetGap(0)
102 l.SetReverse(true)
103
104 return &Completions{
105 keyMap: DefaultKeyMap(),
106 list: l,
107 normalStyle: normalStyle,
108 focusedStyle: focusedStyle,
109 matchStyle: matchStyle,
110 }
111}
112
113// IsOpen returns whether the completions popup is open.
114func (c *Completions) IsOpen() bool {
115 return c.open
116}
117
118// Query returns the current filter query.
119func (c *Completions) Query() string {
120 return c.query
121}
122
123// Size returns the visible size of the popup.
124func (c *Completions) Size() (width, height int) {
125 visible := len(c.filtered)
126 return c.width, min(visible, c.height)
127}
128
129// KeyMap returns the key bindings.
130func (c *Completions) KeyMap() KeyMap {
131 return c.keyMap
132}
133
134// Open opens the completions with file items from the filesystem.
135func (c *Completions) Open(depth, limit int) tea.Cmd {
136 return func() tea.Msg {
137 var msg CompletionItemsLoadedMsg
138 var wg sync.WaitGroup
139 wg.Go(func() {
140 msg.Files = loadFiles(depth, limit)
141 })
142 wg.Go(func() {
143 msg.Resources = loadMCPResources()
144 })
145 wg.Wait()
146 return msg
147 }
148}
149
150// SetItems sets the files and MCP resources and rebuilds the merged list.
151func (c *Completions) SetItems(files []FileCompletionValue, resources []ResourceCompletionValue) {
152 items := make([]list.FilterableItem, 0, len(files)+len(resources))
153
154 // Add files first.
155 for _, file := range files {
156 item := NewCompletionItem(
157 file.Path,
158 file,
159 c.normalStyle,
160 c.focusedStyle,
161 c.matchStyle,
162 )
163 items = append(items, item)
164 }
165
166 // Add MCP resources.
167 for _, resource := range resources {
168 item := NewCompletionItem(
169 resource.MCPName+"/"+cmp.Or(resource.Title, resource.URI),
170 resource,
171 c.normalStyle,
172 c.focusedStyle,
173 c.matchStyle,
174 )
175 items = append(items, item)
176 }
177
178 c.open = true
179 c.query = ""
180 c.allItems = items
181 c.filtered = append([]list.FilterableItem(nil), items...)
182 c.list.SetItems(c.filtered...)
183 c.list.SetFilter("")
184 c.list.Focus()
185
186 c.width = maxWidth
187 c.height = ordered.Clamp(len(items), int(minHeight), int(maxHeight))
188 c.list.SetSize(c.width, c.height)
189 c.list.SelectFirst()
190 c.list.ScrollToSelected()
191
192 c.updateSize()
193}
194
195// Close closes the completions popup.
196func (c *Completions) Close() {
197 c.open = false
198}
199
200// Filter filters the completions with the given query.
201func (c *Completions) Filter(query string) {
202 if !c.open {
203 return
204 }
205
206 if query == c.query {
207 return
208 }
209
210 c.query = query
211 c.applyNamePriorityFilter(query)
212
213 c.updateSize()
214}
215
216func (c *Completions) applyNamePriorityFilter(query string) {
217 if query == "" {
218 c.filtered = append([]list.FilterableItem(nil), c.allItems...)
219 c.list.SetItems(c.filtered...)
220 return
221 }
222
223 c.list.SetItems(c.allItems...)
224 c.list.SetFilter(query)
225 raw := c.list.FilteredItems()
226 filtered := make([]list.FilterableItem, 0, len(raw))
227 for _, item := range raw {
228 filterable, ok := item.(list.FilterableItem)
229 if !ok {
230 continue
231 }
232 filtered = append(filtered, filterable)
233 }
234
235 queryLower := strings.ToLower(strings.TrimSpace(query))
236 slices.SortStableFunc(filtered, func(a, b list.FilterableItem) int {
237 return namePriorityTier(a.Filter(), queryLower) - namePriorityTier(b.Filter(), queryLower)
238 })
239 c.filtered = filtered
240 c.list.SetItems(c.filtered...)
241}
242
243func namePriorityTier(path, queryLower string) int {
244 if queryLower == "" {
245 return tierFallback
246 }
247
248 pathLower := strings.ToLower(path)
249 baseLower := strings.ToLower(filepath.Base(strings.ReplaceAll(path, `\`, `/`)))
250 stemLower := strings.TrimSuffix(baseLower, filepath.Ext(baseLower))
251 for _, rule := range namePriorityRules {
252 if rule.match(pathLower, baseLower, stemLower, queryLower) {
253 return rule.tier
254 }
255 }
256 return tierFallback
257}
258
259func hasPathSegment(pathLower, queryLower string) bool {
260 return slices.Contains(strings.FieldsFunc(pathLower, func(r rune) bool {
261 return r == '/' || r == '\\'
262 }), queryLower)
263}
264
265func (c *Completions) updateSize() {
266 items := c.filtered
267 start, end := c.list.VisibleItemIndices()
268 width := 0
269 for i := start; i <= end; i++ {
270 item := c.list.ItemAt(i)
271 if item == nil {
272 continue
273 }
274 s := item.(interface{ Text() string }).Text()
275 width = max(width, ansi.StringWidth(s))
276 }
277 c.width = ordered.Clamp(width+2, int(minWidth), int(maxWidth))
278 c.height = ordered.Clamp(len(items), int(minHeight), int(maxHeight))
279 c.list.SetSize(c.width, c.height)
280 c.list.SelectFirst()
281 c.list.ScrollToSelected()
282}
283
284// HasItems returns whether there are visible items.
285func (c *Completions) HasItems() bool {
286 return len(c.filtered) > 0
287}
288
289// Update handles key events for the completions.
290func (c *Completions) Update(msg tea.KeyPressMsg) (tea.Msg, bool) {
291 if !c.open {
292 return nil, false
293 }
294
295 switch {
296 case key.Matches(msg, c.keyMap.Up):
297 c.selectPrev()
298 return nil, true
299
300 case key.Matches(msg, c.keyMap.Down):
301 c.selectNext()
302 return nil, true
303
304 case key.Matches(msg, c.keyMap.UpInsert):
305 c.selectPrev()
306 return c.selectCurrent(true), true
307
308 case key.Matches(msg, c.keyMap.DownInsert):
309 c.selectNext()
310 return c.selectCurrent(true), true
311
312 case key.Matches(msg, c.keyMap.Select):
313 return c.selectCurrent(false), true
314
315 case key.Matches(msg, c.keyMap.Cancel):
316 c.Close()
317 return ClosedMsg{}, true
318 }
319
320 return nil, false
321}
322
323// selectPrev selects the previous item with circular navigation.
324func (c *Completions) selectPrev() {
325 items := c.filtered
326 if len(items) == 0 {
327 return
328 }
329 if !c.list.SelectPrev() {
330 c.list.WrapToEnd()
331 }
332 c.list.ScrollToSelected()
333}
334
335// selectNext selects the next item with circular navigation.
336func (c *Completions) selectNext() {
337 items := c.filtered
338 if len(items) == 0 {
339 return
340 }
341 if !c.list.SelectNext() {
342 c.list.WrapToStart()
343 }
344 c.list.ScrollToSelected()
345}
346
347// selectCurrent returns a command with the currently selected item.
348func (c *Completions) selectCurrent(keepOpen bool) tea.Msg {
349 items := c.filtered
350 if len(items) == 0 {
351 return nil
352 }
353
354 selected := c.list.Selected()
355 if selected < 0 || selected >= len(items) {
356 return nil
357 }
358
359 item, ok := items[selected].(*CompletionItem)
360 if !ok {
361 return nil
362 }
363
364 if !keepOpen {
365 c.open = false
366 }
367
368 switch item := item.Value().(type) {
369 case ResourceCompletionValue:
370 return SelectionMsg[ResourceCompletionValue]{
371 Value: item,
372 KeepOpen: keepOpen,
373 }
374 case FileCompletionValue:
375 return SelectionMsg[FileCompletionValue]{
376 Value: item,
377 KeepOpen: keepOpen,
378 }
379 default:
380 return nil
381 }
382}
383
384// Render renders the completions popup.
385func (c *Completions) Render() string {
386 if !c.open {
387 return ""
388 }
389
390 items := c.filtered
391 if len(items) == 0 {
392 return ""
393 }
394
395 return c.list.List.Render()
396}
397
398func loadFiles(depth, limit int) []FileCompletionValue {
399 files, _, _ := fsext.ListDirectory(".", nil, depth, limit)
400 slices.Sort(files)
401 result := make([]FileCompletionValue, 0, len(files))
402 for _, file := range files {
403 result = append(result, FileCompletionValue{
404 Path: strings.TrimPrefix(file, "./"),
405 })
406 }
407 return result
408}
409
410func loadMCPResources() []ResourceCompletionValue {
411 var resources []ResourceCompletionValue
412 for mcpName, mcpResources := range mcp.Resources() {
413 for _, r := range mcpResources {
414 resources = append(resources, ResourceCompletionValue{
415 MCPName: mcpName,
416 URI: r.URI,
417 Title: r.Name,
418 MIMEType: r.MIMEType,
419 })
420 }
421 }
422 return resources
423}