1
- import { SnippetString } from "vscode" ;
1
+ import { commands , SnippetString , workspace } from "vscode" ;
2
2
import { snippets } from "../languages" ;
3
3
import {
4
4
Action ,
@@ -11,6 +11,15 @@ import {
11
11
import displayPendingEditDecorations from "../util/editDisplayUtils" ;
12
12
import { ensureSingleEditor } from "../util/targetUtils" ;
13
13
14
+ interface UserLanguageSnippet {
15
+ snippet ?: string ;
16
+ name ?: string ;
17
+ langId ?: string ;
18
+ defaultScopeType ?: string ;
19
+ }
20
+
21
+ type UserLanguageSnippetMap = Record < string , UserLanguageSnippet > ;
22
+
14
23
export default class WrapWithSnippet implements Action {
15
24
targetPreferences : ActionPreferences [ ] = [
16
25
{
@@ -33,29 +42,40 @@ export default class WrapWithSnippet implements Action {
33
42
) : Promise < ActionReturnValue > {
34
43
const editor = ensureSingleEditor ( targets ) ;
35
44
36
- const languageId = editor . document . languageId ;
37
- const languageSnippets = snippets [ languageId ] ;
38
- if ( languageSnippets == null ) {
39
- throw new Error ( `Snippets not supported for language ${ languageId } ` ) ;
40
- }
45
+ await this . graph . actions . setSelection . run ( [ targets ] ) ;
41
46
42
- const snippetString = languageSnippets [ snippetName ] ;
43
- if ( snippetString == null ) {
44
- throw new Error (
45
- `Snippet ${ snippetName } not supported for language ${ languageId } `
46
- ) ;
47
- }
47
+ const languageId = editor . document . languageId ;
48
+ const snippet = this . getSnippet ( languageId , snippetName ) ;
48
49
49
50
await displayPendingEditDecorations (
50
51
targets ,
51
52
this . graph . editStyles . pendingModification0
52
53
) ;
53
54
54
- await editor . insertSnippet (
55
- snippetString ,
56
- targets . map ( ( target ) => target . selection . selection )
57
- ) ;
55
+ if ( snippet . snippet != null ) {
56
+ await editor . insertSnippet ( snippet . snippet ) ;
57
+ } else {
58
+ await commands . executeCommand ( "editor.action.codeAction" , {
59
+ kind : "refactor.extract.constant" ,
60
+ preferred : true ,
61
+ } ) ;
62
+ }
58
63
59
64
return { } ;
60
65
}
66
+
67
+ private getSnippet ( languageId : string , snippetName : string ) {
68
+ const languageSnippets = snippets [ languageId ] ;
69
+
70
+ const userLanguageSnippets = workspace
71
+ . getConfiguration ( "cursorless" )
72
+ . get < UserLanguageSnippetMap > ( `wrapperSnippets` ) ;
73
+ const snippetString = languageSnippets [ snippetName ] ;
74
+ if ( snippetString == null ) {
75
+ throw new Error (
76
+ `Snippet ${ snippetName } not supported for language ${ languageId } `
77
+ ) ;
78
+ }
79
+ return snippetString ;
80
+ }
61
81
}
0 commit comments