Skip to content

Commit

Permalink
fix(devins-lang): improve file lookup logic in AutoCommand #101
Browse files Browse the repository at this point in the history
The file lookup logic in `FileAutoCommand.kt` has been refactored to use the `lookupFile` method from `AutoCommand.kt`, which simplifies the code and improves readability. The old implementation used `guessProjectDir` and `VirtualFileManager` directly, which was
  • Loading branch information
phodal committed Mar 16, 2024
1 parent b89c9ba commit 8c25c78
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
@@ -1,5 +1,20 @@
package cc.unitmesh.devti.language.compiler

import com.intellij.openapi.project.Project
import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileManager

interface AutoCommand {
fun execute(): String?
}

fun Project.lookupFile(path: String): VirtualFile? {
val projectPath = this.guessProjectDir()?.toNioPath()
val realpath = projectPath?.resolve(path)

val virtualFile =
VirtualFileManager.getInstance().findFileByUrl("file://${realpath?.toAbsolutePath()}")
return virtualFile
}

Expand Up @@ -4,6 +4,7 @@ import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.util.TextRange
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileManager
import com.intellij.psi.PsiManager

Expand All @@ -21,15 +22,11 @@ class FileAutoCommand(private val myProject: Project, private val prop: String)
null
}

val projectPath = myProject.guessProjectDir()?.toNioPath()
val realpath = projectPath?.resolve(prop.trim())

val virtualFile =
VirtualFileManager.getInstance().findFileByUrl("file://${realpath?.toAbsolutePath()}")
val virtualFile = myProject.lookupFile(prop.trim())

val contentsToByteArray = virtualFile?.contentsToByteArray()
if (contentsToByteArray == null) {
logger.warn("File not found: $realpath")
logger.warn("File not found: $virtualFile")
return null
}

Expand Down Expand Up @@ -58,4 +55,4 @@ class FileAutoCommand(private val myProject: Project, private val prop: String)

return output.toString()
}
}
}

0 comments on commit 8c25c78

Please sign in to comment.