summaryrefslogtreecommitdiffstats
path: root/modules/indexer
diff options
context:
space:
mode:
authoryp05327 <576951401@qq.com>2024-03-06 16:24:43 +0900
committerGitHub <noreply@github.com>2024-03-06 07:24:43 +0000
commita2b0fb1a64b0794b808a013089758a49f56d8915 (patch)
tree39714ac6177b5dd29ad2c06960375e8f5d1bc42e /modules/indexer
parent5cddab4f74bbb307ddf13e458c7ac22f93b9283a (diff)
downloadgitea-a2b0fb1a64b0794b808a013089758a49f56d8915.tar.gz
gitea-a2b0fb1a64b0794b808a013089758a49f56d8915.zip
Fix wrong line number in code search result (#29260)
Fix #29136 Before: The result is a table and all line numbers are all in one row. After: Use a separate table column for the line numbers. --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Diffstat (limited to 'modules/indexer')
-rw-r--r--modules/indexer/code/search.go50
1 files changed, 31 insertions, 19 deletions
diff --git a/modules/indexer/code/search.go b/modules/indexer/code/search.go
index e19e22eea0..2ddc2397fa 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
}