Skip to content

Commit 8f78284

Browse files
refactor: update make open diff reflect delete
1 parent 74c17a2 commit 8f78284

File tree

4 files changed

+35
-53
lines changed

4 files changed

+35
-53
lines changed

src/extension/preview.ts

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import type {
2323
DisplayResult,
2424
SearchQuery,
2525
SgSearch,
26+
Diff,
2627
} from '../types'
2728
import { parentPort, streamedPromise } from './common'
2829
import { buildCommand, splitByHighLightToken } from './search'
@@ -93,14 +94,13 @@ function openFile({ filePath, locationsToSelect }: ChildToParent['openFile']) {
9394
async function previewDiff({
9495
filePath,
9596
locationsToSelect,
96-
inputValue,
97-
rewrite,
97+
diffs,
9898
}: ChildToParent['previewDiff']) {
9999
const fileUri = workspaceUriFromFilePath(filePath)
100100
if (!fileUri) {
101101
return
102102
}
103-
await generatePreview(fileUri, inputValue, rewrite)
103+
await generatePreview(fileUri, diffs)
104104
const previewUri = fileUri.with({ scheme: SCHEME })
105105
const filename = path.basename(filePath)
106106
// https://github.com/microsoft/vscode/blob/d63202a5382aa104f5515ea09053a2a21a2587c6/src/vs/workbench/api/common/extHostApiCommands.ts#L422
@@ -166,11 +166,11 @@ async function onCommitChange(payload: ChildToParent['commitChange']) {
166166

167167
async function doChange(
168168
fileUri: Uri,
169-
{ changes }: ChildToParent['commitChange'],
169+
{ diffs }: ChildToParent['commitChange'],
170170
) {
171171
const bytes = await workspace.fs.readFile(fileUri)
172172
const { receiveResult, conclude } = bufferMaker(bytes)
173-
for (const { range, replacement } of changes) {
173+
for (const { range, replacement } of diffs) {
174174
receiveResult(replacement, range.byteOffset)
175175
}
176176
const final = conclude()
@@ -216,13 +216,6 @@ export function activatePreview({ subscriptions }: ExtensionContext) {
216216
)
217217
}
218218

219-
interface ReplaceArg {
220-
bytes: Uint8Array
221-
uri: Uri
222-
inputValue: string
223-
rewrite: string
224-
}
225-
226219
function bufferMaker(bytes: Uint8Array) {
227220
const encoder = new TextEncoder()
228221
let newBuffer = new Uint8Array(bytes.byteLength)
@@ -268,33 +261,17 @@ function bufferMaker(bytes: Uint8Array) {
268261
}
269262
}
270263

271-
async function haveReplace({ bytes, uri, inputValue, rewrite }: ReplaceArg) {
272-
const command = buildCommand({
273-
pattern: inputValue,
274-
rewrite: rewrite,
275-
includeFiles: [uri.fsPath],
276-
})
277-
const { receiveResult, conclude } = bufferMaker(bytes)
278-
await streamedPromise(command!, (results: SgSearch[]) => {
279-
for (const r of results) {
280-
receiveResult(r.replacement!, r.range.byteOffset)
281-
}
282-
})
283-
const final = conclude()
284-
return new TextDecoder('utf-8').decode(final)
285-
}
286-
287-
async function generatePreview(uri: Uri, inputValue: string, rewrite: string) {
264+
async function generatePreview(uri: Uri, diffs: Diff[]) {
288265
if (previewContents.has(uri.path)) {
289266
return
290267
}
291268
// TODO, maybe we also need a rewrite change event?
292269
const bytes = await workspace.fs.readFile(uri)
293-
const replaced = await haveReplace({
294-
bytes,
295-
uri,
296-
inputValue,
297-
rewrite,
298-
})
270+
const { receiveResult, conclude } = bufferMaker(bytes)
271+
for (const { range, replacement } of diffs) {
272+
receiveResult(replacement, range.byteOffset)
273+
}
274+
const final = conclude()
275+
const replaced = new TextDecoder('utf-8').decode(final)
299276
previewContents.set(uri.path, replaced)
300277
}

src/types.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ export interface ParentToChild {
6161
}
6262
}
6363

64+
export interface Diff {
65+
replacement: string
66+
range: RangeInfo
67+
}
68+
6469
export interface ChildToParent {
6570
search: WithId<SearchQuery>
6671
reload: unknown
@@ -77,15 +82,11 @@ export interface ChildToParent {
7782
start: Position
7883
end: Position
7984
}
80-
inputValue: string
81-
rewrite: string
85+
diffs: Diff[]
8286
}
8387
commitChange: WithId<{
8488
filePath: string
85-
changes: {
86-
replacement: string
87-
range: RangeInfo
88-
}[]
89+
diffs: Diff[]
8990
inputValue: string
9091
rewrite: string
9192
}>

src/webview/SearchSidebar/SearchResultList/Actions.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export function MatchActions({ className: parent, match }: ActionsProps) {
5050
const onClick = useCallback(() => {
5151
acceptChangeAndRefresh({
5252
filePath: match.file,
53-
changes: [
53+
diffs: [
5454
{
5555
replacement: match.replacement!,
5656
range: match.range,

src/webview/hooks/useSearch.tsx

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,20 @@ function getSnapshot() {
136136
* Either open a file or preview the diff
137137
*/
138138
export function openAction(payload: OpenPayload) {
139-
if (queryInFlight.rewrite) {
140-
previewDiff({
141-
...payload,
142-
rewrite: queryInFlight.rewrite,
143-
inputValue: queryInFlight.inputValue,
144-
})
145-
} else {
139+
if (!queryInFlight.rewrite) {
146140
openFile(payload)
141+
return
147142
}
143+
const diffs = grouped
144+
.find(g => g[0] === payload.filePath)![1]
145+
.map(n => ({
146+
replacement: n.replacement!,
147+
range: n.range,
148+
}))
149+
previewDiff({
150+
...payload,
151+
diffs,
152+
})
148153
}
149154

150155
export const useSearchResult = () => {
@@ -160,7 +165,7 @@ export { postSearch }
160165

161166
export function acceptChangeAndRefresh(args: {
162167
filePath: string
163-
changes: {
168+
diffs: {
164169
replacement: string
165170
range: RangeInfo
166171
}[]
@@ -173,13 +178,12 @@ export function acceptChangeAndRefresh(args: {
173178
}
174179

175180
export function acceptFileChanges(filePath: string) {
176-
const changes = grouped.find(g => g[0] === filePath)?.[1] || []
177-
// TODO
181+
const diffs = grouped.find(g => g[0] === filePath)?.[1] || []
178182
commitChange({
179183
id,
180184
...queryInFlight,
181185
filePath,
182-
changes: changes.map(c => ({
186+
diffs: diffs.map(c => ({
183187
replacement: c.replacement!,
184188
range: c.range,
185189
})),

0 commit comments

Comments
 (0)