@@ -2,7 +2,7 @@ import { type ChildProcessWithoutNullStreams, spawn } from 'node:child_process'
22import path from 'node:path'
33import { 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'
66import { 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+
163198parentPort . 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+
182236function searchByCode ( ) {
183237 const editor = window . activeTextEditor
184238 if ( ! editor ) {
0 commit comments