@@ -119,20 +119,17 @@ export async function applyChanges(doc: vscode.TextDocument, range: vscode.Range
119119}
120120
121121/**
122- * Creates a temporary file for diff comparison by cloning the original file
123- * and applying the proposed changes within the selected range .
122+ * Creates a temporary file for diff comparison by cloning the original file.
123+ * This is the base implementation used by other diff-related functions .
124124 *
125125 * @param {vscode.Uri } originalFileUri - The URI of the original file.
126- * @param {any } message - The message object containing the proposed code changes.
127- * @param {vscode.Selection } selection - The selection range in the document where the changes are applied.
128- * @returns {Promise<vscode.Uri> } - A promise that resolves to the URI of the temporary file.
126+ * @param {string } scheme - The URI scheme to use for the temporary file.
127+ * @returns {Promise<{tempFileUri: vscode.Uri, doc: vscode.TextDocument}> } - A promise that resolves to the URI of the temporary file and the document.
129128 */
130- export async function createTempFileForDiff (
129+ async function createTempFileForDiffBase (
131130 originalFileUri : vscode . Uri ,
132- message : any ,
133- selection : vscode . Selection ,
134131 scheme : string
135- ) : Promise < vscode . Uri > {
132+ ) : Promise < { tempFileUri : vscode . Uri ; doc : vscode . TextDocument } > {
136133 const errorCode = 'createTempFile'
137134 const id = Date . now ( )
138135 const languageId = ( await vscode . workspace . openTextDocument ( originalFileUri ) ) . languageId
@@ -152,7 +149,7 @@ export async function createTempFileForDiff(
152149 throw ToolkitError . chain ( error , 'Failed to write to temp file' , { code : errorCode } )
153150 }
154151
155- // Apply the proposed changes to the temp file
152+ // Open the temp file as a document
156153 const doc = await vscode . workspace . openTextDocument ( tempFileUri . path )
157154 const languageIdStatus = await vscode . languages . setTextDocumentLanguage ( doc , languageId )
158155 if ( languageIdStatus ) {
@@ -161,13 +158,60 @@ export async function createTempFileForDiff(
161158 getLogger ( ) . error ( 'Diff: Unable to set languageId for %s to: %s' , tempFileUri . fsPath , languageId )
162159 }
163160
161+ return { tempFileUri, doc }
162+ }
163+
164+ /**
165+ * Creates a temporary file for diff comparison by cloning the original file
166+ * and applying the proposed changes within the selected range.
167+ *
168+ * @param {vscode.Uri } originalFileUri - The URI of the original file.
169+ * @param {any } message - The message object containing the proposed code changes.
170+ * @param {vscode.Selection } selection - The selection range in the document where the changes are applied.
171+ * @param {string } scheme - The URI scheme to use for the temporary file.
172+ * @returns {Promise<vscode.Uri> } - A promise that resolves to the URI of the temporary file.
173+ */
174+ export async function createTempFileForDiff (
175+ originalFileUri : vscode . Uri ,
176+ message : any ,
177+ selection : vscode . Selection ,
178+ scheme : string
179+ ) : Promise < vscode . Uri > {
180+ const { tempFileUri, doc } = await createTempFileForDiffBase ( originalFileUri , scheme )
181+
164182 const code = getIndentedCode ( message , doc , selection )
165183 const range = getSelectionFromRange ( doc , selection )
166184
167185 await applyChanges ( doc , range , code )
168186 return tempFileUri
169187}
170188
189+ /**
190+ * Creates a temporary file for diff comparison by cloning the original file
191+ * and replacing the entire content with the provided content.
192+ *
193+ * @param {vscode.Uri } originalFileUri - The URI of the original file.
194+ * @param {string } content - The content to replace the entire document with.
195+ * @param {string } scheme - The URI scheme to use for the temporary file.
196+ * @returns {Promise<vscode.Uri> } - A promise that resolves to the URI of the temporary file.
197+ */
198+ export async function createTempFileForDiffWithContent (
199+ originalFileUri : vscode . Uri ,
200+ content : string ,
201+ scheme : string
202+ ) : Promise < vscode . Uri > {
203+ const { tempFileUri, doc } = await createTempFileForDiffBase ( originalFileUri , scheme )
204+
205+ // Create a range that covers the entire document
206+ const entireDocumentRange = new vscode . Range (
207+ new vscode . Position ( 0 , 0 ) ,
208+ new vscode . Position ( doc . lineCount - 1 , doc . lineAt ( doc . lineCount - 1 ) . text . length )
209+ )
210+
211+ await applyChanges ( doc , entireDocumentRange , content )
212+ return tempFileUri
213+ }
214+
171215/**
172216 * Indents the given code based on the current document's indentation at the selection start.
173217 *
0 commit comments