Skip to content

Commit 280b607

Browse files
committed
stop duplicating windows
1 parent 67bd41c commit 280b607

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ import java.util.concurrent.TimeUnit
6161
* Concrete implementation of [AmazonQLanguageClient] to handle messages sent from server
6262
*/
6363
class AmazonQLanguageClientImpl(private val project: Project) : AmazonQLanguageClient {
64+
private val openFileDiffs = mutableMapOf<String, SimpleDiffRequest>()
65+
6466
override fun telemetryEvent(`object`: Any) {
6567
println(`object`)
6668
}
@@ -285,19 +287,25 @@ class AmazonQLanguageClientImpl(private val project: Project) : AmazonQLanguageC
285287
{
286288
var tempPath: java.nio.file.Path? = null
287289
try {
290+
val diffId = "${params.originalFileUri}:${params.originalFileContent.hashCode()}:${params.fileContent.hashCode()}"
291+
292+
// Check if we already have an open diff for this request
293+
val existingDiff = openFileDiffs[diffId]
294+
if (existingDiff != null) {
295+
return@supplyAsync
296+
}
297+
288298
val fileName = Paths.get(params.originalFileUri).fileName.toString()
289299
// Create a temporary virtual file for syntax highlighting
290300
val fileExtension = fileName.substringAfterLast('.', "")
291301
tempPath = Files.createTempFile(null, ".$fileExtension")
292302
val virtualFile = tempPath.toFile()
293303
.also { it.setReadOnly() }
294304
.toVirtualFile()
295-
296305
val originalContent = params.originalFileContent ?: run {
297306
val sourceFile = File(params.originalFileUri)
298307
if (sourceFile.exists()) sourceFile.readText() else ""
299308
}
300-
301309
val contentFactory = DiffContentFactory.getInstance()
302310
var isNewFile = false
303311
val (leftContent, rightContent) = when {
@@ -320,7 +328,9 @@ class AmazonQLanguageClientImpl(private val project: Project) : AmazonQLanguageC
320328
}
321329
}
322330
}
323-
val diffRequest = SimpleDiffRequest(
331+
332+
// Create a tracking diff request
333+
val diffRequest = object : SimpleDiffRequest(
324334
"$fileName ${message("aws.q.lsp.client.diff_message")}",
325335
leftContent,
326336
rightContent,
@@ -330,7 +340,19 @@ class AmazonQLanguageClientImpl(private val project: Project) : AmazonQLanguageC
330340
isNewFile -> "Created"
331341
else -> "Modified"
332342
}
333-
)
343+
) {
344+
override fun onAssigned(isAssigned: Boolean) {
345+
super.onAssigned(isAssigned)
346+
if (isAssigned) {
347+
// Diff window opened
348+
openFileDiffs[diffId] = this
349+
} else {
350+
// Diff window closed
351+
openFileDiffs.remove(diffId)
352+
}
353+
}
354+
}
355+
334356
(DiffManager.getInstance() as DiffManagerEx).showDiffBuiltin(project, diffRequest)
335357
} catch (e: Exception) {
336358
LOG.warn { "Failed to open file diff: ${e.message}" }

0 commit comments

Comments
 (0)