From 71a72d8a22f2ba5d08415542371f3529a01571dd Mon Sep 17 00:00:00 2001
From: Shiny Nematoda <snematoda.751k2@aleeas.com>
Date: Sun, 10 Mar 2024 10:21:11 +0000
Subject: [PATCH] port 95065e2ad1

---
 services/repository/files/search.go | 49 ++++++++++++++++++++---------
 templates/code/searchresults.tmpl   |  2 +-
 templates/shared/searchfile.tmpl    |  2 +-
 3 files changed, 37 insertions(+), 16 deletions(-)

diff --git a/services/repository/files/search.go b/services/repository/files/search.go
index f8317c4892..716ebe5306 100644
--- a/services/repository/files/search.go
+++ b/services/repository/files/search.go
@@ -16,14 +16,18 @@ import (
 )
 
 type Result struct {
-	RepoID         int64 // ignored
-	Filename       string
-	CommitID       string             // branch
-	UpdatedUnix    timeutil.TimeStamp // ignored
-	Language       string
-	Color          string
-	LineNumbers    []int64
-	FormattedLines template.HTML
+	RepoID      int64              // ignored
+	Filename    string
+	CommitID    string             // branch
+	UpdatedUnix timeutil.TimeStamp // ignored
+	Language    string
+	Color       string
+	Lines       []ResultLine
+}
+
+type ResultLine struct {
+	Num              int64
+	FormattedContent template.HTML
 }
 
 const pHEAD = "HEAD:"
@@ -33,12 +37,12 @@ func NewRepoGrep(ctx context.Context, repo *repo_model.Repository, keyword strin
 	if err != nil {
 		return nil, err
 	}
-
+	
 	data := []*Result{}
 
 	stdout, _, err := git.NewCommand(ctx,
 		"grep",
-		"-1", // n before and after lines
+		"-1",              // n before and after lines
 		"-z",
 		"--heading",
 		"--break",         // easier parsing
@@ -46,7 +50,8 @@ func NewRepoGrep(ctx context.Context, repo *repo_model.Repository, keyword strin
 		"-n",              // line nums
 		"-i",              // ignore case
 		"--full-name",     // full file path, rel to repo
-		//"--column",        // for adding better highlighting support
+		//"--column",      // for adding better highlighting support
+		"-e",              // for queries starting with "-"
 	).
 		AddDynamicArguments(keyword).
 		AddArguments("HEAD").
@@ -57,6 +62,8 @@ func NewRepoGrep(ctx context.Context, repo *repo_model.Repository, keyword strin
 
 	for _, block := range strings.Split(stdout, "\n\n") {
 		res := Result{CommitID: repo.DefaultBranch}
+
+		linenum := []int64{}
 		code := []string{}
 
 		for _, line := range strings.Split(block, "\n") {
@@ -71,18 +78,32 @@ func NewRepoGrep(ctx context.Context, repo *repo_model.Repository, keyword strin
 					continue
 				}
 
-				res.LineNumbers = append(res.LineNumbers, i)
+				linenum = append(linenum, i)
 				code = append(code, after)
 			}
 		}
 
-		if res.Filename == "" || len(code) == 0 || len(res.LineNumbers) == 0 {
+		if res.Filename == "" || len(code) == 0 || len(linenum) == 0 {
 			continue
 		}
 
-		res.FormattedLines, res.Language = highlight.Code(res.Filename, "", strings.Join(code, "\n"))
+		var hl template.HTML
+		
+		hl, res.Language = highlight.Code(res.Filename, "", strings.Join(code, "\n"))
 		res.Color = enry.GetColor(res.Language)
 
+		hlCode := strings.Split(string(hl), "\n")
+		n := min(len(hlCode), len(linenum))
+
+		res.Lines = make([]ResultLine, n)
+
+		for i := 0; i < n; i++ {
+			res.Lines[i] = ResultLine{
+				Num: linenum[i],
+				FormattedContent: template.HTML(hlCode[i]),
+			}
+		}
+
 		data = append(data, &res)
 	}
 
diff --git a/templates/code/searchresults.tmpl b/templates/code/searchresults.tmpl
index 08bb12951d..dca7dea7da 100644
--- a/templates/code/searchresults.tmpl
+++ b/templates/code/searchresults.tmpl
@@ -22,7 +22,7 @@
 				<a role="button" class="ui basic tiny button" rel="nofollow" href="{{$repo.Link}}/src/commit/{{$result.CommitID | PathEscape}}/{{.Filename | PathEscapeSegments}}">{{ctx.Locale.Tr "repo.diff.view_file"}}</a>
 			</h4>
 			<div class="ui attached table segment">
-				{{template "shared/searchfile" dict "RepoLink" $repo.Link "SearchResult" .}}
+				{{template "shared/searchfile" dict "RepoLink" $repo.Link "IsIndexer" true "SearchResult" .}}
 			</div>
 			{{template "shared/searchbottom" dict "root" $ "result" .}}
 		</div>
diff --git a/templates/shared/searchfile.tmpl b/templates/shared/searchfile.tmpl
index 280584e4d1..6ffcd7118b 100644
--- a/templates/shared/searchfile.tmpl
+++ b/templates/shared/searchfile.tmpl
@@ -4,7 +4,7 @@
 			{{range .SearchResult.Lines}}
 				<tr>
 					<td class="lines-num">
-						<a href="{{$.RepoLink}}/src/commit/{{PathEscape $.SearchResult.CommitID}}/{{PathEscapeSegments $.SearchResult.Filename}}#L{{.Num}}"><span>{{.Num}}</span></a>
+						<a href="{{$.RepoLink}}/src/{{if not $.IsIndexer}}branch{{else}}commit{{end}}/{{PathEscape $.SearchResult.CommitID}}/{{PathEscapeSegments $.SearchResult.Filename}}#L{{.Num}}"><span>{{.Num}}</span></a>
 					</td>
 					<td class="lines-code chroma"><code class="code-inner">{{.FormattedContent}}</code></td>
 				</tr>