From 352b459fa8af6ae390ff86eb370b9fa740a5893b Mon Sep 17 00:00:00 2001 From: Luo Tim Date: Wed, 9 Oct 2024 23:37:29 +0800 Subject: [PATCH] Fix IDE compatibility --- .../ai/devchat/plugin/completion/agent/ContextBuilder.kt | 3 +-- src/main/kotlin/ai/devchat/storage/RecentFilesTracker.kt | 5 ++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/ai/devchat/plugin/completion/agent/ContextBuilder.kt b/src/main/kotlin/ai/devchat/plugin/completion/agent/ContextBuilder.kt index 5c7e228..e80c467 100644 --- a/src/main/kotlin/ai/devchat/plugin/completion/agent/ContextBuilder.kt +++ b/src/main/kotlin/ai/devchat/plugin/completion/agent/ContextBuilder.kt @@ -7,7 +7,6 @@ import ai.devchat.common.IDEUtils.foldTextOfLevel import ai.devchat.common.IDEUtils.runInEdtAndGet import ai.devchat.common.Log import ai.devchat.storage.RecentFilesTracker -import com.intellij.openapi.vfs.isFile import com.intellij.psi.PsiFile import com.intellij.psi.util.PsiUtilCore.getPsiFile @@ -157,7 +156,7 @@ class ContextBuilder(val file: PsiFile, val offset: Int) { val project = file.project return runInEdtAndGet { project.getService(RecentFilesTracker::class.java).getRecentFiles().asSequence() - .filter { it.isFile && it.path != filepath } + .filter { it.isValid && !it.isDirectory && it.path != filepath } .map { CodeSnippet(it.path, getPsiFile(project, it).foldTextOfLevel(2)) } .filter { it.content.lines().count(String::isBlank) <= 50 } .takeWhile(::checkAndUpdateTokenCount) diff --git a/src/main/kotlin/ai/devchat/storage/RecentFilesTracker.kt b/src/main/kotlin/ai/devchat/storage/RecentFilesTracker.kt index 012df94..5417c61 100644 --- a/src/main/kotlin/ai/devchat/storage/RecentFilesTracker.kt +++ b/src/main/kotlin/ai/devchat/storage/RecentFilesTracker.kt @@ -10,7 +10,6 @@ import com.intellij.openapi.project.Project import com.intellij.openapi.roots.ProjectFileIndex import com.intellij.openapi.startup.ProjectActivity import com.intellij.openapi.vfs.VirtualFile -import com.intellij.openapi.vfs.isFile import com.intellij.util.messages.MessageBusConnection @@ -26,7 +25,7 @@ class RecentFilesTracker(private val project: Project) { val connection: MessageBusConnection = project.messageBus.connect() connection.subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, object : FileEditorManagerListener { override fun fileOpened(source: FileEditorManager, file: VirtualFile) { - if (file.isFile) { + if (file.isValid && !file.isDirectory) { addRecentFile(file) } } @@ -37,7 +36,7 @@ class RecentFilesTracker(private val project: Project) { } private fun addRecentFile(file: VirtualFile) = runInEdt { - if (file.isFile && projectFileIndex.isInContent(file)) { + if (file.isValid && !file.isDirectory && projectFileIndex.isInContent(file)) { recentFiles.remove(file) recentFiles.add(0, file) if (recentFiles.size > maxSize) {