fix: external file handling

This commit is contained in:
Carl-Robert Linnupuu 2025-05-30 01:29:42 +01:00
parent 14ff01212c
commit c481b8fe94
3 changed files with 49 additions and 37 deletions

View file

@ -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 {

View file

@ -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)
}

View file

@ -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
}
}
}