Skip to content

Commit 930866c

Browse files
feat: improve leading spaces handling
1 parent 17ed998 commit 930866c

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/webview/SearchSidebar/SearchResultList/comps/CodeBlock.tsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,25 @@ const style = {
77
border: '1px solid var(--vscode-editor-findMatchHighlightBackground)'
88
}
99

10+
const LEADING_SPACES_RE = /^\s*/
11+
1012
function 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
}
2938
export 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

Comments
 (0)