diff --git a/commands/comment_edit.go b/commands/comment_edit.go index 91c6d80977a14824b93b7e62d8bfa55d684d7f17..a6b03213c8a5192364a1db554d77dbee650f3016 100644 --- a/commands/comment_edit.go +++ b/commands/comment_edit.go @@ -24,6 +24,7 @@ func newCommentEditCommand() *cobra.Command { RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error { return runCommentEdit(env, options, args) }), + ValidArgsFunction: completeComment(env), } flags := cmd.Flags() diff --git a/commands/helper_completion.go b/commands/helper_completion.go index 847a02883da4bc216ee6532b74dc9b71d0cdb55d..6df0ac517e00bafaf64cfcce18a722348993f595 100644 --- a/commands/helper_completion.go +++ b/commands/helper_completion.go @@ -5,6 +5,7 @@ import ( "sort" "strings" + text "github.com/MichaelMure/go-term-text" "github.com/spf13/cobra" "github.com/MichaelMure/git-bug/bridge" @@ -12,6 +13,7 @@ import ( "github.com/MichaelMure/git-bug/cache" _select "github.com/MichaelMure/git-bug/commands/select" "github.com/MichaelMure/git-bug/entities/bug" + "github.com/MichaelMure/git-bug/entity" ) type validArgsFunction func(cmd *cobra.Command, args []string, toComplete string) (completions []string, directives cobra.ShellCompDirective) @@ -162,6 +164,47 @@ func completeBugAndLabels(env *Env, addOrRemove bool) validArgsFunction { } } +func completeComment(env *Env) validArgsFunction { + return func(cmd *cobra.Command, args []string, toComplete string) (completions []string, directives cobra.ShellCompDirective) { + if err := loadBackend(env)(cmd, args); err != nil { + return completionHandlerError(err) + } + defer func() { + _ = env.backend.Close() + }() + + bugPrefix, _ := entity.SeparateIds(toComplete) + bugCandidate := make([]entity.Id, 0, 5) + + // build a list of possible matching bugs + _, _ = env.backend.ResolveBugExcerptMatcher(func(excerpt *cache.BugExcerpt) bool { + if excerpt.Id.HasPrefix(bugPrefix) { + bugCandidate = append(bugCandidate, excerpt.Id) + } + return false + }) + + completions = make([]string, 0, 5) + + for _, bugId := range bugCandidate { + b, err := env.backend.ResolveBug(bugId) + if err != nil { + return completionHandlerError(err) + } + + for _, comment := range b.Snapshot().Comments { + if comment.CombinedId().HasPrefix(toComplete) { + text.TrimSpace() + message := strings.ReplaceAll(comment.Message, "\n", "") + + completions = append(completions, comment.CombinedId().Human()+"\t"+text.TruncateMax()) + } + } + } + + } +} + func completeFrom(choices []string) validArgsFunction { return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { return choices, cobra.ShellCompDirectiveNoFileComp diff --git a/entities/bug/comment.go b/entities/bug/comment.go index 7835c5a8a75db094fae4e5b7321c5931aa1016f1..acbd83a60cf06ec96456eddf4633424ff8945429 100644 --- a/entities/bug/comment.go +++ b/entities/bug/comment.go @@ -50,3 +50,7 @@ func (c Comment) FormatTime() string { // IsAuthored is a sign post method for gqlgen func (c Comment) IsAuthored() {} + +func (c Comment) Shortened(len int) { + +}