Skip to content

Commit 34accf2

Browse files
committed
refactor
1 parent 7ae4123 commit 34accf2

File tree

1 file changed

+65
-86
lines changed

1 file changed

+65
-86
lines changed

src/run_snippet.ts

Lines changed: 65 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"use strict";
22

33
import * as vscode from "vscode";
4-
import { isString } from "util";
54
import { AssertionError } from "assert";
65

76
export function runMarkdownSnippet() {
@@ -19,19 +18,18 @@ export function runMarkdownSnippet() {
1918
return;
2019
}
2120

22-
// handle selection
2321
// get snippet
2422
const selectedText = editor.document.getText(selection);
2523
const eol = getEol(editor.document.eol);
2624

2725
const content = SelectedContent.parseSelectedText(eol, selectedText);
28-
if (isString(content)) {
26+
if (typeof content === "string") {
2927
// if string is returned, this represents an error message.
3028
vscode.window.showErrorMessage(content);
3129
return;
3230
}
3331

34-
// get current active file's uri
32+
// Get current active file's uri
3533
// unsupported: if not a file, show error message and exit
3634
const uri = editor.document.uri;
3735
if (uri === undefined || uri.scheme !== "file") {
@@ -41,12 +39,11 @@ export function runMarkdownSnippet() {
4139
return;
4240
}
4341

44-
// get configuration
42+
// Get configuration
4543
const config = vscode.workspace.getConfiguration("markdown-run-snippet");
46-
const finalized = FinalizedContent.finalize(config, eol, content);
44+
const finalized = content.toSnippet(config, eol);
4745

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.
5047
vscode.workspace
5148
.openTextDocument({
5249
language: finalized.vscodeType,
@@ -76,85 +73,13 @@ function getEol(eeol: vscode.EndOfLine): string {
7673
return eol;
7774
}
7875

79-
class FinalizedContent {
80-
public mdType: string;
81-
public vscodeType: string;
82-
public rawSnippet: string;
83-
public fullSnippet: string;
84-
76+
class Snippet {
8577
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 = /^(\ *)\$snippet/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(/^\ *\$snippet/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(/\$snippet/, indentedSnippet);
157-
}
78+
public readonly mdType: string,
79+
public readonly vscodeType: string,
80+
public readonly rawSnippet: string,
81+
public readonly fullSnippet: string
82+
) {}
15883
}
15984

16085
class SelectedContent {
@@ -228,6 +153,19 @@ class SelectedContent {
228153

229154
return new SelectedContent(mdType, snippet);
230155
}
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+
}
231169
}
232170

233171
function getMarkdownIndent(text: string): string {
@@ -241,3 +179,44 @@ function getMarkdownIndent(text: string): string {
241179

242180
return indentMatch[1];
243181
}
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 = /^(\ *)\$snippet/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(/^\ *\$snippet/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(/\$snippet/, indentedSnippet);
222+
}

0 commit comments

Comments
 (0)