Skip to content

Commit 16bf69e

Browse files
committed
add an ability to copy snippet to the clipboard
1 parent 7c1212a commit 16bf69e

File tree

5 files changed

+66
-5
lines changed

5 files changed

+66
-5
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "snippet",
33
"displayName": "Snippet",
44
"description": "Insert a snippet from cht.sh for Python, JavaScript, Ruby, C#, Go, Rust (and any other language)",
5-
"version": "1.1.1",
5+
"version": "1.1.2",
66
"publisher": "vscode-snippet",
77
"engines": {
88
"vscode": "^1.74.0"
@@ -105,6 +105,11 @@
105105
"command": "snippet.renameSnippet",
106106
"category": "Snippet"
107107
},
108+
{
109+
"title": "Copy",
110+
"command": "snippet.copySnippet",
111+
"category": "Snippet"
112+
},
108113
{
109114
"title": "New Folder",
110115
"command": "snippet.createFolder",
@@ -147,6 +152,11 @@
147152
"type": "boolean",
148153
"default": false,
149154
"description": "Insert snippet with double click."
155+
},
156+
"snippet.showCopySuccessNotification": {
157+
"type": "boolean",
158+
"default": true,
159+
"description": "Whether to show a notification after the snippet is copied to the clipboard."
150160
}
151161
}
152162
},
@@ -164,15 +174,20 @@
164174
"when": "view == snippetsView",
165175
"group": "snippet.viewItemContext.baseGroup@1"
166176
},
177+
{
178+
"command": "snippet.copySnippet",
179+
"when": "view == snippetsView && viewItem == snippet",
180+
"group": "snippet.viewItemContext.baseGroup@2"
181+
},
167182
{
168183
"command": "snippet.renameSnippet",
169184
"when": "view == snippetsView",
170-
"group": "snippet.viewItemContext.baseGroup@2"
185+
"group": "snippet.viewItemContext.baseGroup@3"
171186
},
172187
{
173188
"command": "snippet.deleteSnippet",
174189
"when": "view == snippetsView",
175-
"group": "snippet.viewItemContext.baseGroup@3"
190+
"group": "snippet.viewItemContext.baseGroup@4"
176191
}
177192
],
178193
"editor/context": [

src/endpoints.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,44 @@ export function renameSnippet(treeProvider: SnippetsTreeProvider) {
335335
};
336336
}
337337

338+
export function copySnippet(treeProvider: SnippetsTreeProvider) {
339+
return async (item: SnippetsTreeItem) => {
340+
if (!item) {
341+
vscode.window.showInformationMessage(
342+
'Copy a snippet right clicking on it in the list and selecting "Copy"'
343+
);
344+
return;
345+
}
346+
347+
const content = treeProvider.storage.getSnippet(item.id);
348+
349+
try {
350+
await vscode.env.clipboard.writeText(content);
351+
352+
if (getConfig("showCopySuccessNotification")) {
353+
const hideNotification = await vscode.window.showInformationMessage(
354+
"The snippet was copied to the clipboard",
355+
{ modal: false },
356+
"Do not show again"
357+
);
358+
359+
if (hideNotification) {
360+
const config = vscode.workspace.getConfiguration("snippet");
361+
await config.update(
362+
"showCopySuccessNotification",
363+
false,
364+
vscode.ConfigurationTarget.Global
365+
);
366+
}
367+
}
368+
} catch {
369+
vscode.window.showErrorMessage(
370+
"Failed to copy the snippet to the clipboard"
371+
);
372+
}
373+
};
374+
}
375+
338376
export function createFolder(treeProvider: SnippetsTreeProvider) {
339377
return async (item?: SnippetsTreeItem) => {
340378
const opt: vscode.InputBoxOptions = {

src/extension.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ export function activate(ctx: vscode.ExtensionContext) {
5959
"snippet.renameSnippet",
6060
endpoints.renameSnippet(snippetsTreeProvider)
6161
);
62+
vscode.commands.registerCommand(
63+
"snippet.copySnippet",
64+
endpoints.copySnippet(snippetsTreeProvider)
65+
);
6266
vscode.commands.registerCommand(
6367
"snippet.createFolder",
6468
endpoints.createFolder(snippetsTreeProvider)

src/snippetsTreeProvider.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ export class SnippetsTreeItem extends vscode.TreeItem {
113113

114114
this.id = id;
115115
this.tooltip = content;
116+
this.contextValue =
117+
collapsibleState === vscode.TreeItemCollapsibleState.None
118+
? "snippet"
119+
: "folder";
116120

117121
if (collapsibleState !== vscode.TreeItemCollapsibleState.None) {
118122
return;

0 commit comments

Comments
 (0)