1
1
"use strict" ;
2
2
3
3
import * as vscode from "vscode" ;
4
- import { isString } from "util" ;
5
4
import { AssertionError } from "assert" ;
6
5
7
6
export function runMarkdownSnippet ( ) {
@@ -19,19 +18,18 @@ export function runMarkdownSnippet() {
19
18
return ;
20
19
}
21
20
22
- // handle selection
23
21
// get snippet
24
22
const selectedText = editor . document . getText ( selection ) ;
25
23
const eol = getEol ( editor . document . eol ) ;
26
24
27
25
const content = SelectedContent . parseSelectedText ( eol , selectedText ) ;
28
- if ( isString ( content ) ) {
26
+ if ( typeof content === "string" ) {
29
27
// if string is returned, this represents an error message.
30
28
vscode . window . showErrorMessage ( content ) ;
31
29
return ;
32
30
}
33
31
34
- // get current active file's uri
32
+ // Get current active file's uri
35
33
// unsupported: if not a file, show error message and exit
36
34
const uri = editor . document . uri ;
37
35
if ( uri === undefined || uri . scheme !== "file" ) {
@@ -41,12 +39,11 @@ export function runMarkdownSnippet() {
41
39
return ;
42
40
}
43
41
44
- // get configuration
42
+ // Get configuration
45
43
const config = vscode . workspace . getConfiguration ( "markdown-run-snippet" ) ;
46
- const finalized = FinalizedContent . finalize ( config , eol , content ) ;
44
+ const finalized = content . toSnippet ( config , eol ) ;
47
45
48
- // open it in window as Untitled new file
49
- // and run it.
46
+ // Open it in window as Untitled new file and run it.
50
47
vscode . workspace
51
48
. openTextDocument ( {
52
49
language : finalized . vscodeType ,
@@ -76,85 +73,13 @@ function getEol(eeol: vscode.EndOfLine): string {
76
73
return eol ;
77
74
}
78
75
79
- class FinalizedContent {
80
- public mdType : string ;
81
- public vscodeType : string ;
82
- public rawSnippet : string ;
83
- public fullSnippet : string ;
84
-
76
+ class Snippet {
85
77
constructor (
86
- mdType : string ,
87
- vscodeType : string ,
88
- rawSnippet : string ,
89
- fullSnippet : string
90
- ) {
91
- this . mdType = mdType ;
92
- this . vscodeType = vscodeType ;
93
- this . rawSnippet = rawSnippet ;
94
- this . fullSnippet = fullSnippet ;
95
- }
96
-
97
- public static finalize (
98
- cfg : vscode . WorkspaceConfiguration ,
99
- eol : string ,
100
- content : SelectedContent
101
- ) : FinalizedContent {
102
- const mdType = content . mdType ;
103
- let vscodeType = FinalizedContent . mdToVscodeFiletype ( cfg , content ) ;
104
- const rawSnippet = content . snippet ;
105
- const fullSnippet = FinalizedContent . applyTemplate ( cfg , content ) . replace (
106
- / \n / g,
107
- eol
108
- ) ;
109
-
110
- // fallback: unregistered mdType
111
- if ( vscodeType === undefined ) {
112
- vscodeType = content . mdType ;
113
- }
114
-
115
- return new FinalizedContent ( mdType , vscodeType , rawSnippet , fullSnippet ) ;
116
- }
117
-
118
- private static mdToVscodeFiletype (
119
- cfg : vscode . WorkspaceConfiguration ,
120
- content : SelectedContent
121
- ) : string | undefined {
122
- const mdToVscodeTypeMap = cfg . get < any > ( "mdToVscodeTypeMap" ) ;
123
- return mdToVscodeTypeMap [ content . mdType ] ;
124
- }
125
-
126
- private static applyTemplate (
127
- cfg : vscode . WorkspaceConfiguration ,
128
- content : SelectedContent
129
- ) : string {
130
- const mdTypeToTemplateMap = cfg . get < any > ( "mdTypeToTemplateMap" ) ;
131
- let template = mdTypeToTemplateMap [ content . mdType ] ;
132
- if ( template === undefined ) {
133
- return content . snippet ;
134
- }
135
-
136
- // detect the depth of indentation
137
- // The template may contain more than one `$snippet`, but the depth of
138
- // indentation is based on the first occurrence.
139
- const indentMatch = / ^ ( \ * ) \$ s n i p p e t / m. exec ( template ) ;
140
- if ( indentMatch === null ) {
141
- // nothing to replace.
142
- return template ;
143
- }
144
-
145
- // remove indent from template
146
- template = template . replace ( / ^ \ * \$ s n i p p e t / m, "$snippet" ) ;
147
-
148
- // insert indentation
149
- const indent = indentMatch [ 1 ] ;
150
- const splitted_rawSnippet = content . snippet . split ( "\n" ) ;
151
- const indentedSnippet = splitted_rawSnippet
152
- . map ( line => {
153
- return indent + line ;
154
- } )
155
- . join ( "\n" ) ;
156
- return template . replace ( / \$ s n i p p e t / , indentedSnippet ) ;
157
- }
78
+ public readonly mdType : string ,
79
+ public readonly vscodeType : string ,
80
+ public readonly rawSnippet : string ,
81
+ public readonly fullSnippet : string
82
+ ) { }
158
83
}
159
84
160
85
class SelectedContent {
@@ -228,6 +153,19 @@ class SelectedContent {
228
153
229
154
return new SelectedContent ( mdType , snippet ) ;
230
155
}
156
+
157
+ public toSnippet ( cfg : vscode . WorkspaceConfiguration , eol : string ) : Snippet {
158
+ const mdType = this . mdType ;
159
+ const maybeVscodeType = mdToVscodeFiletype ( cfg , this ) ;
160
+ const snippet = this . snippet ;
161
+ const fullSnippet = applyTemplate ( cfg , this ) . replace ( / \n / g, eol ) ;
162
+
163
+ // fallback: unregistered mdType
164
+ const vscodeType =
165
+ maybeVscodeType !== undefined ? maybeVscodeType : this . mdType ;
166
+
167
+ return new Snippet ( mdType , vscodeType , snippet , fullSnippet ) ;
168
+ }
231
169
}
232
170
233
171
function getMarkdownIndent ( text : string ) : string {
@@ -241,3 +179,44 @@ function getMarkdownIndent(text: string): string {
241
179
242
180
return indentMatch [ 1 ] ;
243
181
}
182
+
183
+ function mdToVscodeFiletype (
184
+ cfg : vscode . WorkspaceConfiguration ,
185
+ content : SelectedContent
186
+ ) : string | undefined {
187
+ const mdToVscodeTypeMap = cfg . get < any > ( "mdToVscodeTypeMap" ) ;
188
+ return mdToVscodeTypeMap [ content . mdType ] ;
189
+ }
190
+
191
+ function applyTemplate (
192
+ cfg : vscode . WorkspaceConfiguration ,
193
+ content : SelectedContent
194
+ ) : string {
195
+ const mdTypeToTemplateMap = cfg . get < any > ( "mdTypeToTemplateMap" ) ;
196
+ let template = mdTypeToTemplateMap [ content . mdType ] ;
197
+ if ( template === undefined ) {
198
+ return content . snippet ;
199
+ }
200
+
201
+ // detect the depth of indentation
202
+ // The template may contain more than one `$snippet`, but the depth of
203
+ // indentation is based on the first occurrence.
204
+ const indentMatch = / ^ ( \ * ) \$ s n i p p e t / m. exec ( template ) ;
205
+ if ( indentMatch === null ) {
206
+ // nothing to replace.
207
+ return template ;
208
+ }
209
+
210
+ // remove indent from template
211
+ template = template . replace ( / ^ \ * \$ s n i p p e t / m, "$snippet" ) ;
212
+
213
+ // insert indentation
214
+ const indent = indentMatch [ 1 ] ;
215
+ const splitted_rawSnippet = content . snippet . split ( "\n" ) ;
216
+ const indentedSnippet = splitted_rawSnippet
217
+ . map ( line => {
218
+ return indent + line ;
219
+ } )
220
+ . join ( "\n" ) ;
221
+ return template . replace ( / \$ s n i p p e t / , indentedSnippet ) ;
222
+ }
0 commit comments