|
| 1 | +import { Box } from '@chakra-ui/react' |
| 2 | +import React, { useMemo } from 'react' |
| 3 | +import type { SgSearch } from '../../../../types' |
| 4 | +import { openFile } from '../../postMessage' |
| 5 | + |
| 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 |
| 16 | +} |
| 17 | + |
| 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 |
| 27 | + |
| 28 | + return { |
| 29 | + range: [startIdx, endIdx], |
| 30 | + style |
| 31 | + } |
| 32 | + }) |
| 33 | + .sort((a, b) => a.range[0] - b.range[0]) |
| 34 | + |
| 35 | + return codeSegments |
| 36 | +} |
| 37 | + |
| 38 | +interface CodeBlockProps { |
| 39 | + match: SgSearch |
| 40 | +} |
| 41 | +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]) |
| 63 | + |
| 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 | + } |
| 79 | + |
| 80 | + const highlightStartIdx = codeSegments[0].range[0] |
| 81 | + const highlightEndIdx = codeSegments[codeSegments.length - 1].range[1] |
| 82 | + return ( |
| 83 | + <Box |
| 84 | + flex="1" |
| 85 | + textOverflow="ellipsis" |
| 86 | + overflow="hidden" |
| 87 | + whiteSpace="pre" |
| 88 | + fontSize="13px" |
| 89 | + lineHeight="22px" |
| 90 | + height="22px" |
| 91 | + cursor="pointer" |
| 92 | + onClick={() => { |
| 93 | + openFile({ filePath: file, locationsToSelect: match.range }) |
| 94 | + }} |
| 95 | + > |
| 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)} |
| 106 | + </Box> |
| 107 | + ) |
| 108 | +} |
0 commit comments