@@ -42,13 +42,55 @@ function createSnippet(
42
42
) : Snippet {
43
43
return {
44
44
[ name ] : {
45
- prefix : `/ ${ prefixName } ` ,
45
+ prefix : `${ prefixName } ` ,
46
46
scope : languageId ,
47
47
body : [ selection ] ,
48
48
} ,
49
49
} ;
50
50
}
51
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 - z A - Z 0 - 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
+
52
94
async function createWorkspaceCodeSnippet ( ) {
53
95
const editor = vscode . window . activeTextEditor ;
54
96
if ( ! editor ) {
@@ -64,7 +106,12 @@ async function createWorkspaceCodeSnippet() {
64
106
return ;
65
107
}
66
108
67
- const prefixName = sanitizeName ( name ) ;
109
+ let prefixName = sanitizeName ( name ) ;
110
+ const prefixSymbol = vscode . workspace . getConfiguration ( ) . get ( "prefixSymbol" ) ;
111
+
112
+ if ( prefixSymbol ) {
113
+ prefixName = `${ prefixSymbol } ${ prefixName } ` ;
114
+ }
68
115
const snippet = createSnippet (
69
116
name ,
70
117
prefixName ,
@@ -112,7 +159,7 @@ async function createWorkspaceCodeSnippet() {
112
159
try {
113
160
await writeSnippetFile ( snippetFilePath , snippetFileContent ) ;
114
161
vscode . window . setStatusBarMessage (
115
- `Snippet "${ name } " created successfully. You can now use it by typing "/ ${ prefixName } " in a ${ editor . document . languageId } file.` ,
162
+ `Snippet "${ name } " created successfully. You can now use it by typing "${ prefixSymbol } ${ prefixName } " in a ${ editor . document . languageId } file.` ,
116
163
30000
117
164
) ;
118
165
} catch ( error ) {
@@ -129,4 +176,11 @@ export function activate(context: vscode.ExtensionContext) {
129
176
) ;
130
177
131
178
context . subscriptions . push ( disposable ) ;
179
+
180
+ vscode . workspace . onDidChangeConfiguration ( ( event ) => {
181
+ if ( event . affectsConfiguration ( "prefixSymbol" ) ) {
182
+ // Update the snippets
183
+ updatePrefixSymbol ( ) ;
184
+ }
185
+ } ) ;
132
186
}
0 commit comments