aboutsummaryrefslogtreecommitdiffstats
path: root/modules/indexer/code/search.go
diff options
context:
space:
mode:
Diffstat (limited to 'modules/indexer/code/search.go')
-rw-r--r--modules/indexer/code/search.go35
1 files changed, 18 insertions, 17 deletions
diff --git a/modules/indexer/code/search.go b/modules/indexer/code/search.go
index 51c7595cf8..5f35e8073b 100644
--- a/modules/indexer/code/search.go
+++ b/modules/indexer/code/search.go
@@ -70,13 +70,27 @@ func writeStrings(buf *bytes.Buffer, strs ...string) error {
return nil
}
+func HighlightSearchResultCode(filename string, lineNums []int, code string) []ResultLine {
+ // we should highlight the whole code block first, otherwise it doesn't work well with multiple line highlighting
+ hl, _ := highlight.Code(filename, "", code)
+ highlightedLines := strings.Split(string(hl), "\n")
+
+ // The lineNums outputted by highlight.Code might not match the original lineNums, because "highlight" removes the last `\n`
+ lines := make([]ResultLine, min(len(highlightedLines), len(lineNums)))
+ for i := 0; i < len(lines); i++ {
+ lines[i].Num = lineNums[i]
+ lines[i].FormattedContent = template.HTML(highlightedLines[i])
+ }
+ return lines
+}
+
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([]ResultLine, 0, len(contentLines))
+ lineNums := make([]int, 0, len(contentLines))
index := startIndex
for i, line := range contentLines {
var err error
@@ -91,29 +105,16 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res
line[closeActiveIndex:],
)
} else {
- err = writeStrings(&formattedLinesBuffer,
- line,
- )
+ err = writeStrings(&formattedLinesBuffer, line)
}
if err != nil {
return nil, err
}
- lines = append(lines, ResultLine{Num: startLineNum + i})
+ lineNums = append(lineNums, startLineNum+i)
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")
-
- // 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,
@@ -121,7 +122,7 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res
UpdatedUnix: result.UpdatedUnix,
Language: result.Language,
Color: result.Color,
- Lines: lines,
+ Lines: HighlightSearchResultCode(result.Filename, lineNums, formattedLinesBuffer.String()),
}, nil
}