Skip to content

Commit

Permalink
#160 (WIP) Initial VSCode snippet support
Browse files Browse the repository at this point in the history
- Using captures for simplicity
  • Loading branch information
knausj85 committed Jul 14, 2020
1 parent dc216e5 commit 1e4eea5
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 1 deletion.
27 changes: 27 additions & 0 deletions apps/vscode/snippets/csharp_snippets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from talon import Context, actions, ui, Module, app

ctx = Context()
ctx.matches = r'''
app: Code
app: Code - OSS
app: Code
app: Visual Studio Code
app: Code.exe
mode: user.csharp
mode: command
and code.language: csharp
'''
#short name -> ide clip name
ctx.lists["user.snippets"] = {
#"funky": "def",
#"for": "for",
"for each": "foreach",
"while": "while",
"class": "class",
#"class funky": "def(class method)",
#"class static funky": "def(class static method)",
"if": "if",
"else": "else",
"try except": "try",
"try finally": "tryf"
}
28 changes: 28 additions & 0 deletions apps/vscode/snippets/python_snippets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from talon import Context, actions, ui, Module, app

ctx = Context()
ctx.matches = r'''
app: Code
app: Code - OSS
app: Code
app: Visual Studio Code
app: Code.exe
mode: user.python
mode: command
and code.language: python
'''
#short name -> ide clip name
ctx.lists["user.snippets"] = {
"funky": "def",
"for": "for",
"while": "while",
"class": "class",
"class funky": "def(class method)",
"class static funky": "def(class static method)",
"with": "with",
"if": "if",
"if else": "if/else",
"else if": "elif",
"lambda": "lambda",
"try except": "try/except",
}
17 changes: 16 additions & 1 deletion apps/vscode/vscode.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ def find(text: str):

@ctx.action_class('user')
class user_actions:
def snippet_search(text: str):
actions.user.ide_command_palette()
actions.insert("Insert Snippet")
actions.key("enter")
actions.insert(text)

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

def select_word(verb: str):
if not is_mac:
actions.key("ctrl-d")
Expand Down Expand Up @@ -119,4 +133,5 @@ def split_number(index: int):
if is_mac:
actions.key("cmd-{}".format(index))
else:
actions.key("ctrl-{}".format(index))
actions.key("ctrl-{}".format(index))

1 change: 1 addition & 0 deletions apps/vscode/vscode.talon
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ tag(): tabs
tag(): ide
tag(): line_commands
tag(): splits
tag(): snippets
1 change: 1 addition & 0 deletions apps/vscode/win.talon
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ tag(): tabs
tag(): ide
tag(): line_commands
tag(): splits
tag(): snippets
# General
action(user.ide_command_palette):
key(ctrl-shift-p)
Expand Down
24 changes: 24 additions & 0 deletions code/snippets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#defines placeholder actions and captures for ide-specific snippet functionality
from talon import Module, actions, app, Context

mod = Module()
ctx = Context()
mod.tag('snippets', desc='Tag for enabling code snippet-related commands')
mod.list('snippets', desc='List of code snippets')

@mod.capture
def snippets(m) -> list:
"""Returns a snippet name"""

@mod.action_class
class Actions:
def snippet_search(text: str):
"""Triggers the program's snippet search"""

def snippet_insert(text: str):
"""Inserts a snippet"""

@ctx.capture(rule='{user.snippets}')
def snippets(m):
return m.snippets

5 changes: 5 additions & 0 deletions lang/snippets.talon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tag: snippets
-
snip <user.snippets>: user.snippet_insert(user.snippets)
hunt snip <user.text>: user.snippet_search(user.text)
hunt snip: user.snippet_search("")

0 comments on commit 1e4eea5

Please sign in to comment.