Skip to content

Commit 6561309

Browse files
committed
add "find and copy" command
1 parent 4e4c0de commit 6561309

File tree

5 files changed

+69
-33
lines changed

5 files changed

+69
-33
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@
110110
"command": "snippet.copySnippet",
111111
"category": "Snippet"
112112
},
113+
{
114+
"title": "Find and copy",
115+
"command": "snippet.findAndCopy",
116+
"category": "Snippet"
117+
},
113118
{
114119
"title": "New Folder",
115120
"command": "snippet.createFolder",

src/clipboard.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as vscode from "vscode";
2+
import { getConfig } from "./config";
3+
4+
export async function copySnippet(content: string): Promise<void> {
5+
try {
6+
await vscode.env.clipboard.writeText(content);
7+
8+
if (getConfig("showCopySuccessNotification")) {
9+
await showNotification();
10+
}
11+
} catch {
12+
vscode.window.showErrorMessage(
13+
"Failed to copy the snippet to the clipboard"
14+
);
15+
}
16+
}
17+
async function showNotification() {
18+
const doNotShowAgain = await vscode.window.showInformationMessage(
19+
"The snippet was copied to the clipboard",
20+
{ modal: false },
21+
"Do not show again"
22+
);
23+
24+
if (doNotShowAgain) {
25+
const config = vscode.workspace.getConfiguration("snippet");
26+
await config.update(
27+
"showCopySuccessNotification",
28+
false,
29+
vscode.ConfigurationTarget.Global
30+
);
31+
}
32+
}

src/endpoints.ts

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as vscode from "vscode";
2+
import * as clipboard from "./clipboard";
23
import { pickLanguage, getLanguage, getConfig } from "./config";
34
import { query } from "./query";
45
import { encodeRequest } from "./provider";
@@ -345,31 +346,16 @@ export function copySnippet(treeProvider: SnippetsTreeProvider) {
345346
}
346347

347348
const content = treeProvider.storage.getSnippet(item.id);
349+
await clipboard.copySnippet(content);
350+
};
351+
}
348352

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-
);
353+
export function findAndCopy(snippetsStorage: SnippetsStorage) {
354+
return async () => {
355+
const language = await getLanguage();
356+
const userQuery = await query(language, snippetsStorage, true);
358357

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-
}
358+
await clipboard.copySnippet(userQuery.savedSnippetContent);
373359
};
374360
}
375361

src/extension.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ export function activate(ctx: vscode.ExtensionContext) {
6363
"snippet.copySnippet",
6464
endpoints.copySnippet(snippetsTreeProvider)
6565
);
66+
vscode.commands.registerCommand(
67+
"snippet.findAndCopy",
68+
endpoints.findAndCopy(snippetsStorage)
69+
);
6670
vscode.commands.registerCommand(
6771
"snippet.createFolder",
6872
endpoints.createFolder(snippetsTreeProvider)

src/query.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ import SnippetsStorage from "./snippetsStorage";
66
import languages from "./languages";
77

88
function quickPickCustom(
9-
items: vscode.QuickPickItem[]
9+
items: vscode.QuickPickItem[],
10+
clearActiveItems = true
1011
): Promise<string | vscode.QuickPickItem> {
1112
return new Promise((resolve) => {
1213
const quickPick = vscode.window.createQuickPick();
1314
quickPick.title = 'Enter keywords for snippet search (e.g. "read file")';
1415
quickPick.items = items;
1516

1617
quickPick.onDidChangeValue(() => {
17-
quickPick.activeItems = [];
18+
if (clearActiveItems) {
19+
quickPick.activeItems = [];
20+
}
1821
});
1922

2023
quickPick.onDidAccept(() => {
@@ -38,7 +41,8 @@ export interface QueryResult {
3841

3942
export async function query(
4043
language: string,
41-
snippetsStorage: SnippetsStorage
44+
snippetsStorage: SnippetsStorage,
45+
suggestOnlySaved = false
4246
): Promise<QueryResult> {
4347
const suggestions = new Set(
4448
cache.state.get<string[]>(`snippet_suggestions_${language}`, [])
@@ -58,15 +62,20 @@ export async function query(
5862
}
5963
}
6064

61-
for (const suggestion of suggestions) {
62-
const tempQuickItem: vscode.QuickPickItem = {
63-
label: suggestion,
64-
description: "",
65-
};
66-
suggestionsQuickItems.push(tempQuickItem);
65+
if (!suggestOnlySaved) {
66+
for (const suggestion of suggestions) {
67+
const tempQuickItem: vscode.QuickPickItem = {
68+
label: suggestion,
69+
description: "",
70+
};
71+
suggestionsQuickItems.push(tempQuickItem);
72+
}
6773
}
6874

69-
const selectedItem = await quickPickCustom(suggestionsQuickItems);
75+
const selectedItem = await quickPickCustom(
76+
suggestionsQuickItems,
77+
!suggestOnlySaved
78+
);
7079
const input =
7180
typeof selectedItem === "string" ? selectedItem : selectedItem.label;
7281
const savedSnippetContent =

0 commit comments

Comments
 (0)