generated from JetBrains/intellij-platform-plugin-template
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(devins-compiler): add support for writing and auto commands #101
Add WriteAutoCommand to allow writing content to files. Also, process WRITE commands in DevInsCompiler and execute WriteAutoCommand properly. This enables the handling of /write commands in DevIns files.
- Loading branch information
Showing
4 changed files
with
111 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/compiler/WriteAutoCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package cc.unitmesh.devti.language.compiler | ||
|
||
import cc.unitmesh.devti.language.compiler.data.LineInfo | ||
import cc.unitmesh.devti.language.compiler.utils.lookupFile | ||
import cc.unitmesh.devti.util.parser.Code | ||
import com.intellij.openapi.application.ApplicationManager | ||
import com.intellij.openapi.application.runWriteAction | ||
import com.intellij.openapi.command.WriteCommandAction | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiDocumentManager | ||
import com.intellij.psi.PsiManager | ||
|
||
class WriteAutoCommand(val myProject: Project, val prop: String, val content: String) : AutoCommand { | ||
override fun execute(): String? { | ||
val content = Code.parse(content).text | ||
|
||
val range: LineInfo? = LineInfo.fromString(prop) | ||
|
||
// prop name can be src/file.name#L1-L2 | ||
val filename = prop.split("#")[0] | ||
|
||
try { | ||
val virtualFile = myProject.lookupFile(filename) ?: return "<DevliError>: File not found: $prop" | ||
val psiFile = PsiManager.getInstance(myProject).findFile(virtualFile) | ||
?: return "<DevliError>: File not found: $prop" | ||
val document = PsiDocumentManager.getInstance(myProject).getDocument(psiFile) | ||
?: return "<DevliError>: File not found: $prop" | ||
|
||
ApplicationManager.getApplication().invokeLater { | ||
WriteCommandAction.runWriteCommandAction(myProject) { | ||
val startLine = range?.startLine ?: 0 | ||
val endLine = range?.endLine ?: document.lineCount | ||
|
||
val startOffset = document.getLineStartOffset(startLine) | ||
val endOffset = document.getLineEndOffset(endLine - 1) | ||
|
||
document.replaceString(startOffset, endOffset, content) | ||
} | ||
} | ||
|
||
return "Writing to file: $prop" | ||
} catch (e: Exception) { | ||
return "<DevliError>: ${e.message}" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters