Skip to content

Commit 297cd8f

Browse files
authored
refactor: autocompletion (#826)
1 parent 744dda9 commit 297cd8f

13 files changed

Lines changed: 217 additions & 215 deletions

File tree

internal/tui/components/autocomplete/autocomplete.go renamed to internal/tui/components/cmp/autocomplete.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package autocomplete
1+
// Package cmp houses the generic completion component
2+
package cmp
23

34
import (
45
"strings"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package autocomplete
1+
package cmp
22

33
import (
44
"strings"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package autocomplete
1+
package cmp
22

33
import (
44
"strings"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package autocomplete
1+
package cmp
22

33
import tea "charm.land/bubbletea/v2"
44

internal/data/autocomplete/source_test.go renamed to internal/tui/components/cmp/source_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package autocomplete
1+
package cmp
22

33
import (
44
"testing"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package autocomplete
1+
package cmp
22

33
import (
44
"strings"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package autocomplete
1+
package cmp
22

33
import (
44
"strings"

internal/tui/components/detailedit/controller.go renamed to internal/tui/components/cmpcontroller/controller.go

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
package detailedit
1+
// Package cmpcontroller is used to load completions (e.g. from the network)
2+
// for the various modes and using the cmp package to display them
3+
package cmpcontroller
24

35
import (
46
"strings"
@@ -11,8 +13,7 @@ import (
1113
"charm.land/lipgloss/v2"
1214

1315
"github.com/dlvhdr/gh-dash/v4/internal/data"
14-
dataautocomplete "github.com/dlvhdr/gh-dash/v4/internal/data/autocomplete"
15-
popupautocomplete "github.com/dlvhdr/gh-dash/v4/internal/tui/components/autocomplete"
16+
"github.com/dlvhdr/gh-dash/v4/internal/tui/components/cmp"
1617
"github.com/dlvhdr/gh-dash/v4/internal/tui/components/inputbox"
1718
"github.com/dlvhdr/gh-dash/v4/internal/tui/context"
1819
)
@@ -54,7 +55,7 @@ type EnterOptions struct {
5455
Mode Mode
5556
Prompt string
5657
InitialValue string
57-
Source dataautocomplete.Source
58+
Source cmp.Source
5859
Repo RepoRef
5960
SuggestionKind SuggestionKind
6061
EnterFetch FetchPolicy
@@ -86,7 +87,7 @@ type RepoUsersFetchFailedMsg struct {
8687
type Controller struct {
8788
ctx *context.ProgramContext
8889
inputBox inputbox.Model
89-
ac *popupautocomplete.Model
90+
cmp *cmp.Model
9091
mode Mode
9192
prompt string
9293
repo RepoRef
@@ -100,13 +101,13 @@ type Controller struct {
100101

101102
func New(ctx *context.ProgramContext) Controller {
102103
inputBox := inputbox.NewModel(ctx)
103-
ac := popupautocomplete.NewModel(ctx)
104-
inputBox.SetAutocomplete(&ac)
104+
cmp := cmp.NewModel(ctx)
105+
inputBox.SetAutocomplete(&cmp)
105106

106107
return Controller{
107108
ctx: ctx,
108109
inputBox: inputBox,
109-
ac: &ac,
110+
cmp: &cmp,
110111
}
111112
}
112113

@@ -133,13 +134,13 @@ func (c Controller) View() string {
133134

134135
func (c *Controller) SetWidth(width int) {
135136
c.inputBox.SetWidth(width)
136-
c.ac.SetWidth(width - 4)
137+
c.cmp.SetWidth(width - 4)
137138
}
138139

139140
func (c *Controller) UpdateProgramContext(ctx *context.ProgramContext) {
140141
c.ctx = ctx
141142
c.inputBox.UpdateProgramContext(ctx)
142-
c.ac.UpdateProgramContext(ctx)
143+
c.cmp.UpdateProgramContext(ctx)
143144
}
144145

145146
func (c Controller) Exit() Controller {
@@ -176,15 +177,15 @@ func (c Controller) Enter(opts EnterOptions) (Controller, tea.Cmd) {
176177
case SuggestionUsers:
177178
if users, ok := data.CachedRepoUsers(opts.Repo.NameWithOwner); ok {
178179
c.repoUsers = users
179-
c.ac.SetSuggestions(userSuggestions(users))
180+
c.cmp.SetSuggestions(userSuggestions(users))
180181
c.showSuggestionsFromCurrentContext()
181182
} else if opts.EnterFetch != FetchNone {
182183
cmds = append([]tea.Cmd{c.fetchUsers(opts.EnterFetch == FetchWithLoading)}, cmds...)
183184
}
184185
case SuggestionLabels:
185186
if labels, ok := data.CachedRepoLabels(opts.Repo.NameWithOwner); ok {
186187
c.repoLabels = labels
187-
c.ac.SetSuggestions(labelSuggestions(labels))
188+
c.cmp.SetSuggestions(labelSuggestions(labels))
188189
c.showSuggestionsFromCurrentContext()
189190
} else if opts.EnterFetch != FetchNone {
190191
cmds = append([]tea.Cmd{c.fetchLabels(opts.EnterFetch == FetchWithLoading)}, cmds...)
@@ -211,29 +212,29 @@ func (c Controller) Update(msg tea.Msg) (Controller, tea.Cmd, *Submit, bool) {
211212

212213
case RepoLabelsFetchedMsg:
213214
c.repoLabels = msg.Labels
214-
c.ac.SetSuggestions(labelSuggestions(msg.Labels))
215-
cmds = append(cmds, c.ac.SetFetchSuccess())
215+
c.cmp.SetSuggestions(labelSuggestions(msg.Labels))
216+
cmds = append(cmds, c.cmp.SetFetchSuccess())
216217
if c.mode == ModeLabel {
217218
c.showSuggestionsFromCurrentContext()
218219
}
219220
return c, tea.Batch(cmds...), nil, true
220221

221222
case RepoLabelsFetchFailedMsg:
222-
return c, c.ac.SetFetchError(msg.Err), nil, true
223+
return c, c.cmp.SetFetchError(msg.Err), nil, true
223224

224225
case RepoUsersFetchedMsg:
225226
c.repoUsers = msg.Users
226-
c.ac.SetSuggestions(userSuggestions(msg.Users))
227-
cmds = append(cmds, c.ac.SetFetchSuccess())
227+
c.cmp.SetSuggestions(userSuggestions(msg.Users))
228+
cmds = append(cmds, c.cmp.SetFetchSuccess())
228229
if c.mode == ModeComment || c.mode == ModeApprove || c.mode == ModeAssign {
229230
c.showSuggestionsFromCurrentContext()
230231
}
231232
return c, tea.Batch(cmds...), nil, true
232233

233234
case RepoUsersFetchFailedMsg:
234-
return c, c.ac.SetFetchError(msg.Err), nil, true
235+
return c, c.cmp.SetFetchError(msg.Err), nil, true
235236

236-
case popupautocomplete.FetchSuggestionsRequestedMsg:
237+
case cmp.FetchSuggestionsRequestedMsg:
237238
if !c.Active() || c.suggestionKind == SuggestionNone {
238239
return c, nil, nil, false
239240
}
@@ -255,7 +256,7 @@ func (c Controller) Update(msg tea.Msg) (Controller, tea.Cmd, *Submit, bool) {
255256
}
256257

257258
switch {
258-
case key.Matches(msg, popupautocomplete.RefreshSuggestionsKey):
259+
case key.Matches(msg, cmp.RefreshSuggestionsKey):
259260
if c.suggestionKind == SuggestionNone {
260261
return c, nil, nil, true
261262
}
@@ -305,7 +306,7 @@ func (c Controller) Update(msg tea.Msg) (Controller, tea.Cmd, *Submit, bool) {
305306
}
306307
}
307308

308-
var previousContext dataautocomplete.Context
309+
var previousContext cmp.Context
309310
if c.usesAutocomplete() {
310311
previousContext = c.inputBox.CurrentAutocompleteContext()
311312
}
@@ -316,10 +317,10 @@ func (c Controller) Update(msg tea.Msg) (Controller, tea.Cmd, *Submit, bool) {
316317
if c.usesAutocomplete() {
317318
currentContext := c.inputBox.CurrentAutocompleteContext()
318319
if currentContext != previousContext {
319-
if c.hideOnEmpty && currentContext == (dataautocomplete.Context{}) {
320-
c.ac.Hide()
320+
if c.hideOnEmpty && currentContext == (cmp.Context{}) {
321+
c.cmp.Hide()
321322
} else {
322-
c.ac.Show(currentContext.Content, c.inputBox.AutocompleteItemsToExclude())
323+
c.cmp.Show(currentContext.Content, c.inputBox.AutocompleteItemsToExclude())
323324
}
324325
}
325326
}
@@ -328,9 +329,9 @@ func (c Controller) Update(msg tea.Msg) (Controller, tea.Cmd, *Submit, bool) {
328329
}
329330

330331
switch msg.(type) {
331-
case spinner.TickMsg, popupautocomplete.ClearFetchStatusMsg:
332+
case spinner.TickMsg, cmp.ClearFetchStatusMsg:
332333
var acCmd tea.Cmd
333-
*c.ac, acCmd = c.ac.Update(msg)
334+
*c.cmp, acCmd = c.cmp.Update(msg)
334335
return c, acCmd, nil, c.Active() || c.suggestionKind != SuggestionNone
335336
}
336337

@@ -363,21 +364,21 @@ func (c *Controller) setDiscardPrompt() {
363364
}
364365

365366
func (c *Controller) resetAutocompleteState() {
366-
c.ac.Reset()
367-
c.ac.Hide()
368-
c.ac.SetSuggestions(nil)
367+
c.cmp.Reset()
368+
c.cmp.Hide()
369+
c.cmp.SetSuggestions(nil)
369370
}
370371

371372
func (c Controller) showSuggestionsFromCurrentContext() {
372373
if !c.usesAutocomplete() {
373374
return
374375
}
375376
currentContext := c.inputBox.CurrentAutocompleteContext()
376-
if c.hideOnEmpty && currentContext == (dataautocomplete.Context{}) {
377-
c.ac.Hide()
377+
if c.hideOnEmpty && currentContext == (cmp.Context{}) {
378+
c.cmp.Hide()
378379
return
379380
}
380-
c.ac.Show(currentContext.Content, c.inputBox.AutocompleteItemsToExclude())
381+
c.cmp.Show(currentContext.Content, c.inputBox.AutocompleteItemsToExclude())
381382
}
382383

383384
func (c Controller) usesAutocomplete() bool {
@@ -392,7 +393,7 @@ func (c Controller) usesAutocomplete() bool {
392393
func (c Controller) fetchLabels(showLoading bool) tea.Cmd {
393394
var spinnerTickCmd tea.Cmd
394395
if showLoading {
395-
spinnerTickCmd = c.ac.SetFetchLoading()
396+
spinnerTickCmd = c.cmp.SetFetchLoading()
396397
}
397398

398399
fetchCmd := func() tea.Msg {
@@ -412,7 +413,7 @@ func (c Controller) fetchLabels(showLoading bool) tea.Cmd {
412413
func (c Controller) fetchUsers(showLoading bool) tea.Cmd {
413414
var spinnerTickCmd tea.Cmd
414415
if showLoading {
415-
spinnerTickCmd = c.ac.SetFetchLoading()
416+
spinnerTickCmd = c.cmp.SetFetchLoading()
416417
}
417418

418419
fetchCmd := func() tea.Msg {
@@ -429,21 +430,21 @@ func (c Controller) fetchUsers(showLoading bool) tea.Cmd {
429430
return fetchCmd
430431
}
431432

432-
func userSuggestions(users []data.User) []popupautocomplete.Suggestion {
433-
suggestions := make([]popupautocomplete.Suggestion, 0, len(users))
433+
func userSuggestions(users []data.User) []cmp.Suggestion {
434+
suggestions := make([]cmp.Suggestion, 0, len(users))
434435
for _, user := range users {
435-
suggestions = append(suggestions, popupautocomplete.Suggestion{
436+
suggestions = append(suggestions, cmp.Suggestion{
436437
Value: user.Login,
437438
Detail: strings.TrimSpace(user.Name),
438439
})
439440
}
440441
return suggestions
441442
}
442443

443-
func labelSuggestions(labels []data.Label) []popupautocomplete.Suggestion {
444-
suggestions := make([]popupautocomplete.Suggestion, 0, len(labels))
444+
func labelSuggestions(labels []data.Label) []cmp.Suggestion {
445+
suggestions := make([]cmp.Suggestion, 0, len(labels))
445446
for _, label := range labels {
446-
suggestions = append(suggestions, popupautocomplete.Suggestion{
447+
suggestions = append(suggestions, cmp.Suggestion{
447448
Value: label.Name,
448449
Detail: strings.TrimSpace(label.Description),
449450
})

0 commit comments

Comments
 (0)