-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.ts
142 lines (124 loc) · 4.26 KB
/
extension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import * as vscode from "vscode";
import { infoMessage, errorMessage } from "./helpers/message";
import { createSnippet, getFilepath } from "./helpers/file";
import { removeCharactersPrefix, textToArray } from "./helpers/text";
import { Snippet } from "./models/Snippet";
function showFileInEditor(filepath: string) {
vscode.workspace
.openTextDocument(filepath)
.then((doc) => vscode.window.showTextDocument(doc))
.then(() => vscode.commands.executeCommand("cursorBottom"));
}
export function activate(context: vscode.ExtensionContext) {
let disposable = vscode.commands.registerCommand(
"snippets-creator.createSnippet",
async () => {
const activeTextEditor = vscode.window.activeTextEditor;
if (!activeTextEditor) {
return;
}
const selection = activeTextEditor.selection;
const textSelected = activeTextEditor.document.getText(
new vscode.Range(selection.start, selection.end)
);
if (selection.isEmpty) {
errorMessage("Select code blocks");
return;
}
const languages: string[] = await vscode.languages.getLanguages();
let snippet: Snippet = {
prefix: "",
name: "",
scope: [],
languageId: activeTextEditor.document.languageId,
body: textToArray(textSelected),
description: "",
isGlobal: false,
};
const snippetTypes: string[] = [
`Snippet for Language: ${snippet.languageId}`,
"Snippet Global",
];
const snippetType = await vscode.window.showQuickPick(snippetTypes, {
placeHolder: "Select the snippet type",
});
if (snippetType === undefined || snippetType === "") {
errorMessage("Unselected snippet type");
return;
}
snippet.isGlobal = snippetType === "Snippet Global";
if (snippet.isGlobal) {
const languagesOrdened = languages.sort(function (a, b) {
return a.toLowerCase().localeCompare(b.toLowerCase());
});
let quickPickItems: vscode.QuickPickItem[] = languagesOrdened.map(
(item) => {
return {
label: item,
picked: item === snippet.languageId,
};
}
);
const languagesSelected = await vscode.window.showQuickPick(
quickPickItems,
{ placeHolder: "Select languages", canPickMany: true }
);
if (
languagesSelected === undefined ||
languagesSelected?.length === 0
) {
errorMessage("Unselected languages");
return;
}
snippet.scope = languagesSelected?.map((item) => item.label) ?? [];
}
const prefix = await vscode.window.showInputBox({
placeHolder: "Prefix",
prompt: "Enter the snippet prefix [0-9a-z-]:",
});
if (prefix === undefined || prefix === "") {
errorMessage("Prefix not provided");
return;
}
snippet.prefix = removeCharactersPrefix(prefix);
let name = await vscode.window.showInputBox({
placeHolder: "Name",
prompt: "Enter the snippet name (Optional):",
});
if (name === undefined || name === "") {
name = snippet.prefix.replace("-", " ");
}
snippet.name = name;
snippet.description = await vscode.window.showInputBox({
placeHolder: "Description",
prompt: "Enter the snippet description (Optional):",
});
const languagesScapeDollarSign: string[] = [
"php",
"javascript",
"typescript",
];
if (languagesScapeDollarSign.includes(snippet.languageId)) {
let newBody: string[] = [];
snippet.body.forEach((bodyItem: string) => {
const newBodyItem: string = bodyItem.replace(/\$/gim, "\\$");
newBody.push(newBodyItem);
});
snippet.body = newBody;
}
const filepath: string = getFilepath(
snippet.languageId,
snippet.isGlobal
);
if (filepath === undefined || filepath === "") {
errorMessage("Unknown OS");
return;
}
return createSnippet(filepath, snippet)
.then(() => showFileInEditor(filepath))
.then(() => infoMessage("Snippet Saved"));
}
);
context.subscriptions.push(disposable);
}
export function deactivate() {}