visual studio code - Does VSCode have a way to activate a keyboard shortcut only when the cursor is in a particular syntax eleme

I am trying to port some of my SublimeText keyboard bindings to VSCode, and I've run into somethin

I am trying to port some of my SublimeText keyboard bindings to VSCode, and I've run into something I don't know how to do. In SublimeText, I have the keyboard binding:

    // C/C++
    {"keys": [ "alt+enter"], 
     "command": "insert", "args": {"characters": "\"\n\""},
     "context": [
            { "key": "selector", 
              "operator": "equal", 
              "operand": "string.quoted.double.c",
              "match_all": true }, ]
    },

The idea is simply that if I'm in a double-quoted string, typing alt+enter will end the current string, type a newline, and then open a new string.

I have other keybindings for other languages, some with different types of quotes. For instance, python has single-quoted strings, so there's a similar keybinding for that, but it needs to insert single quote characters instead of double quote characters.

The fact that I can localize the key bindings to be within a particular sort of syntax element means that I can "overload" all of these key bindings, and the bindings themselves can be simple (i.e., just insert some characters). Not only that, but alt+enter is a relatively common keystroke to have a shortcut for, so the fact that the keybinding is only active in character strings allows me to use it without clobbering any other bindings for alt+enter.

I'd like to port this to VSCode, but I'm having a few problems. My current keybinding is:

    {
        "key": "cmd+alt+shift+enter",
        "command": "editor.action.insertSnippet",
        "args": {
            "name": "split string"
        }
    }

where I have a snippet whose body is [ "\"", "\"" ]. I'm using a snippet instead of the type command because I'm using an extension (Indent to Bracket) that makes sure that when I type a newline, it indents to the column with the current delimiters (the way SublimeText's indent_to_bracket would). The place I commonly use this keybinding is inside a function call, so this indentation is important to me. I'm no expert in how extensions work, but it seems to override the type command, and it does it in such a way that when I call the type command directly from the key binding, it bypasses the extension, so it doesn't indent the way it would if I had typed a newline. If I insert a snippet, though, the extension sees it and does the right thing.

If there's a way to do this without snippets, that would be great, so I don't have silly little snippets to maintain, but it's working the way it is.

My main concern is that I'd still like to enable this key binding only when the cursor is inside a particular type of string (single or double-quoted). That way, I wouldn't have to use some giant chord of characters to avoid conflicting with other key bindings, and I wouldn't have to type different things depending on what sort of string I'm in. After all, the syntax analysis colors the strings, so it must have parsed this information. I'd just like to make use of it in a "when" clause of my keyboard shortcut.

Is there a way to do this?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745351694a4623870.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信