@@ -4,7 +4,8 @@ import cc.unitmesh.devti.language.compiler.error.DEVINS_ERROR
4
4
import cc.unitmesh.devti.language.compiler.model.LineInfo
5
5
import cc.unitmesh.devti.language.utils.lookupFile
6
6
import cc.unitmesh.devti.util.parser.Code
7
- import com.intellij.openapi.application.WriteAction
7
+ import com.intellij.openapi.application.runReadAction
8
+ import com.intellij.openapi.command.WriteCommandAction
8
9
import com.intellij.openapi.project.Project
9
10
import com.intellij.psi.PsiDocumentManager
10
11
import com.intellij.psi.PsiManager
@@ -19,25 +20,24 @@ class WriteInsCommand(val myProject: Project, val argument: String, val content:
19
20
val virtualFile = myProject.lookupFile(filename) ? : return " $DEVINS_ERROR : File not found: $argument "
20
21
val psiFile = PsiManager .getInstance(myProject).findFile(virtualFile)
21
22
? : return " $DEVINS_ERROR : File not found: $argument "
22
- val document = PsiDocumentManager .getInstance(myProject).getDocument(psiFile)
23
- ? : return " $DEVINS_ERROR : File not found: $argument "
24
23
25
- val resultMsg = WriteAction .computeAndWait<String , Throwable > {
26
- val startLine = range?.startLine ? : 0
27
- val endLine = range?.endLine ? : document.lineCount
24
+ val document = runReadAction {
25
+ PsiDocumentManager .getInstance(myProject).getDocument(psiFile)
26
+ } ? : return " $DEVINS_ERROR : File not found: $argument "
27
+
28
+ val startLine = range?.startLine ? : 0
29
+ val endLine = range?.endLine ? : document.lineCount
28
30
31
+ try {
29
32
val startOffset = document.getLineStartOffset(startLine)
30
33
val endOffset = document.getLineEndOffset(endLine - 1 )
31
-
32
- try {
34
+ WriteCommandAction .runWriteCommandAction(myProject) {
33
35
document.replaceString(startOffset, endOffset, content)
34
- } catch (e: Exception ) {
35
- return @computeAndWait " $DEVINS_ERROR : ${e.message} "
36
36
}
37
37
38
- return @computeAndWait " Writing to file: $argument "
38
+ return " Writing to file: $argument "
39
+ } catch (e: Exception ) {
40
+ return " $DEVINS_ERROR : ${e.message} "
39
41
}
40
-
41
- return resultMsg
42
42
}
43
43
}
0 commit comments