From a596fa969cc28e2dd60acaa84e67e63423aa4c5c Mon Sep 17 00:00:00 2001 From: Carl-Robert Linnupuu Date: Thu, 26 Dec 2024 23:35:56 +0000 Subject: [PATCH] feat: add 'Include Open Files' chat action item (closes #796) --- .../java/ee/carlrobert/codegpt/Icons.java | 2 ++ .../codegpt/ui/textarea/FileSearchService.kt | 19 +++++++++++++++++-- .../suggestion/item/SuggestionActionItems.kt | 16 ++++++++++++++++ .../suggestion/item/SuggestionGroupItems.kt | 2 +- src/main/resources/icons/listFiles.svg | 7 +++++++ src/main/resources/icons/listFiles_dark.svg | 7 +++++++ .../resources/messages/codegpt.properties | 1 + 7 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 src/main/resources/icons/listFiles.svg create mode 100644 src/main/resources/icons/listFiles_dark.svg diff --git a/src/main/java/ee/carlrobert/codegpt/Icons.java b/src/main/java/ee/carlrobert/codegpt/Icons.java index b3e267154..e3eca2d49 100644 --- a/src/main/java/ee/carlrobert/codegpt/Icons.java +++ b/src/main/java/ee/carlrobert/codegpt/Icons.java @@ -43,5 +43,7 @@ public final class Icons { IconLoader.getIcon("/icons/vcs.svg", Icons.class); public static final Icon QuestionMark = IconLoader.getIcon("/icons/questionMark.svg", Icons.class); + public static final Icon ListFiles = + IconLoader.getIcon("/icons/listFiles.svg", Icons.class); public static final Icon StatusBarCompletionInProgress = new AnimatedIcon.Default(); } diff --git a/src/main/kotlin/ee/carlrobert/codegpt/ui/textarea/FileSearchService.kt b/src/main/kotlin/ee/carlrobert/codegpt/ui/textarea/FileSearchService.kt index 86547de20..aa66aefd3 100644 --- a/src/main/kotlin/ee/carlrobert/codegpt/ui/textarea/FileSearchService.kt +++ b/src/main/kotlin/ee/carlrobert/codegpt/ui/textarea/FileSearchService.kt @@ -1,6 +1,7 @@ package ee.carlrobert.codegpt.ui.textarea import com.intellij.openapi.components.Service +import com.intellij.openapi.diagnostic.thisLogger import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VirtualFile import ee.carlrobert.codegpt.CodeGPTKeys @@ -11,10 +12,24 @@ import java.io.File @Service class FileSearchService private constructor(val project: Project) { + companion object { + private val logger = thisLogger() + } + fun addFileToSession(file: VirtualFile) { + addFilesToSession(listOf(file)) + } + + fun addFilesToSession(files: List) { val filesIncluded = project.getUserData(CodeGPTKeys.SELECTED_FILES).orEmpty().toMutableList() - filesIncluded.add(ReferencedFile(File(file.path))) + files.forEach { file -> + try { + filesIncluded.add(ReferencedFile(File(file.path))) + } catch (e: Exception) { + logger.error("Failed to add file to session", e) + } + } updateFilesInSession(filesIncluded) } @@ -26,4 +41,4 @@ class FileSearchService private constructor(val project: Project) { .syncPublisher(IncludeFilesInContextNotifier.FILES_INCLUDED_IN_CONTEXT_TOPIC) .filesIncluded(files) } -} +} \ No newline at end of file diff --git a/src/main/kotlin/ee/carlrobert/codegpt/ui/textarea/suggestion/item/SuggestionActionItems.kt b/src/main/kotlin/ee/carlrobert/codegpt/ui/textarea/suggestion/item/SuggestionActionItems.kt index 93ce66e87..39c85c32b 100644 --- a/src/main/kotlin/ee/carlrobert/codegpt/ui/textarea/suggestion/item/SuggestionActionItems.kt +++ b/src/main/kotlin/ee/carlrobert/codegpt/ui/textarea/suggestion/item/SuggestionActionItems.kt @@ -2,13 +2,16 @@ package ee.carlrobert.codegpt.ui.textarea.suggestion.item import com.intellij.icons.AllIcons import com.intellij.openapi.components.service +import com.intellij.openapi.fileEditor.FileEditorManager import com.intellij.openapi.options.ShowSettingsUtil import com.intellij.openapi.progress.ProgressManager import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VirtualFile +import com.intellij.openapi.vfs.isFile import ee.carlrobert.codegpt.CodeGPTBundle import ee.carlrobert.codegpt.CodeGPTKeys import ee.carlrobert.codegpt.EncodingManager +import ee.carlrobert.codegpt.Icons import ee.carlrobert.codegpt.settings.GeneralSettings import ee.carlrobert.codegpt.settings.documentation.DocumentationSettings import ee.carlrobert.codegpt.settings.documentation.DocumentationsConfigurable @@ -21,6 +24,7 @@ import ee.carlrobert.codegpt.ui.textarea.FileSearchService import ee.carlrobert.codegpt.ui.textarea.PromptTextField import ee.carlrobert.codegpt.util.GitUtil import git4idea.GitCommit +import javax.swing.Icon class FileActionItem(val file: VirtualFile) : SuggestionActionItem { override val displayName = file.name @@ -32,6 +36,18 @@ class FileActionItem(val file: VirtualFile) : SuggestionActionItem { } } +class IncludeOpenFilesActionItem : SuggestionActionItem { + override val displayName: String = + CodeGPTBundle.get("suggestionActionItem.includeOpenFiles.displayName") + override val icon: Icon = Icons.ListFiles + + override fun execute(project: Project, textPane: PromptTextField) { + val openFiles = project.service().openFiles.toList() + project.service().addFilesToSession(openFiles) + textPane.addInlayElement("files", "Open Files", this) + } +} + class FolderActionItem(val folder: VirtualFile) : SuggestionActionItem { override val displayName = folder.name override val icon = AllIcons.Nodes.Folder diff --git a/src/main/kotlin/ee/carlrobert/codegpt/ui/textarea/suggestion/item/SuggestionGroupItems.kt b/src/main/kotlin/ee/carlrobert/codegpt/ui/textarea/suggestion/item/SuggestionGroupItems.kt index 9c4f0a7c4..eec7261e8 100644 --- a/src/main/kotlin/ee/carlrobert/codegpt/ui/textarea/suggestion/item/SuggestionGroupItems.kt +++ b/src/main/kotlin/ee/carlrobert/codegpt/ui/textarea/suggestion/item/SuggestionGroupItems.kt @@ -39,7 +39,7 @@ class FileSuggestionGroupItem(private val project: Project) : SuggestionGroupIte return FileUtil.searchProjectFiles(project, searchText).toFileSuggestions() } - private fun Iterable.toFileSuggestions() = take(10).map { FileActionItem(it) } + private fun Iterable.toFileSuggestions() = take(10).map { FileActionItem(it) } + IncludeOpenFilesActionItem() } class FolderSuggestionGroupItem(private val project: Project) : SuggestionGroupItem { diff --git a/src/main/resources/icons/listFiles.svg b/src/main/resources/icons/listFiles.svg new file mode 100644 index 000000000..38ee91a91 --- /dev/null +++ b/src/main/resources/icons/listFiles.svg @@ -0,0 +1,7 @@ + + + + + + + diff --git a/src/main/resources/icons/listFiles_dark.svg b/src/main/resources/icons/listFiles_dark.svg new file mode 100644 index 000000000..798a2b352 --- /dev/null +++ b/src/main/resources/icons/listFiles_dark.svg @@ -0,0 +1,7 @@ + + + + + + + diff --git a/src/main/resources/messages/codegpt.properties b/src/main/resources/messages/codegpt.properties index 3115cb9e0..542266994 100644 --- a/src/main/resources/messages/codegpt.properties +++ b/src/main/resources/messages/codegpt.properties @@ -300,3 +300,4 @@ suggestionActionItem.webSearch.displayName=Web suggestionActionItem.viewDocumentations.displayName=View all docs suggestionActionItem.createPersona.displayName=Create new persona suggestionActionItem.createDocumentation.displayName=Create new documentation +suggestionActionItem.includeOpenFiles.displayName=Include Open Files