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

Support inline snippets; add Talon snippet api #1329

Merged
merged 3 commits into from
Mar 27, 2023

Conversation

pokey
Copy link
Member

@pokey pokey commented Mar 20, 2023

Note that I created a Talon-side insertion api that supports the full set of options, including substitutions, targets, and scopes, but decided to leave it out for now because I'm not sure exactly how it should look and we don't need it for the mathfly support we're planning to use it for. In particular, we should probably figure out #803 before we implement the more complex api because snippets really want a destination, not a target. I did use the complex Talon-side api to record some tests of the extension api, though

Here is the complex Talon snippet insertion api in case useful at some point

Complex talon api
From e39e03e3a06a6db4f1edb245a5225037d0ea08d3 Mon Sep 17 00:00:00 2001
From: Pokey Rule <[email protected]>
Date: Fri, 24 Mar 2023 14:22:40 +0000
Subject: [PATCH] Complex insert snippet Talon api

---
 cursorless-talon/src/snippets.py | 35 ++++++++++++++++++++++++--------
 1 file changed, 27 insertions(+), 8 deletions(-)

diff --git a/cursorless-talon/src/snippets.py b/cursorless-talon/src/snippets.py
index a9f100f58..409512011 100644
--- a/cursorless-talon/src/snippets.py
+++ b/cursorless-talon/src/snippets.py
@@ -91,15 +91,34 @@ class Actions:
             },
         )
 
-    def cursorless_insert_snippet(body: str):
+    def cursorless_insert_snippet(
+        body: str,
+        target: Optional[dict] = None,
+        scope: Optional[str] = None,
+        snippet_variable: Optional[str] = None,
+        text: Optional[str] = None,
+    ):
         """Inserts a custom snippet"""
-        actions.user.cursorless_implicit_target_command(
-            "insertSnippet",
-            {
-                "type": "custom",
-                "body": body,
-            },
-        )
+        snippet_arg: dict[str, Any] = {
+            "type": "custom",
+            "body": body,
+        }
+        if scope:
+            snippet_arg["scopeType"] = {"type": scope}
+        if snippet_variable:
+            snippet_arg["substitutions"] = {snippet_variable: text}
+
+        if target:
+            actions.user.cursorless_single_target_command_with_arg_list(
+                "insertSnippet",
+                target,
+                [snippet_arg],
+            )
+        else:
+            actions.user.cursorless_implicit_target_command(
+                "insertSnippet",
+                snippet_arg,
+            )
 
     def cursorless_wrap_with_snippet_by_name(
         name: str, variable_name: str, target: dict
-- 
2.39.2

Checklist

@pokey pokey changed the base branch from main to pokey/pnpm-workspace March 20, 2023 17:05
@pokey pokey changed the title Pokey/support-rich-object-to-snippet-action Support rich object to snippet action Mar 20, 2023
@pokey pokey changed the title Support rich object to snippet action Support rich object as argument to snippet action Mar 20, 2023
@pokey pokey changed the title Support rich object as argument to snippet action Support rich object as argument to snippet actions Mar 20, 2023
@pokey pokey force-pushed the pokey/support-rich-object-to-snippet-action branch 2 times, most recently from a8982b1 to 326fca1 Compare March 21, 2023 11:49
@pokey pokey force-pushed the pokey/pnpm-workspace branch from 1e1090f to d26d5ae Compare March 21, 2023 11:50
@pokey pokey force-pushed the pokey/support-rich-object-to-snippet-action branch from 326fca1 to 9e1f8b4 Compare March 21, 2023 11:50
@pokey pokey changed the title Support rich object as argument to snippet actions Support inline snippets; add Talon snippet api Mar 21, 2023
@pokey pokey force-pushed the pokey/support-rich-object-to-snippet-action branch from 61bb092 to 768bec2 Compare March 21, 2023 13:06
Base automatically changed from pokey/pnpm-workspace to main March 21, 2023 13:28
@pokey pokey force-pushed the pokey/support-rich-object-to-snippet-action branch 2 times, most recently from 8d40782 to 74ca863 Compare March 21, 2023 18:31
@pokey pokey changed the base branch from main to pokey/fix-test-case-recorder March 22, 2023 16:00
@pokey pokey force-pushed the pokey/support-rich-object-to-snippet-action branch 5 times, most recently from eb35e6d to ac640e1 Compare March 22, 2023 20:47
@pokey pokey linked an issue Mar 23, 2023 that may be closed by this pull request
@pokey pokey force-pushed the pokey/fix-test-case-recorder branch 3 times, most recently from 8c63db1 to e9f164d Compare March 23, 2023 15:09
Base automatically changed from pokey/fix-test-case-recorder to main March 23, 2023 15:48
@pokey pokey force-pushed the pokey/support-rich-object-to-snippet-action branch 3 times, most recently from 8bf2471 to 84df8d3 Compare March 24, 2023 14:57
@pokey pokey force-pushed the pokey/support-rich-object-to-snippet-action branch from 84df8d3 to 064d992 Compare March 24, 2023 15:21
@pokey pokey force-pushed the pokey/support-rich-object-to-snippet-action branch from 064d992 to 8483c4f Compare March 24, 2023 15:23
@pokey pokey marked this pull request as ready for review March 24, 2023 15:23
@pokey pokey requested a review from AndreasArvidsson March 24, 2023 15:23
@pokey pokey force-pushed the pokey/support-rich-object-to-snippet-action branch from 8483c4f to 37ca842 Compare March 24, 2023 15:35
@pokey pokey force-pushed the pokey/support-rich-object-to-snippet-action branch from 37ca842 to b07458b Compare March 24, 2023 15:50
@@ -1,6 +1,6 @@
languageId: plaintext
command:
version: 4
version: 5
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit annoying we'll have to bump this one every time we increment command version number, but I think it's not unreasonable: we probably do want to be ensuring that the test case recorder is recording tests using the latest command version

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should have a test to make sure that this test actually contains the latest version?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test case recorder test fails on this one if you don't bump. Are you saying we should add another test?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this line always be the last person then?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the command that gets captured by recorder is after we do normalisation / upgrade at the start of running the command. So even if we pass 4 there, the command that gets captured has version 5

As to whether that's the right thing to do, I could go either way. I think slightly better to capture normalised version. If we want to be able to record legacy command, we could add a flag to test case recorder to enable that

@pokey
Copy link
Member Author

pokey commented Mar 24, 2023

Tempted to switch custom to inline everywhere. WDYT @AndreasArvidsson ? We already support "custom" snippets, which means ones that aren't part of the built-in Cursorless snippets, so I think this terminology is confusing

@AndreasArvidsson
Copy link
Member

But isn't that more or less the same thing? In one instance you send a custom snippet in the other case you define a custom snippet.

@pokey
Copy link
Member Author

pokey commented Mar 25, 2023

The confusing thing is that if you want to use one of the custom snippets you've defined, you don't use custom snippet arg, you use named snippet

@pokey pokey added the to discuss Plan to discuss at meet-up label Mar 25, 2023
@pokey
Copy link
Member Author

pokey commented Mar 26, 2023

  • cursorless_insert_custom_snippet => cursorless_insert_snippet
  • cursorless_insert_named_snippet => cursorless_insert_snippet_by_name
  • cursorless_wrap_with_custom_snippet => cursorless_wrap_with_snippet
  • cursorless_wrap_with_named_snippet => cursorless_wrap_with_snippet_by_name

@pokey pokey enabled auto-merge March 27, 2023 09:32
@pokey pokey requested a review from AndreasArvidsson March 27, 2023 09:32
@pokey pokey added this pull request to the merge queue Mar 27, 2023
Merged via the queue into main with commit 94bab18 Mar 27, 2023
@pokey pokey deleted the pokey/support-rich-object-to-snippet-action branch March 27, 2023 09:56
cursorless-bot pushed a commit that referenced this pull request Mar 27, 2023
- Fixes #1324
- Fixes #1325 

Note that I created a Talon-side insertion api that supports the full
set of options, including substitutions, targets, and scopes, but
decided to leave it out for now because I'm not sure exactly how it
should look and we don't need it for the mathfly support we're planning
to use it for. In particular, we should probably figure out #803 before
we implement the more complex api because snippets really want a
destination, not a target. I did use the complex Talon-side api to
record some tests of the extension api, though

Here is the complex Talon snippet insertion api in case useful at some
point

<details><summary>Complex talon api</summary>

```diff
From e39e03e3a06a6db4f1edb245a5225037d0ea08d3 Mon Sep 17 00:00:00 2001
From: Pokey Rule <[email protected]>
Date: Fri, 24 Mar 2023 14:22:40 +0000
Subject: [PATCH] Complex insert snippet Talon api

---
 cursorless-talon/src/snippets.py | 35 ++++++++++++++++++++++++--------
 1 file changed, 27 insertions(+), 8 deletions(-)

diff --git a/cursorless-talon/src/snippets.py b/cursorless-talon/src/snippets.py
index a9f100f58..409512011 100644
--- a/cursorless-talon/src/snippets.py
+++ b/cursorless-talon/src/snippets.py
@@ -91,15 +91,34 @@ class Actions:
             },
         )
 
-    def cursorless_insert_snippet(body: str):
+    def cursorless_insert_snippet(
+        body: str,
+        target: Optional[dict] = None,
+        scope: Optional[str] = None,
+        snippet_variable: Optional[str] = None,
+        text: Optional[str] = None,
+    ):
         """Inserts a custom snippet"""
-        actions.user.cursorless_implicit_target_command(
-            "insertSnippet",
-            {
-                "type": "custom",
-                "body": body,
-            },
-        )
+        snippet_arg: dict[str, Any] = {
+            "type": "custom",
+            "body": body,
+        }
+        if scope:
+            snippet_arg["scopeType"] = {"type": scope}
+        if snippet_variable:
+            snippet_arg["substitutions"] = {snippet_variable: text}
+
+        if target:
+            actions.user.cursorless_single_target_command_with_arg_list(
+                "insertSnippet",
+                target,
+                [snippet_arg],
+            )
+        else:
+            actions.user.cursorless_implicit_target_command(
+                "insertSnippet",
+                snippet_arg,
+            )
 
     def cursorless_wrap_with_snippet_by_name(
         name: str, variable_name: str, target: dict
-- 
2.39.2

```

</details>

## Checklist

- [x] I have added
[tests](https://www.cursorless.org/docs/contributing/test-case-recorder/)
- [x] I have updated the
[docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and
[cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet)
- [x] I have not broken the cheatsheet
thetomcraig-aya pushed a commit to thetomcraig/cursorless that referenced this pull request Mar 27, 2024
- Fixes cursorless-dev#1324
- Fixes cursorless-dev#1325 

Note that I created a Talon-side insertion api that supports the full
set of options, including substitutions, targets, and scopes, but
decided to leave it out for now because I'm not sure exactly how it
should look and we don't need it for the mathfly support we're planning
to use it for. In particular, we should probably figure out cursorless-dev#803 before
we implement the more complex api because snippets really want a
destination, not a target. I did use the complex Talon-side api to
record some tests of the extension api, though

Here is the complex Talon snippet insertion api in case useful at some
point

<details><summary>Complex talon api</summary>

```diff
From e39e03e3a06a6db4f1edb245a5225037d0ea08d3 Mon Sep 17 00:00:00 2001
From: Pokey Rule <[email protected]>
Date: Fri, 24 Mar 2023 14:22:40 +0000
Subject: [PATCH] Complex insert snippet Talon api

---
 cursorless-talon/src/snippets.py | 35 ++++++++++++++++++++++++--------
 1 file changed, 27 insertions(+), 8 deletions(-)

diff --git a/cursorless-talon/src/snippets.py b/cursorless-talon/src/snippets.py
index a9f100f58..409512011 100644
--- a/cursorless-talon/src/snippets.py
+++ b/cursorless-talon/src/snippets.py
@@ -91,15 +91,34 @@ class Actions:
             },
         )
 
-    def cursorless_insert_snippet(body: str):
+    def cursorless_insert_snippet(
+        body: str,
+        target: Optional[dict] = None,
+        scope: Optional[str] = None,
+        snippet_variable: Optional[str] = None,
+        text: Optional[str] = None,
+    ):
         """Inserts a custom snippet"""
-        actions.user.cursorless_implicit_target_command(
-            "insertSnippet",
-            {
-                "type": "custom",
-                "body": body,
-            },
-        )
+        snippet_arg: dict[str, Any] = {
+            "type": "custom",
+            "body": body,
+        }
+        if scope:
+            snippet_arg["scopeType"] = {"type": scope}
+        if snippet_variable:
+            snippet_arg["substitutions"] = {snippet_variable: text}
+
+        if target:
+            actions.user.cursorless_single_target_command_with_arg_list(
+                "insertSnippet",
+                target,
+                [snippet_arg],
+            )
+        else:
+            actions.user.cursorless_implicit_target_command(
+                "insertSnippet",
+                snippet_arg,
+            )
 
     def cursorless_wrap_with_snippet_by_name(
         name: str, variable_name: str, target: dict
-- 
2.39.2

```

</details>

## Checklist

- [x] I have added
[tests](https://www.cursorless.org/docs/contributing/test-case-recorder/)
- [x] I have updated the
[docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and
[cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet)
- [x] I have not broken the cheatsheet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
to discuss Plan to discuss at meet-up
Projects
None yet
2 participants