|
1 | 1 | import * as vscode from "vscode";
|
2 |
| -import * as fs from "fs"; |
3 | 2 | import * as path from "path";
|
| 3 | +import * as fs from "fs/promises"; |
4 | 4 |
|
5 | 5 | export function activate(context: vscode.ExtensionContext) {
|
6 |
| - let disposable = vscode.commands.registerCommand( |
| 6 | + const disposable = vscode.commands.registerCommand( |
7 | 7 | "extension.createWorkspaceSnippet",
|
8 | 8 | async () => {
|
9 | 9 | const editor = vscode.window.activeTextEditor;
|
10 | 10 | if (!editor) {
|
11 |
| - vscode.window.showErrorMessage("No active editor found"); |
| 11 | + vscode.window.showErrorMessage("No active text editor found."); |
12 | 12 | return;
|
13 | 13 | }
|
14 | 14 |
|
15 |
| - const selection = editor.selection; |
16 |
| - const selectedText = editor.document.getText(selection); |
17 |
| - |
18 |
| - if (!selectedText) { |
19 |
| - vscode.window.showErrorMessage("No text selected"); |
| 15 | + const selection = editor.document.getText(editor.selection); |
| 16 | + const name = await vscode.window.showInputBox({ |
| 17 | + prompt: "Enter the name of the snippet", |
| 18 | + }); |
| 19 | + if (!name) { |
20 | 20 | return;
|
21 | 21 | }
|
22 | 22 |
|
23 |
| - const snippetPrefix = await vscode.window.showInputBox({ |
24 |
| - prompt: "Enter the snippet prefix", |
25 |
| - value: "mySnippet", |
| 23 | + const prefix = await vscode.window.showInputBox({ |
| 24 | + prompt: "Enter the prefix of the snippet", |
26 | 25 | });
|
27 |
| - |
28 |
| - if (!snippetPrefix) { |
29 |
| - vscode.window.showErrorMessage("No snippet prefix entered"); |
| 26 | + if (!prefix) { |
30 | 27 | return;
|
31 | 28 | }
|
32 | 29 |
|
33 |
| - const snippetContent = selectedText; |
| 30 | + const snippet = { |
| 31 | + [name]: { |
| 32 | + prefix, |
| 33 | + body: [selection], |
| 34 | + description: `Snippet for ${name}`, |
| 35 | + }, |
| 36 | + }; |
34 | 37 |
|
35 |
| - const snippetsFilePath = path.join( |
36 |
| - vscode.workspace.rootPath || "", |
| 38 | + const workspaceFolder = vscode.workspace.workspaceFolders?.[0]; |
| 39 | + if (!workspaceFolder) { |
| 40 | + vscode.window.showErrorMessage("No workspace folder found."); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + const snippetFilePath = path.join( |
| 45 | + workspaceFolder.uri.fsPath, |
37 | 46 | ".vscode",
|
38 | 47 | "workspace.code-snippets"
|
39 | 48 | );
|
40 | 49 |
|
| 50 | + let snippetFileContent = {}; |
41 | 51 | try {
|
42 |
| - // Create the .vscode directory if it doesn't exist |
43 |
| - fs.mkdirSync(path.dirname(snippetsFilePath), { recursive: true }); |
44 |
| - |
45 |
| - // Create the workspace.code-snippets file if it doesn't exist |
46 |
| - if (!fs.existsSync(snippetsFilePath)) { |
47 |
| - fs.writeFileSync(snippetsFilePath, "{}"); |
48 |
| - } |
49 |
| - |
50 |
| - // Read the contents of the workspace.code-snippets file |
51 |
| - const snippetsFileContent = fs.readFileSync(snippetsFilePath, "utf8"); |
52 |
| - |
53 |
| - // Parse the contents of the file as JSON |
54 |
| - const snippets = JSON.parse(snippetsFileContent); |
55 |
| - |
56 |
| - // Add the new snippet to the JSON object |
57 |
| - snippets[snippetPrefix] = snippetContent; |
| 52 | + const snippetFileContentBuffer = await fs.readFile(snippetFilePath); |
| 53 | + snippetFileContent = JSON.parse(snippetFileContentBuffer.toString()); |
| 54 | + } catch (error) { |
| 55 | + // Ignore errors when reading the snippet file |
| 56 | + } |
58 | 57 |
|
59 |
| - // Write the updated JSON object to the workspace.code-snippets file |
60 |
| - fs.writeFileSync(snippetsFilePath, JSON.stringify(snippets, null, 2)); |
| 58 | + Object.assign(snippetFileContent, snippet); |
61 | 59 |
|
| 60 | + try { |
| 61 | + await fs.writeFile( |
| 62 | + snippetFilePath, |
| 63 | + JSON.stringify(snippetFileContent, null, 2) |
| 64 | + ); |
62 | 65 | vscode.window.showInformationMessage(
|
63 |
| - `Snippet '${snippetPrefix}' created` |
| 66 | + `Snippet "${name}" created successfully.` |
64 | 67 | );
|
65 | 68 | } catch (error) {
|
66 | 69 | vscode.window.showErrorMessage(
|
67 |
| - `Error creating snippet: ${error.message}` |
| 70 | + `Error creating snippet "${name}": ${(error as Error).message}` |
68 | 71 | );
|
69 | 72 | }
|
70 | 73 | }
|
|
0 commit comments