-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathgenerate_snippet.py
71 lines (56 loc) · 2.09 KB
/
generate_snippet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import glob
from pathlib import Path
from talon import Module, actions, registry, settings
from ..targets.target_types import CursorlessExplicitTarget
mod = Module()
@mod.action_class
class Actions:
def private_cursorless_migrate_snippets():
"""Migrate snippets from Cursorless to community format"""
actions.user.private_cursorless_run_rpc_command_no_wait(
"cursorless.migrateSnippets",
str(get_directory_path()),
{
"insertion": registry.lists[
"user.cursorless_insertion_snippet_no_phrase"
][-1],
"insertionWithPhrase": registry.lists[
"user.cursorless_insertion_snippet_single_phrase"
][-1],
"wrapper": registry.lists["user.cursorless_wrapper_snippet"][-1],
},
)
def private_cursorless_generate_snippet_action(target: CursorlessExplicitTarget): # pyright: ignore [reportGeneralTypeIssues]
"""Generate a snippet from the given target"""
actions.user.private_cursorless_command_no_wait(
{
"name": "generateSnippet",
"target": target,
"directory": str(get_directory_path()),
}
)
def get_directory_path() -> Path:
settings_dir = get_setting_dir()
if settings_dir is not None:
return settings_dir
return get_community_snippets_dir()
def get_setting_dir() -> Path | None:
try:
setting_dir = settings.get("user.snippets_dir")
if not setting_dir:
return None
dir = Path(str(setting_dir))
if not dir.is_absolute():
user_dir = Path(actions.path.talon_user())
dir = user_dir / dir
return dir.resolve()
except Exception:
return None
def get_community_snippets_dir() -> Path:
files = glob.iglob(
f"{actions.path.talon_user()}/**/snippets/snippets/*.snippet",
recursive=True,
)
for file in files:
return Path(file).parent
raise ValueError("Could not find community snippets directory")