Skip to content

Commit

Permalink
Added insertions scope for insert snippet action (#1879)
Browse files Browse the repository at this point in the history
`snip funk after air`

## 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
- [x] Add changelog file

---------

Co-authored-by: Pokey Rule <755842+pokey@users.noreply.github.com>
Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored and cursorless-bot committed Nov 7, 2023
1 parent 6c946c3 commit e816c7d
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions src/snippets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import Any, Optional
from typing import Any, Optional, Union

from talon import Module, actions

Expand Down Expand Up @@ -90,14 +90,20 @@ def insert_named_snippet(
insert_snippet(snippet, destination)


def insert_custom_snippet(body: str, destination: CursorlessDestination):
insert_snippet(
{
"type": "custom",
"body": body,
},
destination,
)
def insert_custom_snippet(
body: str,
destination: CursorlessDestination,
scope_types: Optional[list[dict]] = None,
):
snippet = {
"type": "custom",
"body": body,
}

if scope_types:
snippet["scopeTypes"] = scope_types

insert_snippet(snippet, destination)


@mod.action_class
Expand Down Expand Up @@ -127,12 +133,21 @@ def cursorless_insert_snippet_by_name(name: str):
ImplicitDestination(),
)

def cursorless_insert_snippet(body: str):
def cursorless_insert_snippet(
body: str,
destination: Optional[CursorlessDestination] = ImplicitDestination(),
scope_type: Optional[Union[str, list[str]]] = None,
):
"""Cursorless: Insert custom snippet <body>"""
insert_custom_snippet(
body,
ImplicitDestination(),
)
if isinstance(scope_type, str):
scope_type = [scope_type]

if scope_type is not None:
scope_types = [{"type": st} for st in scope_type]
else:
scope_types = None

insert_custom_snippet(body, destination, scope_types)

def cursorless_wrap_with_snippet_by_name(
name: str, variable_name: str, target: CursorlessTarget
Expand Down

0 comments on commit e816c7d

Please sign in to comment.