1- import { SnippetString } from "vscode" ;
1+ import { commands , SnippetString , workspace } from "vscode" ;
22import { snippets } from "../languages" ;
33import {
44 Action ,
@@ -11,6 +11,15 @@ import {
1111import displayPendingEditDecorations from "../util/editDisplayUtils" ;
1212import { ensureSingleEditor } from "../util/targetUtils" ;
1313
14+ interface UserLanguageSnippet {
15+ snippet ?: string ;
16+ name ?: string ;
17+ langId ?: string ;
18+ defaultScopeType ?: string ;
19+ }
20+
21+ type UserLanguageSnippetMap = Record < string , UserLanguageSnippet > ;
22+
1423export default class WrapWithSnippet implements Action {
1524 targetPreferences : ActionPreferences [ ] = [
1625 {
@@ -33,29 +42,40 @@ export default class WrapWithSnippet implements Action {
3342 ) : Promise < ActionReturnValue > {
3443 const editor = ensureSingleEditor ( targets ) ;
3544
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 ] ) ;
4146
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 ) ;
4849
4950 await displayPendingEditDecorations (
5051 targets ,
5152 this . graph . editStyles . pendingModification0
5253 ) ;
5354
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+ }
5863
5964 return { } ;
6065 }
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+ }
6181}
0 commit comments