generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 292
/
DevInsCompiler.kt
261 lines (219 loc) ยท 8.76 KB
/
DevInsCompiler.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package cc.unitmesh.devti.language.compiler
import cc.unitmesh.devti.agent.model.CustomAgentConfig
import cc.unitmesh.devti.custom.compile.VariableTemplateCompiler
import cc.unitmesh.devti.language.compiler.error.DEVINS_ERROR
import cc.unitmesh.devti.language.compiler.exec.*
import cc.unitmesh.devti.language.completion.dataprovider.BuiltinCommand
import cc.unitmesh.devti.language.completion.dataprovider.CustomCommand
import cc.unitmesh.devti.language.completion.dataprovider.ToolHubVariable
import cc.unitmesh.devti.language.parser.CodeBlockElement
import cc.unitmesh.devti.language.psi.DevInFile
import cc.unitmesh.devti.language.psi.DevInTypes
import cc.unitmesh.devti.language.psi.DevInUsed
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.util.elementType
import kotlinx.coroutines.runBlocking
val CACHED_COMPILE_RESULT = mutableMapOf<String, DevInsCompiledResult>()
class DevInsCompiler(
private val myProject: Project,
private val file: DevInFile,
private val editor: Editor? = null,
private val element: PsiElement? = null
) {
private var skipNextCode: Boolean = false
private val logger = logger<DevInsCompiler>()
private val result = DevInsCompiledResult()
private val output: StringBuilder = StringBuilder()
/**
* Todo: build AST tree, then compile
*/
fun compile(): DevInsCompiledResult {
result.input = file.text
file.children.forEach {
when (it.elementType) {
DevInTypes.TEXT_SEGMENT -> output.append(it.text)
DevInTypes.NEWLINE -> output.append("\n")
DevInTypes.CODE -> {
if (skipNextCode) {
skipNextCode = false
return@forEach
}
output.append(it.text)
}
DevInTypes.USED -> processUsed(it as DevInUsed)
DevInTypes.COMMENTS -> {
// ignore comment
}
else -> {
output.append(it.text)
logger.warn("Unknown element type: ${it.elementType}")
}
}
}
result.output = output.toString()
CACHED_COMPILE_RESULT[file.name] = result
return result
}
private fun processUsed(used: DevInUsed) {
val firstChild = used.firstChild
val id = firstChild.nextSibling
when (firstChild.elementType) {
DevInTypes.COMMAND_START -> {
val command = BuiltinCommand.fromString(id?.text ?: "")
if (command == null) {
CustomCommand.fromString(myProject, id?.text ?: "")?.let { cmd ->
DevInFile.fromString(myProject, cmd.content).let { file ->
DevInsCompiler(myProject, file).compile().let {
output.append(it.output)
result.hasError = it.hasError
}
}
return
}
output.append(used.text)
logger.warn("Unknown command: ${id?.text}")
result.hasError = true
return
}
if (!command.requireProps) {
processingCommand(command, "", used, fallbackText = used.text)
return
}
val propElement = id.nextSibling?.nextSibling
val isProp = (propElement.elementType == DevInTypes.COMMAND_PROP)
if (!isProp) {
output.append(used.text)
logger.warn("No command prop found: ${used.text}")
result.hasError = true
return
}
processingCommand(command, propElement!!.text, used, fallbackText = used.text)
}
DevInTypes.AGENT_START -> {
val agentId = id?.text
val configs = CustomAgentConfig.loadFromProject(myProject).filter {
it.name == agentId
}
if (configs.isNotEmpty()) {
result.executeAgent = configs.first()
}
}
DevInTypes.VARIABLE_START -> {
val variableId = id?.text
val variable = ToolHubVariable.lookup(myProject, variableId)
if (variable.isNotEmpty()) {
output.append(variable.joinToString("\n") { it })
return
}
if (editor == null || element == null) {
output.append("$DEVINS_ERROR No context editor found for variable: ${used.text}")
result.hasError = true
return
}
val file = element.containingFile
VariableTemplateCompiler(file.language, file, element, editor).compile(used.text).let {
output.append(it)
}
}
else -> {
logger.warn("Unknown [cc.unitmesh.devti.language.psi.DevInUsed] type: ${firstChild.elementType}")
output.append(used.text)
}
}
}
private fun processingCommand(commandNode: BuiltinCommand, prop: String, used: DevInUsed, fallbackText: String) {
val command: InsCommand = when (commandNode) {
BuiltinCommand.FILE -> {
FileInsCommand(myProject, prop)
}
BuiltinCommand.REV -> {
RevInsCommand(myProject, prop)
}
BuiltinCommand.SYMBOL -> {
result.isLocalCommand = true
SymbolInsCommand(myProject, prop)
}
BuiltinCommand.WRITE -> {
result.isLocalCommand = true
val devInCode: CodeBlockElement? = lookupNextCode(used)
if (devInCode == null) {
PrintInsCommand("/" + commandNode.commandName + ":" + prop)
} else {
WriteInsCommand(myProject, prop, devInCode.text)
}
}
BuiltinCommand.PATCH -> {
result.isLocalCommand = true
val devInCode: CodeBlockElement? = lookupNextCode(used)
if (devInCode == null) {
PrintInsCommand("/" + commandNode.commandName + ":" + prop)
} else {
PatchInsCommand(myProject, prop, devInCode.text)
}
}
BuiltinCommand.COMMIT -> {
result.isLocalCommand = true
val devInCode: CodeBlockElement? = lookupNextCode(used)
if (devInCode == null) {
PrintInsCommand("/" + commandNode.commandName + ":" + prop)
} else {
CommitInsCommand(myProject, devInCode.text)
}
}
BuiltinCommand.RUN -> {
result.isLocalCommand = true
RunInsCommand(myProject, prop)
}
BuiltinCommand.FILE_FUNC -> {
result.isLocalCommand = true
FileFuncInsCommand(myProject, prop)
}
BuiltinCommand.SHELL -> {
result.isLocalCommand = true
ShellInsCommand(myProject, prop)
}
BuiltinCommand.BROWSE -> {
result.isLocalCommand = true
BrowseInsCommand(myProject, prop)
}
else -> {
PrintInsCommand("/" + commandNode.commandName + ":" + prop)
}
}
val execResult = runBlocking { command.execute() }
val isSucceed = execResult?.contains("$DEVINS_ERROR") == false
val result = if (isSucceed) {
val hasReadCodeBlock = commandNode in listOf(
BuiltinCommand.WRITE,
BuiltinCommand.PATCH,
BuiltinCommand.COMMIT
)
if (hasReadCodeBlock) {
skipNextCode = true
}
execResult
} else {
execResult ?: fallbackText
}
output.append(result)
}
private fun lookupNextCode(used: DevInUsed): CodeBlockElement? {
val devInCode: CodeBlockElement?
var next: PsiElement? = used
while (true) {
next = next?.nextSibling
if (next == null) {
devInCode = null
break
}
if (next.elementType == DevInTypes.CODE) {
devInCode = next as CodeBlockElement
break
}
}
return devInCode
}
}