Skip to content

Commit e6c6952

Browse files
committed
feat: add support for closing tabs to right & left in context menu
1 parent 5cbfd12 commit e6c6952

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

src/extensionsIntegrated/TabBar/more-options.js

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ define(function (require, exports, module) {
3737
const items = [
3838
Strings.CLOSE_TAB,
3939
Strings.CLOSE_ACTIVE_TAB,
40+
Strings.CLOSE_TABS_TO_THE_LEFT,
41+
Strings.CLOSE_TABS_TO_THE_RIGHT,
4042
Strings.CLOSE_ALL_TABS,
4143
Strings.CLOSE_UNMODIFIED_TABS,
4244
"---",
@@ -122,6 +124,78 @@ define(function (require, exports, module) {
122124
}
123125
}
124126

127+
/**
128+
* "CLOSE TABS TO THE LEFT"
129+
* This function is responsible for closing all tabs to the left of the right-clicked tab
130+
*
131+
* @param {String} filePath - path of the file that was right-clicked
132+
* @param {String} paneId - the id of the pane in which the file is present
133+
*/
134+
function handleCloseTabsToTheLeft(filePath, paneId) {
135+
if (!filePath) {
136+
return;
137+
}
138+
139+
let workingSet;
140+
workingSet = paneId === "first-pane" ? Global.firstPaneWorkingSet : Global.secondPaneWorkingSet;
141+
if (!workingSet) {
142+
return;
143+
}
144+
145+
// find the index of the current file in the working set
146+
const currentIndex = workingSet.findIndex(entry => entry.path === filePath);
147+
148+
if (currentIndex > 0) { // we only proceed if there are tabs to the left
149+
// get all files to the left of the current file
150+
const filesToClose = workingSet.slice(0, currentIndex);
151+
152+
// Close each file, starting from the rightmost [to avoid index shifts]
153+
for (let i = filesToClose.length - 1; i >= 0; i--) {
154+
const fileObj = FileSystem.getFileForPath(filesToClose[i].path);
155+
CommandManager.execute(
156+
Commands.FILE_CLOSE,
157+
{ file: fileObj, paneId: paneId }
158+
);
159+
}
160+
}
161+
}
162+
163+
164+
/**
165+
* "CLOSE TABS TO THE RIGHT"
166+
* This function is responsible for closing all tabs to the right of the right-clicked tab
167+
*
168+
* @param {String} filePath - path of the file that was right-clicked
169+
* @param {String} paneId - the id of the pane in which the file is present
170+
*/
171+
function handleCloseTabsToTheRight(filePath, paneId) {
172+
if (!filePath) {
173+
return;
174+
}
175+
176+
let workingSet;
177+
workingSet = paneId === "first-pane" ? Global.firstPaneWorkingSet : Global.secondPaneWorkingSet;
178+
if (!workingSet) {
179+
return;
180+
}
181+
182+
// get the index of the current file in the working set
183+
const currentIndex = workingSet.findIndex(entry => entry.path === filePath);
184+
// only proceed if there are tabs to the right
185+
if (currentIndex !== -1 && currentIndex < workingSet.length - 1) {
186+
// get all files to the right of the current file
187+
const filesToClose = workingSet.slice(currentIndex + 1);
188+
189+
for (let i = filesToClose.length - 1; i >= 0; i--) {
190+
const fileObj = FileSystem.getFileForPath(filesToClose[i].path);
191+
CommandManager.execute(
192+
Commands.FILE_CLOSE,
193+
{ file: fileObj, paneId: paneId }
194+
);
195+
}
196+
}
197+
}
198+
125199

126200
/**
127201
* "REOPEN CLOSED FILE"
@@ -187,14 +261,22 @@ define(function (require, exports, module) {
187261
handleCloseActiveTab();
188262
break;
189263
case 2:
264+
// Close tabs to the left
265+
handleCloseTabsToTheLeft(filePath, paneId);
266+
break;
267+
case 3:
268+
// Close tabs to the right
269+
handleCloseTabsToTheRight(filePath, paneId);
270+
break;
271+
case 4:
190272
// Close all tabs
191273
handleCloseAllTabs();
192274
break;
193-
case 3:
275+
case 5:
194276
// Close unmodified tabs
195277
handleCloseUnmodifiedTabs();
196278
break;
197-
case 5:
279+
case 6:
198280
// Reopen closed file
199281
reopenClosedFile();
200282
break;

src/nls/root/strings.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,8 @@ define({
429429
// Tab bar Strings
430430
"CLOSE_TAB": "Close Tab",
431431
"CLOSE_ACTIVE_TAB": "Close Active Tab",
432+
"CLOSE_TABS_TO_THE_RIGHT": "Close Tabs to the Right",
433+
"CLOSE_TABS_TO_THE_LEFT": "Close Tabs to the Left",
432434
"CLOSE_ALL_TABS": "Close All Tabs",
433435
"CLOSE_UNMODIFIED_TABS": "Close Unmodified Tabs",
434436
"REOPEN_CLOSED_FILE": "Reopen Closed File",

0 commit comments

Comments
 (0)