-
-
Notifications
You must be signed in to change notification settings - Fork 88
Support insertSnippet action #304
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
dbf460b
Support insertSnippet action
pokey f7a868f
Fixes from Andreas PR review session
pokey b7db332
Tweaks from PR feedback
pokey 5206139
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2b75841
Make halt overridable
pokey bb353cb
Clean up snippet error handling
pokey 688bb6a
Throw error if multiple snippet definitions match a context
pokey e806960
Improve comment
pokey f99ecdd
Update comment
pokey 33678de
Cleanup
pokey 6dad154
More cleanup
pokey 88dae38
Add doc string
pokey 0d7667d
Support cascading scope types for snippets
pokey 82495fc
Update doc string
pokey 8c654db
Removed duplicate test
pokey 7957b92
Restructure tests
pokey 21f534d
At link to issue
pokey e1be6cb
Merge branch 'main' into insert-snippet
pokey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
cursorless-snippets/functionDeclaration.cursorless-snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
{ | ||
"functionDeclaration": { | ||
"definitions": [ | ||
{ | ||
"scope": { | ||
"langIds": [ | ||
"typescript", | ||
"typescriptreact", | ||
"javascript", | ||
"javascriptreact" | ||
] | ||
}, | ||
"body": [ | ||
"function $name($parameterList) {", | ||
"\t$body", | ||
"}" | ||
], | ||
"variables": { | ||
"name": { | ||
"formatter": "camelCase" | ||
} | ||
} | ||
}, | ||
{ | ||
"scope": { | ||
"langIds": [ | ||
"python" | ||
] | ||
}, | ||
"body": [ | ||
"def $name($parameterList):", | ||
"\t$body" | ||
], | ||
"variables": { | ||
"name": { | ||
"formatter": "snakeCase" | ||
} | ||
} | ||
} | ||
], | ||
"description": "Function declaration", | ||
"variables": { | ||
"body": { | ||
"wrapperScopeType": "statement" | ||
} | ||
}, | ||
"insertionScopeTypes": [ | ||
"namedFunction", | ||
"statement", | ||
"line" | ||
pokey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
] | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,9 @@ | |
"alternative": { | ||
"wrapperScopeType": "statement" | ||
} | ||
} | ||
}, | ||
"insertionScopeTypes": [ | ||
"statement" | ||
] | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,9 @@ | |
"consequence": { | ||
"wrapperScopeType": "statement" | ||
} | ||
} | ||
}, | ||
"insertionScopeTypes": [ | ||
"statement" | ||
] | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,9 @@ | |
"exceptBody": { | ||
"wrapperScopeType": "statement" | ||
} | ||
} | ||
}, | ||
"insertionScopeTypes": [ | ||
"statement" | ||
] | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
app: vscode | ||
tag: user.cursorless_experimental_snippets | ||
- | ||
|
||
{user.cursorless_insert_snippet_action} <user.cursorless_insertion_snippet>: | ||
user.cursorless_implicit_target_command(cursorless_insert_snippet_action, cursorless_insertion_snippet) | ||
|
||
{user.cursorless_insert_snippet_action} <user.cursorless_insertion_snippet> <user.cursorless_positional_target>: | ||
user.cursorless_single_target_command(cursorless_insert_snippet_action, cursorless_positional_target, cursorless_insertion_snippet) | ||
|
||
{user.cursorless_insert_snippet_action} {user.cursorless_insertion_snippet_single_phrase} <user.text> [{user.cursorless_phrase_terminator}]: | ||
user.cursorless_insert_snippet_with_phrase(cursorless_insert_snippet_action, cursorless_insertion_snippet_single_phrase, text) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
from talon import Context, Module, actions, app | ||
|
||
from .csv_overrides import init_csv_and_watch_changes | ||
|
||
mod = Module() | ||
mod.list("cursorless_insert_snippet_action", desc="Cursorless insert snippet action") | ||
|
||
mod.tag( | ||
"cursorless_experimental_snippets", | ||
desc="tag for enabling experimental snippet support", | ||
) | ||
|
||
mod.list("cursorless_wrapper_snippet", desc="Cursorless wrapper snippet") | ||
mod.list( | ||
"cursorless_insertion_snippet_no_phrase", | ||
desc="Cursorless insertion snippets that don't accept a phrase", | ||
) | ||
mod.list( | ||
"cursorless_insertion_snippet_single_phrase", | ||
desc="Cursorless insertion snippet that can accept a single phrase", | ||
) | ||
mod.list("cursorless_phrase_terminator", "Contains term used to terminate a phrase") | ||
|
||
|
||
@mod.capture( | ||
rule="{user.cursorless_insertion_snippet_no_phrase} | {user.cursorless_insertion_snippet_single_phrase}" | ||
) | ||
def cursorless_insertion_snippet(m) -> str: | ||
try: | ||
return m.cursorless_insertion_snippet_no_phrase | ||
except AttributeError: | ||
pass | ||
|
||
return m.cursorless_insertion_snippet_single_phrase.split(".")[0] | ||
|
||
|
||
experimental_snippets_ctx = Context() | ||
experimental_snippets_ctx.matches = r""" | ||
tag: user.cursorless_experimental_snippets | ||
""" | ||
|
||
|
||
# NOTE: Please do not change these dicts. Use the CSVs for customization. | ||
# See https://www.cursorless.org/docs/user/customization/ | ||
wrapper_snippets = { | ||
"else": "ifElseStatement.alternative", | ||
"funk": "functionDeclaration.body", | ||
"if else": "ifElseStatement.consequence", | ||
"if": "ifStatement.consequence", | ||
"try": "tryCatchStatement.body", | ||
"link": "link.text", | ||
} | ||
|
||
# NOTE: Please do not change these dicts. Use the CSVs for customization. | ||
# See https://www.cursorless.org/docs/user/customization/ | ||
insertion_snippets_no_phrase = { | ||
"if": "ifStatement", | ||
"if else": "ifElseStatement", | ||
"try": "tryCatchStatement", | ||
} | ||
|
||
# NOTE: Please do not change these dicts. Use the CSVs for customization. | ||
# See https://www.cursorless.org/docs/user/customization/ | ||
insertion_snippets_single_phrase = { | ||
"funk": "functionDeclaration.name", | ||
"link": "link.text", | ||
} | ||
|
||
|
||
@mod.action_class | ||
class Actions: | ||
def cursorless_insert_snippet_with_phrase( | ||
action: str, snippet_description: str, text: str | ||
): | ||
"""Perform cursorless wrap action""" | ||
snippet_name, snippet_variable = snippet_description.split(".") | ||
actions.user.cursorless_implicit_target_command( | ||
action, snippet_name, {snippet_variable: text} | ||
) | ||
|
||
|
||
def on_ready(): | ||
init_csv_and_watch_changes( | ||
"experimental/wrapper_snippets", | ||
{ | ||
"wrapper_snippet": wrapper_snippets, | ||
}, | ||
allow_unknown_values=True, | ||
default_list_name="wrapper_snippet", | ||
ctx=experimental_snippets_ctx, | ||
) | ||
init_csv_and_watch_changes( | ||
"experimental/insertion_snippets", | ||
{ | ||
"insertion_snippet_no_phrase": insertion_snippets_no_phrase, | ||
}, | ||
allow_unknown_values=True, | ||
default_list_name="insertion_snippet_no_phrase", | ||
ctx=experimental_snippets_ctx, | ||
) | ||
init_csv_and_watch_changes( | ||
"experimental/insertion_snippets_single_phrase", | ||
{ | ||
"insertion_snippet_single_phrase": insertion_snippets_single_phrase, | ||
}, | ||
allow_unknown_values=True, | ||
default_list_name="insertion_snippet_single_phrase", | ||
ctx=experimental_snippets_ctx, | ||
) | ||
init_csv_and_watch_changes( | ||
"experimental/miscellaneous", | ||
{ | ||
"phrase_terminator": {"over": "phraseTerminator"}, | ||
}, | ||
ctx=experimental_snippets_ctx, | ||
) | ||
|
||
|
||
app.register("ready", on_ready) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.