VS 代码 - 选择多光标

VS Code - Selection to multi cursor

有谁知道如何将 vscode 编辑器 selection 范围变成多光标 selection?

例如在这一行: “在这条线之外,我想 select this|”

  1. 运行按键绑定 selection会像这样变成多光标 “在这条线之外,我想 | select 这个 |

假设您只处理一个选择:

const selection = vscode.window.activeTextEditor.selections[0];

const newSelection1 = new vscode.Selection(selection.start, selection.start);
const newSelection2 = new vscode.Selection(selection.end, selection.end);

vscode.window.activeTextEditor.selections = [newSelection1, newSelection2];

选择从左到右和从右到左会得到相同的结果。

我把它做成一个扩展:Convert Selection - 它适用于多个选择。

这里是扩展的完整代码:

const vscode = require('vscode');


/**
 * @param {vscode.ExtensionContext} context
 */
function activate(context) {

    let disposable = vscode.commands.registerCommand('convert-selection.surround', function () {

      const editor = vscode.window.activeTextEditor;
      const selections = editor.selections;
      const newSelections = [];
    
      for (const selection of selections) {
        const newSelection1 = new vscode.Selection(selection.start, selection.start);
        const newSelection2 = new vscode.Selection(selection.end, selection.end);
        newSelections.push(newSelection1, newSelection2);
      }

      editor.selections = newSelections;
    });

    context.subscriptions.push(disposable);
}

exports.activate = activate;

您可以使用扩展 Select By v1.10

执行命令:SelectBy:为选择的锚点和活动位置创建单独的光标selectby.anchorAndActiveSeparate

它将为所有当前选择的每个锚点和活动位置创建新光标。重叠游标被消除。