Skip to content

Commit 1d625c0

Browse files
committed
refactor(devti): replace FileItem with FilePresentation in file search popup- Use FilePresentation instead of FileItem to represent files in the search popup
- Remove redundant properties and methods from FileItem - Update UI rendering to use FilePresentation properties - Improve code maintainability and reduce duplication
1 parent 9e3a1d7 commit 1d625c0

File tree

2 files changed

+16
-33
lines changed

2 files changed

+16
-33
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ data class FilePresentation(
1515
val icon: Icon? = null,
1616
val presentablePath: String = "",
1717
var panel: JPanel? = null,
18-
var namePanel: JPanel? = null
18+
var namePanel: JPanel? = null,
19+
var isRecentFile: Boolean = false
1920
) {
2021
companion object {
2122
fun from(project: Project, file: VirtualFile): FilePresentation {

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

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ class FileSearchPopup(
2929
private val onFilesSelected: (List<VirtualFile>) -> Unit
3030
) {
3131
private var popup: JBPopup? = null
32-
private val fileListModel = DefaultListModel<FileItem>()
32+
private val fileListModel = DefaultListModel<FilePresentation>()
3333
private val fileList = JBList(fileListModel)
3434
private val searchField = JTextField()
3535
private val contentPanel = JPanel(BorderLayout())
36-
private val allProjectFiles = mutableListOf<FileItem>()
36+
private val allProjectFiles = mutableListOf<FilePresentation>()
3737
private val minPopupSize = Dimension(435, 300)
3838

3939
init {
@@ -45,18 +45,19 @@ class FileSearchPopup(
4545
allProjectFiles.clear()
4646
EditorHistoryManager.Companion.getInstance(project).fileList.forEach { file ->
4747
if (file.canBeAdded(project)) {
48-
allProjectFiles.add(FileItem(file, isRecentFile = true))
48+
val presentation = FilePresentation.from(project, file)
49+
presentation.isRecentFile = true
50+
allProjectFiles.add(presentation)
4951
}
5052
}
5153

5254
ProjectFileIndex.getInstance(project).iterateContent { file ->
5355
if (file.canBeAdded(project) &&
5456
!ProjectFileIndex.getInstance(project).isUnderIgnored(file) &&
5557
ProjectFileIndex.getInstance(project).isInContent(file) &&
56-
!allProjectFiles.any { it.file.path == file.path }
58+
!allProjectFiles.any { it.path == file.path }
5759
) {
58-
59-
allProjectFiles.add(FileItem(file))
60+
allProjectFiles.add(FilePresentation.from(project, file))
6061
}
6162
true
6263
}
@@ -79,7 +80,7 @@ class FileSearchPopup(
7980
fileList.addMouseListener(object : MouseAdapter() {
8081
override fun mouseClicked(e: MouseEvent) {
8182
if (e.clickCount == 2) {
82-
val selectedFiles = fileList.selectedValuesList.map { it.file }
83+
val selectedFiles = fileList.selectedValuesList.map { it.virtualFile }
8384
if (selectedFiles.isNotEmpty()) {
8485
onFilesSelected(selectedFiles)
8586
popup?.cancel()
@@ -100,8 +101,8 @@ class FileSearchPopup(
100101
allProjectFiles
101102
} else {
102103
allProjectFiles.filter { item ->
103-
item.file.name.contains(searchText, ignoreCase = true) ||
104-
item.file.path.contains(searchText, ignoreCase = true)
104+
item.name.contains(searchText, ignoreCase = true) ||
105+
item.path.contains(searchText, ignoreCase = true)
105106
}
106107
}
107108

@@ -125,22 +126,13 @@ class FileSearchPopup(
125126
popup?.show(RelativePoint(component, Point(leftOffset, -minPopupSize.height + topOffset)))
126127
}
127128

128-
data class FileItem(
129-
val file: VirtualFile,
130-
val isRecentFile: Boolean = false
131-
) {
132-
val icon = file.fileType.icon
133-
val name = file.name
134-
val path = file.path
135-
}
136-
137-
class FileListCellRenderer : ListCellRenderer<FileItem> {
129+
class FileListCellRenderer() : ListCellRenderer<FilePresentation> {
138130
private val noBorderFocus = BorderFactory.createEmptyBorder(1, 1, 1, 1)
139131

140132
@NotNull
141133
override fun getListCellRendererComponent(
142-
list: JList<out FileItem>?,
143-
value: FileItem,
134+
list: JList<out FilePresentation>?,
135+
value: FilePresentation,
144136
index: Int,
145137
isSelected: Boolean,
146138
cellHasFocus: Boolean
@@ -152,7 +144,7 @@ class FileSearchPopup(
152144
val fileLabel = JBLabel(value.name, value.icon, JBLabel.LEFT)
153145
fileLabel.border = JBUI.Borders.emptyRight(8)
154146

155-
val relativePath = getRelativePath(value)
147+
val relativePath = value.presentablePath
156148
val pathLabel = JBLabel(" - $relativePath", JBLabel.LEFT)
157149
pathLabel.font = UIUtil.getFont(UIUtil.FontSize.SMALL, pathLabel.font)
158150
pathLabel.foreground = UIUtil.getContextHelpForeground()
@@ -183,15 +175,5 @@ class FileSearchPopup(
183175

184176
return mainPanel
185177
}
186-
187-
private fun getRelativePath(item: FileItem): String {
188-
// Try to make the path shorter for display purposes
189-
return try {
190-
val basePath = item.path.substringBeforeLast(item.name, "")
191-
if (basePath.isEmpty()) item.path else basePath
192-
} catch (e: Exception) {
193-
item.path
194-
}
195-
}
196178
}
197179
}

0 commit comments

Comments
 (0)