【问题标题】:Updating editor.selections in vscode Extension在 vscode 扩展中更新 editor.selections
【发布时间】:2020-05-21 11:05:23
【问题描述】:

我目前正在开发一个小型 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)
    );
  });
}

【问题讨论】:

    标签: typescript visual-studio-code vscode-extensions


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-26
      • 1970-01-01
      • 2018-06-17
      • 1970-01-01
      • 2020-05-30
      • 1970-01-01
      相关资源
      最近更新 更多