簡體   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