Skip to content

Commit

Permalink
feat: add 'Include Open Files' chat action item (closes #796)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlrobertoh committed Dec 27, 2024
1 parent 8d6ca73 commit a596fa9
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/main/java/ee/carlrobert/codegpt/Icons.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<VirtualFile>) {
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)
}

Expand All @@ -26,4 +41,4 @@ class FileSearchService private constructor(val project: Project) {
.syncPublisher(IncludeFilesInContextNotifier.FILES_INCLUDED_IN_CONTEXT_TOPIC)
.filesIncluded(files)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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<FileEditorManager>().openFiles.toList()
project.service<FileSearchService>().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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class FileSuggestionGroupItem(private val project: Project) : SuggestionGroupIte
return FileUtil.searchProjectFiles(project, searchText).toFileSuggestions()
}

private fun Iterable<VirtualFile>.toFileSuggestions() = take(10).map { FileActionItem(it) }
private fun Iterable<VirtualFile>.toFileSuggestions() = take(10).map { FileActionItem(it) } + IncludeOpenFilesActionItem()
}

class FolderSuggestionGroupItem(private val project: Project) : SuggestionGroupItem {
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/icons/listFiles.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions src/main/resources/icons/listFiles_dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/main/resources/messages/codegpt.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a596fa9

Please sign in to comment.