@@ -7,16 +7,25 @@ const style = {
77 border : '1px solid var(--vscode-editor-findMatchHighlightBackground)'
88}
99
10+ const LEADING_SPACES_RE = / ^ \s * /
11+
1012function splitByHighLightToken ( search : SgSearch ) {
1113 const { start, end } = search . range
1214 let startIdx = start . column
1315 let endIdx = end . column
1416 let displayLine = search . lines
15- // multiline matches!
17+ // multiline matches! only display the first line!
1618 if ( start . line < end . line ) {
1719 displayLine = search . lines . split ( / \r ? \n / , 1 ) [ 0 ]
1820 endIdx = displayLine . length
1921 }
22+ // strip leading spaces
23+ const leadingSpaces = displayLine . match ( LEADING_SPACES_RE ) ?. [ 0 ] . length
24+ if ( leadingSpaces ) {
25+ displayLine = displayLine . substring ( leadingSpaces )
26+ startIdx -= leadingSpaces
27+ endIdx -= leadingSpaces
28+ }
2029 return {
2130 index : [ startIdx , endIdx ] ,
2231 displayLine
@@ -27,9 +36,8 @@ interface CodeBlockProps {
2736 match : SgSearch
2837}
2938export const CodeBlock = ( { match } : CodeBlockProps ) => {
30- const { file, lines } = match
31-
32- const { index } = splitByHighLightToken ( match )
39+ const { file } = match
40+ const { index, displayLine } = splitByHighLightToken ( match )
3341
3442 const startIdx = index [ 0 ]
3543 const endIdx = index [ 1 ]
@@ -47,9 +55,9 @@ export const CodeBlock = ({ match }: CodeBlockProps) => {
4755 openFile ( { filePath : file , locationsToSelect : match . range } )
4856 } }
4957 >
50- { startIdx <= 0 ? '' : lines . slice ( 0 , startIdx ) }
51- < span style = { style } > { lines . slice ( startIdx , endIdx ) } </ span >
52- { endIdx >= lines . length ? '' : lines . slice ( endIdx ) }
58+ { displayLine . slice ( 0 , startIdx ) }
59+ < span style = { style } > { displayLine . slice ( startIdx , endIdx ) } </ span >
60+ { displayLine . slice ( endIdx ) }
5361 </ Box >
5462 )
5563}
0 commit comments