fix: High CPU usage in new files check (#446) (#474)

* fix: High CPU usage in new files check (#446)

* Resolve absolute path
This commit is contained in:
Rene Leonhardt 2024-04-18 15:36:49 +02:00 committed by GitHub
parent 92d9d5ee20
commit b202d46984
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 35 additions and 24 deletions

View file

@ -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);
}
}

View file

@ -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<FileWatcher>()
.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)
}
}
}

View file

@ -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()
}
}
}