Skip to content

Commit 797e6f7

Browse files
committed
refactor(compiler): simplify file content handling in FileInsCommand
- Replace `contentsToByteArray` with `readText` for direct string content retrieval. - Remove redundant byte-to-string conversion and streamline content handling logic.
1 parent ef3b590 commit 797e6f7

File tree

1 file changed

+17
-20
lines changed
  • exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/compiler/exec

1 file changed

+17
-20
lines changed

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/compiler/exec/FileInsCommand.kt

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import cc.unitmesh.devti.devin.dataprovider.BuiltinCommand
88
import cc.unitmesh.devti.language.compiler.model.LineInfo
99
import cc.unitmesh.devti.language.utils.findFile
1010
import cc.unitmesh.devti.language.utils.lookupFile
11+
import cc.unitmesh.devti.sketch.ui.patch.readText
1112
import com.intellij.openapi.project.Project
1213
import com.intellij.openapi.vfs.VirtualFile
1314
import com.intellij.psi.PsiManager
@@ -35,8 +36,8 @@ class FileInsCommand(private val myProject: Project, private val prop: String) :
3536
virtualFile = myProject.findFile(filename, false)
3637
}
3738

38-
val contentsToByteArray = virtualFile?.contentsToByteArray()
39-
if (contentsToByteArray == null) {
39+
val content = virtualFile?.readText()
40+
if (content == null) {
4041
AutoDevNotifications.warn(myProject, "File not found: $prop")
4142
/// not show error message to just notify
4243
return "File not found: $prop"
@@ -46,29 +47,25 @@ class FileInsCommand(private val myProject: Project, private val prop: String) :
4647

4748
val lang = PsiManager.getInstance(myProject).findFile(virtualFile)?.language?.displayName ?: ""
4849

49-
contentsToByteArray.let { bytes ->
50-
51-
val content = bytes.toString(Charsets.UTF_8)
52-
val fileContent = if (range != null) {
53-
val subContent = try {
54-
content.split("\n").slice(range.startLine - 1 until range.endLine)
55-
.joinToString("\n")
56-
} catch (e: StringIndexOutOfBoundsException) {
57-
content
58-
}
59-
60-
subContent
61-
} else {
50+
val fileContent = if (range != null) {
51+
val subContent = try {
52+
content.split("\n").slice(range.startLine - 1 until range.endLine)
53+
.joinToString("\n")
54+
} catch (e: StringIndexOutOfBoundsException) {
6255
content
6356
}
6457

65-
// add file path
66-
output.append("// File: $prop\n")
67-
output.append("\n```$lang\n")
68-
output.append(fileContent)
69-
output.append("\n```\n")
58+
subContent
59+
} else {
60+
content
7061
}
7162

63+
// add file path
64+
output.append("// File: $prop\n")
65+
output.append("\n```$lang\n")
66+
output.append(fileContent)
67+
output.append("\n```\n")
68+
7269
return output.toString()
7370
}
7471
}

0 commit comments

Comments
 (0)