Skip to content

Commit 6340666

Browse files
committed
fix(devins-lang): improve file writing performance #100
Use WriteAction.computeAndWait for file writing to improve performance and handle exceptions properly.
1 parent cfc96dd commit 6340666

File tree

1 file changed

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

1 file changed

+18
-19
lines changed

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

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ package cc.unitmesh.devti.language.compiler.exec
33
import cc.unitmesh.devti.language.compiler.model.LineInfo
44
import cc.unitmesh.devti.language.utils.lookupFile
55
import cc.unitmesh.devti.util.parser.Code
6-
import com.intellij.openapi.application.ApplicationManager
7-
import com.intellij.openapi.command.WriteCommandAction
6+
import com.intellij.openapi.application.WriteAction
87
import com.intellij.openapi.project.Project
98
import com.intellij.psi.PsiDocumentManager
109
import com.intellij.psi.PsiManager
@@ -16,28 +15,28 @@ class WriteInsCommand(val myProject: Project, val prop: String, val content: Str
1615
val range: LineInfo? = LineInfo.fromString(prop)
1716
val filename = prop.split("#")[0]
1817

19-
try {
20-
val virtualFile = myProject.lookupFile(filename) ?: return "<DevInsError>: File not found: $prop"
21-
val psiFile = PsiManager.getInstance(myProject).findFile(virtualFile)
22-
?: return "<DevInsError>: File not found: $prop"
23-
val document = PsiDocumentManager.getInstance(myProject).getDocument(psiFile)
24-
?: return "<DevInsError>: File not found: $prop"
18+
val virtualFile = myProject.lookupFile(filename) ?: return "<DevInsError>: File not found: $prop"
19+
val psiFile = PsiManager.getInstance(myProject).findFile(virtualFile)
20+
?: return "<DevInsError>: File not found: $prop"
21+
val document = PsiDocumentManager.getInstance(myProject).getDocument(psiFile)
22+
?: return "<DevInsError>: File not found: $prop"
2523

26-
ApplicationManager.getApplication().invokeLater {
27-
WriteCommandAction.runWriteCommandAction(myProject) {
28-
val startLine = range?.startLine ?: 0
29-
val endLine = range?.endLine ?: document.lineCount
24+
val resultMsg = WriteAction.computeAndWait<String, Throwable> {
25+
val startLine = range?.startLine ?: 0
26+
val endLine = range?.endLine ?: document.lineCount
3027

31-
val startOffset = document.getLineStartOffset(startLine)
32-
val endOffset = document.getLineEndOffset(endLine - 1)
28+
val startOffset = document.getLineStartOffset(startLine)
29+
val endOffset = document.getLineEndOffset(endLine - 1)
3330

34-
document.replaceString(startOffset, endOffset, content)
35-
}
31+
try {
32+
document.replaceString(startOffset, endOffset, content)
33+
} catch (e: Exception) {
34+
return@computeAndWait "<DevInsError>: ${e.message}"
3635
}
3736

38-
return "Writing to file: $prop"
39-
} catch (e: Exception) {
40-
return "<DevInsError>: ${e.message}"
37+
return@computeAndWait "Writing to file: $prop"
4138
}
39+
40+
return resultMsg
4241
}
4342
}

0 commit comments

Comments
 (0)