Skip to content

Commit c6933a1

Browse files
fix: add run YAML rule
1 parent 09981a7 commit c6933a1

File tree

7 files changed

+77
-31
lines changed

7 files changed

+77
-31
lines changed

src/extension/preview.ts

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,22 @@ import {
2020
window,
2121
workspace,
2222
} from 'vscode'
23-
import type { ChildToParent, Diff, DisplayResult, SearchQuery, SgSearch } from '../types'
23+
import type {
24+
ChildToParent,
25+
Diff,
26+
DisplayResult,
27+
PatternQuery,
28+
SearchQuery,
29+
SgSearch,
30+
YAMLQuery,
31+
} from '../types'
2432
import { parentPort, streamedPromise } from './common'
2533
import { buildCommand, splitByHighLightToken } from './search'
2634

2735
const SCHEME = 'sgpreview'
2836
let lastPattern = ''
2937
let lastRewrite = ''
38+
let lastYAML = ''
3039

3140
/**
3241
* NB A file will only have one preview at a time
@@ -169,7 +178,15 @@ function closeAllDiffs() {
169178
}
170179
}
171180

172-
function refreshDiff(query: SearchQuery) {
181+
function refreshYAMLDiff(query: YAMLQuery) {
182+
if (query.yaml === lastYAML) {
183+
return
184+
}
185+
previewContents.clear()
186+
closeAllDiffs()
187+
}
188+
189+
function refreshPatternDiff(query: PatternQuery) {
173190
try {
174191
// Clear cache if pattern/rewrite changed
175192
if (query.pattern !== lastPattern || query.rewrite !== lastRewrite) {
@@ -190,6 +207,15 @@ function refreshDiff(query: SearchQuery) {
190207
lastRewrite = query.rewrite
191208
}
192209
}
210+
211+
function refreshDiff(query: SearchQuery) {
212+
if ('yaml' in query) {
213+
return refreshYAMLDiff(query)
214+
} else {
215+
return refreshPatternDiff(query)
216+
}
217+
}
218+
193219
parentPort.onMessage('openFile', openFile)
194220
parentPort.onMessage('previewDiff', previewDiff)
195221
parentPort.onMessage('dismissDiff', dismissDiff)
@@ -206,34 +232,25 @@ async function onReplaceAll(payload: ChildToParent['replaceAll']) {
206232
if (confirmed !== 'Replace') {
207233
return
208234
}
209-
const { id, pattern, rewrite, selector, strictness, lang } = payload
210-
for (const change of payload.changes) {
235+
const { changes, ...other } = payload
236+
for (const change of changes) {
211237
// TODO: chunk change
212238
await onCommitChange({
213-
id,
214-
pattern,
215-
rewrite,
216-
selector,
217-
strictness,
218-
lang,
239+
...other,
219240
...change,
220241
})
221242
}
222243
}
223244

224245
async function onCommitChange(payload: ChildToParent['commitChange']) {
225-
const { filePath, pattern, rewrite, strictness, selector, lang } = payload
246+
const { filePath, ...fields } = payload
226247
const fileUri = workspaceUriFromFilePath(filePath)
227248
if (!fileUri) {
228249
return
229250
}
230251
await doChange(fileUri, payload)
231252
await refreshSearchResult(payload.id, fileUri, {
232-
pattern,
233-
rewrite,
234-
strictness,
235-
selector,
236-
lang,
253+
...fields,
237254
includeFile: filePath,
238255
})
239256
}

src/extension/search.ts

Lines changed: 9 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, YAMLConfig } from '../types'
5+
import type { DisplayResult, PatternQuery, SearchQuery, SgSearch, YAMLConfig } from '../types'
66
import { normalizeCommandForWindows, parentPort, resolveBinary, streamedPromise } from './common'
77

88
/**
@@ -109,6 +109,14 @@ async function uniqueCommand(
109109

110110
// TODO: add unit test for commandBuilder
111111
export function buildCommand(query: SearchQuery) {
112+
if ('yaml' in query) {
113+
return buildYAMLCommand(query)
114+
} else {
115+
return buildPatternCommand(query)
116+
}
117+
}
118+
119+
function buildPatternCommand(query: PatternQuery) {
112120
const { pattern, includeFile, strictness } = query
113121
if (!pattern) {
114122
return

src/types.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,30 @@ export interface DisplayResult {
2727
language: string
2828
}
2929

30-
interface SearchQueryBasic {
30+
interface PatternhQueryBasic {
3131
pattern: string
3232
rewrite: string
3333
strictness: string
3434
selector: string
3535
lang: string
3636
}
3737

38-
export interface SearchQuery extends SearchQueryBasic {
38+
export type PatternQuery = PatternhQueryBasic & {
3939
includeFile: string
4040
}
4141

42+
interface YAMLQueryBasic {
43+
yaml: string
44+
}
45+
46+
export type YAMLQuery = YAMLQueryBasic & {
47+
includeFile: string
48+
}
49+
50+
export type SearchQueryBasic = PatternhQueryBasic | YAMLQueryBasic
51+
52+
export type SearchQuery = PatternQuery | YAMLQuery
53+
4254
export type SgSearch = {
4355
text: string
4456
range: RangeInfo
@@ -82,6 +94,7 @@ export interface Diff {
8294
export interface YAMLConfig {
8395
yaml: string
8496
includeFile: string
97+
rewrite?: boolean
8598
}
8699

87100
export interface ChildToParent {

src/webview/SearchSidebar/SearchProviderMessage/index.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@ const codeStyle = {
2020
} as const
2121

2222
function Empty({ query }: { query: SearchQuery }) {
23-
const { pattern, includeFile } = query
24-
if (!pattern) {
25-
return null
23+
if ('yaml' in query) {
24+
const id = /id:\s*([^,\n]+)/.exec(query.yaml)?.[1]
25+
return (
26+
<div style={style}>
27+
No results found for YAML <code style={codeStyle}>{id}</code>
28+
</div>
29+
)
2630
}
31+
const { pattern, includeFile } = query
2732
return (
2833
<div style={style}>
2934
No results found for <code style={codeStyle}>{pattern}</code>

src/webview/SearchSidebar/SearchWidgetContainer/YamlWidget.tsx

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

63
const styles = stylex.create({
74
yaml: {
@@ -19,7 +16,6 @@ const styles = stylex.create({
1916
})
2017

2118
export default function YamlWidget() {
22-
const [value, setValue] = useState('')
2319
return (
2420
<div {...stylex.props(styles.yaml)}>
2521
</div>

src/webview/hooks/useQuery.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { useEffect, useState } from 'react'
22
import { useBoolean, useDebounce, useLocalStorage } from 'react-use'
3-
import { SearchQuery } from '../../types'
3+
import { PatternQuery, SearchQuery } from '../../types'
44
import { childPort } from '../postMessage'
55
export { SearchQuery }
66
// this is the single sole point of communication
77
// between search query and search result
88
import { postSearch } from './useSearch'
99

10-
const searchQuery: Record<keyof SearchQuery, string> = {
10+
const searchQuery: Record<keyof PatternQuery, string> = {
1111
pattern: '',
1212
strictness: 'smart',
1313
selector: '',
@@ -18,7 +18,7 @@ const searchQuery: Record<keyof SearchQuery, string> = {
1818

1919
type PatternKeys = 'selector'
2020

21-
const LS_KEYS: Record<Exclude<keyof SearchQuery, PatternKeys>, string> = {
21+
const LS_KEYS: Record<Exclude<keyof PatternQuery, PatternKeys>, string> = {
2222
pattern: 'ast-grep-search-panel-input-value',
2323
includeFile: 'ast-grep-search-panel-include-value',
2424
rewrite: 'ast-grep-search-panel-rewrite-value',

src/webview/hooks/useSearch.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const MOD = 1e9 + 7
2020
// maintain the latest search task id and callback
2121
let id = 0
2222
let grouped: [string, DisplayResult[]][] = []
23-
let queryInFlight: SearchQuery = {
23+
let queryInFlight: SearchQuery | YAMLConfig = {
2424
pattern: '',
2525
includeFile: '',
2626
rewrite: '',
@@ -175,11 +175,18 @@ function getSnapshot() {
175175
return version // symbolic snapshot for react
176176
}
177177

178+
function queryHasRewrite() {
179+
if ('yaml' in queryInFlight) {
180+
return /^fix:/.test(queryInFlight.yaml)
181+
}
182+
return !!queryInFlight.rewrite
183+
}
184+
178185
/**
179186
* Either open a file or preview the diff
180187
*/
181188
export function openAction(payload: OpenPayload) {
182-
if (!queryInFlight.rewrite) {
189+
if (!queryHasRewrite()) {
183190
openFile(payload)
184191
return
185192
}

0 commit comments

Comments
 (0)