@@ -2,7 +2,7 @@ import type { Definition, ParentPort, SgSearch } from './types'
22import { Unport , ChannelMessage } from 'unport'
33import * as vscode from 'vscode'
44import { workspace } from 'vscode'
5- import { spawn } from 'node:child_process'
5+ import { type ChildProcessWithoutNullStreams , spawn } from 'node:child_process'
66
77export function activate ( context : vscode . ExtensionContext ) {
88 const provider = new SearchSidebarProvider ( context . extensionUri )
@@ -16,6 +16,8 @@ export function activate(context: vscode.ExtensionContext) {
1616 )
1717}
1818
19+ let child : ChildProcessWithoutNullStreams | undefined
20+
1921async function getPatternRes ( pattern : string ) {
2022 if ( ! pattern ) {
2123 return
@@ -28,17 +30,28 @@ async function getPatternRes(pattern: string) {
2830 // TODO: multi-workspaces support
2931 // TODO: the code here is wrong, but we will change it
3032 let stdout = ''
31- const child = spawn (
32- command ,
33- [ 'run' , '--pattern' , pattern , '--json=compact' ] ,
34- {
35- cwd : uris [ 0 ]
36- }
37- )
33+ if ( child ) {
34+ // kill previous search
35+ child . kill ( 'SIGTERM' )
36+ }
37+ child = spawn ( command , [ 'run' , '--pattern' , pattern , '--json=compact' ] , {
38+ cwd : uris [ 0 ]
39+ } )
3840 child . stdout . on ( 'data' , data => {
3941 stdout += data
4042 } )
41- await new Promise ( r => child . on ( 'close' , r ) )
43+ await new Promise ( ( resolve , reject ) =>
44+ child ! . on ( 'exit' , ( code , signal ) => {
45+ // exit without signal, search ends correctly
46+ // TODO: is it correct now?
47+ if ( ! signal && code === 0 ) {
48+ resolve ( code )
49+ } else {
50+ reject ( [ code , signal ] )
51+ }
52+ } )
53+ )
54+ child = undefined
4255
4356 try {
4457 let res : SgSearch [ ] = JSON . parse ( stdout )
0 commit comments