From 27e48922da32d5db806c98104e7732718219d7a6 Mon Sep 17 00:00:00 2001 From: Andreas Arvidsson Date: Fri, 3 Nov 2023 21:05:00 +0100 Subject: [PATCH 01/10] Add wrapper snippets --- core/snippets/README.md | 2 +- core/snippets/snippets_cursorless.talon | 5 +++++ core/snippets/snippets_insert.py | 28 +++++++++++++++++++++++++ core/tags.py | 1 + 4 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 core/snippets/snippets_cursorless.talon diff --git a/core/snippets/README.md b/core/snippets/README.md index 4a74157d48..6d5f571e3f 100644 --- a/core/snippets/README.md +++ b/core/snippets/README.md @@ -7,7 +7,7 @@ Custom format to represent snippets. - Custom file ending `.snippet`. - Supports syntax highlighting in VSCode via an [extension](https://marketplace.visualstudio.com/items?itemName=AndreasArvidsson.andreas-talon) - Supports auto-formatting in VSCode via an [extension](https://marketplace.visualstudio.com/items?itemName=AndreasArvidsson.andreas-talon) -- Support for insertion and wrapper snippets. Note that while the snippet file syntax here supports wrapper snippets, we still need to add the proper voice commands; stay tuned. +- Support for insertion and wrapper snippets. - Support for phrase formatters. ## Format diff --git a/core/snippets/snippets_cursorless.talon b/core/snippets/snippets_cursorless.talon new file mode 100644 index 0000000000..1ec890cba0 --- /dev/null +++ b/core/snippets/snippets_cursorless.talon @@ -0,0 +1,5 @@ +tag: user.cursorless +- + +{user.snippet_wrapper} wrap : + user.insert_wrapper_snippet_by_name(cursorless_target, snippet_wrapper) diff --git a/core/snippets/snippets_insert.py b/core/snippets/snippets_insert.py index baa7781a23..f352a09776 100644 --- a/core/snippets/snippets_insert.py +++ b/core/snippets/snippets_insert.py @@ -47,3 +47,31 @@ def insert_snippet_by_name_with_phrase(name: str, phrase: str): ) actions.user.insert_snippet_by_name(name, substitutions) + + def insert_wrapper_snippet( + target: Any, + body: str, + variable_name: str, + scope: str = None, + ): + """Wrap the target with snippet""" + var_name = f"${variable_name}" + + if var_name not in body: + raise ValueError(f"Wrapper variable '{var_name}' missing in body '{body}'") + + body = body.replace(f"{var_name}", "$TM_SELECTED_TEXT") + + actions.user.cursorless_wrap_with_snippet(body, target, None, scope) + + def insert_wrapper_snippet_by_name(target: Any, id: str): + """Wrap the target with snippet """ + index = id.rindex(".") + snippet_name = id[:index] + variable_name = id[index + 1] + snippet: Snippet = actions.user.get_snippet(snippet_name) + variable = snippet.get_variable_strict(variable_name) + + actions.user.insert_wrapper_snippet( + target, snippet.body, variable_name, variable.wrapper_scope + ) diff --git a/core/tags.py b/core/tags.py index a9d2f76814..0049c44267 100644 --- a/core/tags.py +++ b/core/tags.py @@ -12,6 +12,7 @@ "readline", "taskwarrior", # commandline tag for taskwarrior commands "tmux", + "cursorless", ] for entry in tagList: mod.tag(entry, f"tag to load {entry} and/or related plugins ") From 9ad395d91a4af1765329213b69cfce2c65652085 Mon Sep 17 00:00:00 2001 From: Andreas Arvidsson Date: Fri, 3 Nov 2023 21:31:28 +0100 Subject: [PATCH 02/10] Update regex --- core/snippets/snippets_insert.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/core/snippets/snippets_insert.py b/core/snippets/snippets_insert.py index f352a09776..f0d8a75ead 100644 --- a/core/snippets/snippets_insert.py +++ b/core/snippets/snippets_insert.py @@ -55,12 +55,14 @@ def insert_wrapper_snippet( scope: str = None, ): """Wrap the target with snippet""" - var_name = f"${variable_name}" + reg = re.compile(rf"\${variable_name}|\$\{{{variable_name}\}}") - if var_name not in body: - raise ValueError(f"Wrapper variable '{var_name}' missing in body '{body}'") + if not reg.search(body): + raise ValueError( + f"Can't wrap non existing variable '{variable_name}' in snippet body '{body}'" + ) - body = body.replace(f"{var_name}", "$TM_SELECTED_TEXT") + body = reg.sub("$TM_SELECTED_TEXT", body) actions.user.cursorless_wrap_with_snippet(body, target, None, scope) From 9cc6af936ac649a692300378918f98c514f09716 Mon Sep 17 00:00:00 2001 From: Andreas Arvidsson Date: Sat, 4 Nov 2023 11:31:24 +0100 Subject: [PATCH 03/10] Remove Cursorless specific logic --- core/snippets/README.md | 2 +- core/snippets/snippets_cursorless.talon | 5 ----- core/snippets/snippets_insert.py | 29 ++++++++++--------------- core/tags.py | 1 - 4 files changed, 13 insertions(+), 24 deletions(-) delete mode 100644 core/snippets/snippets_cursorless.talon diff --git a/core/snippets/README.md b/core/snippets/README.md index 6d5f571e3f..4880db6246 100644 --- a/core/snippets/README.md +++ b/core/snippets/README.md @@ -8,7 +8,7 @@ Custom format to represent snippets. - Supports syntax highlighting in VSCode via an [extension](https://marketplace.visualstudio.com/items?itemName=AndreasArvidsson.andreas-talon) - Supports auto-formatting in VSCode via an [extension](https://marketplace.visualstudio.com/items?itemName=AndreasArvidsson.andreas-talon) - Support for insertion and wrapper snippets. -- Support for phrase formatters. +- Support for phrase formatters. Note that while the snippet file syntax here supports wrapper snippets, you require the [Cursorless](https://marketplace.visualstudio.com/items?itemName=pokey.cursorless) extension for wrapper snippepts to work. ## Format diff --git a/core/snippets/snippets_cursorless.talon b/core/snippets/snippets_cursorless.talon deleted file mode 100644 index 1ec890cba0..0000000000 --- a/core/snippets/snippets_cursorless.talon +++ /dev/null @@ -1,5 +0,0 @@ -tag: user.cursorless -- - -{user.snippet_wrapper} wrap : - user.insert_wrapper_snippet_by_name(cursorless_target, snippet_wrapper) diff --git a/core/snippets/snippets_insert.py b/core/snippets/snippets_insert.py index f0d8a75ead..58048c5393 100644 --- a/core/snippets/snippets_insert.py +++ b/core/snippets/snippets_insert.py @@ -51,29 +51,24 @@ def insert_snippet_by_name_with_phrase(name: str, phrase: str): def insert_wrapper_snippet( target: Any, body: str, - variable_name: str, scope: str = None, ): """Wrap the target with snippet""" + + def insert_wrapper_snippet_by_name(target: Any, name: str): + """Wrap the target with snippet """ + index = name.rindex(".") + snippet_name = name[:index] + variable_name = name[index + 1] + snippet: Snippet = actions.user.get_snippet(snippet_name) + variable = snippet.get_variable_strict(variable_name) reg = re.compile(rf"\${variable_name}|\$\{{{variable_name}\}}") - if not reg.search(body): + if not reg.search(snippet.body): raise ValueError( - f"Can't wrap non existing variable '{variable_name}' in snippet body '{body}'" + f"Can't wrap non existing variable '{variable_name}' in snippet body '{snippet.body}'" ) - body = reg.sub("$TM_SELECTED_TEXT", body) - - actions.user.cursorless_wrap_with_snippet(body, target, None, scope) - - def insert_wrapper_snippet_by_name(target: Any, id: str): - """Wrap the target with snippet """ - index = id.rindex(".") - snippet_name = id[:index] - variable_name = id[index + 1] - snippet: Snippet = actions.user.get_snippet(snippet_name) - variable = snippet.get_variable_strict(variable_name) + body = reg.sub("$TM_SELECTED_TEXT", snippet.body) - actions.user.insert_wrapper_snippet( - target, snippet.body, variable_name, variable.wrapper_scope - ) + actions.user.insert_wrapper_snippet(target, body, variable.wrapper_scope) diff --git a/core/tags.py b/core/tags.py index 0049c44267..a9d2f76814 100644 --- a/core/tags.py +++ b/core/tags.py @@ -12,7 +12,6 @@ "readline", "taskwarrior", # commandline tag for taskwarrior commands "tmux", - "cursorless", ] for entry in tagList: mod.tag(entry, f"tag to load {entry} and/or related plugins ") From 0ce40ba270f3650bffbcbdb39dc4341ede4ebe0e Mon Sep 17 00:00:00 2001 From: Andreas Arvidsson Date: Sat, 4 Nov 2023 14:13:25 +0100 Subject: [PATCH 04/10] Update core/snippets/README.md Co-authored-by: Pokey Rule <755842+pokey@users.noreply.github.com> --- core/snippets/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/snippets/README.md b/core/snippets/README.md index 4880db6246..ae1a02a511 100644 --- a/core/snippets/README.md +++ b/core/snippets/README.md @@ -8,7 +8,7 @@ Custom format to represent snippets. - Supports syntax highlighting in VSCode via an [extension](https://marketplace.visualstudio.com/items?itemName=AndreasArvidsson.andreas-talon) - Supports auto-formatting in VSCode via an [extension](https://marketplace.visualstudio.com/items?itemName=AndreasArvidsson.andreas-talon) - Support for insertion and wrapper snippets. -- Support for phrase formatters. Note that while the snippet file syntax here supports wrapper snippets, you require the [Cursorless](https://marketplace.visualstudio.com/items?itemName=pokey.cursorless) extension for wrapper snippepts to work. +- Support for phrase formatters. Note that while the snippet file syntax here supports wrapper snippets, you will need to install [Cursorless](https://www.cursorless.org/) for wrapper snippets to work. ## Format From 65556e06a282658db5973c4420263a57049dd1d1 Mon Sep 17 00:00:00 2001 From: Andreas Arvidsson Date: Sat, 4 Nov 2023 14:29:59 +0100 Subject: [PATCH 05/10] upd --- core/snippets/snippets_insert.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/snippets/snippets_insert.py b/core/snippets/snippets_insert.py index 58048c5393..181957a3c1 100644 --- a/core/snippets/snippets_insert.py +++ b/core/snippets/snippets_insert.py @@ -49,13 +49,13 @@ def insert_snippet_by_name_with_phrase(name: str, phrase: str): actions.user.insert_snippet_by_name(name, substitutions) def insert_wrapper_snippet( - target: Any, body: str, + target: Any, scope: str = None, ): """Wrap the target with snippet""" - def insert_wrapper_snippet_by_name(target: Any, name: str): + def insert_wrapper_snippet_by_name(name: str, target: Any): """Wrap the target with snippet """ index = name.rindex(".") snippet_name = name[:index] @@ -71,4 +71,4 @@ def insert_wrapper_snippet_by_name(target: Any, name: str): body = reg.sub("$TM_SELECTED_TEXT", snippet.body) - actions.user.insert_wrapper_snippet(target, body, variable.wrapper_scope) + actions.user.insert_wrapper_snippet(body, target, variable.wrapper_scope) From 9e3a0be6c1fac2ea9fce490c7fc96b0c461f407c Mon Sep 17 00:00:00 2001 From: Andreas Arvidsson Date: Wed, 8 Nov 2023 16:57:11 +0100 Subject: [PATCH 06/10] Add insertion scopes --- core/snippets/snippets_parser.py | 1 + 1 file changed, 1 insertion(+) diff --git a/core/snippets/snippets_parser.py b/core/snippets/snippets_parser.py index bd264429fd..1d836b4a86 100644 --- a/core/snippets/snippets_parser.py +++ b/core/snippets/snippets_parser.py @@ -54,6 +54,7 @@ def create_snippet( name=document.name or default_context.name, languages=document.languages or default_context.languages, phrases=document.phrases or default_context.phrases, + insertion_scopes=document.insertionScopes or default_context.insertionScopes, variables=combine_variables(default_context.variables, document.variables), body=normalize_snippet_body_tabs(document.body), ) From 33df09e564e38ffdcf646d928488c32f54c68843 Mon Sep 17 00:00:00 2001 From: Andreas Arvidsson Date: Sun, 12 Nov 2023 18:40:41 +0100 Subject: [PATCH 07/10] Add missing import --- core/snippets/snippets_insert.py | 1 + 1 file changed, 1 insertion(+) diff --git a/core/snippets/snippets_insert.py b/core/snippets/snippets_insert.py index 181957a3c1..af5bdbe615 100644 --- a/core/snippets/snippets_insert.py +++ b/core/snippets/snippets_insert.py @@ -1,4 +1,5 @@ import re +from typing import Any from talon import Module, actions From 3313b0915943b61b88d37597d2d5651a4b84ed48 Mon Sep 17 00:00:00 2001 From: Andreas Arvidsson Date: Sun, 12 Nov 2023 19:23:26 +0100 Subject: [PATCH 08/10] Send wrapper variable name over the wire --- core/snippets/README.md | 2 +- core/snippets/snippets_insert.py | 13 ++++--------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/core/snippets/README.md b/core/snippets/README.md index ae1a02a511..b94aaada42 100644 --- a/core/snippets/README.md +++ b/core/snippets/README.md @@ -8,7 +8,7 @@ Custom format to represent snippets. - Supports syntax highlighting in VSCode via an [extension](https://marketplace.visualstudio.com/items?itemName=AndreasArvidsson.andreas-talon) - Supports auto-formatting in VSCode via an [extension](https://marketplace.visualstudio.com/items?itemName=AndreasArvidsson.andreas-talon) - Support for insertion and wrapper snippets. -- Support for phrase formatters. Note that while the snippet file syntax here supports wrapper snippets, you will need to install [Cursorless](https://www.cursorless.org/) for wrapper snippets to work. +- Support for phrase formatters. Note that while the snippet file syntax here supports wrapper snippets, we still need to add the proper voice commands; stay tuned. ## Format diff --git a/core/snippets/snippets_insert.py b/core/snippets/snippets_insert.py index af5bdbe615..f78c43d653 100644 --- a/core/snippets/snippets_insert.py +++ b/core/snippets/snippets_insert.py @@ -52,6 +52,7 @@ def insert_snippet_by_name_with_phrase(name: str, phrase: str): def insert_wrapper_snippet( body: str, target: Any, + variable_name: str, scope: str = None, ): """Wrap the target with snippet""" @@ -63,13 +64,7 @@ def insert_wrapper_snippet_by_name(name: str, target: Any): variable_name = name[index + 1] snippet: Snippet = actions.user.get_snippet(snippet_name) variable = snippet.get_variable_strict(variable_name) - reg = re.compile(rf"\${variable_name}|\$\{{{variable_name}\}}") - if not reg.search(snippet.body): - raise ValueError( - f"Can't wrap non existing variable '{variable_name}' in snippet body '{snippet.body}'" - ) - - body = reg.sub("$TM_SELECTED_TEXT", snippet.body) - - actions.user.insert_wrapper_snippet(body, target, variable.wrapper_scope) + actions.user.insert_wrapper_snippet( + snippet.body, target, variable.name, variable.wrapper_scope + ) From 6fbda22d4a1d86ee82eab3dd6a40836aa70c5dcb Mon Sep 17 00:00:00 2001 From: Andreas Arvidsson Date: Sun, 12 Nov 2023 19:24:23 +0100 Subject: [PATCH 09/10] Updated readme --- core/snippets/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/snippets/README.md b/core/snippets/README.md index b94aaada42..4a74157d48 100644 --- a/core/snippets/README.md +++ b/core/snippets/README.md @@ -7,8 +7,8 @@ Custom format to represent snippets. - Custom file ending `.snippet`. - Supports syntax highlighting in VSCode via an [extension](https://marketplace.visualstudio.com/items?itemName=AndreasArvidsson.andreas-talon) - Supports auto-formatting in VSCode via an [extension](https://marketplace.visualstudio.com/items?itemName=AndreasArvidsson.andreas-talon) -- Support for insertion and wrapper snippets. -- Support for phrase formatters. Note that while the snippet file syntax here supports wrapper snippets, we still need to add the proper voice commands; stay tuned. +- Support for insertion and wrapper snippets. Note that while the snippet file syntax here supports wrapper snippets, we still need to add the proper voice commands; stay tuned. +- Support for phrase formatters. ## Format From 5b1c925d7f9c19de8867e9ca6c8ba5e68156027a Mon Sep 17 00:00:00 2001 From: Andreas Arvidsson Date: Tue, 14 Nov 2023 20:47:02 +0100 Subject: [PATCH 10/10] Renamed actions --- core/snippets/snippets_insert.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/snippets/snippets_insert.py b/core/snippets/snippets_insert.py index f78c43d653..e3866af740 100644 --- a/core/snippets/snippets_insert.py +++ b/core/snippets/snippets_insert.py @@ -49,7 +49,7 @@ def insert_snippet_by_name_with_phrase(name: str, phrase: str): actions.user.insert_snippet_by_name(name, substitutions) - def insert_wrapper_snippet( + def wrap_with_snippet( body: str, target: Any, variable_name: str, @@ -57,7 +57,7 @@ def insert_wrapper_snippet( ): """Wrap the target with snippet""" - def insert_wrapper_snippet_by_name(name: str, target: Any): + def wrap_with_snippet_by_name(name: str, target: Any): """Wrap the target with snippet """ index = name.rindex(".") snippet_name = name[:index] @@ -65,6 +65,6 @@ def insert_wrapper_snippet_by_name(name: str, target: Any): snippet: Snippet = actions.user.get_snippet(snippet_name) variable = snippet.get_variable_strict(variable_name) - actions.user.insert_wrapper_snippet( + actions.user.wrap_with_snippet( snippet.body, target, variable.name, variable.wrapper_scope )