From aa90c58ec14bc7fe6e6be72e113c8df2cb3db7b9 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Tue, 20 Feb 2024 01:30:52 +0000 Subject: [PATCH] improve --- modules/indexer/code/search.go | 31 ++++++++++++++++++++++++++----- templates/code/searchresults.tmpl | 15 +-------------- templates/repo/search.tmpl | 15 +-------------- templates/shared/searchfile.tmpl | 14 ++++++++++++++ 4 files changed, 42 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 2589a3b5781b7..9ef4dd7c8ab84 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,13 @@ type Result struct { UpdatedUnix timeutil.TimeStamp Language string Color string - Lines map[int]template.HTML + Lines []ResultLine +} + +type ResultLine struct { + Num int + FormattedContent string + IsActive bool } type SearchResultLanguages = internal.SearchResultLanguages @@ -66,12 +72,15 @@ 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 + l := ResultLine{Num: startLineNum + i} + if index < result.EndIndex && result.StartIndex < index+len(line) && result.StartIndex < result.EndIndex { @@ -82,6 +91,7 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res line[openActiveIndex:closeActiveIndex], line[closeActiveIndex:], ) + l.IsActive = true } else { err = writeStrings(&formattedLinesBuffer, line, @@ -91,10 +101,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, l) 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 d70b27d05325d..4a3072103a266 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}} - - - - - {{end}} - -
- {{$k}} - {{$line}}
-
+ {{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 9920ebaec85c6..c2a488b31f753 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}} - - - - - {{end}} - -
- {{$k}} - {{$line}}
-
+ {{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 0000000000000..64b041e60d811 --- /dev/null +++ b/templates/shared/searchfile.tmpl @@ -0,0 +1,14 @@ +
+ + + {{range .result.Lines}} + + + + + {{end}} + +
+ {{.Num}} + {{.FormattedContent}}
+