Skip to content

Commit b202d46

Browse files
fix: High CPU usage in new files check (#446) (#474)
* fix: High CPU usage in new files check (#446) * Resolve absolute path
1 parent 92d9d5e commit b202d46

File tree

3 files changed

+35
-24
lines changed

3 files changed

+35
-24
lines changed

src/main/java/ee/carlrobert/codegpt/settings/configuration/ConfigurationState.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,13 @@ public boolean equals(Object o) {
123123
if (this == o) {
124124
return true;
125125
}
126-
if (o == null || getClass() != o.getClass()) {
126+
if (!(o instanceof ConfigurationState that)) {
127127
return false;
128128
}
129-
ConfigurationState that = (ConfigurationState) o;
130129
return maxTokens == that.maxTokens
131-
&& Double.compare(that.temperature, temperature) == 0
130+
&& Double.compare(temperature, that.temperature) == 0
132131
&& checkForPluginUpdates == that.checkForPluginUpdates
132+
&& checkForNewScreenshots == that.checkForNewScreenshots
133133
&& createNewChatOnEachAction == that.createNewChatOnEachAction
134134
&& ignoreGitCommitTokenLimit == that.ignoreGitCommitTokenLimit
135135
&& methodNameGenerationEnabled == that.methodNameGenerationEnabled
@@ -143,7 +143,8 @@ public boolean equals(Object o) {
143143
@Override
144144
public int hashCode() {
145145
return Objects.hash(systemPrompt, commitMessagePrompt, maxTokens, temperature,
146-
checkForPluginUpdates, createNewChatOnEachAction, ignoreGitCommitTokenLimit,
147-
methodNameGenerationEnabled, captureCompileErrors, autoFormattingEnabled, tableData);
146+
checkForPluginUpdates, checkForNewScreenshots, createNewChatOnEachAction,
147+
ignoreGitCommitTokenLimit, methodNameGenerationEnabled, captureCompileErrors,
148+
autoFormattingEnabled, tableData);
148149
}
149150
}

src/main/kotlin/ee/carlrobert/codegpt/CodeGPTProjectActivity.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ import ee.carlrobert.codegpt.settings.configuration.ConfigurationSettings
1919
import ee.carlrobert.codegpt.settings.service.you.YouSettings
2020
import ee.carlrobert.codegpt.toolwindow.chat.ui.textarea.AttachImageNotifier
2121
import ee.carlrobert.codegpt.ui.OverlayUtil
22+
import io.ktor.util.*
2223
import java.nio.file.Paths
24+
import kotlin.io.path.absolutePathString
2325

2426
class CodeGPTProjectActivity : ProjectActivity {
2527

28+
private val watchExtensions = listOf("jpg", "jpeg", "png")
29+
2630
override suspend fun execute(project: Project) {
2731
EditorActionsUtil.refreshActions()
2832
CredentialsStore.loadAll()
@@ -34,10 +38,11 @@ class CodeGPTProjectActivity : ProjectActivity {
3438
if (!ApplicationManager.getApplication().isUnitTestMode
3539
&& ConfigurationSettings.getCurrentState().isCheckForNewScreenshots
3640
) {
41+
val desktopPath = Paths.get(System.getProperty("user.home"), "Desktop")
3742
project.service<FileWatcher>()
38-
.watch(Paths.get(System.getProperty("user.home"), "Desktop").toFile()) {
39-
if (listOf("jpg", "jpeg", "png").contains(it.extension)) {
40-
showImageAttachmentNotification(project, it.absolutePath)
43+
.watch(desktopPath) {
44+
if (watchExtensions.contains(it.extension.lowercase())) {
45+
showImageAttachmentNotification(project, desktopPath.resolve(it).absolutePathString())
4146
}
4247
}
4348
}
@@ -95,4 +100,4 @@ class CodeGPTProjectActivity : ProjectActivity {
95100
})
96101
.notify(project)
97102
}
98-
}
103+
}

src/main/kotlin/ee/carlrobert/codegpt/FileWatcher.kt

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,33 @@ package ee.carlrobert.codegpt
22

33
import com.intellij.openapi.Disposable
44
import com.intellij.openapi.components.Service
5-
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor
6-
import org.apache.commons.io.monitor.FileAlterationMonitor
7-
import org.apache.commons.io.monitor.FileAlterationObserver
8-
import java.io.File
5+
import java.nio.file.FileSystems
6+
import java.nio.file.Path
7+
import java.nio.file.StandardWatchEventKinds.ENTRY_CREATE
8+
import java.nio.file.WatchKey
9+
import kotlin.concurrent.thread
10+
911

1012
@Service(Service.Level.PROJECT)
1113
class FileWatcher : Disposable {
1214

13-
private var fileMonitor: FileAlterationMonitor? = null
15+
private var fileMonitor: Thread? = null
1416

15-
fun watch(pathToWatch: File, onFileCreated: (File) -> Unit) {
16-
val observer = FileAlterationObserver(pathToWatch)
17-
observer.addListener(object : FileAlterationListenerAdaptor() {
18-
override fun onFileCreate(file: File) {
19-
onFileCreated(file)
17+
fun watch(pathToWatch: Path, onFileCreated: (Path) -> Unit) {
18+
val watchService = FileSystems.getDefault().newWatchService()
19+
pathToWatch.register(watchService, ENTRY_CREATE) // watch for new files
20+
fileMonitor = thread {
21+
var key: WatchKey
22+
while ((watchService.take().also { key = it }) != null) {
23+
for (event in key.pollEvents()) {
24+
onFileCreated(event.context() as Path)
25+
}
26+
key.reset()
2027
}
21-
})
22-
fileMonitor = FileAlterationMonitor(500, observer)
23-
fileMonitor?.start()
28+
}
2429
}
2530

2631
override fun dispose() {
27-
fileMonitor?.stop()
32+
fileMonitor?.interrupt()
2833
}
29-
}
34+
}

0 commit comments

Comments
 (0)