|
1 | 1 | import * as vscode from "vscode";
|
2 |
| -import * as path from "path"; |
3 |
| -import * as fs from "fs/promises"; |
4 |
| - |
5 |
| -interface Snippet { |
6 |
| - [name: string]: { |
7 |
| - prefix: string; |
8 |
| - scope: string; |
9 |
| - body: string[]; |
10 |
| - }; |
11 |
| -} |
12 |
| - |
13 |
| -async function readSnippetFile(snippetFilePath: string): Promise<Snippet> { |
14 |
| - try { |
15 |
| - const snippetFileContentBuffer = await fs.readFile(snippetFilePath); |
16 |
| - return JSON.parse(snippetFileContentBuffer.toString()); |
17 |
| - } catch (error) { |
18 |
| - return {}; |
19 |
| - } |
20 |
| -} |
21 |
| - |
22 |
| -async function writeSnippetFile( |
23 |
| - snippetFilePath: string, |
24 |
| - snippet: Snippet |
25 |
| -): Promise<void> { |
26 |
| - try { |
27 |
| - await fs.writeFile(snippetFilePath, JSON.stringify(snippet, null, 2)); |
28 |
| - } catch (error) { |
29 |
| - throw new Error(`Error writing snippet file: ${(error as Error).message}`); |
30 |
| - } |
31 |
| -} |
32 |
| - |
33 |
| -function sanitizeName(name: string): string { |
34 |
| - return name.replace(/[^a-zA-Z0-9]/g, "-"); |
35 |
| -} |
36 |
| - |
37 |
| -function createSnippet( |
38 |
| - name: string, |
39 |
| - prefixName: string, |
40 |
| - languageId: string, |
41 |
| - selection: string |
42 |
| -): Snippet { |
43 |
| - return { |
44 |
| - [name]: { |
45 |
| - prefix: `${prefixName}`, |
46 |
| - scope: languageId, |
47 |
| - body: [selection], |
48 |
| - }, |
49 |
| - }; |
50 |
| -} |
51 |
| - |
52 |
| -function updatePrefixSymbol() { |
53 |
| - // get snippet file path |
54 |
| - const workspaceFolder = vscode.workspace.workspaceFolders?.[0]; |
55 |
| - if (!workspaceFolder) { |
56 |
| - vscode.window.showErrorMessage("No workspace folder found."); |
57 |
| - return; |
58 |
| - } |
59 |
| - const snippetFilePath = path.join( |
60 |
| - workspaceFolder.uri.fsPath, |
61 |
| - ".vscode", |
62 |
| - "workspace.code-snippets" |
63 |
| - ); |
64 |
| - |
65 |
| - // read snippet file |
66 |
| - readSnippetFile(snippetFilePath).then((snippetFileContent) => { |
67 |
| - // check if snippets contains a special character |
68 |
| - const specialCharRegex = /[^a-zA-Z0-9]/g; |
69 |
| - const hasSpecialChar = Object.keys(snippetFileContent).some((key) => |
70 |
| - specialCharRegex.test(snippetFileContent[key].prefix) |
71 |
| - ); |
72 |
| - // update snippets |
73 |
| - const newSnippetFileContent: Snippet = {}; |
74 |
| - Object.keys(snippetFileContent).forEach((key) => { |
75 |
| - const snippet = snippetFileContent[key]; |
76 |
| - let newPrefix = snippet.prefix; |
77 |
| - if (hasSpecialChar) { |
78 |
| - const prefixSymbol = vscode.workspace |
79 |
| - .getConfiguration() |
80 |
| - .get("prefixSymbol") as string; |
81 |
| - newPrefix = newPrefix.replace(specialCharRegex, prefixSymbol); |
82 |
| - } |
83 |
| - const newSnippet = { |
84 |
| - ...snippet, |
85 |
| - prefix: newPrefix, |
86 |
| - }; |
87 |
| - newSnippetFileContent[key] = newSnippet; |
88 |
| - }); |
89 |
| - // write snippets to file |
90 |
| - writeSnippetFile(snippetFilePath, newSnippetFileContent); |
91 |
| - }); |
92 |
| -} |
93 |
| - |
94 |
| -async function createWorkspaceCodeSnippet() { |
95 |
| - const editor = vscode.window.activeTextEditor; |
96 |
| - if (!editor) { |
97 |
| - vscode.window.showErrorMessage("No active text editor found."); |
98 |
| - return; |
99 |
| - } |
100 |
| - |
101 |
| - const selection = editor.document.getText(editor.selection); |
102 |
| - const name = await vscode.window.showInputBox({ |
103 |
| - prompt: "Enter the name of the snippet", |
104 |
| - }); |
105 |
| - if (!name) { |
106 |
| - return; |
107 |
| - } |
108 |
| - |
109 |
| - let prefixName = sanitizeName(name); |
110 |
| - const prefixSymbol = vscode.workspace.getConfiguration().get("prefixSymbol"); |
111 |
| - |
112 |
| - if (prefixSymbol) { |
113 |
| - prefixName = `${prefixSymbol}${prefixName}`; |
114 |
| - } |
115 |
| - const snippet = createSnippet( |
116 |
| - name, |
117 |
| - prefixName, |
118 |
| - editor.document.languageId, |
119 |
| - selection |
120 |
| - ); |
121 |
| - |
122 |
| - const workspaceFolder = vscode.workspace.workspaceFolders?.[0]; |
123 |
| - if (!workspaceFolder) { |
124 |
| - vscode.window.showErrorMessage("No workspace folder found."); |
125 |
| - return; |
126 |
| - } |
127 |
| - |
128 |
| - const snippetFilePath = path.join( |
129 |
| - workspaceFolder.uri.fsPath, |
130 |
| - ".vscode", |
131 |
| - "workspace.code-snippets" |
132 |
| - ); |
133 |
| - |
134 |
| - let snippetFileContent: Snippet = {}; |
135 |
| - try { |
136 |
| - snippetFileContent = await readSnippetFile(snippetFilePath); |
137 |
| - } catch (error) { |
138 |
| - vscode.window.showErrorMessage( |
139 |
| - `Error reading snippet file: ${(error as Error).message}` |
140 |
| - ); |
141 |
| - return; |
142 |
| - } |
143 |
| - |
144 |
| - if (snippetFileContent[name]) { |
145 |
| - const update = await vscode.window.showQuickPick( |
146 |
| - ["Change name", "Update snippet"], |
147 |
| - { |
148 |
| - placeHolder: `Snippet "${name}" already exists.`, |
149 |
| - } |
150 |
| - ); |
151 |
| - if (update === "Change name") { |
152 |
| - createWorkspaceCodeSnippet(); |
153 |
| - return; |
154 |
| - } |
155 |
| - } |
156 |
| - |
157 |
| - Object.assign(snippetFileContent, snippet); |
158 |
| - |
159 |
| - try { |
160 |
| - await writeSnippetFile(snippetFilePath, snippetFileContent); |
161 |
| - vscode.window.setStatusBarMessage( |
162 |
| - `Snippet "${name}" created successfully. You can now use it by typing "${prefixSymbol}${prefixName}" in a ${editor.document.languageId} file.`, |
163 |
| - 30000 |
164 |
| - ); |
165 |
| - } catch (error) { |
166 |
| - vscode.window.showErrorMessage( |
167 |
| - `Error creating snippet "${name}": ${(error as Error).message}` |
168 |
| - ); |
169 |
| - } |
170 |
| -} |
| 2 | +import { createCodeSnippet } from "./createCodeSnippet"; |
| 3 | +import { updatePrefixSymbol } from "./updatePrefixSymbol"; |
171 | 4 |
|
172 | 5 | export function activate(context: vscode.ExtensionContext) {
|
173 | 6 | const disposable = vscode.commands.registerCommand(
|
174 |
| - "extension.createWorkspaceCodeSnippet", |
175 |
| - createWorkspaceCodeSnippet |
| 7 | + "extension.workspaceCodeSnippet", |
| 8 | + createCodeSnippet |
176 | 9 | );
|
177 | 10 |
|
178 | 11 | context.subscriptions.push(disposable);
|
|
0 commit comments