Skip to content

Commit 2572410

Browse files
committed
feat(newHugePattern): allow to use content under selection as wrap snippet (seperator inserted immdeatly in this case) (setting enabled by default)
fix: fix some weird logic around displaying exitMarker + setting
1 parent 2ca1e8d commit 2572410

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

.vscode/launch.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
{
32
"configurations": [
43
{
@@ -7,9 +6,11 @@
76
"request": "launch",
87
"args": [
98
"--extensionDevelopmentPath=${workspaceFolder}/out",
10-
"--disable-extensions"
9+
// "--disable-extensions"
1110
],
12-
"outFiles": ["${workspaceFolder}/out/**/*.js"]
11+
"outFiles": [
12+
"${workspaceFolder}/out/**/*.js"
13+
]
1314
}
1415
]
1516
}

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
"type": "boolean",
3333
"description": "Wether to trigger completinos after loop snippet insert (tab)",
3434
"default": true
35+
},
36+
"useSelectedContentAsSnippet": {
37+
"type": "boolean",
38+
"default": true
3539
}
3640
}
3741
},

src/loopSnippets.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { getExtensionSetting, registerExtensionCommand, showQuickPick } from 'vscode-framework'
22
import { getActiveRegularEditor } from '@zardoy/vscode-utils'
33

4-
import { window, DecorationRangeBehavior, SnippetString, commands, Range, workspace, Position } from 'vscode'
4+
import { window, DecorationRangeBehavior, SnippetString, commands, Range, workspace, Position, Selection } from 'vscode'
55
import { Disposable } from 'vscode'
66

77
export default () => {
@@ -21,7 +21,17 @@ export default () => {
2121
rangeBehavior: DecorationRangeBehavior.ClosedClosed,
2222
})
2323

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>) => {
2535
const editor = getActiveRegularEditor()
2636
if (!editor) return
2737

@@ -33,17 +43,11 @@ export default () => {
3343
const toOffset = (pos: Position) => editor.document.offsetAt(pos)
3444
const toPos = (offset: number) => editor.document.positionAt(offset)
3545

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> = {
4447
"'' | ": {
4548
wrap: "'$1'",
4649
separator: ' | ',
50+
onlyMidCompletions: true,
4751
},
4852
' && ': {
4953
wrap: undefined,
@@ -60,8 +64,8 @@ export default () => {
6064
}
6165

6266
const selectedVariant =
63-
arg?.separator !== undefined && arg.separator !== undefined
64-
? arg
67+
argSnippet?.wrap !== undefined && argSnippet.separator !== undefined
68+
? argSnippet
6569
: await showQuickPick(
6670
Object.entries(simpleSnippetVariants).map(([key, value]) => ({
6771
label: key,
@@ -72,12 +76,21 @@ export default () => {
7276
},
7377
)
7478
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
7683
const showExitMarker = getExtensionSetting('showExitMarker')
7784
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+
}
7990

80-
let firstInsert = true
91+
/** controls seperator insertion */
92+
let firstInsert = !selectedContentUsed
93+
let expectedEndOffset = toOffset(editor.selection.active)
8194
let snippetJustInserted = false
8295
const doInsert = async () => {
8396
const snippetToInsert = firstInsert ? wrapSnippet : separator + wrapSnippet
@@ -87,7 +100,7 @@ export default () => {
87100
}
88101

89102
if (triggerCompletions) {
90-
await commands.executeCommand('editor.action.triggerSuggest')
103+
void commands.executeCommand('editor.action.triggerSuggest')
91104
}
92105

93106
firstInsert = false

0 commit comments

Comments
 (0)