|
1 | 1 | from dataclasses import dataclass |
2 | | -from typing import Any, Optional |
| 2 | +from typing import Any, Optional, Union |
3 | 3 |
|
4 | 4 | from talon import Module, actions |
5 | 5 |
|
@@ -90,14 +90,20 @@ def insert_named_snippet( |
90 | 90 | insert_snippet(snippet, destination) |
91 | 91 |
|
92 | 92 |
|
93 | | -def insert_custom_snippet(body: str, destination: CursorlessDestination): |
94 | | - insert_snippet( |
95 | | - { |
96 | | - "type": "custom", |
97 | | - "body": body, |
98 | | - }, |
99 | | - destination, |
100 | | - ) |
| 93 | +def insert_custom_snippet( |
| 94 | + body: str, |
| 95 | + destination: CursorlessDestination, |
| 96 | + scope_types: Optional[list[dict]] = None, |
| 97 | +): |
| 98 | + snippet = { |
| 99 | + "type": "custom", |
| 100 | + "body": body, |
| 101 | + } |
| 102 | + |
| 103 | + if scope_types: |
| 104 | + snippet["scopeTypes"] = scope_types |
| 105 | + |
| 106 | + insert_snippet(snippet, destination) |
101 | 107 |
|
102 | 108 |
|
103 | 109 | @mod.action_class |
@@ -127,12 +133,21 @@ def cursorless_insert_snippet_by_name(name: str): |
127 | 133 | ImplicitDestination(), |
128 | 134 | ) |
129 | 135 |
|
130 | | - def cursorless_insert_snippet(body: str): |
| 136 | + def cursorless_insert_snippet( |
| 137 | + body: str, |
| 138 | + destination: Optional[CursorlessDestination] = ImplicitDestination(), |
| 139 | + scope_type: Optional[Union[str, list[str]]] = None, |
| 140 | + ): |
131 | 141 | """Cursorless: Insert custom snippet <body>""" |
132 | | - insert_custom_snippet( |
133 | | - body, |
134 | | - ImplicitDestination(), |
135 | | - ) |
| 142 | + if isinstance(scope_type, str): |
| 143 | + scope_type = [scope_type] |
| 144 | + |
| 145 | + if scope_type is not None: |
| 146 | + scope_types = [{"type": st} for st in scope_type] |
| 147 | + else: |
| 148 | + scope_types = None |
| 149 | + |
| 150 | + insert_custom_snippet(body, destination, scope_types) |
136 | 151 |
|
137 | 152 | def cursorless_wrap_with_snippet_by_name( |
138 | 153 | name: str, variable_name: str, target: CursorlessTarget |
|
0 commit comments