@@ -99,17 +99,23 @@ function openFile({ filePath, locationsToSelect }: ChildToParent['openFile']) {
9999 } )
100100}
101101
102- async function previewDiff ( {
103- filePath,
104- locationsToSelect,
105- diffs,
106- forceReload = false ,
107- } : ChildToParent [ 'previewDiff' ] ) {
102+ async function previewDiff ( param : ChildToParent [ 'previewDiff' ] ) {
103+ const { filePath, diffs } = param
108104 const fileUri = workspaceUriFromFilePath ( filePath )
109105 if ( ! fileUri ) {
110106 return
111107 }
112- await generatePreview ( fileUri , diffs , forceReload )
108+ // if preview not in the preview provider, generate it
109+ if ( ! previewContents . has ( fileUri . path ) ) {
110+ await generatePreview ( fileUri , diffs )
111+ }
112+ doPreview ( fileUri , param )
113+ }
114+
115+ async function doPreview (
116+ fileUri : Uri ,
117+ { filePath, locationsToSelect } : ChildToParent [ 'previewDiff' ] ,
118+ ) {
113119 const previewUri = fileUri . with ( { scheme : SCHEME } )
114120 const filename = path . basename ( filePath )
115121 // https://github.com/microsoft/vscode/blob/d63202a5382aa104f5515ea09053a2a21a2587c6/src/vs/workbench/api/common/extHostApiCommands.ts#L422
@@ -125,6 +131,23 @@ async function previewDiff({
125131 const range = locationToRange ( locationsToSelect )
126132 window . activeTextEditor ?. revealRange ( range , TextEditorRevealType . InCenter )
127133}
134+
135+ async function dismissDiff ( param : ChildToParent [ 'dismissDiff' ] ) {
136+ const { filePath, diffs } = param
137+ const fileUri = workspaceUriFromFilePath ( filePath )
138+ if ( ! fileUri ) {
139+ return
140+ }
141+ // if preview not in the preview provider, skip generate
142+ if ( ! previewContents . has ( fileUri . path ) ) {
143+ return
144+ }
145+ await generatePreview ( fileUri , diffs )
146+ // update the preview snapshot
147+ const previewUri = fileUri . with ( { scheme : SCHEME } )
148+ previewProvider . notifyDiffChange ( previewUri )
149+ }
150+
128151function closeAllDiffs ( ) {
129152 console . debug ( 'Search pattern changed. Closing all diffs.' )
130153 const tabs = window . tabGroups . all . flatMap ( tg => tg . tabs )
@@ -155,6 +178,7 @@ function refreshDiff(query: SearchQuery) {
155178}
156179parentPort . onMessage ( 'openFile' , openFile )
157180parentPort . onMessage ( 'previewDiff' , previewDiff )
181+ parentPort . onMessage ( 'dismissDiff' , dismissDiff )
158182parentPort . onMessage ( 'search' , refreshDiff )
159183parentPort . onMessage ( 'commitChange' , onCommitChange )
160184
@@ -271,10 +295,7 @@ function bufferMaker(bytes: Uint8Array) {
271295 }
272296}
273297
274- async function generatePreview ( uri : Uri , diffs : Diff [ ] , forceReload : boolean ) {
275- if ( previewContents . has ( uri . path ) && ! forceReload ) {
276- return
277- }
298+ async function generatePreview ( uri : Uri , diffs : Diff [ ] ) {
278299 // TODO, maybe we also need a rewrite change event?
279300 const bytes = await workspace . fs . readFile ( uri )
280301 const { receiveResult, conclude } = bufferMaker ( bytes )
@@ -284,8 +305,4 @@ async function generatePreview(uri: Uri, diffs: Diff[], forceReload: boolean) {
284305 const final = conclude ( )
285306 const replaced = new TextDecoder ( 'utf-8' ) . decode ( final )
286307 previewContents . set ( uri . path , replaced )
287- if ( forceReload ) {
288- const previewUri = uri . with ( { scheme : SCHEME } )
289- previewProvider . notifyDiffChange ( previewUri )
290- }
291308}
0 commit comments