Skip to content

Commit 3d077df

Browse files
committed
/doc
1 parent c67f3d7 commit 3d077df

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

src/createCodeSnippet.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import {
88
} from "./snippet";
99
import * as path from "path";
1010

11+
/**
12+
* Creates a code snippet from the selected text in the active text editor.
13+
* Prompts the user to enter a name for the snippet and saves it to the workspace's `.vscode/workspace.code-snippets` file.
14+
* If a snippet with the same name already exists, prompts the user to either change the name or update the existing snippet.
15+
*/
1116
async function createCodeSnippet() {
1217
const editor = vscode.window.activeTextEditor;
1318
if (!editor) {

src/extension.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import * as vscode from "vscode";
22
import { createCodeSnippet } from "./createCodeSnippet";
33
import { updatePrefixSymbol } from "./updatePrefixSymbol";
44

5+
/**
6+
* Activates the extension and registers the `extension.workspaceCodeSnippet` command.
7+
* @param context The extension context.
8+
*/
59
export function activate(context: vscode.ExtensionContext) {
610
const disposable = vscode.commands.registerCommand(
711
"extension.workspaceCodeSnippet",
@@ -10,9 +14,9 @@ export function activate(context: vscode.ExtensionContext) {
1014

1115
context.subscriptions.push(disposable);
1216

17+
// Update prefix symbol when configuration changes
1318
vscode.workspace.onDidChangeConfiguration((event) => {
1419
if (event.affectsConfiguration("prefixSymbol")) {
15-
// Update the snippets
1620
updatePrefixSymbol();
1721
}
1822
});

src/snippet.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ interface Snippet {
88
};
99
}
1010

11+
/**
12+
* Reads a snippet file and returns a snippet object.
13+
* @param snippetFilePath - The path of the file to read.
14+
* @returns A Snippet object.
15+
*/
1116
async function readSnippetFile(snippetFilePath: string): Promise<Snippet> {
1217
try {
1318
const snippetFileContentBuffer = await fs.readFile(snippetFilePath);
@@ -17,6 +22,12 @@ async function readSnippetFile(snippetFilePath: string): Promise<Snippet> {
1722
}
1823
}
1924

25+
/**
26+
* Writes a snippet object to a file.
27+
* @param snippetFilePath - The path of the file to write to.
28+
* @param snippet - The snippet object to write to the file.
29+
* @throws An error if there was a problem writing the file.
30+
*/
2031
async function writeSnippetFile(
2132
snippetFilePath: string,
2233
snippet: Snippet
@@ -32,6 +43,14 @@ function sanitizeName(name: string): string {
3243
return name.replace(/[^a-zA-Z0-9]/g, "-");
3344
}
3445

46+
/**
47+
* Creates a snippet object with the given name, prefix, language ID, and selection.
48+
* @param name - The name of the snippet.
49+
* @param prefixName - The prefix of the snippet.
50+
* @param languageId - The language ID of the snippet.
51+
* @param selection - The code snippet.
52+
* @returns A Snippet object.
53+
*/
3554
function createSnippet(
3655
name: string,
3756
prefixName: string,

src/updatePrefixSymbol.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ import * as vscode from "vscode";
22
import * as path from "path";
33
import { Snippet, readSnippetFile, writeSnippetFile } from "./snippet";
44

5+
/**
6+
* Updates the prefix symbol of all snippets in the workspace.code-snippets file.
7+
* If the `prefixSymbol` configuration setting is set, replaces all non-alphanumeric characters in the prefix with it.
8+
* @returns void
9+
*/
510
function updatePrefixSymbol() {
6-
// Get the path to the snippet file
711
const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
812
if (!workspaceFolder) {
913
vscode.window.showErrorMessage("No workspace folder found.");
@@ -15,15 +19,12 @@ function updatePrefixSymbol() {
1519
"workspace.code-snippets"
1620
);
1721

18-
// Read the snippet file
1922
readSnippetFile(snippetFilePath).then((snippetFileContent) => {
20-
// Check if any snippet prefix contains a special character
2123
const specialCharRegex = /[^a-zA-Z0-9]/g;
2224
const hasSpecialChar = Object.keys(snippetFileContent).some((key) =>
2325
specialCharRegex.test(snippetFileContent[key].prefix)
2426
);
2527

26-
// Update the snippets
2728
const newSnippetFileContent: Snippet = {};
2829
Object.keys(snippetFileContent).forEach((key) => {
2930
const snippet = snippetFileContent[key];
@@ -41,7 +42,6 @@ function updatePrefixSymbol() {
4142
newSnippetFileContent[key] = newSnippet;
4243
});
4344

44-
// Write the updated snippets to the file
4545
writeSnippetFile(snippetFilePath, newSnippetFileContent);
4646
});
4747
}

0 commit comments

Comments
 (0)