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
3
1
import * as vscode from 'vscode'
4
2
5
3
const jsSnippets = require ( '../snippets/snippets.json' )
@@ -14,60 +12,53 @@ type Snippet = {
14
12
const convertSnippetArrayToString = ( snippetArray : Array < string > ) : string =>
15
13
snippetArray . join ( '\n' )
16
14
17
- // this method is called when your extension is activated
18
- // your extension is activated the very first time the command is executed
19
15
export 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
23
20
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 )
35
26
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 ]
39
30
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,
47
37
}
48
- )
49
-
50
- const options = {
51
- matchOnDescription : true ,
52
- matchOnDetail : true ,
53
- placeHolder : 'Search snippet' ,
54
38
}
39
+ )
55
40
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 ) )
66
58
}
67
- )
59
+ } )
68
60
69
61
context . subscriptions . push ( disposable )
70
62
}
71
63
72
- // this method is called when your extension is deactivated
73
64
export function deactivate ( ) { }
0 commit comments