From cc68aaa5abba9f9e40f8d2e0f06760cb6cac3e01 Mon Sep 17 00:00:00 2001 From: deathaxe Date: Sat, 17 May 2025 18:25:52 +0200 Subject: [PATCH 1/2] Describe passing custom arguments to snippets This commit adds description how to pass field or custom argument values to snippets using some small code examples. caused by https://forum.sublimetext.com/t/how-do-i-insert-a-snippet-with-paramn-variables-from-a-plugin/76200 --- docs/guide/extensibility/snippets.md | 90 +++++++++++++++++++++++++++- docs/reference/commands.md | 2 + 2 files changed, 90 insertions(+), 2 deletions(-) diff --git a/docs/guide/extensibility/snippets.md b/docs/guide/extensibility/snippets.md index cb733702..be639442 100644 --- a/docs/guide/extensibility/snippets.md +++ b/docs/guide/extensibility/snippets.md @@ -98,13 +98,12 @@ element's text in examples unless otherwise noted. ### Environment Variables Snippets have access to contextual information in the form of -environment variables. The values of the variables listed below are set +environment variables. The values of variables listed below are set automatically by Sublime Text. | Variable | Description | | -------------------- | --------------------------------------------------------------------- | -| `$PARAM1 .. $PARAMn` | Arguments passed to the `insert_snippet` command. (Not covered here.) | | `$SELECTION` | The text that was selected when the snippet was triggered. | | `$TM_CURRENT_LINE` | Content of the cursor's line when the snippet was triggered. | | `$TM_CURRENT_WORD` | Word under the cursor when the snippet was triggered. | @@ -295,3 +294,90 @@ Transformation: ${TM_FILENAME/(\w+)\.js/\1/g} Transformation: MyModule ``` + +### Passing custom arguments + +Command Palette items, key bindings and plugins +can pass values +of fields (`$1` or `${1:placeholder}`) +or custom arguments (`$any_variable`) +to snippets, +when inserting them via [insert_snippet][] command. + +The following examples insert `"Hello World!"` to buffer. + +Let's assume a generic snippet _Packages/User/My Snippet.sublime-snippet_ +which provides a field `$1` +and expects a custom argument `$subject`: + +```xml + + + Snippet with 2 fields + +``` + +It can be triggered by key bindings with static values being passed. + +```json +[ + { + "keys": ["ctrl+k", "ctrl+w"], + "command": "insert_snippet", + "args": { + "name": "Packages/User/My Snippet.sublime-snippet", + "1": "Hello", + "subject": "World" + } + } +] +``` + +Plugins can pass dynamically determined values. + +```py +import sublime_plugin + +class MyInsertSnippetCommand(sublime_plugin.TextCommand): + def run(self, edit): + field1 = "Hello" + + self.view.run_command( + cmd="insert_snippet", + args={ + "name": "Packages/User/My Snippet.sublime-snippet", + "1": field1, + "subject": self.get_subject() + } + ) + + def get_subject(self): + return "World" +``` + +Field values and arguments can also be passed to inline snippet content. + +```py +import sublime_plugin + +class MyInsertSnippetCommand(sublime_plugin.TextCommand): + def run(self, edit): + + snippet_content = "$1 $subject!" + + field1 = "Hello" + + self.view.run_command( + cmd="insert_snippet", + args={ + "contents": snippet_content, + "1": field1, + "subject": self.get_subject() + } + ) + + def get_subject(self): + return "World" +``` + +[insert_snippet]: ../../reference/commands#insert_snippet diff --git a/docs/reference/commands.md b/docs/reference/commands.md index 2f90a402..09c2be10 100644 --- a/docs/reference/commands.md +++ b/docs/reference/commands.md @@ -331,6 +331,8 @@ Inserts a snippet from a string or *.sublime-snippet* file. backslashes `\` have to be escaped, like in every other JSON string. - **name** (String): [Relative path][About Paths in Command Arguments] to the *.sublime-snippet* file to be inserted. +- **args** (String): a list of key-value pairs (e.g.: `"myparam": "value"`) + specifying values to replace arbitrary variables (e.g.: `$myparam`) with. ::: info See Also [Snippets][] From f9d40e73cae3de413d7e721b475f03835b83bc8f Mon Sep 17 00:00:00 2001 From: deathaxe Date: Sat, 17 May 2025 18:41:49 +0200 Subject: [PATCH 2/2] Add a tip about custom shell variables via metafiles --- docs/guide/extensibility/snippets.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/guide/extensibility/snippets.md b/docs/guide/extensibility/snippets.md index be639442..9241db0b 100644 --- a/docs/guide/extensibility/snippets.md +++ b/docs/guide/extensibility/snippets.md @@ -118,6 +118,14 @@ automatically by Sublime Text. | `$TM_SOFT_TABS` | `YES` if `translate_tabs_to_spaces` is true, otherwise `NO`. | | `$TM_TAB_SIZE` | Spaces per-tab (controlled by the `tab_size` option). | + +::: tip Note +Packages can define more, environment variables via [Shell Variables in Metafiles][shell-vars]. + +[shell-vars]: ../../reference/metadata#shell-variables +::: + + Let's see a simple example of a snippet using variables: ```perl