Skip to content

Commit 649216a

Browse files
refactor: move split line to extension host
1 parent 234bfef commit 649216a

File tree

7 files changed

+65
-52
lines changed

7 files changed

+65
-52
lines changed

src/types.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,27 @@ export type Position = {
66
index?: number
77
}
88

9+
export interface RangeInfo {
10+
byteOffset: {
11+
start: number
12+
end: number
13+
}
14+
start: Position
15+
end: Position
16+
}
17+
18+
export interface DisplayResult {
19+
file: string
20+
startIdx: number
21+
endIdx: number
22+
displayLine: string
23+
lineSpan: number
24+
range: RangeInfo
25+
}
26+
927
export type SgSearch = {
1028
text: string
11-
range: {
12-
byteOffset: {
13-
start: number
14-
end: number
15-
}
16-
start: Position
17-
end: Position
18-
}
29+
range: RangeInfo
1930
file: string
2031
lines: string
2132
language: string
@@ -24,7 +35,7 @@ export type SgSearch = {
2435
export type Definition = {
2536
parent2child: {
2637
searchResultStreaming: {
27-
searchResult: SgSearch[]
38+
searchResult: DisplayResult[]
2839
id: number
2940
inputValue: string
3041
}

src/view.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Definition, ParentPort, SgSearch } from './types'
1+
import type { Definition, ParentPort, SgSearch, DisplayResult } from './types'
22
import { Unport, ChannelMessage } from 'unport'
33
import * as vscode from 'vscode'
44
import { workspace } from 'vscode'
@@ -15,6 +15,36 @@ export function activate(context: vscode.ExtensionContext) {
1515
)
1616
)
1717
}
18+
19+
const LEADING_SPACES_RE = /^\s*/
20+
21+
function splitByHighLightToken(search: SgSearch): DisplayResult {
22+
const { start, end } = search.range
23+
let startIdx = start.column
24+
let endIdx = end.column
25+
let displayLine = search.lines
26+
// multiline matches! only display the first line!
27+
if (start.line < end.line) {
28+
displayLine = search.lines.split(/\r?\n/, 1)[0]
29+
endIdx = displayLine.length
30+
}
31+
// strip leading spaces
32+
const leadingSpaces = displayLine.match(LEADING_SPACES_RE)?.[0].length
33+
if (leadingSpaces) {
34+
displayLine = displayLine.substring(leadingSpaces)
35+
startIdx -= leadingSpaces
36+
endIdx -= leadingSpaces
37+
}
38+
return {
39+
startIdx,
40+
endIdx,
41+
displayLine,
42+
lineSpan: end.line - start.line,
43+
file: search.file,
44+
range: search.range
45+
}
46+
}
47+
1848
type StreamingHandler = (r: SgSearch[]) => void
1949

2050
let child: ChildProcessWithoutNullStreams | undefined
@@ -143,7 +173,7 @@ function setupParentPort(webviewView: vscode.WebviewView) {
143173
await getPatternRes(payload.inputValue, ret => {
144174
parentPort.postMessage('searchResultStreaming', {
145175
...payload,
146-
searchResult: ret
176+
searchResult: ret.map(splitByHighLightToken)
147177
})
148178
})
149179
parentPort.postMessage('searchEnd', payload)

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

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,12 @@
11
import { Box } from '@chakra-ui/react'
2-
import type { SgSearch } from '../../../../types'
2+
import type { DisplayResult } from '../../../../types'
33
import { openFile } from '../../../postMessage'
44

55
const style = {
66
backgroundColor: 'var(--vscode-editor-findMatchHighlightBackground)',
77
border: '1px solid var(--vscode-editor-findMatchHighlightBorder)'
88
}
99

10-
const LEADING_SPACES_RE = /^\s*/
11-
12-
function splitByHighLightToken(search: SgSearch) {
13-
const { start, end } = search.range
14-
let startIdx = start.column
15-
let endIdx = end.column
16-
let displayLine = search.lines
17-
// multiline matches! only display the first line!
18-
if (start.line < end.line) {
19-
displayLine = search.lines.split(/\r?\n/, 1)[0]
20-
endIdx = displayLine.length
21-
}
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-
}
29-
return {
30-
startIdx,
31-
endIdx,
32-
displayLine,
33-
lineSpan: end.line - start.line
34-
}
35-
}
36-
3710
// this is also hardcoded in vscode
3811
const lineIndicatorStyle = {
3912
margin: '0 7px 4px',
@@ -50,11 +23,10 @@ function MultiLineIndicator({ lineSpan }: { lineSpan: number }) {
5023
}
5124

5225
interface CodeBlockProps {
53-
match: SgSearch
26+
match: DisplayResult
5427
}
5528
export const CodeBlock = ({ match }: CodeBlockProps) => {
56-
const { startIdx, endIdx, displayLine, lineSpan } =
57-
splitByHighLightToken(match)
29+
const { startIdx, endIdx, displayLine, lineSpan } = match
5830

5931
return (
6032
<Box

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { HStack, IconButton, VStack, Box } from '@chakra-ui/react'
22
import { HiOutlineChevronDown, HiOutlineChevronRight } from 'react-icons/hi'
33
import { useBoolean } from 'react-use'
4-
import type { SgSearch } from '../../../../types'
4+
import type { DisplayResult } from '../../../../types'
55
import { CodeBlock } from './CodeBlock'
66
import { FileLink } from './FileLink'
77
import { VSCodeBadge } from '@vscode/webview-ui-toolkit/react'
88
import { memo } from 'react'
99

1010
interface CodeBlockListProps {
11-
matches: SgSearch[]
11+
matches: DisplayResult[]
1212
}
1313

1414
const CodeBlockList = memo(({ matches }: CodeBlockListProps) => {
@@ -37,7 +37,7 @@ const CodeBlockList = memo(({ matches }: CodeBlockListProps) => {
3737

3838
interface TreeItemProps {
3939
filePath: string
40-
matches: SgSearch[]
40+
matches: DisplayResult[]
4141
}
4242

4343
const TreeItem = ({ filePath, matches }: TreeItemProps) => {

src/webview/SearchSidebar/SearchResultList/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { memo } from 'react'
2-
import { SgSearch } from '../../../types'
2+
import { DisplayResult } from '../../../types'
33
import TreeItem from './comps/TreeItem'
44

55
interface SearchResultListProps {
6-
matches: Array<[string, SgSearch[]]>
6+
matches: Array<[string, DisplayResult[]]>
77
}
88

99
const SearchResultList = ({ matches }: SearchResultListProps) => {

src/webview/hooks/useSearch.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { SgSearch } from '../postMessage'
1+
import type { DisplayResult } from '../postMessage'
22
import { childPort } from '../postMessage'
33
import {
44
useCallback,
@@ -14,7 +14,7 @@ const MOD = 1e9 + 7
1414

1515
// maintain the latest search task id and callback
1616
let id = 0
17-
let searchResult: SgSearch[] = []
17+
let searchResult: DisplayResult[] = []
1818
let queryInFlight = ''
1919
let searching = true
2020
let notify = () => {}
@@ -45,8 +45,8 @@ childPort.onMessage('searchEnd', event => {
4545
notify()
4646
})
4747

48-
function groupBy(matches: SgSearch[]) {
49-
const groups = new Map<string, SgSearch[]>()
48+
function groupBy(matches: DisplayResult[]) {
49+
const groups = new Map<string, DisplayResult[]>()
5050
for (const match of matches) {
5151
if (!groups.has(match.file)) {
5252
groups.set(match.file, [])

src/webview/postMessage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ChildPort, Definition } from '../types'
22
import { Unport } from 'unport'
3-
export type { SgSearch } from '../types'
3+
export type { DisplayResult } from '../types'
44

55
// @ts-expect-error
66
let vscode = acquireVsCodeApi()

0 commit comments

Comments
 (0)