update last file save timestamp and implement caching in diff calculation

This commit is contained in:
ferenci84 2025-03-21 15:19:56 +01:00
parent ed6f001eb7
commit defb963cfc
5 changed files with 52 additions and 1 deletions

View file

@ -1,4 +1,5 @@
package com.github.continuedev.continueintellijextension.activities
import IntelliJIDE
import com.intellij.openapi.fileEditor.FileEditorManagerListener
import com.github.continuedev.continueintellijextension.auth.AuthListener
import com.github.continuedev.continueintellijextension.auth.ContinueAuthService
@ -185,6 +186,8 @@ class ContinuePluginStartupActivity : StartupActivity, DumbAware {
// Notify core of content changes
if (changedURIs.isNotEmpty()) {
continuePluginService.updateLastFileSaveTimestamp()
val data = mapOf("uris" to changedURIs)
continuePluginService.coreMessenger?.request("files/changed", data, null) { _ -> }
}

View file

@ -45,6 +45,10 @@ class IdeProtocolClient(
)
}
fun updateLastFileSaveTimestamp() {
(ide as IntelliJIDE).updateLastFileSaveTimestamp()
}
fun handleMessage(msg: String, respond: (Any?) -> Unit) {
coroutineScope.launch(Dispatchers.IO) {
val message = Gson().fromJson(msg, Message::class.java)
@ -202,6 +206,11 @@ class IdeProtocolClient(
respond(null)
}
"updateLastFileSaveTimestamp" -> {
(ide as IntelliJIDE).updateLastFileSaveTimestamp()
respond(null)
}
"showVirtualFile" -> {
val params = Gson().fromJson(
dataElement.toString(),

View file

@ -51,8 +51,19 @@ import java.nio.file.Paths
class IntelliJIDE(
private val project: Project,
private val continuePluginService: ContinuePluginService,
private var lastFileSaveTimestamp: Long = System.currentTimeMillis()
) : IDE {
// Add a simple cache for diff results
private data class DiffCache(
val timestamp: Long,
val diffs: List<String>
)
// Cache the last diff result
private var diffCache: DiffCache? = null
) : IDE {
private val ripgrep: String
init {
@ -66,6 +77,14 @@ class IntelliJIDE(
Paths.get(pluginPath.toString(), "ripgrep", "bin", "rg" + if (os == OS.WINDOWS) ".exe" else "").toString()
}
/**
* Updates the timestamp when a file is saved
*/
override fun updateLastFileSaveTimestamp() {
println("Updating last file save timestamp")
lastFileSaveTimestamp = System.currentTimeMillis()
}
override suspend fun getIdeInfo(): IdeInfo {
val applicationInfo = ApplicationInfo.getInstance()
val ideName: String = applicationInfo.fullApplicationName
@ -110,6 +129,16 @@ class IntelliJIDE(
}
override suspend fun getDiff(includeUnstaged: Boolean): List<String> {
println("Getting diff, cache timestamp: ${diffCache?.timestamp}, current timestamp: $lastFileSaveTimestamp")
// Check if we have a valid cache entry
if (diffCache != null && diffCache!!.timestamp == lastFileSaveTimestamp) {
println("Using cached diff")
return diffCache!!.diffs
}
// If no cache hit, compute the diff
println("Computing fresh diff")
val workspaceDirs = workspaceDirectories()
val diffs = mutableListOf<String>()
@ -144,6 +173,8 @@ class IntelliJIDE(
diffs.add(output.toString())
}
// Cache the result
diffCache = DiffCache(lastFileSaveTimestamp, diffs)
return diffs
}

View file

@ -41,4 +41,8 @@ class ContinuePluginService : Disposable, DumbAware {
) {
continuePluginWindow?.browser?.sendToWebview(messageType, data, messageId)
}
fun updateLastFileSaveTimestamp() {
ideProtocolClient?.updateLastFileSaveTimestamp()
}
}

View file

@ -192,6 +192,10 @@ interface IDE {
// Callbacks
fun onDidChangeActiveTextEditor(callback: (filepath: String) -> Unit)
fun updateLastFileSaveTimestamp() {
// Default implementation does nothing
}
}
data class GetGhTokenArgs(