@@ -76,6 +76,7 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res
76
76
77
77
contentLines := strings .SplitAfter (result .Content [startIndex :endIndex ], "\n " )
78
78
lines := make ([]ResultLine , 0 , len (contentLines ))
79
+ highlightedLines := make ([]string , 0 , len (contentLines ))
79
80
index := startIndex
80
81
for i , line := range contentLines {
81
82
var err error
@@ -100,11 +101,17 @@ func searchResult(result *internal.SearchResult, startIndex, endIndex int) (*Res
100
101
101
102
lines = append (lines , ResultLine {Num : startLineNum + i })
102
103
index += len (line )
104
+
105
+ // highlight.Code will remove the last `\n`, e.g. if input is `a\n`, output will be `a`
106
+ // Then the length of `lines` and `highlightedLines` will not be equal, so we need to add an empty line manually
107
+ if line == "" {
108
+ highlightedLines = append (highlightedLines , "" )
109
+ }
103
110
}
104
111
105
112
// we should highlight the whole code block first, otherwise it doesn't work well with multiple line highlighting
106
113
hl , _ := highlight .Code (result .Filename , "" , formattedLinesBuffer .String ())
107
- highlightedLines := strings .Split (string (hl ), "\n " )
114
+ highlightedLines = append ( strings .Split (string (hl ), "\n " ), highlightedLines ... )
108
115
109
116
if len (highlightedLines ) != len (lines ) {
110
117
return nil , fmt .Errorf ("the length of line numbers [%d] don't match the length of highlighted contents [%d]" , len (lines ), len (highlightedLines ))
0 commit comments