From e816c7d9eed141808c99c519857d8151714aed87 Mon Sep 17 00:00:00 2001 From: Andreas Arvidsson Date: Tue, 7 Nov 2023 19:55:56 +0100 Subject: [PATCH] Added insertions scope for insert snippet action (#1879) `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> --- src/snippets.py | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/src/snippets.py b/src/snippets.py index 3f15bcde..5cd9a832 100644 --- a/src/snippets.py +++ b/src/snippets.py @@ -1,5 +1,5 @@ from dataclasses import dataclass -from typing import Any, Optional +from typing import Any, Optional, Union from talon import Module, actions @@ -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 @@ -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 """ - 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