Skip to content

Commit 8e5d263

Browse files
refactor: rename query
1 parent 09c4cd2 commit 8e5d263

File tree

7 files changed

+44
-35
lines changed

7 files changed

+44
-35
lines changed

src/extension/preview.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,10 @@ function closeAllDiffs() {
165165
function refreshDiff(query: SearchQuery) {
166166
try {
167167
// Clear cache if pattern/rewrite changed
168-
if (query.inputValue !== lastPattern || query.rewrite !== lastRewrite) {
168+
if (query.pattern !== lastPattern || query.rewrite !== lastRewrite) {
169169
previewContents.clear()
170170
}
171-
if (query.inputValue !== lastPattern) {
171+
if (query.pattern !== lastPattern) {
172172
closeAllDiffs()
173173
return
174174
}
@@ -179,7 +179,7 @@ function refreshDiff(query: SearchQuery) {
179179
closeAllDiffs()
180180
} finally {
181181
// use finally to ensure updated
182-
lastPattern = query.inputValue
182+
lastPattern = query.pattern
183183
lastRewrite = query.rewrite
184184
}
185185
}
@@ -199,28 +199,32 @@ async function onReplaceAll(payload: ChildToParent['replaceAll']) {
199199
if (confirmed !== 'Replace') {
200200
return
201201
}
202-
const { id, inputValue, rewrite } = payload
202+
const { id, pattern, rewrite, selector, strictness } = payload
203203
for (const change of payload.changes) {
204204
// TODO: chunk change
205205
await onCommitChange({
206206
id,
207-
inputValue,
207+
pattern,
208208
rewrite,
209+
selector,
210+
strictness,
209211
...change,
210212
})
211213
}
212214
}
213215

214216
async function onCommitChange(payload: ChildToParent['commitChange']) {
215-
const { filePath, inputValue, rewrite } = payload
217+
const { filePath, pattern, rewrite, strictness, selector } = payload
216218
const fileUri = workspaceUriFromFilePath(filePath)
217219
if (!fileUri) {
218220
return
219221
}
220222
await doChange(fileUri, payload)
221223
await refreshSearchResult(payload.id, fileUri, {
222-
inputValue,
224+
pattern,
223225
rewrite,
226+
strictness,
227+
selector,
224228
includeFile: filePath,
225229
})
226230
}
@@ -244,7 +248,7 @@ async function refreshSearchResult(
244248
query: SearchQuery,
245249
) {
246250
const command = buildCommand({
247-
pattern: query.inputValue,
251+
pattern: query.pattern,
248252
rewrite: query.rewrite,
249253
includeFiles: [query.includeFile],
250254
})

src/extension/search.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ interface Handlers {
140140

141141
function getPatternRes(query: SearchQuery, handlers: Handlers) {
142142
const proc = buildCommand({
143-
pattern: query.inputValue,
143+
pattern: query.pattern,
144144
includeFiles: [query.includeFile],
145145
rewrite: query.rewrite,
146146
})

src/types.ts

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

30-
export interface SearchQuery {
31-
inputValue: string
32-
includeFile: string
30+
interface SearchQueryBasic {
31+
pattern: string
3332
rewrite: string
3433
strictness: string
3534
selector: string
3635
}
3736

37+
export interface SearchQuery extends SearchQueryBasic {
38+
includeFile: string
39+
}
40+
3841
export type SgSearch = {
3942
text: string
4043
range: RangeInfo
@@ -98,20 +101,20 @@ export interface ChildToParent {
98101
}
99102
diffs: Diff[]
100103
}
101-
commitChange: WithId<{
102-
filePath: string
103-
diffs: Diff[]
104-
inputValue: string
105-
rewrite: string
106-
}>
107-
replaceAll: WithId<{
108-
inputValue: string
109-
rewrite: string
110-
changes: {
104+
commitChange: WithId<
105+
{
111106
filePath: string
112107
diffs: Diff[]
113-
}[]
114-
}>
108+
} & SearchQueryBasic
109+
>
110+
replaceAll: WithId<
111+
{
112+
changes: {
113+
filePath: string
114+
diffs: Diff[]
115+
}[]
116+
} & SearchQueryBasic
117+
>
115118
}
116119

117120
export type Definition = {

src/webview/SearchSidebar/SearchProviderMessage/index.tsx

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

2222
function Empty({ query }: { query: SearchQuery }) {
23-
const { inputValue, includeFile } = query
24-
if (!inputValue) {
23+
const { pattern, includeFile } = query
24+
if (!pattern) {
2525
return null
2626
}
2727
return (
2828
<div style={style}>
29-
No results found for <code style={codeStyle}>{inputValue}</code>
29+
No results found for <code style={codeStyle}>{pattern}</code>
3030
{includeFile ? ` in '${includeFile}'` : null}.
3131
<br />
3232
If this is not expected, you can try:

src/webview/SearchSidebar/SearchWidgetContainer/SearchWidget.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ function ReplaceBar() {
7979
}
8080

8181
function SearchWidgetContainer() {
82-
const [inputValue, setInputValue] = useSearchField('inputValue')
82+
const [pattern, setPattern] = useSearchField('pattern')
8383
const [isExpanded, toggleIsExpanded] = useBoolean(hasInitialRewrite())
8484
// sadly unport does not support unsub
8585
useEffectOnce(() => {
8686
childPort.onMessage('clearSearchResults', () => {
87-
setInputValue('')
87+
setPattern('')
8888
})
8989
})
9090
return (
@@ -95,8 +95,8 @@ function SearchWidgetContainer() {
9595
<div {...stylex.props(styles.inputs)}>
9696
<SearchInput
9797
placeholder="Search"
98-
value={inputValue}
99-
onChange={setInputValue}
98+
value={pattern}
99+
onChange={setPattern}
100100
onKeyEnterUp={refreshResult}
101101
/>
102102
{isExpanded ? <ReplaceBar /> : null}

src/webview/hooks/useQuery.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export { SearchQuery }
88
import { postSearch } from './useSearch'
99

1010
const searchQuery: Record<keyof SearchQuery, string> = {
11-
inputValue: '',
11+
pattern: '',
1212
strictness: 'smart',
1313
selector: '',
1414
includeFile: '',
@@ -18,7 +18,7 @@ const searchQuery: Record<keyof SearchQuery, string> = {
1818
type PatternKeys = 'strictness' | 'selector'
1919

2020
const LS_KEYS: Record<Exclude<keyof SearchQuery, PatternKeys>, string> = {
21-
inputValue: 'ast-grep-search-panel-input-value',
21+
pattern: 'ast-grep-search-panel-input-value',
2222
includeFile: 'ast-grep-search-panel-include-value',
2323
rewrite: 'ast-grep-search-panel-rewrite-value',
2424
}
@@ -28,7 +28,7 @@ export function refreshResult() {
2828
}
2929
childPort.onMessage('refreshAllSearch', refreshResult)
3030
childPort.onMessage('clearSearchResults', () => {
31-
searchQuery.inputValue = ''
31+
searchQuery.pattern = ''
3232
refreshResult()
3333
})
3434

src/webview/hooks/useSearch.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ const MOD = 1e9 + 7
2020
let id = 0
2121
let grouped: [string, DisplayResult[]][] = []
2222
let queryInFlight: SearchQuery = {
23-
inputValue: '',
23+
pattern: '',
2424
includeFile: '',
2525
rewrite: '',
26+
strictness: 'smart',
27+
selector: '',
2628
}
2729
let searching = true
2830
let searchError: Error | null = null

0 commit comments

Comments
 (0)