Skip to content

Commit ad656a0

Browse files
committed
Refactor updatePrefixSymbol function to use
async/await
1 parent 3d077df commit ad656a0

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

src/updatePrefixSymbol.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Snippet, readSnippetFile, writeSnippetFile } from "./snippet";
77
* If the `prefixSymbol` configuration setting is set, replaces all non-alphanumeric characters in the prefix with it.
88
* @returns void
99
*/
10-
function updatePrefixSymbol() {
10+
async function updatePrefixSymbol() {
1111
const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
1212
if (!workspaceFolder) {
1313
vscode.window.showErrorMessage("No workspace folder found.");
@@ -19,31 +19,30 @@ function updatePrefixSymbol() {
1919
"workspace.code-snippets"
2020
);
2121

22-
readSnippetFile(snippetFilePath).then((snippetFileContent) => {
23-
const specialCharRegex = /[^a-zA-Z0-9]/g;
24-
const hasSpecialChar = Object.keys(snippetFileContent).some((key) =>
25-
specialCharRegex.test(snippetFileContent[key].prefix)
26-
);
22+
const snippetFileContent = await readSnippetFile(snippetFilePath);
23+
const specialCharRegex = /[^a-zA-Z0-9]/g;
24+
const hasSpecialChar = Object.keys(snippetFileContent).some((key) =>
25+
specialCharRegex.test(snippetFileContent[key].prefix)
26+
);
2727

28-
const newSnippetFileContent: Snippet = {};
29-
Object.keys(snippetFileContent).forEach((key) => {
30-
const snippet = snippetFileContent[key];
31-
let newPrefix = snippet.prefix;
32-
if (hasSpecialChar) {
33-
const prefixSymbol = vscode.workspace
34-
.getConfiguration()
35-
.get("prefixSymbol") as string;
36-
newPrefix = newPrefix.replace(specialCharRegex, prefixSymbol);
37-
}
38-
const newSnippet = {
39-
...snippet,
40-
prefix: newPrefix,
41-
};
42-
newSnippetFileContent[key] = newSnippet;
43-
});
28+
const newSnippetFileContent: Snippet = {};
29+
for (const key of Object.keys(snippetFileContent)) {
30+
const snippet = snippetFileContent[key];
31+
let newPrefix = snippet.prefix;
32+
if (hasSpecialChar) {
33+
const prefixSymbol = vscode.workspace
34+
.getConfiguration()
35+
.get("prefixSymbol") as string;
36+
newPrefix = newPrefix.replace(specialCharRegex, prefixSymbol);
37+
}
38+
const newSnippet = {
39+
...snippet,
40+
prefix: newPrefix,
41+
};
42+
newSnippetFileContent[key] = newSnippet;
43+
}
4444

45-
writeSnippetFile(snippetFilePath, newSnippetFileContent);
46-
});
45+
await writeSnippetFile(snippetFilePath, newSnippetFileContent);
4746
}
4847

4948
export { updatePrefixSymbol };

0 commit comments

Comments
 (0)