Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import com.google.gson.Gson
import com.intellij.diff.DiffContentFactory
import com.intellij.diff.DiffManager
import com.intellij.diff.DiffManagerEx
import com.intellij.diff.requests.SimpleDiffRequest
import com.intellij.notification.NotificationType
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.fileChooser.FileChooserFactory
import com.intellij.openapi.fileChooser.FileSaverDescriptor
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.openapi.vfs.VirtualFileManager
import migration.software.aws.toolkits.jetbrains.settings.AwsSettings
import org.eclipse.lsp4j.ConfigurationParams
Expand Down Expand Up @@ -48,6 +50,7 @@
import software.aws.toolkits.jetbrains.settings.CodeWhispererSettings
import software.aws.toolkits.resources.message
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
import java.util.UUID
import java.util.concurrent.CompletableFuture
Expand Down Expand Up @@ -269,40 +272,58 @@
return CompletableFuture.completedFuture(Unit)
}

private fun File.toVirtualFile() = LocalFileSystem.getInstance().findFileByIoFile(this)

Check warning on line 275 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/AmazonQLanguageClientImpl.kt

View check run for this annotation

Codecov / codecov/patch

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/AmazonQLanguageClientImpl.kt#L275

Added line #L275 was not covered by tests

override fun openFileDiff(params: OpenFileDiffParams): CompletableFuture<Unit> =
CompletableFuture.supplyAsync(
{
var tempPath: java.nio.file.Path? = null

Check warning on line 280 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/AmazonQLanguageClientImpl.kt

View check run for this annotation

Codecov / codecov/patch

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/AmazonQLanguageClientImpl.kt#L280

Added line #L280 was not covered by tests
try {
val contentFactory = DiffContentFactory.getInstance()
val fileName = Paths.get(params.originalFileUri).fileName.toString()

// Create a temporary virtual file for syntax highlighting
val fileExtension = fileName.substringAfterLast('.', "")
tempPath = Files.createTempFile(null, ".$fileExtension")
val virtualFile = tempPath.toFile().toVirtualFile()

Check warning on line 288 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/AmazonQLanguageClientImpl.kt

View check run for this annotation

Codecov / codecov/patch

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/AmazonQLanguageClientImpl.kt#L286-L288

Added lines #L286 - L288 were not covered by tests

val originalContent = params.originalFileContent ?: run {
val file = File(params.originalFileUri)
if (file.exists()) file.readText() else ""
}

val (leftContent, rightContent) = when {
params.isDeleted -> {
// For deleted files, show original on left, empty on right
contentFactory.create(originalContent) to
contentFactory.create(originalContent, virtualFile) to

Check warning on line 298 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/AmazonQLanguageClientImpl.kt

View check run for this annotation

Codecov / codecov/patch

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/AmazonQLanguageClientImpl.kt#L298

Added line #L298 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create has an option for file type, so i think we can keep this in memory and avoid the temp file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that earlier and wasn't seeing the highlighting being applied. Agree that avoiding the temp file would be ideal. I'll try again

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you should also pass in the project as a parameter so it's taking dependencies into account?

contentFactory.createEmpty()
}
else -> {
// For new or modified files
val newContent = params.fileContent.orEmpty()
contentFactory.create(originalContent) to
contentFactory.create(newContent)
contentFactory.create(originalContent, virtualFile) to
contentFactory.create(newContent, virtualFile)

Check warning on line 305 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/AmazonQLanguageClientImpl.kt

View check run for this annotation

Codecov / codecov/patch

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/AmazonQLanguageClientImpl.kt#L304-L305

Added lines #L304 - L305 were not covered by tests
}
}

val diffRequest = SimpleDiffRequest(
"$fileName ${message("aws.q.lsp.client.diff_message")}",
leftContent,
rightContent,
"Original",
if (params.isDeleted) "Deleted" else "Modified"
)
DiffManager.getInstance().showDiff(project, diffRequest)

(DiffManager.getInstance() as DiffManagerEx).showDiffBuiltin(project, diffRequest)

Check warning on line 317 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/AmazonQLanguageClientImpl.kt

View check run for this annotation

Codecov / codecov/patch

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/AmazonQLanguageClientImpl.kt#L317

Added line #L317 was not covered by tests
} catch (e: Exception) {
LOG.warn { "Failed to open file diff: ${e.message}" }
} finally {
// Clean up the temporary file
try {

Check warning on line 322 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/AmazonQLanguageClientImpl.kt

View check run for this annotation

Codecov / codecov/patch

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/AmazonQLanguageClientImpl.kt#L322

Added line #L322 was not covered by tests
tempPath?.let { Files.deleteIfExists(it) }
} catch (e: Exception) {
LOG.warn { "Failed to delete temporary file: ${e.message}" }

Check warning on line 325 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/AmazonQLanguageClientImpl.kt

View check run for this annotation

Codecov / codecov/patch

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/AmazonQLanguageClientImpl.kt#L324-L325

Added lines #L324 - L325 were not covered by tests
}
}
},
ApplicationManager.getApplication()::invokeLater
Expand Down
Loading