From 779615b43cf50b9e0a55eb1294d09f9b2f6dd5f1 Mon Sep 17 00:00:00 2001 From: David Vo Date: Wed, 27 Dec 2023 12:45:38 +1100 Subject: [PATCH] lang: Add tag for brace-delimited code blocks (#1332) ## 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 enter between brackets, which isn't true for all editors. The C# implementation implemented the same idea, but appears to assume enter _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. --- lang/c/c.talon | 1 + lang/csharp/csharp.py | 4 ---- lang/csharp/csharp.talon | 1 + lang/css/css.py | 4 ---- lang/css/css.talon | 1 + lang/java/java.talon | 1 + lang/javascript/javascript.py | 4 ---- lang/javascript/javascript.talon | 1 + lang/php/php.py | 5 ----- lang/php/php.talon | 1 + lang/proto/proto.talon | 3 +++ lang/r/r.talon | 1 + lang/rust/rust.py | 5 ----- lang/rust/rust.talon | 1 + lang/scala/scala.py | 5 ----- lang/scala/scala.talon | 1 + lang/tags/imperative.py | 16 ++++++++++++++-- lang/terraform/terraform.talon | 3 +++ 18 files changed, 29 insertions(+), 29 deletions(-) diff --git a/lang/c/c.talon b/lang/c/c.talon index 538ce606e8..722e42d2e2 100644 --- a/lang/c/c.talon +++ b/lang/c/c.talon @@ -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 diff --git a/lang/csharp/csharp.py b/lang/csharp/csharp.py index ec725e1b64..105a18b77e 100644 --- a/lang/csharp/csharp.py +++ b/lang/csharp/csharp.py @@ -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") diff --git a/lang/csharp/csharp.talon b/lang/csharp/csharp.talon index d59ff34b67..cf8e9f7064 100644 --- a/lang/csharp/csharp.talon +++ b/lang/csharp/csharp.talon @@ -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 diff --git a/lang/css/css.py b/lang/css/css.py index 241088326d..26d4457de3 100644 --- a/lang/css/css.py +++ b/lang/css/css.py @@ -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(" + ") diff --git a/lang/css/css.talon b/lang/css/css.talon index bd9d0fd986..8c5b023fa7 100644 --- a/lang/css/css.talon +++ b/lang/css/css.talon @@ -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 diff --git a/lang/java/java.talon b/lang/java/java.talon index cc8e65efe1..c5a0e186e6 100644 --- a/lang/java/java.talon +++ b/lang/java/java.talon @@ -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 diff --git a/lang/javascript/javascript.py b/lang/javascript/javascript.py index 2c3c69bcf1..83fe54f629 100644 --- a/lang/javascript/javascript.py +++ b/lang/javascript/javascript.py @@ -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") diff --git a/lang/javascript/javascript.talon b/lang/javascript/javascript.talon index c05b6092d1..54de1d0ff1 100644 --- a/lang/javascript/javascript.talon +++ b/lang/javascript/javascript.talon @@ -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 diff --git a/lang/php/php.py b/lang/php/php.py index 57454b10dc..d3d245ab85 100644 --- a/lang/php/php.py +++ b/lang/php/php.py @@ -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() diff --git a/lang/php/php.talon b/lang/php/php.talon index 4b1e165d7a..df40fd4e26 100644 --- a/lang/php/php.talon +++ b/lang/php/php.talon @@ -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 diff --git a/lang/proto/proto.talon b/lang/proto/proto.talon index a5441f5cde..0c909ca406 100644 --- a/lang/proto/proto.talon +++ b/lang/proto/proto.talon @@ -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 " diff --git a/lang/r/r.talon b/lang/r/r.talon index b9240c1735..042132ea01 100644 --- a/lang/r/r.talon +++ b/lang/r/r.talon @@ -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 diff --git a/lang/rust/rust.py b/lang/rust/rust.py index a485c12ccd..dcb0e85c6d 100644 --- a/lang/rust/rust.py +++ b/lang/rust/rust.py @@ -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 ") diff --git a/lang/rust/rust.talon b/lang/rust/rust.talon index 80953a29f3..dd2911c2a4 100644 --- a/lang/rust/rust.talon +++ b/lang/rust/rust.talon @@ -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 diff --git a/lang/scala/scala.py b/lang/scala/scala.py index 4b75694cc8..cae483e982 100644 --- a/lang/scala/scala.py +++ b/lang/scala/scala.py @@ -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(" => ") diff --git a/lang/scala/scala.talon b/lang/scala/scala.talon index bd0cc8903a..795900ffca 100644 --- a/lang/scala/scala.talon +++ b/lang/scala/scala.talon @@ -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 diff --git a/lang/tags/imperative.py b/lang/tags/imperative.py index 948c7aac10..66f451816c 100644 --- a/lang/tags/imperative.py +++ b/lang/tags/imperative.py @@ -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 @@ -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") diff --git a/lang/terraform/terraform.talon b/lang/terraform/terraform.talon index 437258e402..e27751ebe1 100644 --- a/lang/terraform/terraform.talon +++ b/lang/terraform/terraform.talon @@ -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 @@ -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)