Skip to content

Commit 08f5783

Browse files
feat: refactor code segments
1 parent 0043032 commit 08f5783

File tree

1 file changed

+20
-75
lines changed

1 file changed

+20
-75
lines changed
Lines changed: 20 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,36 @@
11
import { Box } from '@chakra-ui/react'
2-
import React, { useMemo } from 'react'
32
import type { SgSearch } from '../../../../types'
43
import { openFile } from '../../postMessage'
54

6-
type HighLightToken = {
7-
start: {
8-
line: number
9-
column: number
10-
}
11-
end: {
12-
line: number
13-
column: number
14-
}
15-
style: React.CSSProperties
5+
const style = {
6+
backgroundColor: 'var(--vscode-editor-findMatchHighlightBackground)',
7+
border: '1px solid var(--vscode-editor-findMatchHighlightBackground)'
168
}
179

18-
function splitByHighLightTokens(tokens: HighLightToken[]) {
19-
const codeSegments = tokens
20-
.map(({ start, end, style }) => {
21-
// TODO: multilines highlight
22-
const { column: startColumn } = start
23-
const { column: endColumn } = end
24-
25-
const startIdx = startColumn
26-
const endIdx = endColumn
10+
function splitByHighLightToken(search: SgSearch) {
11+
const { start, end } = search.range
12+
// TODO: multilines highlight
13+
const { column: startColumn } = start
14+
const { column: endColumn } = end
2715

28-
return {
29-
range: [startIdx, endIdx],
30-
style
31-
}
32-
})
33-
.sort((a, b) => a.range[0] - b.range[0])
16+
const startIdx = startColumn
17+
const endIdx = endColumn
3418

35-
return codeSegments
19+
return {
20+
index: [startIdx, endIdx]
21+
}
3622
}
3723

3824
interface CodeBlockProps {
3925
match: SgSearch
4026
}
4127
export const CodeBlock = ({ match }: CodeBlockProps) => {
42-
const { file, lines, range } = match
43-
const matchHighlight = [
44-
{
45-
start: {
46-
line: range.start.line,
47-
column: range.start.column
48-
},
49-
end: {
50-
line: range.end.line,
51-
column: range.end.column
52-
},
53-
style: {
54-
backgroundColor: 'var(--vscode-editor-findMatchHighlightBackground)',
55-
border: '1px solid var(--vscode-editor-findMatchHighlightBackground)'
56-
}
57-
}
58-
]
59-
60-
const codeSegments = useMemo(() => {
61-
return splitByHighLightTokens(matchHighlight)
62-
}, [lines, matchHighlight])
28+
const { file, lines } = match
6329

64-
if (codeSegments.length === 0) {
65-
return (
66-
<Box
67-
flex="1"
68-
textOverflow="ellipsis"
69-
overflow="hidden"
70-
whiteSpace="pre"
71-
fontSize="13px"
72-
lineHeight="22px"
73-
height="22px"
74-
>
75-
{lines}
76-
</Box>
77-
)
78-
}
30+
const { index } = splitByHighLightToken(match)
7931

80-
const highlightStartIdx = codeSegments[0].range[0]
81-
const highlightEndIdx = codeSegments[codeSegments.length - 1].range[1]
32+
const startIdx = index[0]
33+
const endIdx = index[1]
8234
return (
8335
<Box
8436
flex="1"
@@ -93,16 +45,9 @@ export const CodeBlock = ({ match }: CodeBlockProps) => {
9345
openFile({ filePath: file, locationsToSelect: match.range })
9446
}}
9547
>
96-
{highlightStartIdx <= 0 ? '' : lines.slice(0, highlightStartIdx)}
97-
{codeSegments.map(({ range, style }) => {
98-
const [start, end] = range
99-
return (
100-
<span style={style} key={`${start}-${end}`}>
101-
{lines.slice(start, end)}
102-
</span>
103-
)
104-
})}
105-
{highlightEndIdx >= lines.length ? '' : lines.slice(highlightEndIdx)}
48+
{startIdx <= 0 ? '' : lines.slice(0, startIdx)}
49+
<span style={style}>{lines.slice(startIdx, endIdx)}</span>
50+
{endIdx >= lines.length ? '' : lines.slice(endIdx)}
10651
</Box>
10752
)
10853
}

0 commit comments

Comments
 (0)