1- import type { Definition , ParentPort , SgSearch } from './types'
1+ import type { Definition , ParentPort , SgSearch , DisplayResult } from './types'
22import { Unport , ChannelMessage } from 'unport'
33import * as vscode from 'vscode'
44import { 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+
1848type StreamingHandler = ( r : SgSearch [ ] ) => void
1949
2050let 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 )
0 commit comments