Sublime Forum

Re-enter snippet field cycle

#1

When entering a snippet with multiple fields, I often find it necessary to break the field cycle (default Esc) so that I may enter another snippet or a completion using tab (otherwise tab takes me to the next field in the snippet). Having done this, I am of course no longer able to cycle through the fields of the original snippet.

I was wondering whether it was possible to somehow re-enter the fields of a snippet (or competition) that was previously exited?

0 Likes

#2

not that I know of, but why not just set

"auto_complete_with_fields": true,

in your preferences so you won’t need to cancel out the snippet mode to get completions?

0 Likes

#3

I did notice that setting, thanks for the suggestion. Completions aren’t the problem, rather inserting a snippet inside another.

0 Likes

#4

ah, understood. hmm, all I can suggest atm is that if you frequently embed one snippet in another, create a new snippet that will encompass them both

maybe vote for these in case it will inspire sublimehq to work on them:


0 Likes

#5

I could do that for the one or two most used, I suppose, but there are perhaps 10-15 snippets I use in LaTeX often and it would be v. nice to be able to use any within any other. Thanks for confirming that this probably isn’t possible.

What is interesting is that if I insert a snippet, then, in the first field (say), insert a snippet via the command palette (Ctrl+Shift+P on Windows) then things work nicely; hitting tab moves my cursor through the fields of the second snippet and then moves to the first (‘outer’) snippet.

Having a closer look, if the first snippet has 3 fields and the second 2, then on inserting the second snippet the status bar changes from ‘Field 1 of 3’ to ‘Field 1 of 5’. This suggests to me that it is possible to ‘nest’ snippets, only there isn’t a convenient way to insert them (as completing with tab will not complete the insert, but move to the next field).

0 Likes

#6

So essentially, I’m looking for an "auto_complete_with_fields": true, setting but for snippet insertions instead of the normal tab completions.

0 Likes

#7

Ooo, I am getting close now. The following keybinding inserts a specific snippet (without using tab).

{ "keys": ["alt+s"], "command": "insert_snippet", "args": {"name" : "Packages/User/snippet_name.sublime-snippet"}}

Is there a way I can generalise this keybinding to work with other snippets? In particular, is there anyway to use the preceding text as an argument of a command (so I could type snippet_name+Alt+s to trigger the snippet called snippet_name)?

0 Likes

#8

You would need a plugin to do this since it relies on dynamic content, but it’s definitely doable.

0 Likes

#9

I had some success with a plugin that would insert a snippet from the User/ directory, although didn’t know how to scale this up to include completions from Package completion files.

Giving up on that route for the time being, I noticed the use of the command insert_best_completion in the Default key bindings. This seemed to be exactly what I wanted, since if I type in the exact name of a completion/snippet I can be sure that the first item in the completion listing is the one I want. Indeed, with the keybinding
{ "keys": ["ctrl+tab"], "command": "insert_best_completion"}
(note that this overrides the command currently bound to ctrl+tab, "command": "next_view_in_stack")

I was able to nest as many user-defined snippets as I liked, and cycling through the fields in a stack-like manner. For some reason, this didn’t work with the completions provided by completion files. Looking at the unofficial docs, I find this for the command:

which is interesting (I couldn’t even find the command on the official reference). Does anyone know the reason for this useless tag or the failure of the command for completions defined in user files? I can provide an example of the command failing to work as expected.

Regardless, I have finally found a solution that works with both use defined snippets and other completions: the macro
[
{“command”: “auto_complete”},
{“command”: “commit_completion”}
]
When bound to a key press and invoked, these two commands in succession have the expected effect of the insert_best_completion command.

I am slightly perturbed by the appearance of the second command in the unofficial docs:

but have not encountered any issues so far.

0 Likes

#10

I should note that I have used the following settings throughout:
"auto_complete_commit_on_tab": true
"auto_complete_with_fields": true
Although I think the latter should perhaps get a probably useless tag; from what I can see the macro works with both set to false.

0 Likes

#11

the undocs command reference does mention that it is unfinished. If you find some commands useful, feel free to raise a PR to simply remove the irrelevant comments:

0 Likes

#12

For the record – you may also want to know about this keybinding trick. Remap tab to sth else as field switcher if there are fields to switch.

{
    "keys": [
      "alt+ctrl+k"
    ],
    "command": "next_field",
    "context": [
      {
        "key": "has_next_field",
        "operator": "equal",
        "operand": true
      }
    ]
  }
0 Likes

#13

Good point - my solution is only necessary if you wish to keep the ability to cycle through fields with tab (and assign the auto_complete+commit_completion macro to some other key binding).

0 Likes

#14

My fuller (tested and stable) solution is to have the convenient alt+ctrl[+shift]+k behave like [shift+]f3 in all contexts as expected:

  //--------------------------------------------------------------------
  // __For_nested_snippets
  //--------------------------------------------------------------------

{
    "keys": [
      "alt+ctrl+k"
    ],
    "command": "next_field",
    "context": [
      {
        "key": "has_next_field",
        "operator": "equal",
        "operand": true
      }
    ]
  },
  {
    "keys": [
      "alt+ctrl+shift+k"
    ],
    "command": "prev_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
      }
    ]
  },
    //--------------------------------------------------------------------
  // __Find_Next
  //--------------------------------------------------------------------

{
    "keys": [
      "alt+ctrl+k"
    ],
    "command": "find_next"
  },
  {
    "keys": [
      "alt+ctrl+shift+k"
    ],
    "command": "find_prev"
  },
    //--------------------------------------------------------------------
  // __Latex_commands
  //--------------------------------------------------------------------
  {
    "keys": [
      "shift+alt+ctrl+k"
    ],
    "command": "prev_bookmark",
    "context": [
      {
        "key": "selector",
        "operator": "equal",
        "operand": "text.tex.latex"
      }
    ]
  },
  {
    "keys": [
      "alt+ctrl+k"
    ],
    "command": "next_bookmark",
    "context": [
      {
        "key": "selector",
        "operator": "equal",
        "operand": "text.tex.latex"
      }
    ]
  },
0 Likes