Skip to content

Commit 1910cbf

Browse files
committed
reuse contentController
1 parent 64f196c commit 1910cbf

File tree

4 files changed

+27
-81
lines changed

4 files changed

+27
-81
lines changed

packages/amazonq/src/lsp/chat/messages.ts

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,8 @@ import { Disposable, LanguageClient, Position, TextDocumentIdentifier } from 'vs
4848
import * as jose from 'jose'
4949
import { AmazonQChatViewProvider } from './webviewProvider'
5050
import { AuthUtil } from 'aws-core-vscode/codewhisperer'
51-
import {
52-
amazonQDiffScheme,
53-
AmazonQPromptSettings,
54-
messages,
55-
textDocumentUtil,
56-
amazonQTabSuffix,
57-
disposeOnEditorClose,
58-
} from 'aws-core-vscode/shared'
59-
import { ContentProvider, DefaultAmazonQAppInitContext, messageDispatcher } from 'aws-core-vscode/amazonq'
60-
import path from 'path'
51+
import { AmazonQPromptSettings, messages } from 'aws-core-vscode/shared'
52+
import { DefaultAmazonQAppInitContext, messageDispatcher, EditorContentController } from 'aws-core-vscode/amazonq'
6153

6254
export function registerLanguageServerEventListener(languageClient: LanguageClient, provider: AmazonQChatViewProvider) {
6355
languageClient.info(
@@ -365,21 +357,20 @@ export function registerMessageListeners(
365357
})
366358

367359
languageClient.onNotification(openFileDiffNotificationType.method, async (params: OpenFileDiffParams) => {
368-
const uri = vscode.Uri.parse(params.originalFileUri)
369-
const tempFileUri = await textDocumentUtil.createTempFileForDiffWithContent(
370-
uri,
371-
params.fileContent ?? '',
372-
amazonQDiffScheme
373-
)
374-
const contentProvider = new ContentProvider(tempFileUri)
375-
const disposable = vscode.workspace.registerTextDocumentContentProvider(amazonQDiffScheme, contentProvider)
376-
await vscode.commands.executeCommand(
377-
'vscode.diff',
378-
uri,
379-
tempFileUri,
380-
`${path.basename(params.originalFileUri)} ${amazonQTabSuffix}`
360+
const edc = new EditorContentController()
361+
const uri = params.originalFileUri
362+
const doc = await vscode.workspace.openTextDocument(uri)
363+
const entireDocumentSelection = new vscode.Selection(
364+
new vscode.Position(0, 0),
365+
new vscode.Position(doc.lineCount - 1, doc.lineAt(doc.lineCount - 1).text.length)
381366
)
382-
disposeOnEditorClose(uri, disposable)
367+
await edc.viewDiff({
368+
context: {
369+
activeFileContext: { filePath: params.originalFileUri },
370+
focusAreaContext: { selectionInsideExtendedCodeBlock: entireDocumentSelection },
371+
},
372+
code: params.fileContent,
373+
})
383374
})
384375
}
385376

packages/core/src/amazonq/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export * as authConnection from '../auth/connection'
4747
export * as featureConfig from './webview/generators/featureConfig'
4848
export * as messageDispatcher from './webview/messages/messageDispatcher'
4949
import { FeatureContext } from '../shared/featureConfig'
50-
export { ContentProvider } from './commons/controllers/contentController'
50+
export { EditorContentController } from './commons/controllers/contentController'
5151

5252
/**
5353
* main from createMynahUI is a purely browser dependency. Due to this

packages/core/src/shared/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export * as funcUtil from './utilities/functionUtils'
5656
export { fs } from './fs/fs'
5757
export * from './handleUninstall'
5858
export { CrashMonitoring } from './crashMonitoring'
59-
export { amazonQDiffScheme, amazonQTabSuffix } from './constants'
59+
export { amazonQDiffScheme } from './constants'
6060
export * from './featureConfig'
6161
export { i18n } from './i18n-helper'
6262
export * from './icons'
@@ -74,4 +74,3 @@ export * as BaseLspInstaller from './lsp/baseLspInstaller'
7474
export * as collectionUtil from './utilities/collectionUtils'
7575
export * from './datetime'
7676
export * from './performance/marks'
77-
export { disposeOnEditorClose } from './utilities/editorUtilities'

packages/core/src/shared/utilities/textDocumentUtilities.ts

Lines changed: 10 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,20 @@ 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-
* This is the base implementation used by other diff-related functions.
122+
* Creates a temporary file for diff comparison by cloning the original file
123+
* and applying the proposed changes within the selected range.
124124
*
125125
* @param {vscode.Uri} originalFileUri - The URI of the original 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.
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.
128129
*/
129-
async function createTempFileForDiffBase(
130+
export async function createTempFileForDiff(
130131
originalFileUri: vscode.Uri,
132+
message: any,
133+
selection: vscode.Selection,
131134
scheme: string
132-
): Promise<{ tempFileUri: vscode.Uri; doc: vscode.TextDocument }> {
135+
): Promise<vscode.Uri> {
133136
const errorCode = 'createTempFile'
134137
const id = Date.now()
135138
const languageId = (await vscode.workspace.openTextDocument(originalFileUri)).languageId
@@ -149,7 +152,7 @@ async function createTempFileForDiffBase(
149152
throw ToolkitError.chain(error, 'Failed to write to temp file', { code: errorCode })
150153
}
151154

152-
// Open the temp file as a document
155+
// Apply the proposed changes to the temp file
153156
const doc = await vscode.workspace.openTextDocument(tempFileUri.path)
154157
const languageIdStatus = await vscode.languages.setTextDocumentLanguage(doc, languageId)
155158
if (languageIdStatus) {
@@ -158,60 +161,13 @@ async function createTempFileForDiffBase(
158161
getLogger().error('Diff: Unable to set languageId for %s to: %s', tempFileUri.fsPath, languageId)
159162
}
160163

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-
182164
const code = getIndentedCode(message, doc, selection)
183165
const range = getSelectionFromRange(doc, selection)
184166

185167
await applyChanges(doc, range, code)
186168
return tempFileUri
187169
}
188170

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-
215171
/**
216172
* Indents the given code based on the current document's indentation at the selection start.
217173
*

0 commit comments

Comments
 (0)