@@ -3,8 +3,7 @@ package cc.unitmesh.devti.language.compiler.exec
3
3
import cc.unitmesh.devti.language.compiler.model.LineInfo
4
4
import cc.unitmesh.devti.language.utils.lookupFile
5
5
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
8
7
import com.intellij.openapi.project.Project
9
8
import com.intellij.psi.PsiDocumentManager
10
9
import com.intellij.psi.PsiManager
@@ -16,28 +15,28 @@ class WriteInsCommand(val myProject: Project, val prop: String, val content: Str
16
15
val range: LineInfo ? = LineInfo .fromString(prop)
17
16
val filename = prop.split(" #" )[0 ]
18
17
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 "
25
23
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
30
27
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 )
33
30
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} "
36
35
}
37
36
38
- return " Writing to file: $prop "
39
- } catch (e: Exception ) {
40
- return " <DevInsError>: ${e.message} "
37
+ return @computeAndWait " Writing to file: $prop "
41
38
}
39
+
40
+ return resultMsg
42
41
}
43
42
}
0 commit comments