Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delimiter pairs #1612

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions core/edit/delimiter_pair.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from talon import Module, actions

mod = Module()

mod.list("delimiter_pair", "List of matching pair delimiters")


@mod.capture(rule="{user.delimiter_pair}")
def delimiter_pair(m) -> list[str]:
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved
pair = m.delimiter_pair.split()
assert len(pair) == 2
open = pair[0] if pair[0] != "space" else " "
close = pair[1] if pair[1] != "space" else " "
return [open, close]
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved


@mod.action_class
class Actions:
def delimiter_pair_insert(pair: list[str]):
"""Insert a delimiter pair <pair> leaving the cursor in the middle"""
left, right = pair
actions.insert(f"{left}{right}")
for _ in right:
actions.edit.left()
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved

def delimiter_pair_wrap_selection(pair: list[str]):
"""Wrap selection with delimiter pair <pair>"""
left, right = pair
selected = actions.edit.selected_text()
actions.insert(f"{left}{selected}{right}")
for _ in right:
actions.edit.left()
11 changes: 11 additions & 0 deletions core/edit/delimiter_pair.talon-list
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
list: user.delimiter_pair
-

AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved
round: ( )
index: [ ]
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved
diamond: < >
curly: { }
twin: "' '"
quad: '" "'
skis: ` `
padding: space space
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 3 additions & 1 deletion core/edit/edit.talon
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,14 @@ new line above: edit.line_insert_up()
new line below | slap: edit.line_insert_down()

# Insert padding with optional symbols
(pad | padding): user.insert_between(" ", " ")
(pad | padding) <user.symbol_key>+:
insert(" ")
user.insert_many(symbol_key_list)
insert(" ")

# Insert delimiter pairs
<user.delimiter_pair>: user.delimiter_pair_insert(delimiter_pair)
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved

# Undo/redo
undo that: edit.undo()
redo that: edit.redo()
Expand Down
48 changes: 41 additions & 7 deletions core/edit/edit_command_actions.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,66 @@
from dataclasses import dataclass
from typing import Callable
from typing import Callable, Union

from talon import Module, actions


@dataclass
class EditAction:
class EditSimpleAction:
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved
type: str

def __str__(self):
return self.type


@dataclass
class EditInsertAction(EditAction):
class EditInsertAction:
type = "insert"
text: str

def __str__(self):
return self.type


@dataclass
class EditWrapAction:
type = "wrapWithDelimiterPair"
pair: list[str]

def __str__(self):
return self.type


@dataclass
class EditFormatAction(EditAction):
class EditFormatAction:
type = "applyFormatter"
formatters: str

def __str__(self):
return self.type


EditAction = Union[
EditSimpleAction,
EditInsertAction,
EditWrapAction,
EditFormatAction,
]

mod = Module()
mod.list("edit_action", desc="Actions for the edit command")


@mod.capture(rule="{user.edit_action}")
def edit_simple_action(m) -> EditAction:
return EditAction(m.edit_action)
def edit_simple_action(m) -> EditSimpleAction:
return EditSimpleAction(m.edit_action)


@mod.capture(rule="<user.delimiter_pair> wrap")
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved
def edit_wrap_action(m) -> EditWrapAction:
return EditWrapAction(m.delimiter_pair)

@mod.capture(rule="<user.edit_simple_action>")

@mod.capture(rule="<user.edit_simple_action> | <user.edit_wrap_action>")
def edit_action(m) -> EditAction:
return m[0]

Expand Down Expand Up @@ -62,6 +92,10 @@ def run_action_callback(action: EditAction):
assert isinstance(action, EditInsertAction)
actions.insert(action.text)

case "wrapWithDelimiterPair":
assert isinstance(action, EditWrapAction)
return lambda: actions.user.delimiter_pair_wrap_selection(action.pair)

case "applyFormatter":
assert isinstance(action, EditFormatAction)
actions.user.formatters_reformat_selection(action.formatters)
Expand Down