Skip to content

Commit 2dc49b5

Browse files
feat: improve refresh result!
fix #168
1 parent ee22dff commit 2dc49b5

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

src/extension/preview.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ async function refreshSearchResult(
200200
parentPort.postMessage('refreshSearchResult', {
201201
id,
202202
updatedResults,
203+
fileName: query.includeFile,
203204
})
204205
}
205206

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export interface ParentToChild {
5757
refreshSearchResult: {
5858
id: number
5959
updatedResults: DisplayResult[]
60+
fileName: string
6061
}
6162
}
6263

src/webview/hooks/useSearch.tsx

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@ childPort.onMessage('error', event => {
7878
notify()
7979
})
8080

81+
childPort.onMessage('refreshSearchResult', event => {
82+
if (event.id !== id) {
83+
return
84+
}
85+
const { fileName, updatedResults } = event
86+
const temp = new Map(grouped)
87+
if (updatedResults.length === 0) {
88+
temp.delete(fileName)
89+
} else {
90+
temp.set(fileName, updatedResults)
91+
}
92+
grouped = [...temp.entries()]
93+
notify()
94+
})
95+
8196
function groupBy(matches: DisplayResult[]) {
8297
const groups = new Map<string, DisplayResult[]>()
8398
for (const match of matches) {
@@ -99,6 +114,18 @@ function merge(newEntries: Map<string, DisplayResult[]>) {
99114
return [...temp.entries()]
100115
}
101116

117+
function updateResult(newEntries: Map<string, DisplayResult[]>) {
118+
// first, clone the old map for react
119+
for (const [fileName, results] of newEntries) {
120+
if (results.length === 0) {
121+
temp.delete(fileName)
122+
} else {
123+
temp.set(fileName, results)
124+
}
125+
}
126+
return [...temp.entries()]
127+
}
128+
102129
// version is for react to update view
103130
let version = 114514
104131
function subscribe(onChange: () => void): () => void {
@@ -143,22 +170,11 @@ export const useSearchResult = () => {
143170
}
144171
export { postSearch }
145172

146-
function clearOneFile(file: string) {
147-
const pairs = grouped.find(n => n[0] === file)
148-
if (!pairs) {
149-
return
150-
}
151-
pairs[1] = []
152-
grouped = grouped.slice()
153-
notify()
154-
}
155-
156173
export function acceptChangeAndRefresh(args: {
157174
filePath: string
158175
replacement: string
159176
range: RangeInfo
160177
}) {
161-
clearOneFile(args.filePath)
162178
commitChange({
163179
id,
164180
...queryInFlight,

0 commit comments

Comments
 (0)