Skip to content

Commit 2738ae8

Browse files
committed
feat: add numberOfTabs preference feature
1 parent 9447858 commit 2738ae8

File tree

1 file changed

+52
-12
lines changed
  • src/extensionsIntegrated/TabBar

1 file changed

+52
-12
lines changed

src/extensionsIntegrated/TabBar/main.js

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ define(function (require, exports, module) {
140140
getAllFilesFromWorkingSet();
141141

142142
// if no files are present in a pane, we want to hide the tab bar for that pane
143+
const $firstTabBar = $('#phoenix-tab-bar');
144+
const $secondTabBar = $('#phoenix-tab-bar-2');
145+
143146
if (firstPaneWorkingSet.length === 0 && ($('#phoenix-tab-bar'))) {
144147
Helper._hideTabBar($('#phoenix-tab-bar'));
145148
}
@@ -148,18 +151,56 @@ define(function (require, exports, module) {
148151
Helper._hideTabBar($('#phoenix-tab-bar-2'));
149152
}
150153

151-
// to add tabs one by one to the tab bar
152-
if (firstPaneWorkingSet.length > 0) {
153-
for (let i = 0; i < firstPaneWorkingSet.length; i++) {
154-
// Note: here we add the element to the tab bar directly and not the tab-container
155-
$('#phoenix-tab-bar').append(createTab(firstPaneWorkingSet[i]));
154+
// get the count of tabs that we want to display in the tab bar (from preference settings)
155+
// from preference settings or working set whichever smaller
156+
let tabsCountP1 = Math.min(firstPaneWorkingSet.length, Preference.tabBarNumberOfTabs);
157+
let tabsCountP2 = Math.min(secondPaneWorkingSet.length, Preference.tabBarNumberOfTabs);
158+
159+
// the value is generally '-1', but we check for less than 0 so that it can handle edge cases gracefully
160+
// if the value is negative then we display all tabs
161+
if (Preference.tabBarNumberOfTabs < 0) {
162+
tabsCountP1 = firstPaneWorkingSet.length;
163+
tabsCountP2 = secondPaneWorkingSet.length;
164+
}
165+
166+
// get the active editor and path once to reuse for both panes
167+
const activeEditor = EditorManager.getActiveEditor();
168+
const activePath = activeEditor ? activeEditor.document.file.fullPath : null;
169+
170+
// handle the first pane tabs
171+
if (firstPaneWorkingSet.length > 0 && tabsCountP1 > 0 && $firstTabBar.length) {
172+
// get the top n entries for the first pane
173+
let displayedEntries = firstPaneWorkingSet.slice(0, tabsCountP1);
174+
175+
// if the active file isn't already visible but exists in the working set, force-include it
176+
if (activePath && !displayedEntries.some(entry => entry.path === activePath)) {
177+
let activeEntry = firstPaneWorkingSet.find(entry => entry.path === activePath);
178+
if (activeEntry) {
179+
// replace the last tab with the active file.
180+
displayedEntries[displayedEntries.length - 1] = activeEntry;
181+
}
156182
}
183+
184+
// add each tab to the first pane's tab bar
185+
displayedEntries.forEach(function (entry) {
186+
$firstTabBar.append(createTab(entry));
187+
});
157188
}
158189

159-
if (secondPaneWorkingSet.length > 0) {
160-
for (let i = 0; i < secondPaneWorkingSet.length; i++) {
161-
$('#phoenix-tab-bar-2').append(createTab(secondPaneWorkingSet[i]));
190+
// for second pane tabs
191+
if (secondPaneWorkingSet.length > 0 && tabsCountP2 > 0 && $secondTabBar.length) {
192+
let displayedEntries2 = secondPaneWorkingSet.slice(0, tabsCountP2);
193+
194+
if (activePath && !displayedEntries2.some(entry => entry.path === activePath)) {
195+
let activeEntry = secondPaneWorkingSet.find(entry => entry.path === activePath);
196+
if (activeEntry) {
197+
displayedEntries2[displayedEntries2.length - 1] = activeEntry;
198+
}
162199
}
200+
201+
displayedEntries2.forEach(function (entry) {
202+
$secondTabBar.append(createTab(entry));
203+
});
163204
}
164205
}
165206

@@ -168,7 +209,7 @@ define(function (require, exports, module) {
168209
* Creates the tab bar and adds it to the DOM
169210
*/
170211
function createTabBar() {
171-
if (!Preference.tabBarEnabled) {
212+
if (!Preference.tabBarEnabled || Preference.numberOfTabs === 0) {
172213
return;
173214
}
174215

@@ -327,14 +368,13 @@ define(function (require, exports, module) {
327368
/**
328369
* This is called when the tab bar preference is changed
329370
* It takes care of creating or cleaning up the tab bar
330-
*
331-
* TODO: handle the number of tabs functionality
332371
*/
333372
function preferenceChanged() {
334373
Preference.tabBarEnabled = PreferencesManager.get(Preference.PREFERENCES_TAB_BAR).showTabBar;
335374
Preference.tabBarNumberOfTabs = PreferencesManager.get(Preference.PREFERENCES_TAB_BAR).numberOfTabs;
336375

337-
if (Preference.tabBarEnabled) {
376+
// preference should be enabled and number of tabs should be greater than 0
377+
if (Preference.tabBarEnabled && Preference.tabBarNumberOfTabs !== 0) {
338378
createTabBar();
339379
} else {
340380
cleanupTabBar();

0 commit comments

Comments
 (0)