From a35301f74d3be65f19c7fd5a970834ca04fd1050 Mon Sep 17 00:00:00 2001 From: vycdev2 Date: Mon, 10 Aug 2026 03:11:28 +0000 Subject: [PATCH] fix: apply live statistics statusbar layout settings --- CHANGELOG.md | 1 + src/statusbars/statistics.ts | 38 ++++++++-- test/statusbar-layout-settings.spec.ts | 97 ++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 4 deletions(-) create mode 100644 test/statusbar-layout-settings.spec.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 8aa4c24..e48b319 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ### Version 5.3.0 (Unreleased) +- Fixed the statistics status bar to apply `todo.statistics.statusbar.alignment` and `todo.statistics.statusbar.priority` changes without requiring an extension reload. - Fixed fulfilled promise batches so errors in optional rejection observers do not discard successful results. - Fixed dependency indexing so one unreadable or removed Todo file no longer hides dependencies from other workspace files. - Fixed embedded todo parsing for file paths that start with a number. diff --git a/src/statusbars/statistics.ts b/src/statusbars/statistics.ts index 9bc1121..1761035 100644 --- a/src/statusbars/statistics.ts +++ b/src/statusbars/statistics.ts @@ -11,6 +11,10 @@ class Statistics { itemProps; config; tokens; + layout: { + alignment: vscode.StatusBarAlignment; + priority: number; + }; constructor() { this.item = this._initItem(); @@ -19,14 +23,39 @@ class Statistics { this.update(); } - _initItem() { - const alignment = + _getLayout() { + return { + alignment: Config.getKey('statistics.statusbar.alignment') === 'right' ? vscode.StatusBarAlignment.Right : vscode.StatusBarAlignment.Left, - priority = Config.getKey('statistics.statusbar.priority'); + priority: Config.getKey('statistics.statusbar.priority'), + }; + } + + _initItem() { + const layout = this._getLayout(); + + this.layout = layout; + + return vscode.window.createStatusBarItem(layout.alignment, layout.priority); + } + + _updateLayout() { + const layout = this._getLayout(); + + if ( + this.layout && + this.layout.alignment === layout.alignment && + this.layout.priority === layout.priority + ) + return; - return vscode.window.createStatusBarItem(alignment, priority); + const previousItem = this.item; + + this.item = this._initItem(); + this.itemProps = {}; + if (previousItem) previousItem.dispose(); } _setItemProp(prop, value, _set = true) { @@ -42,6 +71,7 @@ class Statistics { } update() { + this._updateLayout(); this.config = Config.get(); this.tokens = Utils.statistics.tokens.global; diff --git a/test/statusbar-layout-settings.spec.ts b/test/statusbar-layout-settings.spec.ts new file mode 100644 index 0000000..6f8e1e5 --- /dev/null +++ b/test/statusbar-layout-settings.spec.ts @@ -0,0 +1,97 @@ +import { expect } from 'chai'; + +function loadStatistics(settings) { + const NodeModule = require('module'); + const originalLoad = NodeModule._load; + const statusItems = []; + const config = { + getKey(key) { + return settings[key]; + }, + get() { + return { + statistics: { + statusbar: { + alignment: settings.alignment, + priority: settings.priority, + enabled: true, + color: '', + command: '', + text: '', + tooltip: '', + }, + }, + }; + }, + }; + const vscode = { + StatusBarAlignment: { Left: 'left', Right: 'right' }, + window: { + createStatusBarItem(alignment, priority) { + const item = { + alignment, + priority, + show() {}, + hide() {}, + dispose() { + this.disposed = true; + }, + }; + statusItems.push(item); + return item; + }, + activeTextEditor: {}, + }, + }; + const utils = { + editor: { isSupported: () => true }, + statistics: { + tokens: { global: {} }, + condition: { is: () => true }, + template: { render: (value) => value }, + }, + }; + const subjectPath = require.resolve('../src/statusbars/statistics'); + const cached = require.cache[subjectPath]; + + NodeModule._load = function (request, parent, isMain) { + if (request === 'vscode') return vscode; + if (request === '../config') return { default: config }; + if (request === '../utils') return { default: utils }; + + return originalLoad.call(this, request, parent, isMain); + }; + + try { + delete require.cache[subjectPath]; + return { instance: require(subjectPath).default, statusItems }; + } finally { + NodeModule._load = originalLoad; + delete require.cache[subjectPath]; + if (cached) require.cache[subjectPath] = cached; + } +} + +describe('Status bar layout settings', () => { + it('applies alignment and priority changes to the live statistics item', () => { + const settings = { + alignment: 'left', + priority: 1, + 'statistics.statusbar.alignment': 'left', + 'statistics.statusbar.priority': 1, + 'statistics.statusbar.enabled': true, + }; + const { instance, statusItems } = loadStatistics(settings); + + settings.alignment = 'right'; + settings.priority = 42; + settings['statistics.statusbar.alignment'] = 'right'; + settings['statistics.statusbar.priority'] = 42; + instance.update(); + + expect(statusItems).to.have.length(2); + expect(statusItems[0].disposed).to.equal(true); + expect(statusItems[1].alignment).to.equal('right'); + expect(statusItems[1].priority).to.equal(42); + }); +});