繁体   English   中英

在 vscode 扩展中更新 editor.selections

[英]Updating editor.selections in vscode Extension

我目前正在开发一个小的 vscode 扩展。 在扩展代码中,我想通过相应地更新保存 GUI 中当前存在的所有选择的TextEditor.selections数组来将多选的每个选择向右移动一个字符。

TextEditor.selection设置为新的vscode.Selection允许更新TextEditor.selections数组的主要选择。 这样,GUI 中的主要选择就会正确更新。 但是,将TextEditor.selections[n]设置为新选择不会导致 GUI 更新。 因此,我目前只能更新主要选择,而不是全部更新。 似乎设置速记TextEditor.selection会发布 GUI 更新,而设置TextEditor.selections[n]不会。

那么,如何更新TextEditor.selections中的所有选择?

这是我更新选择的代码:

import * as vscode from "vscode";

function updateSelections(
  editor: vscode.TextEditor,
  selections: vscode.Selection[]
) {
  selections.forEach((sel) => {
    const selStart = sel.start;
    const selEnd = sel.end;

    // TODO: only primary selection appears to be editable ; "editor.selections[n] = something" does not change anything
    // This works
    editor.selection = new vscode.Selection(
      selStart.translate(0, 1),
      selEnd.translate(0, 1)
    );
    // This does not work
    editor.selections[0] = new vscode.Selection(
      selStart.translate(0, 1),
      selEnd.translate(0, 1)
    );
  });
}

如果您使用更新的选择分配一个新数组,它会起作用

editor.selections = editor.selections.map( sel => new vscode.Selection(sel.start.translate(0,1), sel.end.translate(0,1)));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM