Skip to content

Commit 437d2f1

Browse files
committed
fix(devti): correct language detection in code blocks #101
The commit corrects an issue where the code block language was not being detected correctly for `csharp` and `fsharp`. The language names have been updated to their standard forms, and the file type is now correctly set to PlainTextFileType if the original file type is UnknownFileType.
1 parent 3bcf5a6 commit 437d2f1

File tree

3 files changed

+11
-14
lines changed

3 files changed

+11
-14
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,10 @@ class AutoDevInput(
137137
}
138138

139139
fun recreateDocument() {
140+
// val language = findLanguage("DevIn")
141+
val language = findLanguage("Markdown")
140142
val file =
141-
LightVirtualFile("AutoDevInput-" + UUID.randomUUID(), findLanguage("Markdown"), "")
143+
LightVirtualFile("AutoDevInput-" + UUID.randomUUID(), language, "")
142144

143145
val document =
144146
file.findDocument() ?: throw IllegalStateException("Can't create in-memory document")

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ class Code(val language: Language, val text: String, val isComplete: Boolean) {
6161
fun findLanguage(languageName: String): Language {
6262
val fixedLanguage = when (languageName) {
6363
"csharp" -> "c#"
64-
"fsharp" -> "f#"
6564
"cpp" -> "c++"
6665
else -> languageName
6766
}

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,6 @@ class CodeBlockView(
9090
}
9191

9292
companion object {
93-
private fun createCodeViewerFile(language: Language, content: String): LightVirtualFile {
94-
val file = LightVirtualFile(AUTODEV_SNIPPET_NAME, language, content)
95-
if (file.fileType == UnknownFileType.INSTANCE) {
96-
file.fileType = PlainTextFileType.INSTANCE
97-
}
98-
99-
return file
100-
}
10193

10294
private fun createCodeViewerEditor(
10395
project: Project,
@@ -159,12 +151,16 @@ class CodeBlockView(
159151
message: CompletableMessage,
160152
): CodePartEditorInfo {
161153
val forceFoldEditorByDefault = message.getRole() === ChatRole.User
162-
val createCodeViewerFile = createCodeViewerFile(language, graphProperty.get())
154+
val file = LightVirtualFile(AUTODEV_SNIPPET_NAME, language, graphProperty.get())
155+
if (file.fileType == UnknownFileType.INSTANCE) {
156+
file.fileType = PlainTextFileType.INSTANCE
157+
}
158+
163159
val document: Document =
164-
createCodeViewerFile.findDocument() ?: throw IllegalStateException("Document not found")
160+
file.findDocument() ?: throw IllegalStateException("Document not found")
165161

166162
val editor: EditorEx =
167-
createCodeViewerEditor(project, createCodeViewerFile, document, disposable)
163+
createCodeViewerEditor(project, file, document, disposable)
168164

169165
val toolbarActionGroup = ActionUtil.getActionGroup("AutoDev.ToolWindow.Snippet.Toolbar")!!
170166
toolbarActionGroup.let {
@@ -195,7 +191,7 @@ class CodeBlockView(
195191
editorFragment.setCollapsed(forceFoldEditorByDefault)
196192
editorFragment.updateExpandCollapseLabel()
197193

198-
return CodePartEditorInfo(graphProperty, editorFragment.getContent(), editor, createCodeViewerFile)
194+
return CodePartEditorInfo(graphProperty, editorFragment.getContent(), editor, file)
199195
}
200196
}
201197
}

0 commit comments

Comments
 (0)