Sublime Forum

Completions within completions

#1

Hello. If you try to start a new completion, while within another completion, by pressing Tab or Ctrl-Space, ST tends to forget about the first completions’ fields. That is, tabbing will not resume the original completion. [At least, it used to do this for me…]

Create or add the following to your User - Key Bindings.

[code]
{ “keys”: “ctrl+alt+right”], “command”: “insert_best_completion”, “args”: {“default”: “”, “exact”: true} },
{ “keys”: “ctrl+alt+right”], “command”: “insert_best_completion”, “args”: {“default”: “”, “exact”: false},
“context”:

        { "key": "setting.tab_completion", "operator": "equal", "operand": true }
    ]
},
{ "keys": "ctrl+alt+right"], "command": "replace_completion_with_next_completion", "context":
    
        { "key": "last_command", "operator": "equal", "operand": "insert_best_completion" },
        { "key": "setting.tab_completion", "operator": "equal", "operand": true }
    ]
},
{ "keys": "ctrl+alt+left"], "command": "replace_completion_with_prev_completion", "context":
    
        { "key": "last_command", "operator": "equal", "operand": "insert_best_completion" },
        { "key": "setting.tab_completion", "operator": "equal", "operand": true }
    ]
},
// insert best completion for current word:
{ "keys": "ctrl+shift+c"], "command": "run_macro_file", 
    "args": {"file": "Packages/User/bestcomp.sublime-macro"} }

][/code]
Then create the following macro named bestcomp.sublime-macro.

{ "args": { "by": "characters", "forward": false }, "command": "move" }, { "args": { "by": "word_ends", "forward": true }, "command": "move" }, { "args": { "default": "", "exact": false }, "command": "insert_best_completion" } ]
[You can just copy the above to a new, blank file and save it with the correct name and extension.]

When you are within a completion type the name (or the first few letters) of the completion you wish to use and press Ctrl-Shift-C to insert this completion. You should then be able to Tab through the remaining fields. *

If you have pressed Ctrl-Shift-C and the wrong completion is assumed, press Ctrl-Alt-Left or Ctrl-Alt-Right to cycle backwards and forwards through other completions.

This has particular relevance to my jQuery completions file (NB it requires a jQuery syntax file!). If I use my ready() completion:

$(document).ready(handler)

the word handler is highlighted and I can press Ctrl-Shift-C to obtain:

$(document).ready(function (|) { | }|)|
with the tab/field positions as indicated by the pipes. This is because the words **handler **(and callback) are also completions within my jQuery completions file.

As a matter of fact, it tends to work with just the Ctrl-Alt-Left/Right shortcuts, without the need for the macro (Ctrl-Shift-C) file. But it’s nice to have alternatives :sunglasses:*

0 Likes