@@ -48,36 +48,37 @@ open class CodeHighlightSketch(
48
48
open val project : Project ,
49
49
open val text : String ,
50
50
private var ideaLanguage : Language ? = null ,
51
- val editorLineThreshold : Int = 6
51
+ val editorLineThreshold : Int = 6 ,
52
+ val fileName : String? = null
52
53
) : JBPanel<CodeHighlightSketch>(BorderLayout ()), DataProvider, LangSketch, Disposable {
53
54
private val devinLineThreshold = 10
54
55
private val minDevinLineThreshold = 1
55
56
private var isDevIns = false
56
57
57
- private var textLanguage: String? = null
58
+ private var textLanguage: String? = if (ideaLanguage != null ) ideaLanguage?.displayName else null
58
59
59
60
var editorFragment: EditorFragment ? = null
60
61
var previewEditor: FileEditor ? = null
61
62
private var hasSetupAction = false
62
63
63
64
init {
64
65
if (text.isNotNullOrEmpty() && (ideaLanguage?.displayName != " Markdown" && ideaLanguage != PlainTextLanguage .INSTANCE )) {
65
- initEditor(text)
66
+ initEditor(text, fileName )
66
67
}
67
68
}
68
69
69
70
private fun String?.isNotNullOrEmpty (): Boolean {
70
71
return this != null && this .isNotEmpty()
71
72
}
72
73
73
- fun initEditor (text : String ) {
74
+ fun initEditor (text : String , fileName : String? = null ) {
74
75
if (hasSetupAction) return
75
76
hasSetupAction = true
76
77
77
78
val editor = if (ideaLanguage?.displayName == " Markdown" ) {
78
- createMarkdownPreviewEditor(text) ? : createCodeViewerEditor(project, text, ideaLanguage, this )
79
+ createMarkdownPreviewEditor(text) ? : createCodeViewerEditor(project, text, ideaLanguage, fileName, this )
79
80
} else {
80
- createCodeViewerEditor(project, text, ideaLanguage, this )
81
+ createCodeViewerEditor(project, text, ideaLanguage, fileName, this )
81
82
}
82
83
83
84
border = JBEmptyBorder (8 )
@@ -159,7 +160,14 @@ open class CodeHighlightSketch(
159
160
if (ideaLanguage?.displayName == " DevIn" ) {
160
161
val currentText = getViewText()
161
162
if (currentText.startsWith(" /" + BuiltinCommand .WRITE .commandName + " :" )) {
163
+ // / get fileName after : and before \n
162
164
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 )
163
171
return
164
172
}
165
173
@@ -208,6 +216,7 @@ open class CodeHighlightSketch(
208
216
project : Project ,
209
217
text : String ,
210
218
ideaLanguage : Language ? ,
219
+ fileName : String? ,
211
220
disposable : Disposable ,
212
221
): EditorEx {
213
222
var editorText = text
@@ -231,7 +240,11 @@ open class CodeHighlightSketch(
231
240
editorText = newLines.joinToString(" \n " )
232
241
}
233
242
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
+ }
235
248
val document: Document = file.findDocument() ? : throw IllegalStateException (" Document not found" )
236
249
237
250
return createCodeViewerEditor(project, file, document, disposable, isShowLineNo)
@@ -320,6 +333,9 @@ open class CodeHighlightSketch(
320
333
}
321
334
}
322
335
336
+ /* *
337
+ * Add Write Command Action
338
+ */
323
339
private fun CodeHighlightSketch.processWriteCommand (currentText : String ) {
324
340
val button = JButton (" Write to file" ).apply {
325
341
preferredSize = JBUI .size(100 , 30 )
0 commit comments