Created
November 30, 2024 10:11
-
-
Save mhrlife/3d26a861feb1fca6084aa1a69bf1d06a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Necessary imports | |
import com.intellij.openapi.actionSystem.* | |
import com.intellij.openapi.application.ApplicationManager | |
import com.intellij.openapi.fileEditor.* | |
import com.intellij.openapi.project.Project | |
import com.intellij.openapi.util.Key | |
import com.intellij.openapi.vfs.* | |
import java.awt.Toolkit | |
import java.awt.datatransfer.StringSelection | |
// Define a key for project UserData | |
val RECENT_FILES_KEY = Key.create<MutableList<VirtualFile>>("RecentFilesList") | |
// Function to get or create the recent files list | |
fun getRecentFilesList(project: Project): MutableList<VirtualFile> { | |
var list = project.getUserData(RECENT_FILES_KEY) | |
if (list == null) { | |
list = mutableListOf() | |
project.putUserData(RECENT_FILES_KEY, list) | |
} | |
return list | |
} | |
// Function to add a file to the recent files list | |
fun addToRecentFiles(project: Project, file: VirtualFile?) { | |
if (file != null) { | |
val recentFilesList = getRecentFilesList(project) | |
recentFilesList.remove(file) // Remove if it already exists to prevent duplicates | |
recentFilesList.add(0, file) // Add to the front | |
if (recentFilesList.size > 10) { | |
recentFilesList.removeAt(recentFilesList.size - 1) // Keep the size to 10 | |
} | |
} | |
} | |
// Add the listener to the project | |
fun addRecentFilesListener(project: Project) { | |
val connection = project.messageBus.connect() | |
connection.subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, object : FileEditorManagerListener { | |
override fun fileOpened(source: FileEditorManager, file: VirtualFile) { | |
addToRecentFiles(project, file) | |
} | |
override fun selectionChanged(event: FileEditorManagerEvent) { | |
addToRecentFiles(project, event.newFile) | |
} | |
}) | |
} | |
// Define the action | |
class CopyRecentFilesAction : AnAction("Copy Recent Files Context") { | |
override fun actionPerformed(e: AnActionEvent) { | |
val project = e.project ?: return | |
ApplicationManager.getApplication().runReadAction { | |
val recentFilesList = getRecentFilesList(project) | |
val lastFiles = recentFilesList.take(10) | |
val sb = StringBuilder() | |
sb.append("Please read the following code snippets carefully. These are recent files from my project, complete with their paths relative to the project directory. They reflect the project's structure, coding practices, and style guidelines.\n\nWhen assisting me:\n- Use these code snippets to understand the existing architecture and codebase.\n- Maintain consistency with the project's coding style, practices, and conventions as demonstrated in the snippets.\n- Ensure that any generated code integrates seamlessly with the existing project structure and follows the same patterns.\n- After reviewing the code, please help me answer my questions or generate code, keeping in mind the context provided.\n\n# Here are the code snippets:\n\n") | |
val projectBaseDir = project.basePath?.let { VfsUtil.findFileByIoFile(java.io.File(it), true) } | |
for (virtualFile in lastFiles) { | |
// Get the file path relative to the project base directory | |
val filePath = if (projectBaseDir != null) { | |
VfsUtilCore.getRelativePath(virtualFile, projectBaseDir, '/') | |
} else { | |
virtualFile.path // Fallback to absolute path if base directory is not available | |
} | |
val fileContent = try { | |
VfsUtilCore.loadText(virtualFile) | |
} catch (e: Exception) { | |
"// Error reading file: ${e.message}" | |
} | |
sb.append("``` $filePath\n") | |
sb.append(fileContent) | |
sb.append("\n```\n\n") | |
} | |
sb.append("\n\n# My Question") | |
val result = sb.toString() | |
// Copy to clipboard | |
val clipboard = Toolkit.getDefaultToolkit().systemClipboard | |
val selection = StringSelection(result) | |
clipboard.setContents(selection, null) | |
} | |
} | |
} | |
// Register and add the action to the Tools menu | |
val actionId = "CopyRecentFilesLiveAction" | |
val actionManager = ActionManager.getInstance() | |
// Unregister the previous action if it exists | |
if (actionManager.getAction(actionId) != null) { | |
actionManager.unregisterAction(actionId) | |
} | |
val action = CopyRecentFilesAction() | |
actionManager.registerAction(actionId, action) | |
val menuGroup = actionManager.getAction("ToolsMenu") as DefaultActionGroup | |
// Check if the action is already added to the menu | |
if (menuGroup.getChildren(null).none { it.templatePresentation.text == action.templatePresentation.text }) { | |
menuGroup.add(action) | |
} | |
// Add the listener to all open projects | |
val projectManager = com.intellij.openapi.project.ProjectManager.getInstance() | |
projectManager.openProjects.forEach { project -> | |
addRecentFilesListener(project) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
LivePlugin: Copy Recent Files Content to Clipboard
Overview
This LivePlugin script for GoLand (or any IntelliJ-based IDE) allows you to copy the content of your last 5 recently opened files to the clipboard. It includes the full file paths relative to your project directory, making it ideal for providing context when seeking assistance or generating code with tools like ChatGPT.
How It Works
Setup Instructions
Prerequisites
Installation Steps
1. Install LivePlugin
File
>Settings
(on Windows/Linux) orGoLand
>Preferences
(on macOS).Plugins
from the sidebar.Marketplace
tab and search for "LivePlugin".2. Create a New Live Plugin
Plugins
in the menu bar.LivePlugin
>New Plugin
.3. Add the Plugin Script
Project
view, expand theLivePlugin
directory.CopyRecentFilesLivePlugin
) and findplugin.kts
inside it.plugin.kts
for editing.4. Paste the Code
Replace the entire content of
plugin.kts
with the code from your gist. Ensure that you copy all the code accurately.5. Save the Script
plugin.kts
file (Ctrl+S
orCmd+S
).Usage Instructions
Run the Plugin Action
Tools
in the menu bar.Assigning a Keyboard Shortcut (Optional)
File
>Settings
(Windows/Linux) orGoLand
>Preferences
(macOS).Keymap
from the sidebar.Ctrl
+Shift
+R
), then click OK.Using the Plugin