|
| 1 | +from typing import Any, Optional |
| 2 | + |
1 | 3 | from talon import Module, actions, app
|
2 | 4 |
|
3 | 5 | from .csv_overrides import init_csv_and_watch_changes
|
|
27 | 29 | @mod.capture(
|
28 | 30 | rule="{user.cursorless_insertion_snippet_no_phrase} | {user.cursorless_insertion_snippet_single_phrase}"
|
29 | 31 | )
|
30 |
| -def cursorless_insertion_snippet(m) -> str: |
| 32 | +def cursorless_insertion_snippet(m) -> dict: |
31 | 33 | try:
|
32 |
| - return m.cursorless_insertion_snippet_no_phrase |
| 34 | + name = m.cursorless_insertion_snippet_no_phrase |
33 | 35 | except AttributeError:
|
34 |
| - pass |
| 36 | + name = m.cursorless_insertion_snippet_single_phrase.split(".")[0] |
35 | 37 |
|
36 |
| - return m.cursorless_insertion_snippet_single_phrase.split(".")[0] |
| 38 | + return {"type": "named", "name": name} |
37 | 39 |
|
38 | 40 |
|
39 | 41 | # NOTE: Please do not change these dicts. Use the CSVs for customization.
|
@@ -65,13 +67,76 @@ def cursorless_insertion_snippet(m) -> str:
|
65 | 67 |
|
66 | 68 | @mod.action_class
|
67 | 69 | class Actions:
|
68 |
| - def cursorless_insert_snippet_with_phrase( |
| 70 | + def private_cursorless_insert_snippet_with_phrase( |
69 | 71 | action: str, snippet_description: str, text: str
|
70 | 72 | ):
|
71 | 73 | """Perform cursorless wrap action"""
|
72 | 74 | snippet_name, snippet_variable = snippet_description.split(".")
|
73 | 75 | actions.user.cursorless_implicit_target_command(
|
74 |
| - action, snippet_name, {snippet_variable: text} |
| 76 | + action, |
| 77 | + { |
| 78 | + "type": "named", |
| 79 | + "name": snippet_name, |
| 80 | + "substitutions": {snippet_variable: text}, |
| 81 | + }, |
| 82 | + ) |
| 83 | + |
| 84 | + def cursorless_insert_snippet_by_name(name: str): |
| 85 | + """Inserts a named snippet""" |
| 86 | + actions.user.cursorless_implicit_target_command( |
| 87 | + "insertSnippet", |
| 88 | + { |
| 89 | + "type": "named", |
| 90 | + "name": name, |
| 91 | + }, |
| 92 | + ) |
| 93 | + |
| 94 | + def cursorless_insert_snippet(body: str): |
| 95 | + """Inserts a custom snippet""" |
| 96 | + actions.user.cursorless_implicit_target_command( |
| 97 | + "insertSnippet", |
| 98 | + { |
| 99 | + "type": "custom", |
| 100 | + "body": body, |
| 101 | + }, |
| 102 | + ) |
| 103 | + |
| 104 | + def cursorless_wrap_with_snippet_by_name( |
| 105 | + name: str, variable_name: str, target: dict |
| 106 | + ): |
| 107 | + """Wrap target with a named snippet""" |
| 108 | + actions.user.cursorless_single_target_command_with_arg_list( |
| 109 | + "wrapWithSnippet", |
| 110 | + target, |
| 111 | + [ |
| 112 | + { |
| 113 | + "type": "named", |
| 114 | + "name": name, |
| 115 | + "variableName": variable_name, |
| 116 | + } |
| 117 | + ], |
| 118 | + ) |
| 119 | + |
| 120 | + def cursorless_wrap_with_snippet( |
| 121 | + body: str, |
| 122 | + target: dict, |
| 123 | + variable_name: Optional[str] = None, |
| 124 | + scope: Optional[str] = None, |
| 125 | + ): |
| 126 | + """Wrap target with a custom snippet""" |
| 127 | + snippet_arg: dict[str, Any] = { |
| 128 | + "type": "custom", |
| 129 | + "body": body, |
| 130 | + } |
| 131 | + if scope is not None: |
| 132 | + snippet_arg["scopeType"] = {"type": scope} |
| 133 | + if variable_name is not None: |
| 134 | + snippet_arg["variableName"] = variable_name |
| 135 | + |
| 136 | + actions.user.cursorless_single_target_command_with_arg_list( |
| 137 | + "wrapWithSnippet", |
| 138 | + target, |
| 139 | + [snippet_arg], |
75 | 140 | )
|
76 | 141 |
|
77 | 142 |
|
|
0 commit comments