-
Notifications
You must be signed in to change notification settings - Fork 783
/
language_modes.py
73 lines (56 loc) · 2.16 KB
/
language_modes.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
72
73
from talon import Context, Module, actions, app
from .code_languages import code_languages, code_special_file_map
mod = Module()
ctx = Context()
ctx_forced = Context()
ctx_forced.matches = r"""
tag: user.code_language_forced
"""
mod.tag("code_language_forced", "This tag is active when a language mode is forced")
mod.list("language_mode", desc="Name of a programming language mode.")
# Maps spoken forms to language ids
ctx.lists["user.language_mode"] = {
spoken_form: language.id
for language in code_languages
for spoken_form in language.spoken_forms
}
# Maps extension to language ids
extension_lang_map = {
f".{ext}": lang.id for lang in code_languages for ext in lang.extensions
}
language_ids = {lang.id for lang in code_languages}
forced_language = ""
@ctx.action_class("code")
class CodeActions:
def language():
file_name = actions.win.filename()
if file_name in code_special_file_map:
return code_special_file_map[file_name]
file_extension = actions.win.file_ext()
return extension_lang_map.get(file_extension, "")
@ctx_forced.action_class("code")
class ForcedCodeActions:
def language():
return forced_language
@mod.action_class
class Actions:
def code_set_language_mode(language: str):
"""Sets the active language mode, and disables extension matching"""
global forced_language
assert language in language_ids
forced_language = language
# Update tags to force a context refresh. Otherwise `code.language` will not update.
# Necessary to first set an empty list otherwise you can't move from one forced language to another.
ctx.tags = []
ctx.tags = ["user.code_language_forced"]
def code_clear_language_mode():
"""Clears the active language mode, and re-enables code.language: extension matching"""
global forced_language
forced_language = ""
ctx.tags = []
def code_show_forced_language_mode():
"""Show the active language for this context"""
if forced_language:
app.notify(f"Forced language: {forced_language}")
else:
app.notify("No language forced")