@@ -2,6 +2,53 @@ import * as vscode from "vscode";
22import * as path from "path" ;
33import * as fs from "fs/promises" ;
44
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 - z A - Z 0 - 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+
552export function activate ( context : vscode . ExtensionContext ) {
653 const disposable = vscode . commands . registerCommand (
754 "extension.createWorkspaceCodeSnippet" ,
@@ -13,26 +60,21 @@ export function activate(context: vscode.ExtensionContext) {
1360 }
1461
1562 const selection = editor . document . getText ( editor . selection ) ;
16- let name = await vscode . window . showInputBox ( {
63+ const name = await vscode . window . showInputBox ( {
1764 prompt : "Enter the name of the snippet" ,
1865 } ) ;
1966 if ( ! name ) {
2067 return ;
2168 }
2269
23- // Sanitize the name for the prefix
24- const prefixName = name . replace ( / [ ^ a - z A - Z 0 - 9 ] / g, "-" ) ;
25-
26- // Create the snippet
27- const snippet = {
28- [ name ] : {
29- prefix : `/${ prefixName } ` ,
30- scope : editor . document . languageId ,
31- body : [ selection ] ,
32- } ,
33- } ;
70+ const prefixName = sanitizeName ( name ) ;
71+ const snippet = createSnippet (
72+ name ,
73+ prefixName ,
74+ editor . document . languageId ,
75+ selection
76+ ) ;
3477
35- // Write the snippet to the workspace file
3678 const workspaceFolder = vscode . workspace . workspaceFolders ?. [ 0 ] ;
3779 if ( ! workspaceFolder ) {
3880 vscode . window . showErrorMessage ( "No workspace folder found." ) ;
@@ -45,23 +87,22 @@ export function activate(context: vscode.ExtensionContext) {
4587 "workspace.code-snippets"
4688 ) ;
4789
48- let snippetFileContent = { } ;
90+ let snippetFileContent : Snippet = { } ;
4991 try {
50- const snippetFileContentBuffer = await fs . readFile ( snippetFilePath ) ;
51- snippetFileContent = JSON . parse ( snippetFileContentBuffer . toString ( ) ) ;
92+ snippetFileContent = await readSnippetFile ( snippetFilePath ) ;
5293 } catch ( error ) {
53- // Ignore errors when reading the snippet file
94+ vscode . window . showErrorMessage (
95+ `Error reading snippet file: ${ ( error as Error ) . message } `
96+ ) ;
97+ return ;
5498 }
5599
56100 Object . assign ( snippetFileContent , snippet ) ;
57101
58102 try {
59- await fs . writeFile (
60- snippetFilePath ,
61- JSON . stringify ( snippetFileContent , null , 2 )
62- ) ;
103+ await writeSnippetFile ( snippetFilePath , snippetFileContent ) ;
63104 vscode . window . showInformationMessage (
64- `Snippet "${ name } " created successfully. You can now use it by typing "/${ prefixName } " in a file.`
105+ `Snippet "${ name } " created successfully. You can now use it by typing "/${ prefixName } " in a ${ editor . document . languageId } file.`
65106 ) ;
66107 } catch ( error ) {
67108 vscode . window . showErrorMessage (
0 commit comments