Skip to content

Commit 4bb8831

Browse files
feat: add experimental yaml support
fix #167
1 parent 7663796 commit 4bb8831

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

src/extension/search.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { type ChildProcessWithoutNullStreams, spawn } from 'node:child_process'
22
import path from 'node:path'
33
import { commands, type ExtensionContext, window, workspace } from 'vscode'
44

5-
import type { DisplayResult, SearchQuery, SgSearch } from '../types'
5+
import type { DisplayResult, SearchQuery, SgSearch, YAMLConfig } from '../types'
66
import { normalizeCommandForWindows, parentPort, resolveBinary, streamedPromise } from './common'
77

88
/**
@@ -160,6 +160,41 @@ function getPatternRes(query: SearchQuery, handlers: Handlers) {
160160
return uniqueCommand(proc, handlers.onData)
161161
}
162162

163+
function buildYAMLCommand(config: YAMLConfig) {
164+
const { yaml, includeFile } = config
165+
if (!yaml) {
166+
return
167+
}
168+
const command = resolveBinary()
169+
const { normalizedCommand, shell } = normalizeCommandForWindows(command)
170+
const uris = workspace.workspaceFolders?.map(i => i.uri?.fsPath) ?? []
171+
const args = ['scan', '--inline-rules', yaml, '--json=stream']
172+
const validIncludeFile = includeFile.split(',').filter(Boolean)
173+
const hasGlobPattern = validIncludeFile.some(i => i.includes('*'))
174+
if (hasGlobPattern) {
175+
args.push(...validIncludeFile.map(i => `--globs=${i}`))
176+
} else {
177+
args.push(...validIncludeFile)
178+
}
179+
console.debug('scanning', config, normalizedCommand, args)
180+
// TODO: multi-workspaces support
181+
return spawn(normalizedCommand, args, {
182+
shell,
183+
cwd: uris[0],
184+
})
185+
}
186+
187+
function getYAMLRes(config: YAMLConfig, handlers: Handlers) {
188+
const proc = buildYAMLCommand(config)
189+
if (proc) {
190+
proc.on('error', error => {
191+
console.debug('ast-grep CLI runs error')
192+
handlers.onError(error)
193+
})
194+
}
195+
return uniqueCommand(proc, handlers.onData)
196+
}
197+
163198
parentPort.onMessage('search', async payload => {
164199
const onData = (ret: SgSearch[]) => {
165200
parentPort.postMessage('searchResultStreaming', {
@@ -179,6 +214,25 @@ parentPort.onMessage('search', async payload => {
179214
parentPort.postMessage('searchEnd', payload)
180215
})
181216

217+
parentPort.onMessage('yaml', async payload => {
218+
const onData = (ret: SgSearch[]) => {
219+
parentPort.postMessage('searchResultStreaming', {
220+
...payload,
221+
searchResult: ret.map(splitByHighLightToken),
222+
})
223+
}
224+
await getYAMLRes(payload, {
225+
onData,
226+
onError(error) {
227+
parentPort.postMessage('error', {
228+
error,
229+
...payload,
230+
})
231+
},
232+
})
233+
parentPort.postMessage('searchEnd', payload)
234+
})
235+
182236
function searchByCode() {
183237
const editor = window.activeTextEditor
184238
if (!editor) {

src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,14 @@ export interface Diff {
7878
range: RangeInfo
7979
}
8080

81+
export interface YAMLConfig {
82+
yaml: string
83+
includeFile: string
84+
}
85+
8186
export interface ChildToParent {
8287
search: WithId<SearchQuery>
88+
yaml: WithId<YAMLConfig>
8389
reload: unknown
8490
openFile: {
8591
filePath: string

src/webview/SearchSidebar/SearchWidgetContainer/YamlWidget.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as stylex from '@stylexjs/stylex'
22
import { VSCodeButton, VSCodeTextArea } from '@vscode/webview-ui-toolkit/react'
33
import { useState } from 'react'
4+
import { postYAML } from '../../hooks/useSearch'
45

56
const styles = stylex.create({
67
yaml: {
@@ -36,7 +37,7 @@ export default function YamlWidget() {
3637
appearance="primary"
3738
onClick={() => {
3839
// Handle save or apply logic here
39-
console.log('YAML Config:', value)
40+
postYAML({ yaml: value, includeFile: '' })
4041
}}
4142
>
4243
Search

src/webview/hooks/useSearch.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useSyncExternalStore } from 'react'
2+
import { YAMLConfig } from '../../types'
23
import {
34
childPort,
45
commitChange,
@@ -67,6 +68,15 @@ function postSearch(searchQuery: SearchQuery) {
6768
notify()
6869
}
6970

71+
export function postYAML(config: YAMLConfig) {
72+
id = (id + 1) % MOD
73+
childPort.postMessage('yaml', { id, ...config })
74+
searching = true
75+
hasStaleResult = true
76+
searchError = null
77+
notify()
78+
}
79+
7080
childPort.onMessage('searchResultStreaming', event => {
7181
const { id: eventId, ...query } = event
7282
if (eventId !== id) {

0 commit comments

Comments
 (0)