Skip to content

Commit 5f8dc29

Browse files
committed
fix(devins-lang): improve file content extraction
#100 - Improve file content extraction to handle line ranges more accurately and efficiently. The changes in `FileInsCommand.kt` allow for more precise extraction of file content based on line ranges, enhancing the accuracy and efficiency of the process. Additionally, in `DevInsCompiler.kt`, a new case for handling comments has been added to ignore comments during processing. Lastly, in `DevInsFlowProcessor.kt`, the usage of `runReadAction` has been added to ensure safe access to the PSI file.
1 parent 4d09889 commit 5f8dc29

File tree

4 files changed

+10
-6
lines changed

4 files changed

+10
-6
lines changed

docs/autodev-arch.svg

Lines changed: 3 additions & 3 deletions
Loading

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ class DevInsCompiler(
4343

4444
output.append(it.text)
4545
}
46-
4746
DevInTypes.USED -> processUsed(it as DevInUsed)
47+
DevInTypes.COMMENTS -> {
48+
// ignore comment
49+
}
4850
else -> {
4951
output.append(it.text)
5052
logger.warn("Unknown element type: ${it.elementType}")

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ class FileInsCommand(private val myProject: Project, private val prop: String) :
3838
val content = bytes.toString(Charsets.UTF_8)
3939
val fileContent = if (range != null) {
4040
val subContent = try {
41-
content.substring(range.startLine, range.endLine)
41+
content.split("\n").slice(range.startLine - 1 until range.endLine)
42+
.joinToString("\n")
4243
} catch (e: StringIndexOutOfBoundsException) {
4344
content
4445
}

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/run/flow/DevInsFlowProcessor.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cc.unitmesh.devti.language.run.flow
33
import cc.unitmesh.devti.language.psi.DevInFile
44
import cc.unitmesh.devti.language.psi.DevInVisitor
55
import com.intellij.execution.process.ProcessEvent
6+
import com.intellij.openapi.application.runReadAction
67
import com.intellij.openapi.components.Service
78
import com.intellij.openapi.project.Project
89
import com.intellij.psi.PsiComment
@@ -28,7 +29,7 @@ class DevInsFlowProcessor(val project: Project) {
2829
* continue get last compile result
2930
*/
3031
fun process(output: String, event: ProcessEvent, scriptPath: String) {
31-
val devInFile: DevInFile? = DevInFile.lookup(project, scriptPath)
32+
val devInFile: DevInFile? = runReadAction { DevInFile.lookup(project, scriptPath) }
3233
if (event.exitCode == 0) {
3334
// continue
3435
}

0 commit comments

Comments
 (0)