Search code examples
sublimetext3spell-checkingsublime-text-plugin

Pop-up spelling suggestions context menu with spell checking in Sublime Text 3


I have coded a keybinding the the context menu with:

command: context_menu

to see the spelling suggestions but do to the way it goes to the next word and puts the cursor at the end of the word thid doesn't work. But if the cursor is inside the word or at the beginning of the word it does work. Is there an easy way to say go back one character then bring up the context menu?

Also is it possible to get ctrl+n and ctrl+p working in the context menu. Assume the context menu has the default bindings turned off.

Ended up using the suggestion of the answer below but just slightly different solution to bring you back to the beginning of the word after the command is run I did this:

    {
"keys": ["ctrl+alt+w"],
// Force ability to bring up context menu
"command": "chain",
"context": [ { "key": "selection_empty", "operator": "equal", "operand": true } ],
"args": {
    "commands": [
        // ["move", {"by":"word", "forward": false, "extend": false}],
        ["sbp_move_word", {"direction": -1}],
        ["context_menu"],
        ["move", {"by":"wordends", "forward": true, "extend": false}],
] }
},

// Do not use "find_under_expand" if selection is made
{
"keys": ["ctrl+alt+w"],
"command": "chain",
"context": [ { "key": "selection_empty", "operator": "equal", "operand": false } ],
"args": { "commands": [
            ["sbp_move_word", {"direction": -1}],
    // ["move", {"by":"word", "forward": false, "extend": false}],
    ["context_menu"], 
    ["move", {"by":"wordends", "forward": true, "extend": true}],
] }
},

For some reason I had problems using the regular move command but should work fine too so commented it out above the sublemacs one:


Solution

  • [1]

     

    Install:

    Chain Of Command

     



     

    [2]

     

    Add this to your key bindings:

    // Use "find_under_expand" if no selection is made
    {
            "keys": ["ctrl+super+alt+t"],
            "command": "chain",
            "context": [ { "key": "selection_empty", "operator": "equal", "operand": true } ],
            "args": {
                    "commands": [
                            ["find_under_expand"],
                            ["select_word_beginning"],
                            ["context_menu"],
            ] }
    },
    
    // Do not use "find_under_expand" if selection is made
    {
            "keys": ["ctrl+super+alt+t"],
            "command": "chain",
            "context": [ { "key": "selection_empty", "operator": "not_equal",   "operand": true } ],
            "args": {
                    "commands": [
                            ["select_word_beginning"],
                            ["context_menu"],
            ] }
    },
    

     

    The find_under_expand command will select the word at the caret, which enables context_menu to consistently execute with spelling suggestions.

    The second key-binding does not utilize find_under_expand, as this would cause multiple instances of your already selected text to be selected.

     



     

    [3]

     

    Save this as SelectWordBeginnings.py somewhere in your /Packages/ directory:

    import sublime, sublime_plugin
    
    class SelectWordBeginningCommand ( sublime_plugin.TextCommand ):
    
        def run ( self, edit ):
    
            view = self.view
            selections = view.sel()
    
            if len ( selections ) == 0:
                return
    
            selection = selections[0]
    
            if selection.a < selection.b:
                newSelection = sublime.Region ( selection.b, selection.a )
                view.selection.clear()
                view.selection.add ( newSelection )
    

      This command will invert selected regions so the caret is always at the beginning of the word.