Skip to content

Commit ee7e5ec

Browse files
committed
Refactor code to separate files
1 parent e03818e commit ee7e5ec

File tree

5 files changed

+202
-175
lines changed

5 files changed

+202
-175
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,22 @@
1919
"contributes": {
2020
"commands": [
2121
{
22-
"command": "extension.createWorkspaceCodeSnippet",
23-
"title": "Create Workspace Code Snippet",
22+
"command": "extension.workspaceCodeSnippet",
23+
"title": "New Workspace Code Snippet",
2424
"category": "Snippets"
2525
}
2626
],
2727
"menus": {
2828
"editor/context": [
2929
{
30-
"command": "extension.createWorkspaceCodeSnippet",
30+
"command": "extension.workspaceCodeSnippet",
3131
"group": "navigation"
3232
}
3333
]
3434
},
3535
"keybindings": [
3636
{
37-
"command": "extension.createWorkspaceCodeSnippet",
37+
"command": "extension.workspaceCodeSnippet",
3838
"key": "cmd+k cmd+z"
3939
}
4040
],

src/createCodeSnippet.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import * as vscode from "vscode";
2+
import {
3+
Snippet,
4+
createSnippet,
5+
readSnippetFile,
6+
sanitizeName,
7+
writeSnippetFile,
8+
} from "./snippet";
9+
import * as path from "path";
10+
11+
async function createCodeSnippet() {
12+
const editor = vscode.window.activeTextEditor;
13+
if (!editor) {
14+
vscode.window.showErrorMessage("No active text editor found.");
15+
return;
16+
}
17+
18+
const selection = editor.document.getText(editor.selection);
19+
const name = await vscode.window.showInputBox({
20+
prompt: "Enter the name of the snippet",
21+
});
22+
if (!name) {
23+
return;
24+
}
25+
26+
let prefixName = sanitizeName(name);
27+
const prefixSymbol = vscode.workspace.getConfiguration().get("prefixSymbol");
28+
29+
if (prefixSymbol) {
30+
prefixName = `${prefixSymbol}${prefixName}`;
31+
}
32+
const snippet = createSnippet(
33+
name,
34+
prefixName,
35+
editor.document.languageId,
36+
selection
37+
);
38+
39+
const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
40+
if (!workspaceFolder) {
41+
vscode.window.showErrorMessage("No workspace folder found.");
42+
return;
43+
}
44+
45+
const snippetFilePath = path.join(
46+
workspaceFolder.uri.fsPath,
47+
".vscode",
48+
"workspace.code-snippets"
49+
);
50+
51+
let snippetFileContent: Snippet = {};
52+
try {
53+
snippetFileContent = await readSnippetFile(snippetFilePath);
54+
} catch (error) {
55+
vscode.window.showErrorMessage(
56+
`Error reading snippet file: ${(error as Error).message}`
57+
);
58+
return;
59+
}
60+
61+
if (snippetFileContent[name]) {
62+
const update = await vscode.window.showQuickPick(
63+
["Change name", "Update snippet"],
64+
{
65+
placeHolder: `Snippet "${name}" already exists.`,
66+
}
67+
);
68+
if (update === "Change name") {
69+
createCodeSnippet();
70+
return;
71+
}
72+
}
73+
74+
Object.assign(snippetFileContent, snippet);
75+
76+
try {
77+
await writeSnippetFile(snippetFilePath, snippetFileContent);
78+
vscode.window.setStatusBarMessage(
79+
`Snippet "${name}" created successfully. You can now use it by typing "${prefixSymbol}${prefixName}" in a ${editor.document.languageId} file.`,
80+
30000
81+
);
82+
} catch (error) {
83+
vscode.window.showErrorMessage(
84+
`Error creating snippet "${name}": ${(error as Error).message}`
85+
);
86+
}
87+
}
88+
89+
export { createCodeSnippet };

src/extension.ts

Lines changed: 4 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -1,178 +1,11 @@
11
import * as vscode from "vscode";
2-
import * as path from "path";
3-
import * as fs from "fs/promises";
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-zA-Z0-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-
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-
94-
async function createWorkspaceCodeSnippet() {
95-
const editor = vscode.window.activeTextEditor;
96-
if (!editor) {
97-
vscode.window.showErrorMessage("No active text editor found.");
98-
return;
99-
}
100-
101-
const selection = editor.document.getText(editor.selection);
102-
const name = await vscode.window.showInputBox({
103-
prompt: "Enter the name of the snippet",
104-
});
105-
if (!name) {
106-
return;
107-
}
108-
109-
let prefixName = sanitizeName(name);
110-
const prefixSymbol = vscode.workspace.getConfiguration().get("prefixSymbol");
111-
112-
if (prefixSymbol) {
113-
prefixName = `${prefixSymbol}${prefixName}`;
114-
}
115-
const snippet = createSnippet(
116-
name,
117-
prefixName,
118-
editor.document.languageId,
119-
selection
120-
);
121-
122-
const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
123-
if (!workspaceFolder) {
124-
vscode.window.showErrorMessage("No workspace folder found.");
125-
return;
126-
}
127-
128-
const snippetFilePath = path.join(
129-
workspaceFolder.uri.fsPath,
130-
".vscode",
131-
"workspace.code-snippets"
132-
);
133-
134-
let snippetFileContent: Snippet = {};
135-
try {
136-
snippetFileContent = await readSnippetFile(snippetFilePath);
137-
} catch (error) {
138-
vscode.window.showErrorMessage(
139-
`Error reading snippet file: ${(error as Error).message}`
140-
);
141-
return;
142-
}
143-
144-
if (snippetFileContent[name]) {
145-
const update = await vscode.window.showQuickPick(
146-
["Change name", "Update snippet"],
147-
{
148-
placeHolder: `Snippet "${name}" already exists.`,
149-
}
150-
);
151-
if (update === "Change name") {
152-
createWorkspaceCodeSnippet();
153-
return;
154-
}
155-
}
156-
157-
Object.assign(snippetFileContent, snippet);
158-
159-
try {
160-
await writeSnippetFile(snippetFilePath, snippetFileContent);
161-
vscode.window.setStatusBarMessage(
162-
`Snippet "${name}" created successfully. You can now use it by typing "${prefixSymbol}${prefixName}" in a ${editor.document.languageId} file.`,
163-
30000
164-
);
165-
} catch (error) {
166-
vscode.window.showErrorMessage(
167-
`Error creating snippet "${name}": ${(error as Error).message}`
168-
);
169-
}
170-
}
2+
import { createCodeSnippet } from "./createCodeSnippet";
3+
import { updatePrefixSymbol } from "./updatePrefixSymbol";
1714

1725
export function activate(context: vscode.ExtensionContext) {
1736
const disposable = vscode.commands.registerCommand(
174-
"extension.createWorkspaceCodeSnippet",
175-
createWorkspaceCodeSnippet
7+
"extension.workspaceCodeSnippet",
8+
createCodeSnippet
1769
);
17710

17811
context.subscriptions.push(disposable);

src/snippet.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import * as fs from "fs/promises";
2+
3+
interface Snippet {
4+
[name: string]: {
5+
prefix: string;
6+
scope: string;
7+
body: string[];
8+
};
9+
}
10+
11+
async function readSnippetFile(snippetFilePath: string): Promise<Snippet> {
12+
try {
13+
const snippetFileContentBuffer = await fs.readFile(snippetFilePath);
14+
return JSON.parse(snippetFileContentBuffer.toString());
15+
} catch (error) {
16+
return {};
17+
}
18+
}
19+
20+
async function writeSnippetFile(
21+
snippetFilePath: string,
22+
snippet: Snippet
23+
): Promise<void> {
24+
try {
25+
await fs.writeFile(snippetFilePath, JSON.stringify(snippet, null, 2));
26+
} catch (error) {
27+
throw new Error(`Error writing snippet file: ${(error as Error).message}`);
28+
}
29+
}
30+
31+
function sanitizeName(name: string): string {
32+
return name.replace(/[^a-zA-Z0-9]/g, "-");
33+
}
34+
35+
function createSnippet(
36+
name: string,
37+
prefixName: string,
38+
languageId: string,
39+
selection: string
40+
): Snippet {
41+
return {
42+
[name]: {
43+
prefix: `${prefixName}`,
44+
scope: languageId,
45+
body: [selection],
46+
},
47+
};
48+
}
49+
50+
export {
51+
Snippet,
52+
createSnippet,
53+
readSnippetFile,
54+
writeSnippetFile,
55+
sanitizeName,
56+
};

0 commit comments

Comments
 (0)