From 449f7036c70cee9a2d13d0df7614aacf63b8e852 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michael=20Mur=C3=A9?= <batolettre@gmail.com>
Date: Tue, 23 Aug 2022 23:24:05 +0200
Subject: [PATCH] WIP

---
 commands/comment_edit.go      |  1 +
 commands/helper_completion.go | 43 +++++++++++++++++++++++++++++++++++
 entities/bug/comment.go       |  4 ++++
 3 files changed, 48 insertions(+)

diff --git a/commands/comment_edit.go b/commands/comment_edit.go
index 91c6d809..a6b03213 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 847a0288..6df0ac51 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 7835c5a8..acbd83a6 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) {
+
+}
-- 
GitLab