s-blog

编辑器 tab 关闭事件处理

ssssmy · 2026-06-05 · 2 min · VsCode插件开发

编辑器 tab 关闭时的处理逻辑——移除被关闭的 tab DOM、重算标签、重绘;无 tab 时清空容器。

src/vs/workbench/browser/parts/editor/tabsTitleControl.ts

private handleClosedEditors(): void {
  // There are tabs to show
  if (this.group.activeEditor) {
    const tabsContainer = assertIsDefined(this.tabsContainer);
    while (tabsContainer.children.length > this.group.count) {
      // 从容器移除一个 tab(必须是最后一个,以保持索引顺序)
      (tabsContainer.lastChild as HTMLElement).remove();
      dispose(this.tabDisposables.pop());
    }
    // 移除 label 后需要重算所有 label
    this.computeTabLabels();
    // 重绘所有 tab
    this.redraw();
  }
  // No tabs to show
  else {
    if (this.tabsContainer) {
      clearNode(this.tabsContainer);
    }
    this.tabDisposables = dispose(this.tabDisposables);
    this.tabResourceLabels.clear();
    this.tabLabels = [];
    this.tabActionBars = [];
    this.clearEditorActionsToolbar();
  }
}

原文链接:https://www.ssssmy.com/notes/mo-dou-ide-yuan-ma-bian-ji-qi-tab-guan-bi-shi-jian-chu-li