Skip to content

Commit bd7d443

Browse files
committed
fix extension code, update package.json
1 parent 408260a commit bd7d443

File tree

3 files changed

+44
-109
lines changed

3 files changed

+44
-109
lines changed

README.md

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1 @@
1-
# workspace-snippets README
2-
3-
This is the README for your extension "workspace-snippets". After writing up a brief description, we recommend including the following sections.
4-
5-
## Features
6-
7-
Describe specific features of your extension including screenshots of your extension in action. Image paths are relative to this README file.
8-
9-
For example if there is an image subfolder under your extension project workspace:
10-
11-
\!\[feature X\]\(images/feature-x.png\)
12-
13-
> Tip: Many popular extensions utilize animations. This is an excellent way to show off your extension! We recommend short, focused animations that are easy to follow.
14-
15-
## Requirements
16-
17-
If you have any requirements or dependencies, add a section describing those and how to install and configure them.
18-
19-
## Extension Settings
20-
21-
Include if your extension adds any VS Code settings through the `contributes.configuration` extension point.
22-
23-
For example:
24-
25-
This extension contributes the following settings:
26-
27-
* `myExtension.enable`: Enable/disable this extension.
28-
* `myExtension.thing`: Set to `blah` to do something.
29-
30-
## Known Issues
31-
32-
Calling out known issues can help limit users opening duplicate issues against your extension.
33-
34-
## Release Notes
35-
36-
Users appreciate release notes as you update your extension.
37-
38-
### 1.0.0
39-
40-
Initial release of ...
41-
42-
### 1.0.1
43-
44-
Fixed issue #.
45-
46-
### 1.1.0
47-
48-
Added features X, Y, and Z.
49-
50-
---
51-
52-
## Following extension guidelines
53-
54-
Ensure that you've read through the extensions guidelines and follow the best practices for creating your extension.
55-
56-
* [Extension Guidelines](https://code.visualstudio.com/api/references/extension-guidelines)
57-
58-
## Working with Markdown
59-
60-
You can author your README using Visual Studio Code. Here are some useful editor keyboard shortcuts:
61-
62-
* Split the editor (`Cmd+\` on macOS or `Ctrl+\` on Windows and Linux).
63-
* Toggle preview (`Shift+Cmd+V` on macOS or `Shift+Ctrl+V` on Windows and Linux).
64-
* Press `Ctrl+Space` (Windows, Linux, macOS) to see a list of Markdown snippets.
65-
66-
## For more information
67-
68-
* [Visual Studio Code's Markdown Support](http://code.visualstudio.com/docs/languages/markdown)
69-
* [Markdown Syntax Reference](https://help.github.com/articles/markdown-basics/)
70-
71-
**Enjoy!**
1+
Create workspace snippet from selection

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
22
"name": "workspace-snippets",
33
"displayName": "Workspace snippets",
4-
"description": "Create workspace snippets from selection",
4+
"author": "Felix Swinkels",
5+
"homepage": "https://felix.surf/",
6+
"description": "Create workspace snippet from selection",
57
"version": "0.0.1",
68
"engines": {
79
"vscode": "^1.82.0"

src/extension.ts

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,73 @@
11
import * as vscode from "vscode";
2-
import * as fs from "fs";
32
import * as path from "path";
3+
import * as fs from "fs/promises";
44

55
export function activate(context: vscode.ExtensionContext) {
6-
let disposable = vscode.commands.registerCommand(
6+
const disposable = vscode.commands.registerCommand(
77
"extension.createWorkspaceSnippet",
88
async () => {
99
const editor = vscode.window.activeTextEditor;
1010
if (!editor) {
11-
vscode.window.showErrorMessage("No active editor found");
11+
vscode.window.showErrorMessage("No active text editor found.");
1212
return;
1313
}
1414

15-
const selection = editor.selection;
16-
const selectedText = editor.document.getText(selection);
17-
18-
if (!selectedText) {
19-
vscode.window.showErrorMessage("No text selected");
15+
const selection = editor.document.getText(editor.selection);
16+
const name = await vscode.window.showInputBox({
17+
prompt: "Enter the name of the snippet",
18+
});
19+
if (!name) {
2020
return;
2121
}
2222

23-
const snippetPrefix = await vscode.window.showInputBox({
24-
prompt: "Enter the snippet prefix",
25-
value: "mySnippet",
23+
const prefix = await vscode.window.showInputBox({
24+
prompt: "Enter the prefix of the snippet",
2625
});
27-
28-
if (!snippetPrefix) {
29-
vscode.window.showErrorMessage("No snippet prefix entered");
26+
if (!prefix) {
3027
return;
3128
}
3229

33-
const snippetContent = selectedText;
30+
const snippet = {
31+
[name]: {
32+
prefix,
33+
body: [selection],
34+
description: `Snippet for ${name}`,
35+
},
36+
};
3437

35-
const snippetsFilePath = path.join(
36-
vscode.workspace.rootPath || "",
38+
const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
39+
if (!workspaceFolder) {
40+
vscode.window.showErrorMessage("No workspace folder found.");
41+
return;
42+
}
43+
44+
const snippetFilePath = path.join(
45+
workspaceFolder.uri.fsPath,
3746
".vscode",
3847
"workspace.code-snippets"
3948
);
4049

50+
let snippetFileContent = {};
4151
try {
42-
// Create the .vscode directory if it doesn't exist
43-
fs.mkdirSync(path.dirname(snippetsFilePath), { recursive: true });
44-
45-
// Create the workspace.code-snippets file if it doesn't exist
46-
if (!fs.existsSync(snippetsFilePath)) {
47-
fs.writeFileSync(snippetsFilePath, "{}");
48-
}
49-
50-
// Read the contents of the workspace.code-snippets file
51-
const snippetsFileContent = fs.readFileSync(snippetsFilePath, "utf8");
52-
53-
// Parse the contents of the file as JSON
54-
const snippets = JSON.parse(snippetsFileContent);
55-
56-
// Add the new snippet to the JSON object
57-
snippets[snippetPrefix] = snippetContent;
52+
const snippetFileContentBuffer = await fs.readFile(snippetFilePath);
53+
snippetFileContent = JSON.parse(snippetFileContentBuffer.toString());
54+
} catch (error) {
55+
// Ignore errors when reading the snippet file
56+
}
5857

59-
// Write the updated JSON object to the workspace.code-snippets file
60-
fs.writeFileSync(snippetsFilePath, JSON.stringify(snippets, null, 2));
58+
Object.assign(snippetFileContent, snippet);
6159

60+
try {
61+
await fs.writeFile(
62+
snippetFilePath,
63+
JSON.stringify(snippetFileContent, null, 2)
64+
);
6265
vscode.window.showInformationMessage(
63-
`Snippet '${snippetPrefix}' created`
66+
`Snippet "${name}" created successfully.`
6467
);
6568
} catch (error) {
6669
vscode.window.showErrorMessage(
67-
`Error creating snippet: ${error.message}`
70+
`Error creating snippet "${name}": ${(error as Error).message}`
6871
);
6972
}
7073
}

0 commit comments

Comments
 (0)