Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions packages/amazonq/src/lsp/chat/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,25 @@ import {
ShowDocumentRequest,
contextCommandsNotificationType,
ContextCommandParams,
openFileDiffNotificationType,
OpenFileDiffParams,
} from '@aws/language-server-runtimes/protocol'
import { v4 as uuidv4 } from 'uuid'
import * as vscode from 'vscode'
import { Disposable, LanguageClient, Position, TextDocumentIdentifier } from 'vscode-languageclient'
import * as jose from 'jose'
import { AmazonQChatViewProvider } from './webviewProvider'
import { AuthUtil } from 'aws-core-vscode/codewhisperer'
import { AmazonQPromptSettings, messages } from 'aws-core-vscode/shared'
import { DefaultAmazonQAppInitContext, messageDispatcher } from 'aws-core-vscode/amazonq'
import {
amazonQDiffScheme,
AmazonQPromptSettings,
messages,
textDocumentUtil,
amazonQTabSuffix,
disposeOnEditorClose,
} from 'aws-core-vscode/shared'
import { ContentProvider, DefaultAmazonQAppInitContext, messageDispatcher } from 'aws-core-vscode/amazonq'
import path from 'path'

export function registerLanguageServerEventListener(languageClient: LanguageClient, provider: AmazonQChatViewProvider) {
languageClient.info(
Expand Down Expand Up @@ -353,6 +363,24 @@ export function registerMessageListeners(
params: params,
})
})

languageClient.onNotification(openFileDiffNotificationType.method, async (params: OpenFileDiffParams) => {
const uri = vscode.Uri.parse(params.originalFileUri)
const tempFileUri = await textDocumentUtil.createTempFileForDiffWithContent(
uri,
params.fileContent ?? '',
amazonQDiffScheme
)
const contentProvider = new ContentProvider(tempFileUri)
const disposable = vscode.workspace.registerTextDocumentContentProvider(amazonQDiffScheme, contentProvider)
await vscode.commands.executeCommand(
'vscode.diff',
uri,
tempFileUri,
`${path.basename(params.originalFileUri)} ${amazonQTabSuffix}`
)
disposeOnEditorClose(uri, disposable)
})
}

function isServerEvent(command: string) {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/amazonq/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export * as authConnection from '../auth/connection'
export * as featureConfig from './webview/generators/featureConfig'
export * as messageDispatcher from './webview/messages/messageDispatcher'
import { FeatureContext } from '../shared/featureConfig'
export { ContentProvider } from './commons/controllers/contentController'

/**
* main from createMynahUI is a purely browser dependency. Due to this
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export * as funcUtil from './utilities/functionUtils'
export { fs } from './fs/fs'
export * from './handleUninstall'
export { CrashMonitoring } from './crashMonitoring'
export { amazonQDiffScheme } from './constants'
export { amazonQDiffScheme, amazonQTabSuffix } from './constants'
export * from './featureConfig'
export { i18n } from './i18n-helper'
export * from './icons'
Expand All @@ -74,3 +74,4 @@ export * as BaseLspInstaller from './lsp/baseLspInstaller'
export * as collectionUtil from './utilities/collectionUtils'
export * from './datetime'
export * from './performance/marks'
export { disposeOnEditorClose } from './utilities/editorUtilities'
64 changes: 54 additions & 10 deletions packages/core/src/shared/utilities/textDocumentUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,17 @@ export async function applyChanges(doc: vscode.TextDocument, range: vscode.Range
}

/**
* Creates a temporary file for diff comparison by cloning the original file
* and applying the proposed changes within the selected range.
* Creates a temporary file for diff comparison by cloning the original file.
* This is the base implementation used by other diff-related functions.
*
* @param {vscode.Uri} originalFileUri - The URI of the original file.
* @param {any} message - The message object containing the proposed code changes.
* @param {vscode.Selection} selection - The selection range in the document where the changes are applied.
* @returns {Promise<vscode.Uri>} - A promise that resolves to the URI of the temporary file.
* @param {string} scheme - The URI scheme to use for the temporary file.
* @returns {Promise<{tempFileUri: vscode.Uri, doc: vscode.TextDocument}>} - A promise that resolves to the URI of the temporary file and the document.
*/
export async function createTempFileForDiff(
async function createTempFileForDiffBase(
originalFileUri: vscode.Uri,
message: any,
selection: vscode.Selection,
scheme: string
): Promise<vscode.Uri> {
): Promise<{ tempFileUri: vscode.Uri; doc: vscode.TextDocument }> {
const errorCode = 'createTempFile'
const id = Date.now()
const languageId = (await vscode.workspace.openTextDocument(originalFileUri)).languageId
Expand All @@ -152,7 +149,7 @@ export async function createTempFileForDiff(
throw ToolkitError.chain(error, 'Failed to write to temp file', { code: errorCode })
}

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

return { tempFileUri, doc }
}

/**
* Creates a temporary file for diff comparison by cloning the original file
* and applying the proposed changes within the selected range.
*
* @param {vscode.Uri} originalFileUri - The URI of the original file.
* @param {any} message - The message object containing the proposed code changes.
* @param {vscode.Selection} selection - The selection range in the document where the changes are applied.
* @param {string} scheme - The URI scheme to use for the temporary file.
* @returns {Promise<vscode.Uri>} - A promise that resolves to the URI of the temporary file.
*/
export async function createTempFileForDiff(
originalFileUri: vscode.Uri,
message: any,
selection: vscode.Selection,
scheme: string
): Promise<vscode.Uri> {
const { tempFileUri, doc } = await createTempFileForDiffBase(originalFileUri, scheme)

const code = getIndentedCode(message, doc, selection)
const range = getSelectionFromRange(doc, selection)

await applyChanges(doc, range, code)
return tempFileUri
}

/**
* Creates a temporary file for diff comparison by cloning the original file
* and replacing the entire content with the provided content.
*
* @param {vscode.Uri} originalFileUri - The URI of the original file.
* @param {string} content - The content to replace the entire document with.
* @param {string} scheme - The URI scheme to use for the temporary file.
* @returns {Promise<vscode.Uri>} - A promise that resolves to the URI of the temporary file.
*/
export async function createTempFileForDiffWithContent(
originalFileUri: vscode.Uri,
content: string,
scheme: string
): Promise<vscode.Uri> {
const { tempFileUri, doc } = await createTempFileForDiffBase(originalFileUri, scheme)

// Create a range that covers the entire document
const entireDocumentRange = new vscode.Range(
new vscode.Position(0, 0),
new vscode.Position(doc.lineCount - 1, doc.lineAt(doc.lineCount - 1).text.length)
)

await applyChanges(doc, entireDocumentRange, content)
return tempFileUri
}

/**
* Indents the given code based on the current document's indentation at the selection start.
*
Expand Down