Skip to content

Commit

Permalink
lang: Add tag for brace-delimited code blocks (#1332)
Browse files Browse the repository at this point in the history
## Problem

Several languages had the same, or similar, copy-pasted implementations
of `code_block` to insert braces and leave the cursor on a blank line
between them. These all assume the editor will appropriately expand
<kbd>enter</kbd> between brackets, which isn't true for all editors.

The C# implementation implemented the same idea, but appears to assume
<kbd>enter</kbd> _isn't_ expanded.

Furthermore, some C-like languages were missing a `code_block` impl
entirely.

## Solution

Similar to #720, this adds a common implementation for braced code
blocks, which can be overridden on a per-editor basis, and adds the tag
to the appropriate languages.
  • Loading branch information
auscompgeek authored Dec 27, 2023
1 parent 1c7c967 commit 779615b
Show file tree
Hide file tree
Showing 18 changed files with 29 additions and 29 deletions.
1 change: 1 addition & 0 deletions lang/c/c.talon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ code.language: c
-
tag(): user.code_imperative

tag(): user.code_block_c_like
tag(): user.code_comment_line
tag(): user.code_comment_block_c_like
tag(): user.code_data_bool
Expand Down
4 changes: 0 additions & 4 deletions lang/csharp/csharp.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,6 @@ def code_operator_bitwise_right_shift():
def code_operator_bitwise_right_shift_assignment():
actions.auto_insert(" >>= ")

def code_block():
actions.insert("{}")
actions.key("left enter enter up tab")

def code_self():
actions.auto_insert("this")

Expand Down
1 change: 1 addition & 0 deletions lang/csharp/csharp.talon
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ code.language: csharp
tag(): user.code_imperative
tag(): user.code_object_oriented

tag(): user.code_block_c_like
tag(): user.code_comment_line
tag(): user.code_comment_block_c_like
tag(): user.code_data_bool
Expand Down
4 changes: 0 additions & 4 deletions lang/css/css.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,6 @@

@ctx.action_class("user")
class UserActions:
def code_block():
actions.user.insert_between("{", "}")
actions.key("enter")

def code_operator_addition():
actions.insert(" + ")

Expand Down
1 change: 1 addition & 0 deletions lang/css/css.talon
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
code.language: css
code.language: scss
-
tag(): user.code_block_c_like
tag(): user.code_comment_block_c_like
tag(): user.code_functions_common
tag(): user.code_libraries
Expand Down
1 change: 1 addition & 0 deletions lang/java/java.talon
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ code.language: java
tag(): user.code_imperative
tag(): user.code_object_oriented

tag(): user.code_block_c_like
tag(): user.code_comment_line
tag(): user.code_comment_block_c_like
tag(): user.code_data_bool
Expand Down
4 changes: 0 additions & 4 deletions lang/javascript/javascript.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,6 @@ def code_state_else():
actions.user.insert_between(" else {", "}")
actions.key("enter")

def code_block():
actions.user.insert_between("{", "}")
actions.key("enter")

def code_self():
actions.auto_insert("this")

Expand Down
1 change: 1 addition & 0 deletions lang/javascript/javascript.talon
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ code.language: typescriptreact
tag(): user.code_imperative
tag(): user.code_object_oriented

tag(): user.code_block_c_like
tag(): user.code_comment_line
tag(): user.code_comment_block_c_like
tag(): user.code_data_bool
Expand Down
5 changes: 0 additions & 5 deletions lang/php/php.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ def code_operator_object_accessor():
def code_define_class():
actions.auto_insert("class ")

def code_block():
actions.insert("{}")
actions.edit.left()
actions.key("enter")

def code_import():
actions.auto_insert("use ;")
actions.edit.left()
Expand Down
1 change: 1 addition & 0 deletions lang/php/php.talon
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ tag(): user.code_imperative
tag(): user.code_object_oriented
tag(): user.code_libraries

tag(): user.code_block_c_like
tag(): user.code_comment_line
tag(): user.code_comment_block
tag(): user.code_comment_documentation
Expand Down
3 changes: 3 additions & 0 deletions lang/proto/proto.talon
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
code.language: protobuf
-
tag(): user.code_block_c_like

# this is pretty bare-bones, further contributions welcome
block: user.code_block()

state message: "message "
state package: "package "
state reserved: "reserved "
Expand Down
1 change: 1 addition & 0 deletions lang/r/r.talon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ code.language: r
-
tag(): user.code_imperative

tag(): user.code_block_c_like
tag(): user.code_comment_line
tag(): user.code_data_bool
tag(): user.code_data_null
Expand Down
5 changes: 0 additions & 5 deletions lang/rust/rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,6 @@ def code_comment_documentation():

# tag: imperative

def code_block():
actions.auto_insert("{}")
actions.edit.left()
actions.key("enter")

def code_state_if():
actions.auto_insert("if ")

Expand Down
1 change: 1 addition & 0 deletions lang/rust/rust.talon
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ tag(): user.code_comment_line
tag(): user.code_comment_block_c_like
tag(): user.code_comment_documentation

tag(): user.code_block_c_like
tag(): user.code_imperative
tag(): user.code_object_oriented

Expand Down
5 changes: 0 additions & 5 deletions lang/scala/scala.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@

@ctx.action_class("user")
class UserActions:
def code_block():
actions.insert("{}")
actions.edit.left()
actions.key("enter")

def code_operator_lambda():
actions.insert(" => ")

Expand Down
1 change: 1 addition & 0 deletions lang/scala/scala.talon
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ code.language: scala
tag(): user.code_imperative
tag(): user.code_object_oriented

tag(): user.code_block_c_like
tag(): user.code_comment_line
tag(): user.code_comment_block_c_like
tag(): user.code_data_bool
Expand Down
16 changes: 14 additions & 2 deletions lang/tags/imperative.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
from talon import Context, Module
from talon import Context, Module, actions

ctx = Context()
c_like_ctx = Context()
mod = Module()

mod.tag(
"code_imperative",
desc="Tag for enabling basic imperative programming commands (loops, functions, etc)",
)
mod.tag("code_block_c_like", desc="Language uses C style code blocks, i.e. braces")

c_like_ctx.matches = """
tag: self.code_block_c_like
"""


@mod.action_class
Expand Down Expand Up @@ -58,3 +63,10 @@ def code_next():

def code_try_catch():
"""Inserts try/catch. If selection is true, does so around the selection"""


@c_like_ctx.action_class("self")
class CActions:
def code_block():
actions.user.insert_between("{", "}")
actions.key("enter")
3 changes: 3 additions & 0 deletions lang/terraform/terraform.talon
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
code.language: terraform
-
tag(): user.code_block_c_like
tag(): user.code_comment_block_c_like
tag(): user.code_comment_line
tag(): user.code_data_bool
Expand All @@ -9,6 +10,8 @@ tag(): user.code_operators_assignment
tag(): user.code_operators_lambda
tag(): user.code_operators_math

block: user.code_block()

state {user.terraform_module_block}:
user.code_terraform_module_block(user.terraform_module_block)

Expand Down

0 comments on commit 779615b

Please sign in to comment.