From afaa60e6c9e03292fc8c8ccbfd1f5c3ba540903f Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Mon, 19 Feb 2024 08:50:24 +0000 Subject: [PATCH 1/6] fix --- modules/indexer/code/search.go | 39 ++++++++++++++----------------- templates/code/searchresults.tmpl | 8 +++---- templates/repo/search.tmpl | 8 +++---- 3 files changed, 25 insertions(+), 30 deletions(-) diff --git a/modules/indexer/code/search.go b/modules/indexer/code/search.go index e19e22eea0e1..2589a3b5781b 100644 --- a/modules/indexer/code/search.go +++ b/modules/indexer/code/search.go @@ -16,14 +16,13 @@ import ( // Result a search result to display type Result struct { - RepoID int64 - Filename string - CommitID string - UpdatedUnix timeutil.TimeStamp - Language string - Color string - LineNumbers []int - FormattedLines template.HTML + RepoID int64 + Filename string + CommitID string + UpdatedUnix timeutil.TimeStamp + Language string + Color string + Lines map[int]template.HTML } type SearchResultLanguages = internal.SearchResultLanguages @@ -67,12 +66,11 @@ 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") - lineNumbers := make([]int, len(contentLines)) + lines := make(map[int]template.HTML, 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) && @@ -93,21 +91,18 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res return nil, err } - lineNumbers[i] = startLineNum + i + lines[startLineNum+i], _ = highlight.Code(result.Filename, "", formattedLinesBuffer.String()) index += len(line) } - highlighted, _ := highlight.Code(result.Filename, "", formattedLinesBuffer.String()) - return &Result{ - RepoID: result.RepoID, - Filename: result.Filename, - CommitID: result.CommitID, - UpdatedUnix: result.UpdatedUnix, - Language: result.Language, - Color: result.Color, - LineNumbers: lineNumbers, - FormattedLines: highlighted, + RepoID: result.RepoID, + Filename: result.Filename, + CommitID: result.CommitID, + UpdatedUnix: result.UpdatedUnix, + Language: result.Language, + Color: result.Color, + Lines: lines, }, nil } diff --git a/templates/code/searchresults.tmpl b/templates/code/searchresults.tmpl index bb21a5e0dcad..d70b27d05325 100644 --- a/templates/code/searchresults.tmpl +++ b/templates/code/searchresults.tmpl @@ -25,14 +25,14 @@
+ {{range $k, $line := .Lines}} - + + {{end}}
- {{range .LineNumbers}} - {{.}} - {{end}} + {{$k}} {{.FormattedLines}}{{$line}}
diff --git a/templates/repo/search.tmpl b/templates/repo/search.tmpl index b616b4de3231..9920ebaec85c 100644 --- a/templates/repo/search.tmpl +++ b/templates/repo/search.tmpl @@ -47,14 +47,14 @@
+ {{range $k, $line := .Lines}} - + + {{end}}
- {{range .LineNumbers}} - {{.}} - {{end}} + {{$k}} {{.FormattedLines}}{{$line}}
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 2/6] 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}} - - - - - {{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 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}} - - - - - {{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 000000000000..ff22f86f1d45 --- /dev/null +++ b/templates/shared/searchfile.tmpl @@ -0,0 +1,14 @@ +
+ + + {{range .result.Lines}} + + + + + {{end}} + +
+ {{.Num}} + {{.FormattedContent}}
+
From d83421e52d5f3e798325176c9d023d666f2ec222 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Tue, 20 Feb 2024 04:14:47 +0000 Subject: [PATCH 3/6] fix double-escaping --- modules/indexer/code/search.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/indexer/code/search.go b/modules/indexer/code/search.go index 0cad660da549..74f7ed40b823 100644 --- a/modules/indexer/code/search.go +++ b/modules/indexer/code/search.go @@ -7,6 +7,7 @@ import ( "bytes" "context" "fmt" + "html/template" "strings" "code.gitea.io/gitea/modules/highlight" @@ -27,7 +28,7 @@ type Result struct { type ResultLine struct { Num int - FormattedContent string + FormattedContent template.HTML } type SearchResultLanguages = internal.SearchResultLanguages @@ -101,6 +102,7 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res index += len(line) } + // we should highlight the whole code block first, otherwise it doesn't work well with multiple line highlighting hl, _ := highlight.Code(result.Filename, "", formattedLinesBuffer.String()) highlightedLines := strings.Split(string(hl), "\n") @@ -109,7 +111,7 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res } for i := 0; i < len(lines); i++ { - lines[i].FormattedContent = highlightedLines[i] + lines[i].FormattedContent = template.HTML(highlightedLines[i]) } return &Result{ From f08b007eb4d47c424c6ea9b80ab7499a214e62a3 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Thu, 22 Feb 2024 01:09:45 +0000 Subject: [PATCH 4/6] change format --- templates/code/searchresults.tmpl | 2 +- templates/repo/search.tmpl | 2 +- templates/shared/searchfile.tmpl | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/templates/code/searchresults.tmpl b/templates/code/searchresults.tmpl index 4a3072103a26..08bb12951d82 100644 --- a/templates/code/searchresults.tmpl +++ b/templates/code/searchresults.tmpl @@ -22,7 +22,7 @@ {{ctx.Locale.Tr "repo.diff.view_file"}}
- {{template "shared/searchfile" dict "repolink" $repo.Link "result" .}} + {{template "shared/searchfile" dict "RepoLink" $repo.Link "SearchResult" .}}
{{template "shared/searchbottom" dict "root" $ "result" .}} diff --git a/templates/repo/search.tmpl b/templates/repo/search.tmpl index c2a488b31f75..4aa2cd0171a1 100644 --- a/templates/repo/search.tmpl +++ b/templates/repo/search.tmpl @@ -44,7 +44,7 @@ {{ctx.Locale.Tr "repo.diff.view_file"}}
- {{template "shared/searchfile" dict "repolink" $.SourcePath "result" .}} + {{template "shared/searchfile" dict "RepoLink" $.SourcePath "SearchResult" .}}
{{template "shared/searchbottom" dict "root" $ "result" .}} diff --git a/templates/shared/searchfile.tmpl b/templates/shared/searchfile.tmpl index ff22f86f1d45..280584e4d1f9 100644 --- a/templates/shared/searchfile.tmpl +++ b/templates/shared/searchfile.tmpl @@ -1,10 +1,10 @@
- {{range .result.Lines}} + {{range .SearchResult.Lines}} From a8e53fc372b6dee4aa0ba075ebc4d619177b9cee Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Tue, 27 Feb 2024 00:51:41 +0000 Subject: [PATCH 5/6] fix ci fail --- modules/indexer/code/search.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/modules/indexer/code/search.go b/modules/indexer/code/search.go index 74f7ed40b823..f9ca14ea1cea 100644 --- a/modules/indexer/code/search.go +++ b/modules/indexer/code/search.go @@ -76,6 +76,7 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res contentLines := strings.SplitAfter(result.Content[startIndex:endIndex], "\n") lines := make([]ResultLine, 0, len(contentLines)) + highlightedLines := make([]string, 0, len(contentLines)) index := startIndex for i, line := range contentLines { var err error @@ -100,11 +101,17 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res lines = append(lines, ResultLine{Num: startLineNum + i}) index += len(line) + + // highlight.Code will remove the last `\n`, e.g. if input is `a\n`, output will be `a` + // Then the length of `lines` and `highlightedLines` will not be equal, so we need to add an empty line manually + if line == "" { + highlightedLines = append(highlightedLines, "") + } } // we should highlight the whole code block first, otherwise it doesn't work well with multiple line highlighting hl, _ := highlight.Code(result.Filename, "", formattedLinesBuffer.String()) - highlightedLines := strings.Split(string(hl), "\n") + highlightedLines = append(strings.Split(string(hl), "\n"), highlightedLines...) 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)) From 5f8ff714e30077caf0f8ab02ea70285b02a6c15e Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Tue, 5 Mar 2024 23:50:31 +0800 Subject: [PATCH 6/6] Update search.go --- modules/indexer/code/search.go | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/modules/indexer/code/search.go b/modules/indexer/code/search.go index f9ca14ea1cea..2ddc2397fa19 100644 --- a/modules/indexer/code/search.go +++ b/modules/indexer/code/search.go @@ -6,7 +6,6 @@ package code import ( "bytes" "context" - "fmt" "html/template" "strings" @@ -76,7 +75,6 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res contentLines := strings.SplitAfter(result.Content[startIndex:endIndex], "\n") lines := make([]ResultLine, 0, len(contentLines)) - highlightedLines := make([]string, 0, len(contentLines)) index := startIndex for i, line := range contentLines { var err error @@ -101,22 +99,15 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res lines = append(lines, ResultLine{Num: startLineNum + i}) index += len(line) - - // highlight.Code will remove the last `\n`, e.g. if input is `a\n`, output will be `a` - // Then the length of `lines` and `highlightedLines` will not be equal, so we need to add an empty line manually - if line == "" { - highlightedLines = append(highlightedLines, "") - } } // we should highlight the whole code block first, otherwise it doesn't work well with multiple line highlighting hl, _ := highlight.Code(result.Filename, "", formattedLinesBuffer.String()) - highlightedLines = append(strings.Split(string(hl), "\n"), highlightedLines...) - - 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)) - } + highlightedLines := strings.Split(string(hl), "\n") + // The lines outputted by highlight.Code might not match the original lines, because "highlight" removes the last `\n` + lines = lines[:min(len(highlightedLines), len(lines))] + highlightedLines = highlightedLines[:len(lines)] for i := 0; i < len(lines); i++ { lines[i].FormattedContent = template.HTML(highlightedLines[i]) }
- {{.Num}} + {{.Num}} {{.FormattedContent}}