diff --git a/modules/indexer/code/search.go b/modules/indexer/code/search.go
index e19e22eea0e..2ddc2397fa1 100644
--- a/modules/indexer/code/search.go
+++ b/modules/indexer/code/search.go
@@ -16,14 +16,18 @@ 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 []ResultLine
+}
+
+type ResultLine struct {
+ Num int
+ FormattedContent template.HTML
}
type SearchResultLanguages = internal.SearchResultLanguages
@@ -70,7 +74,7 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res
var formattedLinesBuffer bytes.Buffer
contentLines := strings.SplitAfter(result.Content[startIndex:endIndex], "\n")
- lineNumbers := make([]int, len(contentLines))
+ lines := make([]ResultLine, 0, len(contentLines))
index := startIndex
for i, line := range contentLines {
var err error
@@ -93,21 +97,29 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res
return nil, err
}
- lineNumbers[i] = startLineNum + i
+ lines = append(lines, ResultLine{Num: startLineNum + i})
index += len(line)
}
- highlighted, _ := highlight.Code(result.Filename, "", formattedLinesBuffer.String())
+ // 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")
+
+ // 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])
+ }
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 bb21a5e0dca..08bb12951d8 100644
--- a/templates/code/searchresults.tmpl
+++ b/templates/code/searchresults.tmpl
@@ -22,20 +22,7 @@
{{ctx.Locale.Tr "repo.diff.view_file"}}
-
-
-
-
-
- {{range .LineNumbers}}
- {{.}}
- {{end}}
- |
- {{.FormattedLines}} |
-
-
-
-
+ {{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 7b3ad7282ec..7513d444cc1 100644
--- a/templates/repo/search.tmpl
+++ b/templates/repo/search.tmpl
@@ -44,20 +44,7 @@
{{ctx.Locale.Tr "repo.diff.view_file"}}
-
-
-
-
-
- {{range .LineNumbers}}
- {{.}}
- {{end}}
- |
- {{.FormattedLines}} |
-
-
-
-
+ {{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
new file mode 100644
index 00000000000..280584e4d1f
--- /dev/null
+++ b/templates/shared/searchfile.tmpl
@@ -0,0 +1,14 @@
+
+
+
+ {{range .SearchResult.Lines}}
+
+
+ {{.Num}}
+ |
+ {{.FormattedContent}} |
+
+ {{end}}
+
+
+