diff --git a/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/actions/AutoApplyAction.kt b/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/actions/AutoApplyAction.kt index 3e185e9b..bad4235a 100644 --- a/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/actions/AutoApplyAction.kt +++ b/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/actions/AutoApplyAction.kt @@ -54,13 +54,12 @@ class AutoApplyAction( } val selectedEditor = EditorUtil.getSelectedEditor(project) - anActionLink.text = if (selectedEditor?.virtualFile == null) { - "Apply" - } else { - "Apply to ${selectedEditor.virtualFile.name}" - } - anActionLink.isEnabled = selectedEditor != null - anActionLink.isVisible = true + val selectedEditorFile = selectedEditor?.virtualFile + val canApply = selectedEditorFile != null && selectedEditorFile.isWritable + + anActionLink.text = if (canApply) "Apply to ${selectedEditorFile.name}" else "Apply" + anActionLink.isEnabled = canApply + anActionLink.isVisible = canApply } override fun createCustomComponent(presentation: Presentation, place: String): JComponent { diff --git a/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/header/HeaderPanel.kt b/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/header/HeaderPanel.kt index f91b7343..508a417e 100644 --- a/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/header/HeaderPanel.kt +++ b/src/main/kotlin/ee/carlrobert/codegpt/toolwindow/chat/editor/header/HeaderPanel.kt @@ -17,6 +17,7 @@ import com.intellij.ui.components.JBLabel import com.intellij.util.ui.JBUI import com.intellij.util.ui.components.BorderLayoutPanel import ee.carlrobert.codegpt.toolwindow.chat.editor.diff.DiffStatsComponent +import ee.carlrobert.codegpt.util.file.FileUtil import java.awt.BorderLayout import java.awt.Dimension import java.io.File @@ -43,14 +44,7 @@ abstract class HeaderPanel(protected val config: HeaderConfig) : BorderLayoutPan font = JBUI.Fonts.smallFont() } - protected var virtualFile: VirtualFile? = config.filePath?.let { - try { - LocalFileSystem.getInstance().refreshAndFindFileByIoFile(File(it)) - } catch (t: Throwable) { - logger.error(t) - null - } - } + protected var virtualFile: VirtualFile? = FileUtil.resolveVirtualFile(config.filePath) private val rightPanel = JPanel().apply { layout = BoxLayout(this, BoxLayout.X_AXIS) @@ -70,7 +64,7 @@ abstract class HeaderPanel(protected val config: HeaderConfig) : BorderLayoutPan protected fun setupUI() { setupPanelAppearance() - setupFilePathOrLanguageLabel(virtualFile) + setupFileLinkOrLanguageLabel(virtualFile) rightPanel.removeAll() initializeRightPanel(rightPanel) @@ -107,26 +101,25 @@ abstract class HeaderPanel(protected val config: HeaderConfig) : BorderLayoutPan minimumSize = Dimension(preferredSize.width, 32) } - protected fun setupFilePathOrLanguageLabel(virtualFile: VirtualFile?) { + private fun setupFileLinkOrLanguageLabel(virtualFile: VirtualFile?) { val filePath = config.filePath - if (filePath != null) { - if (virtualFile == null) { - val newFileLink = createNewFileLink(filePath, config.editorEx) - addToLeft(newFileLink) - } else { - addToLeft(JPanel().apply { - layout = BoxLayout(this, BoxLayout.X_AXIS) - isOpaque = false - add(ActionLink(virtualFile.name) { - OpenFileAction.openFile(virtualFile, config.project) - }.apply { - setExternalLinkIcon() - }) - add(statsComponent) - }) - } - } else { - addToLeft(createLanguageLabel()) + when { + filePath == null -> addToLeft(createLanguageLabel()) + virtualFile == null -> addToLeft(createNewFileLink(filePath, config.editorEx)) + else -> addToLeft(createFileLinkPanel(virtualFile)) + } + } + + private fun createFileLinkPanel(virtualFile: VirtualFile): JPanel { + return JPanel().apply { + layout = BoxLayout(this, BoxLayout.X_AXIS) + isOpaque = false + add(ActionLink(virtualFile.name) { + OpenFileAction.openFile(virtualFile, config.project) + }.apply { + setExternalLinkIcon() + }) + add(statsComponent) } } @@ -157,7 +150,7 @@ abstract class HeaderPanel(protected val config: HeaderConfig) : BorderLayoutPan } remove(actionLink) - setupFilePathOrLanguageLabel(newFile) + setupFileLinkOrLanguageLabel(newFile) OpenFileAction.openFile(newFile, config.project) } diff --git a/src/main/kotlin/ee/carlrobert/codegpt/util/file/FileUtil.kt b/src/main/kotlin/ee/carlrobert/codegpt/util/file/FileUtil.kt index b48bae76..6cade673 100644 --- a/src/main/kotlin/ee/carlrobert/codegpt/util/file/FileUtil.kt +++ b/src/main/kotlin/ee/carlrobert/codegpt/util/file/FileUtil.kt @@ -6,9 +6,10 @@ import com.fasterxml.jackson.databind.ObjectMapper import com.intellij.openapi.diagnostic.thisLogger import com.intellij.openapi.progress.ProgressIndicator import com.intellij.openapi.util.io.FileUtil.createDirectory +import com.intellij.openapi.vfs.JarFileSystem +import com.intellij.openapi.vfs.LocalFileSystem import com.intellij.openapi.vfs.VfsUtilCore import com.intellij.openapi.vfs.VirtualFile -import com.intellij.psi.search.* import ee.carlrobert.codegpt.settings.service.llama.LlamaSettings.getLlamaModelsPath import java.io.File import java.io.FileOutputStream @@ -25,6 +26,7 @@ import java.nio.file.StandardOpenOption import java.text.DecimalFormat import java.util.* import java.util.regex.Pattern +import kotlin.Throws object FileUtil { @@ -234,4 +236,22 @@ object FileUtil { ) } } + + fun resolveVirtualFile(filePath: String?): VirtualFile? { + if (filePath == null) return null + return try { + if (filePath.contains("!")) { + val jarSeparatorIndex = filePath.indexOf('!') + val archivePath = filePath.substring(0, jarSeparatorIndex) + val internalPath = filePath.substring(jarSeparatorIndex + 1).removePrefix("/") + val jarFileSystemPath = "$archivePath!/$internalPath" + JarFileSystem.getInstance().findFileByPath(jarFileSystemPath) + } else { + LocalFileSystem.getInstance().refreshAndFindFileByIoFile(File(filePath)) + } + } catch (t: Throwable) { + logger.error(t) + null + } + } } \ No newline at end of file