ST3 swap priority of tab function within a snippet (nested snippets)

孤街浪徒 提交于 2019-12-24 01:46:08

问题


Find myself frequently invoking a snippet within a snippet, but of course when I go to expend the nested snippet, the tab key moves me to either the next entry of the first snippet or the end of the first snippet (at which point, I have to replace the cursor at the end of the tab-trigger expression for the 2nd snippet and hit tab, at which point the 2nd snippet is expanded).

eg. given snippet [ content A ${1:First point in A} and ${2: Second point in A} ] with tab-trigger tabtrigA and snippet [ content B ] with tab-trigger tabtrigB

I'd like to be able to do the following:

In[1]:

tabtrigA % Hit tab to expand snippet A

Out[1]:

 [ content **A First point in A** and ${2: Second point in A} ] % where everything between ** ** is highlighted

Now replace **...** content with tabtrigB

In[2]:

     [ content tabtrigB* and ${2: Second point in A} ] % where * marks location of cursor.

and hitting tab would result in:

Out[2]:

 [ content [ content B ]* and ${2: Second point in A} ] % where * marks location of cursor

and hitting tab again would then jump to second entry of snippet A

Obviously this is tiresome: is it possible to switch the priority assignment of tab so that it first acts as a tab-trigger and only jumps to next entry if there is no tab-trigger?


Update: as of April 2019, still no solution for triggering a snippet within a snippet.


回答1:


I don't think sublime can tell this snippets' next_field from that snippet's next_field. You can only ask if it has_next_field, any. But you could go with workarounds:


Trigger your nested snippet with something else than TAB:

  1. Through command palette by giving your nested snippet a description. The snippet below will be callable from the palette as Snippet: description_for_command_palette.


<snippet>
<content><![CDATA[
[ content B ]
]]></content>
<description>description_for_command_palette</description>
</snippet>
  1. Through keybinding to the snippet path:

{ "keys": ["ctrl+0"], "command": "insert_snippet", "args": {"name": "Packages/User/your_snippet.sublime-snippet"}}

  1. Through a keybinding to an anonymous snippet:

{ "keys": ["ctrl+0"], "command": "insert_snippet", "args": {"contents": "[ content B ]"}}


Overwrite the keybinding to go to the next field in snippet to, say, enter.

Just add two keybindings:

{ "keys": ["enter"], "command": "next_field", "context":
[
{ "key": "has_next_field", "operator": "equal", "operand": true }
]
},
{ "keys": ["tab"], "command": "insert_best_completion", "args": {"default": "\t", "exact": false}, "context":
[
{ "key": "has_next_field", "operator": "equal", "operand": true }
]
}


来源:https://stackoverflow.com/questions/43563431/st3-swap-priority-of-tab-function-within-a-snippet-nested-snippets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!