Skip to content

Commit c1e19d6

Browse files
feat: implement replaceAll without opening
1 parent 91fffcb commit c1e19d6

File tree

5 files changed

+32
-42
lines changed

5 files changed

+32
-42
lines changed

src/extension/preview.ts

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
TextEditorRevealType,
1919
TabInputTextDiff,
2020
EventEmitter,
21-
WorkspaceEdit,
2221
} from 'vscode'
2322
import type {
2423
ChildToParent,
@@ -182,9 +181,9 @@ parentPort.onMessage('previewDiff', previewDiff)
182181
parentPort.onMessage('dismissDiff', dismissDiff)
183182
parentPort.onMessage('search', refreshDiff)
184183
parentPort.onMessage('commitChange', onCommitChange)
185-
parentPort.onMessage('applyEdit', onApplyEdit)
184+
parentPort.onMessage('replaceAll', onReplaceAll)
186185

187-
async function onApplyEdit(payload: ChildToParent['applyEdit']) {
186+
async function onReplaceAll(payload: ChildToParent['replaceAll']) {
188187
const confirmed = await window.showInformationMessage(
189188
'Replace all occurrences across all files?',
190189
{ modal: true },
@@ -193,24 +192,14 @@ async function onApplyEdit(payload: ChildToParent['applyEdit']) {
193192
if (confirmed !== 'Replace') {
194193
return
195194
}
196-
for (const { filePath, replacements } of payload) {
197-
const workspaceEdit = new WorkspaceEdit()
198-
const documentUri = workspaceUriFromFilePath(filePath)!
199-
200-
for (const { range, text } of replacements) {
201-
workspaceEdit.replace(
202-
documentUri,
203-
new Range(
204-
range.start.line,
205-
range.start.column,
206-
range.end.line,
207-
range.end.column,
208-
),
209-
text,
210-
)
211-
}
212-
await workspace.applyEdit(workspaceEdit)
213-
await workspace.save(documentUri)
195+
const { id, inputValue, rewrite } = payload
196+
for (const change of payload.changes) {
197+
onCommitChange({
198+
id,
199+
inputValue,
200+
rewrite,
201+
...change,
202+
})
214203
}
215204
}
216205

src/types.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,14 @@ export interface ChildToParent {
9898
inputValue: string
9999
rewrite: string
100100
}>
101-
applyEdit: readonly {
102-
filePath: string
103-
replacements: readonly {
104-
range: RangeInfo
105-
text: string
101+
replaceAll: WithId<{
102+
inputValue: string
103+
rewrite: string
104+
changes: {
105+
filePath: string
106+
diffs: Diff[]
106107
}[]
107-
}[]
108+
}>
108109
}
109110

110111
export type Definition = {

src/webview/SearchSidebar/SearchWidgetContainer/SearchWidget.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
refreshResult,
55
hasInitialRewrite,
66
} from '../../hooks/useQuery'
7-
import { useSearchResult, replaceAll } from '../../hooks/useSearch'
7+
import { useSearchResult, acceptAllChanges } from '../../hooks/useSearch'
88
import { SearchInput } from './SearchInput'
99
import { useBoolean } from 'react-use'
1010
import { VscChevronRight, VscChevronDown, VscReplaceAll } from 'react-icons/vsc'
@@ -62,7 +62,7 @@ function ReplaceBar() {
6262
title="Replace All"
6363
appearance="icon"
6464
disabled={disabled}
65-
onClick={replaceAll}
65+
onClick={acceptAllChanges}
6666
{...stylex.props(styles.replaceAll)}
6767
>
6868
<VscReplaceAll />

src/webview/hooks/useSearch.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
commitChange,
88
childPort,
99
type RangeInfo,
10-
applyEdit,
10+
replaceAll,
1111
} from '../postMessage'
1212
import { useSyncExternalStore } from 'react'
1313
import type { SearchQuery } from './useQuery'
@@ -217,16 +217,16 @@ export function acceptFileChanges(filePath: string) {
217217
/**
218218
* Replace all matches in the search results
219219
*/
220-
export function replaceAll() {
221-
applyEdit(
222-
grouped.map(([filePath, diffs]) => ({
223-
filePath,
224-
replacements: diffs.map(d => ({
225-
range: d.range,
226-
text: d.replacement!,
227-
})),
228-
})),
229-
)
220+
export function acceptAllChanges() {
221+
const changes = grouped.map(([filePath, diffs]) => ({
222+
filePath,
223+
diffs: diffs.map(d => ({ replacement: d.replacement!, range: d.range })),
224+
}))
225+
replaceAll({
226+
id,
227+
...queryInFlight,
228+
changes,
229+
})
230230
}
231231

232232
export function dismissOneMatch(match: DisplayResult) {

src/webview/postMessage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ export function commitChange(diff: ChildToParent['commitChange']) {
3535
childPort.postMessage('commitChange', diff)
3636
}
3737

38-
export function applyEdit(payload: ChildToParent['applyEdit']) {
39-
childPort.postMessage('applyEdit', payload)
38+
export function replaceAll(payload: ChildToParent['replaceAll']) {
39+
childPort.postMessage('replaceAll', payload)
4040
}

0 commit comments

Comments
 (0)