1- // The module 'vscode' contains the VS Code extensibility API
2- // Import the module and reference it with the alias vscode in your code below
31import * as vscode from 'vscode'
42
53const jsSnippets = require ( '../snippets/snippets.json' )
@@ -14,60 +12,53 @@ type Snippet = {
1412const convertSnippetArrayToString = ( snippetArray : Array < string > ) : string =>
1513 snippetArray . join ( '\n' )
1614
17- // this method is called when your extension is activated
18- // your extension is activated the very first time the command is executed
1915export function activate ( context : vscode . ExtensionContext ) {
20- // Use the console to output diagnostic information (console.log) and errors (console.error)
21- // This line of code will only be executed once when your extension is activated
22- console . log ( 'Congratulations, your extension "snippet-search" is now active!' )
16+ const {
17+ commands : { registerCommand } ,
18+ window : { showQuickPick, activeTextEditor } ,
19+ } = vscode
2320
24- // The command has been defined in the package.json file
25- // Now provide the implementation of the command with registerCommand
26- // The commandId parameter must match the command field in package.json
27- const disposable = vscode . commands . registerCommand (
28- 'extension.snippetSearch' ,
29- async ( ) => {
30- const javascriptSnippets = Object . entries ( jsSnippets as Array < Snippet > )
31- const typescriptSnippets = Object . entries ( tsSnippets as Array < Snippet > )
32- const snippetsArray : Array < [ string , Snippet ] > = javascriptSnippets . concat (
33- typescriptSnippets
34- )
21+ const disposable = registerCommand ( 'extension.snippetSearch' , async ( ) => {
22+ const javascriptSnippets = Object . entries ( jsSnippets as Array < Snippet > )
23+ const typescriptSnippets = Object . entries ( tsSnippets as Array < Snippet > )
24+ const snippetsArray : Array < [ string , Snippet ] > =
25+ javascriptSnippets . concat ( typescriptSnippets )
3526
36- const items = snippetsArray . map (
37- ( [ shortDescription , { prefix, body, description } ] , index ) => {
38- const value = typeof prefix === 'string' ? prefix : prefix [ 0 ]
27+ const items = snippetsArray . map (
28+ ( [ shortDescription , { prefix, body, description } ] , index ) => {
29+ const value = typeof prefix === 'string' ? prefix : prefix [ 0 ]
3930
40- return {
41- id : index ,
42- description : description || shortDescription ,
43- label : value ,
44- value,
45- body,
46- }
31+ return {
32+ id : index ,
33+ description : description || shortDescription ,
34+ label : value ,
35+ value,
36+ body,
4737 }
48- )
49-
50- const options = {
51- matchOnDescription : true ,
52- matchOnDetail : true ,
53- placeHolder : 'Search snippet' ,
5438 }
39+ )
5540
56- const snippet = ( await vscode . window . showQuickPick ( items , options ) ) || {
57- body : '' ,
58- }
59- const activeTextEditor = vscode . window . activeTextEditor
60- const body =
61- typeof snippet . body === 'string'
62- ? snippet . body
63- : convertSnippetArrayToString ( snippet . body )
64- activeTextEditor &&
65- activeTextEditor . insertSnippet ( new vscode . SnippetString ( body ) )
41+ const options = {
42+ matchOnDescription : true ,
43+ matchOnDetail : true ,
44+ placeHolder : 'Search snippet' ,
45+ }
46+
47+ const snippet = ( await showQuickPick ( items , options ) ) || {
48+ body : '' ,
49+ }
50+
51+ const body =
52+ typeof snippet . body === 'string'
53+ ? snippet . body
54+ : convertSnippetArrayToString ( snippet . body )
55+
56+ if ( activeTextEditor ) {
57+ activeTextEditor . insertSnippet ( new vscode . SnippetString ( body ) )
6658 }
67- )
59+ } )
6860
6961 context . subscriptions . push ( disposable )
7062}
7163
72- // this method is called when your extension is deactivated
7364export function deactivate ( ) { }
0 commit comments