Skip to content

Commit e42fcd8

Browse files
committed
refactor(gui): rename FileListViewModel to RelatedFileListViewModel
- Rename FileListViewModel to RelatedFileListViewModel for better clarity - Update related imports and usages in affected modules - Refactor some code for improved readability and maintainability
1 parent fdefe3e commit e42fcd8

File tree

3 files changed

+33
-31
lines changed

3 files changed

+33
-31
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import cc.unitmesh.devti.agent.custom.model.CustomAgentState
77
import cc.unitmesh.devti.gui.chat.ui.file.WorkspaceFileToolbar
88
import cc.unitmesh.devti.gui.chat.ui.file.RelatedFileListCellRenderer
99
import cc.unitmesh.devti.gui.chat.ui.file.WorkspaceFilePanel
10-
import cc.unitmesh.devti.gui.chat.ui.viewmodel.FileListViewModel
10+
import cc.unitmesh.devti.gui.chat.ui.file.RelatedFileListViewModel
1111
import cc.unitmesh.devti.llms.tokenizer.Tokenizer
1212
import cc.unitmesh.devti.llms.tokenizer.TokenizerFactory
1313
import cc.unitmesh.devti.provider.RelatedClassesProvider
@@ -71,8 +71,8 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
7171
private val inputPanel = BorderLayoutPanel()
7272
val focusableComponent: JComponent get() = input
7373

74-
private val fileListViewModel = FileListViewModel(project)
75-
private val elementsList = JBList(fileListViewModel.getListModel())
74+
private val relatedFileListViewModel = RelatedFileListViewModel(project)
75+
private val elementsList = JBList(relatedFileListViewModel.getListModel())
7676

7777
private val workspaceFilePanel: WorkspaceFilePanel
7878

@@ -178,7 +178,7 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
178178
scrollPane.verticalScrollBarPolicy = JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED
179179
scrollPane.horizontalScrollBarPolicy = JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED
180180

181-
val toolbar = WorkspaceFileToolbar.createToolbar(project, fileListViewModel, input)
181+
val toolbar = WorkspaceFileToolbar.createToolbar(project, relatedFileListViewModel, input)
182182

183183
val headerPanel = JPanel(BorderLayout())
184184
headerPanel.add(toolbar, BorderLayout.NORTH)
@@ -204,7 +204,7 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
204204

205205
val currentFile = FileEditorManager.getInstance(project).selectedFiles.firstOrNull()
206206
currentFile?.let {
207-
fileListViewModel.addFileIfAbsent(currentFile, first = true)
207+
relatedFileListViewModel.addFileIfAbsent(currentFile, first = true)
208208
}
209209
}
210210

@@ -215,7 +215,7 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
215215
override fun selectionChanged(event: FileEditorManagerEvent) {
216216
val file = event.newFile ?: return
217217
ApplicationManager.getApplication().invokeLater {
218-
fileListViewModel.addFileIfAbsent(file, true)
218+
relatedFileListViewModel.addFileIfAbsent(file, true)
219219
}
220220
}
221221
}
@@ -251,11 +251,11 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
251251
val index = list.locationToIndex(e.point)
252252
if (index == -1) return
253253

254-
val wrapper = fileListViewModel.getListModel().getElementAt(index)
254+
val wrapper = relatedFileListViewModel.getListModel().getElementAt(index)
255255
val cellBounds = list.getCellBounds(index, index)
256256

257-
val actionType = fileListViewModel.determineFileAction(wrapper, e.point, cellBounds)
258-
val actionPerformed = fileListViewModel.handleFileAction(wrapper, actionType) { vfile, relativePath ->
257+
val actionType = relatedFileListViewModel.determineFileAction(wrapper, e.point, cellBounds)
258+
val actionPerformed = relatedFileListViewModel.handleFileAction(wrapper, actionType) { vfile, relativePath ->
259259
if (relativePath != null) {
260260
workspaceFilePanel.addFileToWorkspace(vfile)
261261
ApplicationManager.getApplication().invokeLater {
@@ -273,7 +273,7 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
273273
}
274274

275275
private fun updateElements(elements: List<PsiElement>?) {
276-
elements?.forEach { fileListViewModel.addFileIfAbsent(it.containingFile.virtualFile) }
276+
elements?.forEach { relatedFileListViewModel.addFileIfAbsent(it.containingFile.virtualFile) }
277277
}
278278

279279
fun showStopButton() {

core/src/main/kotlin/cc/unitmesh/devti/gui/chat/ui/viewmodel/FileListViewModel.kt renamed to core/src/main/kotlin/cc/unitmesh/devti/gui/chat/ui/file/RelatedFileListViewModel.kt

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
1-
package cc.unitmesh.devti.gui.chat.ui.viewmodel
1+
package cc.unitmesh.devti.gui.chat.ui.file
22

3-
import cc.unitmesh.devti.gui.chat.ui.file.FilePresentation
43
import cc.unitmesh.devti.util.isInProject
54
import com.intellij.diff.editor.DiffVirtualFileBase
5+
import com.intellij.icons.AllIcons
66
import com.intellij.openapi.Disposable
77
import com.intellij.openapi.fileEditor.FileEditorManager
88
import com.intellij.openapi.project.Project
99
import com.intellij.openapi.vfs.VirtualFile
1010
import java.util.EventListener
1111
import javax.swing.DefaultListModel
1212
import java.awt.Point
13+
import java.awt.Rectangle
14+
import javax.swing.JLabel
15+
import javax.swing.JPanel
16+
17+
interface FileListChangeListener : EventListener {
18+
fun onFileAdded(file: FilePresentation)
19+
fun onFileRemoved(file: FilePresentation)
20+
fun onListCleared()
21+
}
22+
23+
enum class FileActionType {
24+
INSERT,
25+
REMOVE,
26+
NONE
27+
}
1328

1429
/**
1530
* ViewModel for managing the list of file presentations.
1631
*/
17-
class FileListViewModel(private val project: Project) : Disposable {
32+
class RelatedFileListViewModel(private val project: Project) : Disposable {
1833
private val listModel = DefaultListModel<FilePresentation>()
1934

20-
interface FileListChangeListener : EventListener {
21-
fun onFileAdded(file: FilePresentation)
22-
fun onFileRemoved(file: FilePresentation)
23-
fun onListCleared()
24-
}
25-
26-
enum class FileActionType {
27-
INSERT,
28-
REMOVE,
29-
NONE
30-
}
31-
3235
private val listeners = mutableListOf<FileListChangeListener>()
3336

3437
fun addChangeListener(listener: FileListChangeListener) {
@@ -153,16 +156,16 @@ class FileListViewModel(private val project: Project) : Disposable {
153156
* @return The appropriate action type
154157
*/
155158
fun determineFileAction(filePresentation: FilePresentation, componentPoint: Point,
156-
componentBounds: java.awt.Rectangle): FileActionType {
159+
componentBounds: Rectangle): FileActionType {
157160
// Extract component hit detection logic
158161
val hitComponent = filePresentation.panel?.components?.firstOrNull {
159162
it.contains(componentPoint.x - componentBounds.x - it.x, it.height - 1)
160163
}
161164

162165
return when {
163-
hitComponent is javax.swing.JPanel -> FileActionType.INSERT
164-
hitComponent is javax.swing.JLabel &&
165-
hitComponent.icon == com.intellij.icons.AllIcons.Actions.Close -> FileActionType.REMOVE
166+
hitComponent is JPanel -> FileActionType.INSERT
167+
hitComponent is JLabel &&
168+
hitComponent.icon == AllIcons.Actions.Close -> FileActionType.REMOVE
166169
else -> FileActionType.NONE
167170
}
168171
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import cc.unitmesh.devti.AutoDevBundle
44
import cc.unitmesh.devti.gui.chat.ui.AutoDevInput
55
import cc.unitmesh.devti.gui.chat.ui.AutoDevInputSection
66
import cc.unitmesh.devti.gui.chat.ui.mediumFontFunction
7-
import cc.unitmesh.devti.gui.chat.ui.viewmodel.FileListViewModel
87
import com.intellij.openapi.project.Project
98
import com.intellij.ui.components.JBLabel
109
import com.intellij.ui.components.labels.LinkLabel
@@ -17,7 +16,7 @@ import java.awt.Container
1716
import kotlin.collections.forEach
1817

1918
object WorkspaceFileToolbar {
20-
fun createToolbar(project: Project, viewModel: FileListViewModel, input: AutoDevInput): JToolBar {
19+
fun createToolbar(project: Project, viewModel: RelatedFileListViewModel, input: AutoDevInput): JToolBar {
2120
val toolbar = JToolBar()
2221
toolbar.isFloatable = false
2322

0 commit comments

Comments
 (0)