Skip to content

Commit 4133f23

Browse files
committed
feat(code-highlight): add fileName support and improve write command #208
- Added `fileName` parameter to `initEditor` and related methods to support custom filenames. - Enhanced `processWriteCommand` to handle filename extraction and language detection. - Disabled action for specific file extensions in `AutoDevDependenciesCheck`.
1 parent d78a46a commit 4133f23

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

core/src/main/kotlin/cc/unitmesh/devti/sketch/ui/code/CodeHighlightSketch.kt

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,36 +48,37 @@ open class CodeHighlightSketch(
4848
open val project: Project,
4949
open val text: String,
5050
private var ideaLanguage: Language? = null,
51-
val editorLineThreshold: Int = 6
51+
val editorLineThreshold: Int = 6,
52+
val fileName: String? = null
5253
) : JBPanel<CodeHighlightSketch>(BorderLayout()), DataProvider, LangSketch, Disposable {
5354
private val devinLineThreshold = 10
5455
private val minDevinLineThreshold = 1
5556
private var isDevIns = false
5657

57-
private var textLanguage: String? = null
58+
private var textLanguage: String? = if (ideaLanguage != null) ideaLanguage?.displayName else null
5859

5960
var editorFragment: EditorFragment? = null
6061
var previewEditor: FileEditor? = null
6162
private var hasSetupAction = false
6263

6364
init {
6465
if (text.isNotNullOrEmpty() && (ideaLanguage?.displayName != "Markdown" && ideaLanguage != PlainTextLanguage.INSTANCE)) {
65-
initEditor(text)
66+
initEditor(text, fileName)
6667
}
6768
}
6869

6970
private fun String?.isNotNullOrEmpty(): Boolean {
7071
return this != null && this.isNotEmpty()
7172
}
7273

73-
fun initEditor(text: String) {
74+
fun initEditor(text: String, fileName: String? = null) {
7475
if (hasSetupAction) return
7576
hasSetupAction = true
7677

7778
val editor = if (ideaLanguage?.displayName == "Markdown") {
78-
createMarkdownPreviewEditor(text) ?: createCodeViewerEditor(project, text, ideaLanguage, this)
79+
createMarkdownPreviewEditor(text) ?: createCodeViewerEditor(project, text, ideaLanguage, fileName, this)
7980
} else {
80-
createCodeViewerEditor(project, text, ideaLanguage, this)
81+
createCodeViewerEditor(project, text, ideaLanguage, fileName, this)
8182
}
8283

8384
border = JBEmptyBorder(8)
@@ -159,7 +160,14 @@ open class CodeHighlightSketch(
159160
if (ideaLanguage?.displayName == "DevIn") {
160161
val currentText = getViewText()
161162
if (currentText.startsWith("/" + BuiltinCommand.WRITE.commandName + ":")) {
163+
/// get fileName after : and before \n
162164
processWriteCommand(currentText)
165+
val fileName = currentText.lines().firstOrNull()?.substringAfter(":")
166+
val ext = fileName?.substringAfterLast(".")
167+
val parse = CodeFence.parse(editorFragment!!.editor.document.text)
168+
val language = if (ext != null) CodeFence.findLanguage(ext) else ideaLanguage
169+
val sketch = CodeHighlightSketch(project, parse.text, language, editorLineThreshold, fileName)
170+
add(sketch, BorderLayout.SOUTH)
163171
return
164172
}
165173

@@ -208,6 +216,7 @@ open class CodeHighlightSketch(
208216
project: Project,
209217
text: String,
210218
ideaLanguage: Language?,
219+
fileName: String?,
211220
disposable: Disposable,
212221
): EditorEx {
213222
var editorText = text
@@ -231,7 +240,11 @@ open class CodeHighlightSketch(
231240
editorText = newLines.joinToString("\n")
232241
}
233242

234-
val file = LightVirtualFile("shire.${ext}", language, editorText)
243+
val file = if (fileName != null) {
244+
LightVirtualFile(fileName, language, editorText)
245+
} else {
246+
LightVirtualFile("shire.${ext}", language, editorText)
247+
}
235248
val document: Document = file.findDocument() ?: throw IllegalStateException("Document not found")
236249

237250
return createCodeViewerEditor(project, file, document, disposable, isShowLineNo)
@@ -320,6 +333,9 @@ open class CodeHighlightSketch(
320333
}
321334
}
322335

336+
/**
337+
* Add Write Command Action
338+
*/
323339
private fun CodeHighlightSketch.processWriteCommand(currentText: String) {
324340
val button = JButton("Write to file").apply {
325341
preferredSize = JBUI.size(100, 30)

exts/ext-dependencies/src/233/main/kotlin/cc/unitmesh/dependencies/AutoDevDependenciesCheck.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class AutoDevDependenciesCheck : AnAction("Check dependencies has Issues") {
1818
val document = editor.document
1919
val file = FileDocumentManager.getInstance().getFile(document) ?: return
2020

21+
if (file.extension == "md" || file.extension == "txt" || file.extension == "devin") {
22+
e.presentation.isEnabled = false
23+
return
24+
}
25+
2126
val psiFile = PsiManager.getInstance(project).findFile(file) ?: return
2227
e.presentation.isEnabled = PackageService.getInstance(project).declaredDependencies(psiFile).isNotEmpty()
2328
}

0 commit comments

Comments
 (0)