Skip to content

Commit

Permalink
[Experimental] Add support for forcing lanuages via a command. Re-wor…
Browse files Browse the repository at this point in the history
…k code.lanuage to use app-specific win.file_ext()/win.file_name() actions.
  • Loading branch information
knausj85 committed Jun 1, 2020
1 parent 8f9ee23 commit bba1b56
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 26 deletions.
82 changes: 82 additions & 0 deletions apps/vscode/linux/vscode.talon
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Microsoft - Visual Studio Code
# see app/vscode.talon for voice commands

os: linux
app: Code
app: Code - OSS
-

action(user.ide_refactor):
key(ctrl-a)
key(ctrl-shift-i)
key(ctrl-s)

action(user.ide_refactor_in_line):
key(ctrl-shift-i)
key(ctrl-s)

action(user.ide_refactor_rename):
key(f2)

action(user.ide_follow):
key(f12)

action(user.ide_go_back):
key(alt-left)

action(user.ide_go_forward):
key(alt-right)

action(user.ide_up_cursor):
key(ctrl-shift-up)

action(user.ide_down_cursor):
key(ctrl-shift-down)

action(user.ide_toggle_terminal):
key(ctrl-`)

action(user.ide_terminal_new):
key(ctrl-shift-`)

action(user.ide_terminal_focus_previous):
key(alt-left)

action(user.ide_terminal_focus_next):
key(alt-right)

action(user.ide_terminal_trash):
key(ctrl-shift-delete)

action(user.ide_terminal_scroll_down):
key(shift-pgdown)

action(user.ide_terminal_scroll_up):
key(shift-pgup)

action(user.ide_toggle_comment):
key(ctrl-/)

action(user.ide_smart):
key(ctrl-space)

action(user.ide_parameter_hints):
key(ctrl-shift-space)

action(user.ide_done):
key(tab)

action(user.ide_show_explorer):
key(ctrl-shift-e)

action(user.ide_show_search):
key(ctrl-shift-f)

action(user.ide_show_source_control):
key(ctrl-shift-g)

action(user.ide_show_debug):
key(ctrl-shift-d)

action(user.ide_show_extensions):
key(ctrl-shift-x)
File renamed without changes.
22 changes: 22 additions & 0 deletions apps/vscode/vscode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from talon import Context, actions, ui, Module

ctx_vscode = Context()

ctx_vscode.matches = r'''
app: Code
app: Code - OSS
app: Code
app: Visual Studio Code
app: Code.exe
'''
@ctx_vscode.action_class('win')
class win_actions:
def filename():
title = actions.win.title()
result = title.split(" - ")[0]
if "." in result:
return result
return ""

def file_ext():
return actions.win.filename().split(".")[-1]
File renamed without changes.
File renamed without changes.
19 changes: 19 additions & 0 deletions apps/win/notepad++.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from talon import Context, actions, ui, Module
ctx = Context()

ctx.matches = r'''
app: Notepad++ : a free (GNU) source code editor
app: notepad++.exe
'''
@ctx.action_class('win')
class win_actions:
def filename():
title = actions.win.title()
result = title.split(" - ")[0]
if "." in result:
print(result.split("\\")[-1])
return result.split("\\")[-1]
return ""

def file_ext():
return actions.win.filename().split(".")[-1]
78 changes: 56 additions & 22 deletions code/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,67 @@
key = actions.key

extension_lang_map = {
"py" : "python",
"cs" : "csharp",
"cpp" : "cplusplus",
"h" : "cplusplus",
"talon": "talon",
"py": "python",
"cs": "csharp",
"cpp": "cplusplus",
"h": "cplusplus",
"talon": "talon",
"gdb": "gdb",
"md": "markdown",
"sh": "bash",
"go": "go"
}

# The [^\\\/] is specifically to avoid matching something like a .talon folder
# that would otherwise cause the .talon file action to load
regex_ext = re.compile("[^\\\/]\.(\S*)\s*")
forced_language = False

@ctx.action_class('code')
class CodeActions:
class code_actions:
def language():
title = ui.active_window().title
#print(str(ui.active_app()))
#workaround for VS Code on Mac. The title is "",
#but the doc is correct.
if title == "":
title = ui.active_window().doc

m = regex_ext.search(title)
if m:
extension = m.group(1)
if extension in extension_lang_map:

return extension_lang_map[extension]
result = ""
if not forced_language:
file_extension = actions.win.file_ext()
file_name = actions.win.filename()

if file_extension != "":
result = file_extension
#it should always be the last split...
elif file_name != "" and "." in file_name:
result = file_name.split(".")[-1]

if result in extension_lang_map:
result = extension_lang_map[result]

#print("code.language: " + result)
return result

mod = Module()

#create a mode for each defined language
for __, lang in extension_lang_map.items():
mod.mode(lang)

@mod.action_class
class Actions:
def code_set_language_mode(language: str):
"""Sets the active language mode, and disables extension matching"""
global forced_language
for __, lang in extension_lang_map.items():
if lang != language:
actions.mode.disable("user.{}".format(lang))
else:
return extension
return ""
actions.mode.enable("user.{}".format(lang))

forced_language = True

def code_clear_language_mode():
"""Clears the active language mode, and re-enables code.language: extension matching"""
global forced_language
forced_language = False

for __, lang in extension_lang_map.items():
actions.mode.disable("user.{}".format(lang))



4 changes: 3 additions & 1 deletion lang/go.talon
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
code.language: go
mode: user.go
mode: command
and code.language: go
-
variadic: "..."
logical and: " && "
Expand Down
4 changes: 3 additions & 1 deletion lang/python.talon
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
code.language: python
mode: user.python
mode: command
and code.language: python
-
logical and: " and "
logical or: " or "
Expand Down
4 changes: 3 additions & 1 deletion lang/talon.talon
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
code.language: talon
mode: user.talon
mode: command
and code.language: talon
-
insert:
insert('insert("")')
Expand Down
9 changes: 8 additions & 1 deletion modes/modes.talon
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@ talon mode: speech.enable()
^command mode$:
mode.disable("sleep")
mode.disable("dictation")
mode.enable("command")
mode.enable("command")

^force see sharp$: user.code_set_language_mode("csharp")
^force see plus plus$: user.code_set_language_mode("cplusplus")
^force python$: user.code_set_language_mode("python")
^force go language$: user.code_set_language_mode("go")
^force (talon | talent) language$: user.code_set_language_mode("talon")
^clear language modes$: user.code_clear_language_mode()

0 comments on commit bba1b56

Please sign in to comment.