将 selection in Visual Studio 代码配置为 select 整个标识符

Configure expand selection in Visual Studio Code to select the whole identifier

VSCode 中第一次调用扩展选择(默认为 Alt-Shift-RightArrow)选择光标下的驼峰式子词。例如,当光标放在驼峰标识符(如 GetComponentsInChildren)内时,它会选择 "Components" 或 "Children"。

我已经在 C# 源代码中测试过了。我安装了 "C# for Visual Studio Code (powered by OmniSharp)" 扩展程序。

是否可以配置扩展选择以包括整个标识符 GetComponentsInChildren?我真的更喜欢它与其他导航和选择选项的行为一致(Ctrl-RightArrow - 跳到右边,Ctrl-D - 将选择添加到下一个查找匹配项)?

最初我认为您可以简单地为连续的 运行 2 smartSelect.expand 命令创建一个宏(如下面的注释代码所示)。这确实有效 - 除了像 someword 没有驼峰式的单个单词 - 然后第一个 grow 选择光标下的整个单词,第二个 grow 选择行或封闭块,而不是你想要什么。

"multiCommand.commands": [    // in settings.json

  {
    "command": "multiCommand.selectWord",
    "sequence": [

      // "editor.action.smartSelect.expand",
      // "editor.action.smartSelect.expand",

      "cursorWordStartLeft",
      "cursorWordRightSelect",
    ]
  }
]

所以我寻找另一种方法来做到这一点,我想我用上面的宏(使用多命令扩展)找到了它。这在 keybindings.json:

{
  "key": "shift+alt+right",    // disable so you can use the same keybinding
  "command": "-editor.action.smartSelect.expand",
  "when": "editorTextFocus"
},

{
  "key": "shift+alt+right",    // trigger the macro with the same keybinding if you wish
  "command": "extension.multiCommand.execute",
  "args": { "command": "multiCommand.selectWord" },
  "when": "editorTextFocus"
},

这种方法的缺点是您丢失了当前单词之外的 smartSelect.expand - 例如,它不会扩展到包含块。您决定这对您是否重要。如果您使用不同的键绑定,那么您不必禁用 smartSelect grow 命令,您可以同时使用这两个选项。


从 v1.44 开始,Add Selection to Next Find Match 的行为在计算单词定义的方式上发生了变化。也许它对你更好。

The command Add Selection to Next Find Match (kb(editor.action.addSelectionToNextFindMatch)) now respects the setting editor.wordSeparators. Previously, it used to pick up the word definition as defined by the current file's language.

有关于在 when 子句中添加更多内容的评论。这对我来说并不完全有效,但我能够添加到它以获得我期望的结果。

我的keybindings.json:

...
{
    "key": "cmd+up",    // disable so you can use the same keybinding
    "command": "-editor.action.smartSelect.expand",
    "when": "editorTextFocus"
},
{
    "key": "cmd+up", // when the editor has a word, grow the selection (this ignore camel case)
    "command": "extension.multiCommand.execute",
    "args": { "command": "editor.action.smartSelect.grow" },
    "when": "editorTextFocus && editorHasSelection"
},
{
    "key": "cmd+up", // when the editor doesn't have a word selected, select entire word, ignoring camelcase (editor.action.smartSelect.grow won't ignore camel case on first word select)
    "command": "extension.multiCommand.execute",
    "args": { "command": "multiCommand.selectWord" },
    "when": "editorTextFocus && !editorHasSelection"
}
...

我的settings.json(与原始答案相同):

...
"multiCommand.commands": [
    {
      "command": "multiCommand.selectWord",
      "sequence": [
        "cursorWordStartLeft",
        "cursorWordRightSelect",
      ]
    }
  ]
...