import type { Memento } from 'vscode' import { commands } from 'vscode' /** 开关存在 workspaceState 的哪个键、标题栏按哪个上下文键换着显示「隐藏 / 显示测试文件」。 */ export interface TestsToggleKeys { state: string context: string } /** * 一栏标题栏上「隐藏 / 显示测试文件」的开关:各栏各一个(主栏的审查、「未提交的更改」),按工作区记住; * 上下文键跟着开关走,标题栏按它换着显示两个按钮。 */ export class TestsToggle { private value: boolean /** * @param state workspaceState * @param keys 开关记在哪、上下文键叫什么 * @param changed 开关换了:这一栏按新开关重摆 */ constructor(private readonly state: Memento, private readonly keys: TestsToggleKeys, private readonly changed: () => void) { this.value = state.get(keys.state) ?? false void commands.executeCommand('setContext', keys.context, this.value) } /** 隐藏着测试文件:摆树时把测试文件剔掉 */ get hidden(): boolean { return this.value } /** 标题栏的「隐藏 / 显示测试文件」:换开关、按工作区记住,这一栏马上按新开关重摆。 */ async set(hidden: boolean): Promise { this.value = hidden this.changed() await Promise.all([ commands.executeCommand('setContext', this.keys.context, hidden), this.state.update(this.keys.state, hidden || undefined), ]) } }