Skip to content

Commit 134d25d

Browse files
committed
change prefix symbol with vscode settings
1 parent 363d1c9 commit 134d25d

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

package.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,17 @@
3737
"command": "extension.createWorkspaceCodeSnippet",
3838
"key": "cmd+k cmd+z"
3939
}
40-
]
40+
],
41+
"configuration": {
42+
"type": "object",
43+
"title": "Workspace Code Snippets",
44+
"properties": {
45+
"prefixSymbol": {
46+
"type": "string",
47+
"default": "/"
48+
}
49+
}
50+
}
4151
},
4252
"scripts": {
4353
"vscode:prepublish": "npm run package",

src/extension.ts

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,55 @@ function createSnippet(
4242
): Snippet {
4343
return {
4444
[name]: {
45-
prefix: `/${prefixName}`,
45+
prefix: `${prefixName}`,
4646
scope: languageId,
4747
body: [selection],
4848
},
4949
};
5050
}
5151

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+
5294
async function createWorkspaceCodeSnippet() {
5395
const editor = vscode.window.activeTextEditor;
5496
if (!editor) {
@@ -64,7 +106,12 @@ async function createWorkspaceCodeSnippet() {
64106
return;
65107
}
66108

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+
}
68115
const snippet = createSnippet(
69116
name,
70117
prefixName,
@@ -112,7 +159,7 @@ async function createWorkspaceCodeSnippet() {
112159
try {
113160
await writeSnippetFile(snippetFilePath, snippetFileContent);
114161
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.`,
116163
30000
117164
);
118165
} catch (error) {
@@ -129,4 +176,11 @@ export function activate(context: vscode.ExtensionContext) {
129176
);
130177

131178
context.subscriptions.push(disposable);
179+
180+
vscode.workspace.onDidChangeConfiguration((event) => {
181+
if (event.affectsConfiguration("prefixSymbol")) {
182+
// Update the snippets
183+
updatePrefixSymbol();
184+
}
185+
});
132186
}

0 commit comments

Comments
 (0)