From dd3aaae30b00845a71240e3676d60585f3ceb6ce Mon Sep 17 00:00:00 2001
From: yp05327 <576951401@qq.com>
Date: Tue, 20 Feb 2024 01:44:55 +0000
Subject: [PATCH] improve
---
modules/indexer/code/search.go | 27 ++++++++++++++++++++++-----
templates/code/searchresults.tmpl | 15 +--------------
templates/repo/search.tmpl | 15 +--------------
templates/shared/searchfile.tmpl | 14 ++++++++++++++
4 files changed, 38 insertions(+), 33 deletions(-)
create mode 100644 templates/shared/searchfile.tmpl
diff --git a/modules/indexer/code/search.go b/modules/indexer/code/search.go
index 2589a3b5781b..0cad660da549 100644
--- a/modules/indexer/code/search.go
+++ b/modules/indexer/code/search.go
@@ -6,7 +6,7 @@ package code
import (
"bytes"
"context"
- "html/template"
+ "fmt"
"strings"
"code.gitea.io/gitea/modules/highlight"
@@ -22,7 +22,12 @@ type Result struct {
UpdatedUnix timeutil.TimeStamp
Language string
Color string
- Lines map[int]template.HTML
+ Lines []ResultLine
+}
+
+type ResultLine struct {
+ Num int
+ FormattedContent string
}
type SearchResultLanguages = internal.SearchResultLanguages
@@ -66,11 +71,12 @@ func writeStrings(buf *bytes.Buffer, strs ...string) error {
func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Result, error) {
startLineNum := 1 + strings.Count(result.Content[:startIndex], "\n")
+ var formattedLinesBuffer bytes.Buffer
+
contentLines := strings.SplitAfter(result.Content[startIndex:endIndex], "\n")
- lines := make(map[int]template.HTML, len(contentLines))
+ lines := make([]ResultLine, 0, len(contentLines))
index := startIndex
for i, line := range contentLines {
- var formattedLinesBuffer bytes.Buffer
var err error
if index < result.EndIndex &&
result.StartIndex < index+len(line) &&
@@ -91,10 +97,21 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res
return nil, err
}
- lines[startLineNum+i], _ = highlight.Code(result.Filename, "", formattedLinesBuffer.String())
+ lines = append(lines, ResultLine{Num: startLineNum + i})
index += len(line)
}
+ hl, _ := highlight.Code(result.Filename, "", formattedLinesBuffer.String())
+ highlightedLines := strings.Split(string(hl), "\n")
+
+ if len(highlightedLines) != len(lines) {
+ return nil, fmt.Errorf("the length of line numbers [%d] don't match the length of highlighted contents [%d]", len(lines), len(highlightedLines))
+ }
+
+ for i := 0; i < len(lines); i++ {
+ lines[i].FormattedContent = highlightedLines[i]
+ }
+
return &Result{
RepoID: result.RepoID,
Filename: result.Filename,
diff --git a/templates/code/searchresults.tmpl b/templates/code/searchresults.tmpl
index d70b27d05325..4a3072103a26 100644
--- a/templates/code/searchresults.tmpl
+++ b/templates/code/searchresults.tmpl
@@ -22,20 +22,7 @@
{{ctx.Locale.Tr "repo.diff.view_file"}}
-
-
-
- {{range $k, $line := .Lines}}
-
-
- {{$k}}
- |
- {{$line}} |
-
- {{end}}
-
-
-
+ {{template "shared/searchfile" dict "repolink" $repo.Link "result" .}}
{{template "shared/searchbottom" dict "root" $ "result" .}}
diff --git a/templates/repo/search.tmpl b/templates/repo/search.tmpl
index 9920ebaec85c..c2a488b31f75 100644
--- a/templates/repo/search.tmpl
+++ b/templates/repo/search.tmpl
@@ -44,20 +44,7 @@
{{ctx.Locale.Tr "repo.diff.view_file"}}
-
-
-
- {{range $k, $line := .Lines}}
-
-
- {{$k}}
- |
- {{$line}} |
-
- {{end}}
-
-
-
+ {{template "shared/searchfile" dict "repolink" $.SourcePath "result" .}}
{{template "shared/searchbottom" dict "root" $ "result" .}}
diff --git a/templates/shared/searchfile.tmpl b/templates/shared/searchfile.tmpl
new file mode 100644
index 000000000000..ff22f86f1d45
--- /dev/null
+++ b/templates/shared/searchfile.tmpl
@@ -0,0 +1,14 @@
+
+
+
+ {{range .result.Lines}}
+
+
+ {{.Num}}
+ |
+ {{.FormattedContent}} |
+
+ {{end}}
+
+
+