Skip to content

Commit d082c04

Browse files
committed
feat(gui): append workspace files to input text #358
- Add function getAllFilesFormat in WorkspacePanel to get formatted file list - Update text property in AutoDevInputSection to include workspace files - Refactor WorkspacePanel to use FilePresentation instead of VirtualFile directly
1 parent cbb6147 commit d082c04

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

core/src/main/kotlin/cc/unitmesh/devti/gui/chat/ui/AutoDevInputSection.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
8888

8989
var text: String
9090
get() {
91-
return input.text
91+
val files = workspacePanel.getAllFilesFormat()
92+
return input.text + "\n" + files
9293
}
9394
set(text) {
9495
input.recreateDocument()
@@ -256,7 +257,6 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
256257
val actionPerformed = fileListViewModel.handleFileAction(wrapper, actionType) { vfile, relativePath ->
257258
if (relativePath != null) {
258259
workspacePanel.addFileToWorkspace(vfile)
259-
260260
ApplicationManager.getApplication().invokeLater {
261261
if (!vfile.isValid) return@invokeLater
262262
val psiFile = PsiManager.getInstance(project).findFile(vfile) ?: return@invokeLater

core/src/main/kotlin/cc/unitmesh/devti/gui/chat/ui/WorkspacePanel.kt

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class WorkspacePanel(
1818
private val project: Project,
1919
private val input: AutoDevInput
2020
) : JPanel(BorderLayout()) {
21-
private val workspaceFiles = mutableListOf<VirtualFile>()
21+
private val workspaceFiles = mutableListOf<FilePresentation>()
2222
private val filesPanel = JPanel(WrapLayout(FlowLayout.LEFT, 2, 2))
2323

2424
init {
@@ -56,19 +56,12 @@ class WorkspacePanel(
5656
}
5757

5858
fun addFileToWorkspace(file: VirtualFile) {
59-
if (!workspaceFiles.contains(file)) {
60-
workspaceFiles.add(file)
59+
val filePresentation = FilePresentation.from(project, file)
60+
if (workspaceFiles.none { it.virtualFile == file }) {
61+
workspaceFiles.add(filePresentation)
6162
updateFilesPanel()
6263

63-
val relativePath = try {
64-
project.basePath?.let { basePath ->
65-
file.path.substringAfter(basePath).removePrefix("/")
66-
} ?: file.path
67-
} catch (e: Exception) {
68-
file.path
69-
}
70-
71-
input.appendText("\n/file:$relativePath")
64+
// input.appendText("\n/file:${filePresentation.relativePath(project)}")
7265
}
7366
}
7467

@@ -88,9 +81,9 @@ class WorkspacePanel(
8881
})
8982
filesPanel.add(addButton)
9083

91-
for (file in workspaceFiles) {
92-
val fileLabel = FileItemPanel(project, file) {
93-
removeFile(file)
84+
for (filePresentation in workspaceFiles) {
85+
val fileLabel = FileItemPanel(project, filePresentation) {
86+
removeFile(filePresentation)
9487
}
9588
filesPanel.add(fileLabel)
9689
}
@@ -99,20 +92,30 @@ class WorkspacePanel(
9992
filesPanel.repaint()
10093
}
10194

102-
private fun removeFile(file: VirtualFile) {
103-
workspaceFiles.remove(file)
95+
private fun removeFile(filePresentation: FilePresentation) {
96+
workspaceFiles.remove(filePresentation)
10497
updateFilesPanel()
10598
}
10699

107100
fun clear() {
108101
workspaceFiles.clear()
109102
updateFilesPanel()
110103
}
104+
105+
fun getAllFiles(): List<FilePresentation> {
106+
return workspaceFiles.toList()
107+
}
108+
109+
fun getAllFilesFormat(): String {
110+
return workspaceFiles.joinToString(separator = "\n") {
111+
"\n/file:${it.presentablePath}"
112+
}
113+
}
111114
}
112115

113116
class FileItemPanel(
114117
private val project: Project,
115-
private val file: VirtualFile,
118+
private val filePresentation: FilePresentation,
116119
private val onRemove: () -> Unit
117120
) : JPanel(FlowLayout(FlowLayout.LEFT, 2, 0)) {
118121
init {
@@ -123,8 +126,7 @@ class FileItemPanel(
123126
background = JBColor(0xEDF4FE, 0x313741)
124127
isOpaque = true
125128

126-
val icon = file.fileType.icon
127-
val fileLabel = JBLabel(file.name, icon, JBLabel.LEFT)
129+
val fileLabel = JBLabel(filePresentation.name, filePresentation.icon, JBLabel.LEFT)
128130

129131
val removeLabel = JBLabel(AllIcons.Actions.Close)
130132
removeLabel.cursor = Cursor(Cursor.HAND_CURSOR)
@@ -227,3 +229,4 @@ class WrapLayout : FlowLayout {
227229
}
228230
}
229231
}
232+

0 commit comments

Comments
 (0)