Skip to content

Commit

Permalink
fix(devti): correct language detection in code blocks #101
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
phodal committed Mar 12, 2024
1 parent 3bcf5a6 commit 437d2f1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
4 changes: 3 additions & 1 deletion src/main/kotlin/cc/unitmesh/devti/gui/chat/AutoDevInput.kt
Expand Up @@ -137,8 +137,10 @@ class AutoDevInput(
}

fun recreateDocument() {
// val language = findLanguage("DevIn")
val language = findLanguage("Markdown")
val file =
LightVirtualFile("AutoDevInput-" + UUID.randomUUID(), findLanguage("Markdown"), "")
LightVirtualFile("AutoDevInput-" + UUID.randomUUID(), language, "")

val document =
file.findDocument() ?: throw IllegalStateException("Can't create in-memory document")
Expand Down
1 change: 0 additions & 1 deletion src/main/kotlin/cc/unitmesh/devti/util/parser/CodeUtil.kt
Expand Up @@ -61,7 +61,6 @@ class Code(val language: Language, val text: String, val isComplete: Boolean) {
fun findLanguage(languageName: String): Language {
val fixedLanguage = when (languageName) {
"csharp" -> "c#"
"fsharp" -> "f#"
"cpp" -> "c++"
else -> languageName
}
Expand Down
20 changes: 8 additions & 12 deletions src/main/kotlin/com/intellij/temporary/gui/block/CodeBlockView.kt
Expand Up @@ -90,14 +90,6 @@ class CodeBlockView(
}

companion object {
private fun createCodeViewerFile(language: Language, content: String): LightVirtualFile {
val file = LightVirtualFile(AUTODEV_SNIPPET_NAME, language, content)
if (file.fileType == UnknownFileType.INSTANCE) {
file.fileType = PlainTextFileType.INSTANCE
}

return file
}

private fun createCodeViewerEditor(
project: Project,
Expand Down Expand Up @@ -159,12 +151,16 @@ class CodeBlockView(
message: CompletableMessage,
): CodePartEditorInfo {
val forceFoldEditorByDefault = message.getRole() === ChatRole.User
val createCodeViewerFile = createCodeViewerFile(language, graphProperty.get())
val file = LightVirtualFile(AUTODEV_SNIPPET_NAME, language, graphProperty.get())
if (file.fileType == UnknownFileType.INSTANCE) {
file.fileType = PlainTextFileType.INSTANCE
}

val document: Document =
createCodeViewerFile.findDocument() ?: throw IllegalStateException("Document not found")
file.findDocument() ?: throw IllegalStateException("Document not found")

val editor: EditorEx =
createCodeViewerEditor(project, createCodeViewerFile, document, disposable)
createCodeViewerEditor(project, file, document, disposable)

val toolbarActionGroup = ActionUtil.getActionGroup("AutoDev.ToolWindow.Snippet.Toolbar")!!
toolbarActionGroup.let {
Expand Down Expand Up @@ -195,7 +191,7 @@ class CodeBlockView(
editorFragment.setCollapsed(forceFoldEditorByDefault)
editorFragment.updateExpandCollapseLabel()

return CodePartEditorInfo(graphProperty, editorFragment.getContent(), editor, createCodeViewerFile)
return CodePartEditorInfo(graphProperty, editorFragment.getContent(), editor, file)
}
}
}
Expand Down

0 comments on commit 437d2f1

Please sign in to comment.