Skip to content

Commit d6fa92a

Browse files
committed
refactor(core): rename Code to CodeFence and update usages
Update the `Code` class to `CodeFence` and replace all instances of `Code` with `CodeFence` throughout the codebase. This includes imports, variable declarations, and method calls. The `CodeFence` class now also includes an optional `extension` parameter. Update related test classes and method names to reflect the change from `Code` to `CodeFence`.
1 parent 69adfe9 commit d6fa92a

File tree

18 files changed

+83
-80
lines changed

18 files changed

+83
-80
lines changed

core/src/main/kotlin/cc/unitmesh/devti/agent/CustomAgentChatProcessor.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import cc.unitmesh.devti.provider.ContextPrompter
1111
import cc.unitmesh.devti.provider.devins.LanguagePromptProcessor
1212
import cc.unitmesh.devti.provider.devins.CustomAgentContext
1313
import cc.unitmesh.devti.util.LLMCoroutineScope
14-
import cc.unitmesh.devti.util.parser.Code
14+
import cc.unitmesh.devti.util.parser.CodeFence
1515
import com.intellij.openapi.components.Service
1616
import com.intellij.openapi.components.service
1717
import com.intellij.openapi.diagnostic.logger
@@ -56,7 +56,7 @@ class CustomAgentChatProcessor(val project: Project) {
5656
llmProvider.appendLocalMessage(content, ChatRole.Assistant)
5757
message.reRenderAssistantOutput()
5858

59-
val code = Code.parse(content)
59+
val code = CodeFence.parse(content)
6060
if (code.language.displayName == "DevIn") {
6161
devInCode = code.text
6262
}
@@ -76,7 +76,7 @@ class CustomAgentChatProcessor(val project: Project) {
7676
ui.hiddenProgressBar()
7777
ui.updateUI()
7878

79-
val code = Code.parse(msg)
79+
val code = CodeFence.parse(msg)
8080
if (code.language.displayName == "DevIn") {
8181
devInCode = code.text
8282
}

core/src/main/kotlin/cc/unitmesh/devti/custom/tasks/FileGenerateTask.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import cc.unitmesh.cf.core.llms.LlmMsg
44
import cc.unitmesh.cf.core.parser.MarkdownCode
55
import cc.unitmesh.devti.AutoDevBundle
66
import cc.unitmesh.devti.llms.LlmFactory
7-
import cc.unitmesh.devti.util.parser.Code
7+
import cc.unitmesh.devti.util.parser.CodeFence
88
import com.intellij.openapi.fileEditor.FileEditorManager
99
import com.intellij.openapi.progress.ProgressIndicator
1010
import com.intellij.openapi.progress.ProgressManager
@@ -18,7 +18,6 @@ import kotlinx.coroutines.flow.cancellable
1818
import kotlinx.coroutines.runBlocking
1919
import java.nio.file.Path
2020
import kotlin.io.path.Path
21-
import kotlinx.coroutines.flow.*
2221

2322
class FileGenerateTask(
2423
@JvmField val project: Project,
@@ -58,7 +57,7 @@ class FileGenerateTask(
5857
}
5958

6059
if (codeOnly) {
61-
val code = Code.parse(result).text
60+
val code = CodeFence.parse(result).text
6261
file.writeText(code)
6362
refreshAndOpenInEditor(file.toPath(), projectRoot)
6463
return

core/src/main/kotlin/cc/unitmesh/devti/gui/chat/AutoDevInput.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package cc.unitmesh.devti.gui.chat
22

33
import cc.unitmesh.devti.settings.LanguageChangedCallback.placeholder
4-
import cc.unitmesh.devti.util.parser.Code.Companion.findLanguage
4+
import cc.unitmesh.devti.util.parser.CodeFence.Companion.findLanguage
55
import com.intellij.openapi.Disposable
66
import com.intellij.openapi.actionSystem.*
77
import com.intellij.openapi.actionSystem.ex.AnActionListener

core/src/main/kotlin/cc/unitmesh/devti/gui/pair/AutoDevPairToolWindow.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import cc.unitmesh.devti.gui.chat.ChatRole
99
import cc.unitmesh.devti.pair.arch.ProjectPackageTree
1010
import cc.unitmesh.devti.pair.tasking.Tasking
1111
import cc.unitmesh.devti.provider.architecture.LayeredArchProvider
12-
import cc.unitmesh.devti.util.parser.Code
13-
import com.intellij.lang.html.HTMLLanguage
12+
import cc.unitmesh.devti.util.parser.CodeFence
1413
import com.intellij.openapi.Disposable
1514
import com.intellij.openapi.fileTypes.PlainTextLanguage
1615
import com.intellij.openapi.project.Project
@@ -54,7 +53,7 @@ class AutoDevPairToolWindow(val project: Project, val disposable: Disposable) :
5453
row {
5554
val text = layeredArch?.print() ?: "No Layered Arch"
5655
val block = CodeBlock(SimpleMessage(text, text, ChatRole.User))
57-
block.code = Code(PlainTextLanguage.INSTANCE, text, true)
56+
block.code = CodeFence(PlainTextLanguage.INSTANCE, text, true)
5857

5958
val codeBlockView = CodeBlockView(block, project, disposable)
6059
val component = codeBlockView.getComponent()

core/src/main/kotlin/cc/unitmesh/devti/intentions/action/task/BaseCompletionTask.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import cc.unitmesh.devti.llms.LlmFactory
99
import cc.unitmesh.devti.settings.AutoDevSettingsState
1010
import cc.unitmesh.devti.statusbar.AutoDevStatus
1111
import cc.unitmesh.devti.statusbar.AutoDevStatusService
12-
import cc.unitmesh.devti.util.parser.Code
12+
import cc.unitmesh.devti.util.parser.CodeFence
1313
import com.intellij.openapi.actionSystem.CustomShortcutSet
1414
import com.intellij.openapi.actionSystem.KeyboardShortcut
1515
import com.intellij.openapi.application.invokeLater
@@ -70,7 +70,7 @@ abstract class BaseCompletionTask(private val request: CodeCompletionRequest) :
7070
return@collect
7171
}
7272

73-
val parsedContent = Code.parse(char).text;
73+
val parsedContent = CodeFence.parse(char).text;
7474

7575
suggestion.append(parsedContent)
7676
invokeLater {

core/src/main/kotlin/cc/unitmesh/devti/intentions/action/task/LivingDocumentationTask.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ import cc.unitmesh.devti.provider.LivingDocumentation
77
import cc.unitmesh.devti.custom.document.LivingDocumentationType
88
import cc.unitmesh.devti.statusbar.AutoDevStatus
99
import cc.unitmesh.devti.statusbar.AutoDevStatusService
10-
import cc.unitmesh.devti.util.parser.Code
10+
import cc.unitmesh.devti.util.parser.CodeFence
1111
import com.intellij.openapi.diagnostic.logger
1212
import com.intellij.openapi.editor.Editor
1313
import com.intellij.openapi.progress.ProgressIndicator
1414
import com.intellij.openapi.progress.Task
1515
import com.intellij.psi.PsiElement
1616
import kotlinx.coroutines.runBlocking
17-
import kotlinx.coroutines.flow.*
1817

1918
/**
2019
* The `LivingDocumentationTask` class represents a background task for generating living documentation.
@@ -50,7 +49,7 @@ class LivingDocumentationTask(
5049

5150
// result maybe surroding by ```
5251
if (result.startsWith("```") && result.endsWith("```")) {
53-
result = Code.parse(result).text
52+
result = CodeFence.parse(result).text
5453
}
5554

5655
documentation.updateDoc(target, result, type, editor)

core/src/main/kotlin/cc/unitmesh/devti/util/parser/CodeUtil.kt

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package cc.unitmesh.devti.util.parser
33
import com.intellij.lang.Language
44
import com.intellij.openapi.fileTypes.PlainTextLanguage
55

6-
class Code(val language: Language, val text: String, val isComplete: Boolean) {
6+
class CodeFence(val language: Language, val text: String, val isComplete: Boolean, val extension: String? = "") {
77
companion object {
8-
fun parse(content: String): Code {
8+
fun parse(content: String): CodeFence {
99
val regex = Regex("```([\\w#+\\s]*)")
10-
val lines = content.lines()
10+
val lines = content.replace("\\n", "\n").lines()
1111

1212
var codeStarted = false
1313
var codeClosed = false
@@ -30,36 +30,16 @@ class Code(val language: Language, val text: String, val isComplete: Boolean) {
3030
}
3131
}
3232

33-
var startIndex = 0
34-
var endIndex = codeBuilder.length - 1
35-
36-
while (startIndex <= endIndex) {
37-
if (!codeBuilder[startIndex].isWhitespace()) {
38-
break
39-
}
40-
startIndex++
41-
}
42-
43-
while (endIndex >= startIndex) {
44-
if (!codeBuilder[endIndex].isWhitespace()) {
45-
break
46-
}
47-
endIndex--
48-
}
49-
50-
var trimmedCode = codeBuilder.substring(startIndex, endIndex + 1).toString()
33+
val trimmedCode = codeBuilder.trim().toString()
5134
val language = findLanguage(languageId ?: "")
35+
val extension =
36+
language.associatedFileType?.defaultExtension ?: lookupFileExt(languageId ?: "txt")
5237

53-
// if content is not empty, but code is empty, then it's a markdown
54-
if (trimmedCode.isEmpty()) {
55-
return Code(findLanguage("markdown"), content.replace("\\n", "\n"), codeClosed)
56-
}
57-
58-
if (languageId == "devin" || languageId == "devins") {
59-
trimmedCode = trimmedCode.replace("\\`\\`\\`", "```")
38+
return if (trimmedCode.isEmpty()) {
39+
CodeFence(language, "", codeClosed, extension)
40+
} else {
41+
CodeFence(language, trimmedCode, codeClosed, extension)
6042
}
61-
62-
return Code(language, trimmedCode, codeClosed)
6343
}
6444

6545
/**
@@ -83,5 +63,34 @@ class Code(val language: Language, val text: String, val isComplete: Boolean) {
8363
return registeredLanguages.find { it.displayName.equals(fixedLanguage, ignoreCase = true) }
8464
?: PlainTextLanguage.INSTANCE
8565
}
66+
67+
fun lookupFileExt(languageId: String): String {
68+
return when (languageId.lowercase()) {
69+
"c#" -> "cs"
70+
"c++" -> "cpp"
71+
"c" -> "c"
72+
"java" -> "java"
73+
"javascript" -> "js"
74+
"kotlin" -> "kt"
75+
"python" -> "py"
76+
"ruby" -> "rb"
77+
"swift" -> "swift"
78+
"typescript" -> "ts"
79+
"markdown" -> "md"
80+
"sql" -> "sql"
81+
"plantuml" -> "puml"
82+
"shell" -> "sh"
83+
"objective-c" -> "m"
84+
"objective-c++" -> "mm"
85+
"go" -> "go"
86+
"html" -> "html"
87+
"css" -> "css"
88+
"dart" -> "dart"
89+
"scala" -> "scala"
90+
"rust" -> "rs"
91+
"http request" -> "http"
92+
else -> languageId
93+
}
94+
}
8695
}
8796
}

core/src/main/kotlin/com/intellij/temporary/gui/block/CodeBlockView.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
package com.intellij.temporary.gui.block
33

44
import cc.unitmesh.devti.gui.chat.ChatRole
5-
import cc.unitmesh.devti.util.parser.Code
5+
import cc.unitmesh.devti.util.parser.CodeFence
66
import com.intellij.lang.Language
77
import com.intellij.openapi.Disposable
88
import com.intellij.openapi.actionSystem.ActionGroup
@@ -70,7 +70,7 @@ class CodeBlockView(
7070
}
7171

7272
private fun updateOrCreateCodeView(): CodePartEditorInfo? {
73-
val code: Code = getBlock().code
73+
val code: CodeFence = getBlock().code
7474
if (editorInfo == null) {
7575
val graphProperty = PropertyGraph(null, false).property(code.text)
7676
val editorInfo: CodePartEditorInfo = createCodeViewer(

core/src/main/kotlin/com/intellij/temporary/gui/block/MessageBlock.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
22
package com.intellij.temporary.gui.block
33

4-
import cc.unitmesh.devti.util.parser.Code
4+
import cc.unitmesh.devti.util.parser.CodeFence
55
import com.intellij.lang.Language
66

77
interface MessageBlock {
@@ -65,13 +65,13 @@ class TextBlock(val msg: CompletableMessage) : AbstractMessageBlock(msg) {
6565
class CodeBlock(private val msg: CompletableMessage, language: Language = Language.ANY) : AbstractMessageBlock(msg) {
6666
override var type: MessageBlockType = MessageBlockType.CodeEditor
6767

68-
var code: Code
68+
var code: CodeFence
6969

7070
init {
71-
this.code = Code(language, msg.text, false)
71+
this.code = CodeFence(language, msg.text, false)
7272
}
7373

7474
override fun onContentChanged(content: String) {
75-
this.code = Code.parse(content)
75+
this.code = CodeFence.parse(content)
7676
}
7777
}

core/src/main/kotlin/com/intellij/temporary/inlay/codecomplete/LLMInlayManagerImpl.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.intellij.temporary.inlay.codecomplete
22

33
import cc.unitmesh.devti.intentions.action.task.CodeCompletionRequest
44
import cc.unitmesh.devti.intentions.action.task.CodeCompletionTask
5-
import cc.unitmesh.devti.util.parser.Code
5+
import cc.unitmesh.devti.util.parser.CodeFence
66
import cc.unitmesh.devti.util.parser.PostCodeProcessor
77
import com.intellij.injected.editor.EditorWindow
88
import com.intellij.openapi.Disposable
@@ -116,7 +116,7 @@ class LLMInlayManagerImpl : LLMInlayManager {
116116
CodeCompletionTask(request).execute { completion ->
117117
if (completion.isEmpty()) return@execute
118118

119-
val completeCode = Code.parse(completion).text
119+
val completeCode = CodeFence.parse(completion).text
120120
currentCompletion = PostCodeProcessor(request.prefixText, request.suffixText, completeCode).execute()
121121

122122
WriteCommandAction.runWriteCommandAction(editor.project) {

0 commit comments

Comments
 (0)