1
1
import { getExtensionSetting , registerExtensionCommand , showQuickPick } from 'vscode-framework'
2
2
import { getActiveRegularEditor } from '@zardoy/vscode-utils'
3
3
4
- import { window , DecorationRangeBehavior , SnippetString , commands , Range , workspace , Position } from 'vscode'
4
+ import { window , DecorationRangeBehavior , SnippetString , commands , Range , workspace , Position , Selection } from 'vscode'
5
5
import { Disposable } from 'vscode'
6
6
7
7
export default ( ) => {
@@ -21,7 +21,17 @@ export default () => {
21
21
rangeBehavior : DecorationRangeBehavior . ClosedClosed ,
22
22
} )
23
23
24
- registerExtensionCommand ( 'insertLoopSnippet' , async ( _ , arg ?: { separator : string ; wrap : string } ) => {
24
+ type SimpleSnippetOptions = {
25
+ wrap : string | undefined
26
+ separator : string
27
+ /**
28
+ * If true, then snippet can be exited by typing
29
+ * @default false
30
+ */
31
+ onlyMidCompletions ?: boolean
32
+ }
33
+
34
+ registerExtensionCommand ( 'insertLoopSnippet' , async ( _ , argSnippet ?: Partial < SimpleSnippetOptions > ) => {
25
35
const editor = getActiveRegularEditor ( )
26
36
if ( ! editor ) return
27
37
@@ -33,17 +43,11 @@ export default () => {
33
43
const toOffset = ( pos : Position ) => editor . document . offsetAt ( pos )
34
44
const toPos = ( offset : number ) => editor . document . positionAt ( offset )
35
45
36
- let expectedEndOffset = toOffset ( editor . selection . active )
37
-
38
- type SimpleSnippetVariant = {
39
- wrap : string | undefined
40
- separator : string
41
- }
42
-
43
- const simpleSnippetVariants : Record < string , SimpleSnippetVariant > = {
46
+ const simpleSnippetVariants : Record < string , SimpleSnippetOptions > = {
44
47
"'' | " : {
45
48
wrap : "'$1'" ,
46
49
separator : ' | ' ,
50
+ onlyMidCompletions : true ,
47
51
} ,
48
52
' && ' : {
49
53
wrap : undefined ,
@@ -60,8 +64,8 @@ export default () => {
60
64
}
61
65
62
66
const selectedVariant =
63
- arg ?. separator !== undefined && arg . separator !== undefined
64
- ? arg
67
+ argSnippet ?. wrap !== undefined && argSnippet . separator !== undefined
68
+ ? argSnippet
65
69
: await showQuickPick (
66
70
Object . entries ( simpleSnippetVariants ) . map ( ( [ key , value ] ) => ( {
67
71
label : key ,
@@ -72,12 +76,21 @@ export default () => {
72
76
} ,
73
77
)
74
78
if ( ! selectedVariant ) return
75
- const { wrap : wrapSnippet = '' , separator } = selectedVariant
79
+ // todo-low multicursor support
80
+ const selectedContentUsed = ! selectedVariant . wrap && getExtensionSetting ( 'useSelectedContentAsSnippet' )
81
+ const defaultWrapSnippet = selectedContentUsed ? editor . document . getText ( editor . selection ) : ''
82
+ const { wrap : wrapSnippet = defaultWrapSnippet , separator, onlyMidCompletions } = selectedVariant
76
83
const showExitMarker = getExtensionSetting ( 'showExitMarker' )
77
84
const triggerCompletions = getExtensionSetting ( 'triggerCompletions' )
78
- const snippetCanExitByTyping = ! showExitMarker || ! ! wrapSnippet
85
+ const snippetCanExitByTyping = showExitMarker && ! ! onlyMidCompletions
86
+
87
+ if ( selectedContentUsed ) {
88
+ editor . selection = new Selection ( editor . selection . end , editor . selection . end )
89
+ }
79
90
80
- let firstInsert = true
91
+ /** controls seperator insertion */
92
+ let firstInsert = ! selectedContentUsed
93
+ let expectedEndOffset = toOffset ( editor . selection . active )
81
94
let snippetJustInserted = false
82
95
const doInsert = async ( ) => {
83
96
const snippetToInsert = firstInsert ? wrapSnippet : separator + wrapSnippet
@@ -87,7 +100,7 @@ export default () => {
87
100
}
88
101
89
102
if ( triggerCompletions ) {
90
- await commands . executeCommand ( 'editor.action.triggerSuggest' )
103
+ void commands . executeCommand ( 'editor.action.triggerSuggest' )
91
104
}
92
105
93
106
firstInsert = false
0 commit comments