From b202d46984568b10189dd5a2be486e2df248c3c0 Mon Sep 17 00:00:00 2001 From: Rene Leonhardt <65483435+reneleonhardt@users.noreply.github.com> Date: Thu, 18 Apr 2024 15:36:49 +0200 Subject: [PATCH] fix: High CPU usage in new files check (#446) (#474) * fix: High CPU usage in new files check (#446) * Resolve absolute path --- .../configuration/ConfigurationState.java | 11 +++--- .../codegpt/CodeGPTProjectActivity.kt | 13 ++++--- .../ee/carlrobert/codegpt/FileWatcher.kt | 35 +++++++++++-------- 3 files changed, 35 insertions(+), 24 deletions(-) diff --git a/src/main/java/ee/carlrobert/codegpt/settings/configuration/ConfigurationState.java b/src/main/java/ee/carlrobert/codegpt/settings/configuration/ConfigurationState.java index dbf9bb6e..4704acec 100644 --- a/src/main/java/ee/carlrobert/codegpt/settings/configuration/ConfigurationState.java +++ b/src/main/java/ee/carlrobert/codegpt/settings/configuration/ConfigurationState.java @@ -123,13 +123,13 @@ public class ConfigurationState { if (this == o) { return true; } - if (o == null || getClass() != o.getClass()) { + if (!(o instanceof ConfigurationState that)) { return false; } - ConfigurationState that = (ConfigurationState) o; return maxTokens == that.maxTokens - && Double.compare(that.temperature, temperature) == 0 + && Double.compare(temperature, that.temperature) == 0 && checkForPluginUpdates == that.checkForPluginUpdates + && checkForNewScreenshots == that.checkForNewScreenshots && createNewChatOnEachAction == that.createNewChatOnEachAction && ignoreGitCommitTokenLimit == that.ignoreGitCommitTokenLimit && methodNameGenerationEnabled == that.methodNameGenerationEnabled @@ -143,7 +143,8 @@ public class ConfigurationState { @Override public int hashCode() { return Objects.hash(systemPrompt, commitMessagePrompt, maxTokens, temperature, - checkForPluginUpdates, createNewChatOnEachAction, ignoreGitCommitTokenLimit, - methodNameGenerationEnabled, captureCompileErrors, autoFormattingEnabled, tableData); + checkForPluginUpdates, checkForNewScreenshots, createNewChatOnEachAction, + ignoreGitCommitTokenLimit, methodNameGenerationEnabled, captureCompileErrors, + autoFormattingEnabled, tableData); } } diff --git a/src/main/kotlin/ee/carlrobert/codegpt/CodeGPTProjectActivity.kt b/src/main/kotlin/ee/carlrobert/codegpt/CodeGPTProjectActivity.kt index 736ae2c9..02f7dce2 100644 --- a/src/main/kotlin/ee/carlrobert/codegpt/CodeGPTProjectActivity.kt +++ b/src/main/kotlin/ee/carlrobert/codegpt/CodeGPTProjectActivity.kt @@ -19,10 +19,14 @@ import ee.carlrobert.codegpt.settings.configuration.ConfigurationSettings import ee.carlrobert.codegpt.settings.service.you.YouSettings import ee.carlrobert.codegpt.toolwindow.chat.ui.textarea.AttachImageNotifier import ee.carlrobert.codegpt.ui.OverlayUtil +import io.ktor.util.* import java.nio.file.Paths +import kotlin.io.path.absolutePathString class CodeGPTProjectActivity : ProjectActivity { + private val watchExtensions = listOf("jpg", "jpeg", "png") + override suspend fun execute(project: Project) { EditorActionsUtil.refreshActions() CredentialsStore.loadAll() @@ -34,10 +38,11 @@ class CodeGPTProjectActivity : ProjectActivity { if (!ApplicationManager.getApplication().isUnitTestMode && ConfigurationSettings.getCurrentState().isCheckForNewScreenshots ) { + val desktopPath = Paths.get(System.getProperty("user.home"), "Desktop") project.service() - .watch(Paths.get(System.getProperty("user.home"), "Desktop").toFile()) { - if (listOf("jpg", "jpeg", "png").contains(it.extension)) { - showImageAttachmentNotification(project, it.absolutePath) + .watch(desktopPath) { + if (watchExtensions.contains(it.extension.lowercase())) { + showImageAttachmentNotification(project, desktopPath.resolve(it).absolutePathString()) } } } @@ -95,4 +100,4 @@ class CodeGPTProjectActivity : ProjectActivity { }) .notify(project) } -} \ No newline at end of file +} diff --git a/src/main/kotlin/ee/carlrobert/codegpt/FileWatcher.kt b/src/main/kotlin/ee/carlrobert/codegpt/FileWatcher.kt index 8d7a9123..a698f4e9 100644 --- a/src/main/kotlin/ee/carlrobert/codegpt/FileWatcher.kt +++ b/src/main/kotlin/ee/carlrobert/codegpt/FileWatcher.kt @@ -2,28 +2,33 @@ package ee.carlrobert.codegpt import com.intellij.openapi.Disposable import com.intellij.openapi.components.Service -import org.apache.commons.io.monitor.FileAlterationListenerAdaptor -import org.apache.commons.io.monitor.FileAlterationMonitor -import org.apache.commons.io.monitor.FileAlterationObserver -import java.io.File +import java.nio.file.FileSystems +import java.nio.file.Path +import java.nio.file.StandardWatchEventKinds.ENTRY_CREATE +import java.nio.file.WatchKey +import kotlin.concurrent.thread + @Service(Service.Level.PROJECT) class FileWatcher : Disposable { - private var fileMonitor: FileAlterationMonitor? = null + private var fileMonitor: Thread? = null - fun watch(pathToWatch: File, onFileCreated: (File) -> Unit) { - val observer = FileAlterationObserver(pathToWatch) - observer.addListener(object : FileAlterationListenerAdaptor() { - override fun onFileCreate(file: File) { - onFileCreated(file) + fun watch(pathToWatch: Path, onFileCreated: (Path) -> Unit) { + val watchService = FileSystems.getDefault().newWatchService() + pathToWatch.register(watchService, ENTRY_CREATE) // watch for new files + fileMonitor = thread { + var key: WatchKey + while ((watchService.take().also { key = it }) != null) { + for (event in key.pollEvents()) { + onFileCreated(event.context() as Path) + } + key.reset() } - }) - fileMonitor = FileAlterationMonitor(500, observer) - fileMonitor?.start() + } } override fun dispose() { - fileMonitor?.stop() + fileMonitor?.interrupt() } -} \ No newline at end of file +}