Skip to content

Commit

Permalink
add the Visual Studio code
Browse files Browse the repository at this point in the history
  • Loading branch information
jira-zz committed Dec 4, 2020
1 parent 42ebece commit b6934a3
Show file tree
Hide file tree
Showing 4 changed files with 512 additions and 0 deletions.
43 changes: 43 additions & 0 deletions vscode/snippets/csharp_snippets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from talon import Context, actions, ui, Module, app

# from user.knausj_talon.code.snippet_watcher import snippet_watcher
import os

ctx = Context()
ctx.matches = r"""
app: vscode
mode: user.csharp
mode: command
and code.language: csharp
"""
# short name -> ide clip name
ctx.lists["user.snippets"] = {
"class": "class",
"else": "else",
"for each": "foreach",
"if": "if",
"try except": "try",
"try finally": "tryf",
"while": "while",
# "class funky": "def(class method)",
# "class static funky": "def(class static method)",
# "for": "for",
# "funky": "def",
}


# def update_list(watch_list):
# ctx.lists["user.snippets"] = watch_list


# # there's probably a way to do this without
# snippet_path = None
# if app.platform == "windows":
# snippet_path = os.path.expandvars(r"%AppData%\Code\User\snippets")
# elif app.platform == "mac":
# snippet_path = os.path.expanduser(
# "~/Library/Application Support/Code/User/snippets"
# )
# if snippet_path:
# watcher2 = snippet_watcher({snippet_path: ["csharp.json",],}, update_list,)
# print("reloaded!")
44 changes: 44 additions & 0 deletions vscode/snippets/python_snippets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from talon import Context, actions, ui, Module, app

# from user.knausj_talon.code.snippet_watcher import snippet_watcher
import os

ctx = Context()
ctx.matches = r"""
app: vscode
mode: user.python
mode: command
and code.language: python
"""
# short name -> ide clip name
ctx.lists["user.snippets"] = {
"class funky": "def(class method)",
"class static funky": "def(class static method)",
"class": "class",
"else if": "elif",
"for": "for",
"funky": "def",
"if else": "if/else",
"if": "if",
"lambda": "lambda",
"try except": "try/except",
"while": "while",
"with": "with",
}


# def update_list(watch_list):
# ctx.lists["user.snippets"] = watch_list


# # there's probably a way to do this without
# snippet_path = None
# if app.platform == "windows":
# snippet_path = os.path.expandvars(r"%AppData%\Code\User\snippets")
# elif app.platform == "mac":
# snippet_path = os.path.expanduser(
# "~/Library/Application Support/Code/User/snippets"
# )
# if snippet_path:
# watcher = snippet_watcher({snippet_path: ["python.json",],}, update_list,)

226 changes: 226 additions & 0 deletions vscode/vscode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
from talon import Context, actions, ui, Module, app, clip
from typing import List, Union

is_mac = app.platform == "mac"

ctx = Context()
mod = Module()
mod.apps.vscode = "app.name: Code.exe"
mod.apps.vscode = "app.name: Visual Studio Code"
mod.apps.vscode = "app.name: Code"
mod.apps.vscode = "app.name: Code - OSS"

ctx.matches = r"""
app: vscode
"""


@ctx.action_class("win")
class win_actions:
def filename():
title = actions.win.title()
# this doesn't seem to be necessary on VSCode for Mac
# if title == "":
# title = ui.active_window().doc

if is_mac:
result = title.split(" — ")[0]
else:
result = title.split(" - ")[0]

if "." in result:
return result

return ""

def file_ext():
return actions.win.filename().split(".")[-1]


@ctx.action_class("edit")
class edit_actions:
def find(text: str):
if is_mac:
actions.key("cmd-f")
else:
actions.key("ctrl-f")

actions.insert(text)

def line_swap_up():
actions.key("alt-up")

def line_swap_down():
actions.key("alt-down")

def line_clone():
actions.key("shift-alt-down")

def jump_line(n: int):
actions.user.vscode("workbench.action.gotoLine")
actions.insert(str(n))
actions.key("enter")


@mod.action_class
class Actions:
def vscode(command: str):
"""Execute command via command palette. Preserves the clipboard."""
# Clip is noticeably faster than insert
if not is_mac:
actions.key("ctrl-shift-p")
else:
actions.key("cmd-shift-p")

actions.user.paste(f"{command}")
actions.key("enter")

def vscode_ignore_clipboard(command: str):
"""Execute command via command palette. Does NOT preserve the clipboard for commands like copyFilePath"""
clip.set_text(f"{command}")
if not is_mac:
actions.key("ctrl-shift-p")
else:
actions.key("cmd-shift-p")
actions.edit.paste()
actions.key("enter")


@ctx.action_class("user")
class user_actions:
# snippet.py support beginHelp close
def snippet_search(text: str):
actions.user.vscode("Insert Snippet")
actions.insert(text)

def snippet_insert(text: str):
"""Inserts a snippet"""
actions.user.vscode("Insert Snippet")
actions.insert(text)
actions.key("enter")

def snippet_create():
"""Triggers snippet creation"""
actions.user.vscode("Preferences: Configure User Snippets")

# snippet.py support end

def tab_jump(number: int):
if number < 10:
if is_mac:
actions.key("ctrl-{}".format(number))
else:
actions.key("alt-{}".format(number))

def tab_final():
if is_mac:
actions.key("ctrl-0")
else:
actions.key("alt-0")

# splits.py support begin
def split_number(index: int):
"""Navigates to a the specified split"""
if index < 9:
if is_mac:
actions.key("cmd-{}".format(index))
else:
actions.key("ctrl-{}".format(index))

# splits.py support end

# find_and_replace.py support begin

def find(text: str):
"""Triggers find in current editor"""
if is_mac:
actions.key("cmd-f")
else:
actions.key("ctrl-f")

if text:
actions.insert(text)

def find_next():
actions.key("enter")

def find_previous():
actions.key("shift-enter")

def find_everywhere(text: str):
"""Triggers find across project"""
if is_mac:
actions.key("cmd-shift-f")
else:
actions.key("ctrl-shift-f")

if text:
actions.insert(text)

def find_toggle_match_by_case():
"""Toggles find match by case sensitivity"""
if is_mac:
actions.key("alt-cmd-c")
else:
actions.key("alt-c")

def find_toggle_match_by_word():
"""Toggles find match by whole words"""
if is_mac:
actions.key("cmd-alt-w")
else:
actions.key("alt-w")

def find_toggle_match_by_regex():
"""Toggles find match by regex"""
if is_mac:
actions.key("cmd-alt-r")
else:
actions.key("alt-r")

def replace(text: str):
"""Search and replaces in the active editor"""
if is_mac:
actions.key("alt-cmd-f")
else:
actions.key("ctrl-h")

if text:
actions.insert(text)

def replace_everywhere(text: str):
"""Search and replaces in the entire project"""
if is_mac:
actions.key("cmd-shift-h")
else:
actions.key("ctrl-shift-h")

if text:
actions.insert(text)

def replace_confirm():
"""Confirm replace at current position"""
if is_mac:
actions.key("shift-cmd-1")
else:
actions.key("ctrl-shift-1")

def replace_confirm_all():
"""Confirm replace all"""
if is_mac:
actions.key("cmd-enter")
else:
actions.key("ctrl-alt-enter")

def select_previous_occurrence(text: str):
actions.edit.find(text)
actions.sleep("100ms")
actions.key("shift-enter esc")

def select_next_occurrence(text: str):
actions.edit.find(text)
actions.sleep("100ms")
actions.key("esc")

# find_and_replace.py support end

Loading

0 comments on commit b6934a3

Please sign in to comment.