Skip to content

Commit 5b85269

Browse files
feat: factor out child process handling
1 parent 5682f29 commit 5b85269

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

src/view.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,47 @@ export function activate(context: vscode.ExtensionContext) {
1818

1919
let child: ChildProcessWithoutNullStreams | undefined
2020

21-
async function getPatternRes(pattern: string) {
22-
if (!pattern) {
23-
return
24-
}
25-
let command = workspace
26-
.getConfiguration('astGrep')
27-
.get('serverPath', 'ast-grep')
28-
const uris = workspace.workspaceFolders?.map(i => i.uri?.fsPath) ?? []
29-
30-
// TODO: multi-workspaces support
31-
// TODO: the code here is wrong, but we will change it
21+
function streamedPromise(
22+
proc: ChildProcessWithoutNullStreams
23+
): Promise<string> {
3224
let stdout = ''
3325
if (child) {
3426
// kill previous search
3527
child.kill('SIGTERM')
3628
}
37-
child = spawn(command, ['run', '--pattern', pattern, '--json=compact'], {
38-
cwd: uris[0]
39-
})
29+
child = proc
4030
child.stdout.on('data', data => {
4131
stdout += data
4232
})
43-
await new Promise((resolve, reject) =>
33+
return new Promise((resolve, reject) =>
4434
child!.on('exit', (code, signal) => {
4535
// exit without signal, search ends correctly
4636
// TODO: is it correct now?
4737
if (!signal && code === 0) {
48-
resolve(code)
38+
child = undefined
39+
resolve(stdout)
4940
} else {
5041
reject([code, signal])
5142
}
5243
})
5344
)
54-
child = undefined
45+
}
5546

47+
async function getPatternRes(pattern: string) {
48+
if (!pattern) {
49+
return
50+
}
51+
let command = workspace
52+
.getConfiguration('astGrep')
53+
.get('serverPath', 'ast-grep')
54+
const uris = workspace.workspaceFolders?.map(i => i.uri?.fsPath) ?? []
55+
56+
// TODO: multi-workspaces support
57+
// TODO: the code here is wrong, but we will change it
58+
let proc = spawn(command, ['run', '--pattern', pattern, '--json=compact'], {
59+
cwd: uris[0]
60+
})
61+
const stdout = await streamedPromise(proc)
5662
try {
5763
let res: SgSearch[] = JSON.parse(stdout)
5864
return res

0 commit comments

Comments
 (0)