@@ -2,6 +2,53 @@ import * as vscode from "vscode";
2
2
import * as path from "path" ;
3
3
import * as fs from "fs/promises" ;
4
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 - 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
+
5
52
export function activate ( context : vscode . ExtensionContext ) {
6
53
const disposable = vscode . commands . registerCommand (
7
54
"extension.createWorkspaceCodeSnippet" ,
@@ -13,26 +60,21 @@ export function activate(context: vscode.ExtensionContext) {
13
60
}
14
61
15
62
const selection = editor . document . getText ( editor . selection ) ;
16
- let name = await vscode . window . showInputBox ( {
63
+ const name = await vscode . window . showInputBox ( {
17
64
prompt : "Enter the name of the snippet" ,
18
65
} ) ;
19
66
if ( ! name ) {
20
67
return ;
21
68
}
22
69
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
+ ) ;
34
77
35
- // Write the snippet to the workspace file
36
78
const workspaceFolder = vscode . workspace . workspaceFolders ?. [ 0 ] ;
37
79
if ( ! workspaceFolder ) {
38
80
vscode . window . showErrorMessage ( "No workspace folder found." ) ;
@@ -45,23 +87,22 @@ export function activate(context: vscode.ExtensionContext) {
45
87
"workspace.code-snippets"
46
88
) ;
47
89
48
- let snippetFileContent = { } ;
90
+ let snippetFileContent : Snippet = { } ;
49
91
try {
50
- const snippetFileContentBuffer = await fs . readFile ( snippetFilePath ) ;
51
- snippetFileContent = JSON . parse ( snippetFileContentBuffer . toString ( ) ) ;
92
+ snippetFileContent = await readSnippetFile ( snippetFilePath ) ;
52
93
} 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 ;
54
98
}
55
99
56
100
Object . assign ( snippetFileContent , snippet ) ;
57
101
58
102
try {
59
- await fs . writeFile (
60
- snippetFilePath ,
61
- JSON . stringify ( snippetFileContent , null , 2 )
62
- ) ;
103
+ await writeSnippetFile ( snippetFilePath , snippetFileContent ) ;
63
104
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.`
65
106
) ;
66
107
} catch ( error ) {
67
108
vscode . window . showErrorMessage (
0 commit comments