Skip to content
This repository was archived by the owner on May 15, 2026. It is now read-only.

Commit 8722906

Browse files
committed
feat: implement auto-close Roo tabs feature
- Add autoCloseRooTabs and autoCloseAllRooTabs settings to global settings - Create PostEditBehaviorUtils class to handle tab closing logic - Update DiffViewProvider to track Roo-opened tabs and close them after save - Add FileEditingOptions component for settings UI - Update SettingsView and webviewMessageHandler to support new settings - Add tests for PostEditBehaviorUtils (some mocking issues to be resolved) Closes #6003
1 parent 9fce90b commit 8722906

10 files changed

Lines changed: 600 additions & 5 deletions

File tree

‎packages/types/src/global-settings.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ export const globalSettingsSchema = z.object({
7272
autoCondenseContextPercent: z.number().optional(),
7373
maxConcurrentFileReads: z.number().optional(),
7474

75+
// File editing options
76+
autoCloseRooTabs: z.boolean().optional(),
77+
autoCloseAllRooTabs: z.boolean().optional(),
78+
7579
browserToolEnabled: z.boolean().optional(),
7680
browserViewportSize: z.string().optional(),
7781
screenshotQuality: z.number().optional(),

‎src/core/task/Task.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,13 @@ export class Task extends EventEmitter<ClineEvents> {
263263
this.diffViewProvider = new DiffViewProvider(this.cwd)
264264
this.enableCheckpoints = enableCheckpoints
265265

266+
// Set up auto-close settings for DiffViewProvider
267+
provider.getState().then((state) => {
268+
const autoCloseRooTabs = (state as any).autoCloseRooTabs ?? false
269+
const autoCloseAllRooTabs = (state as any).autoCloseAllRooTabs ?? false
270+
this.diffViewProvider.setAutoCloseSettings(autoCloseRooTabs, autoCloseAllRooTabs)
271+
})
272+
266273
this.rootTask = rootTask
267274
this.parentTask = parentTask
268275
this.taskNumber = taskNumber

‎src/core/webview/webviewMessageHandler.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,14 @@ export const webviewMessageHandler = async (
10441044
await updateGlobalState("writeDelayMs", message.value)
10451045
await provider.postStateToWebview()
10461046
break
1047+
case "autoCloseRooTabs":
1048+
await updateGlobalState("autoCloseRooTabs", message.bool ?? false)
1049+
await provider.postStateToWebview()
1050+
break
1051+
case "autoCloseAllRooTabs":
1052+
await updateGlobalState("autoCloseAllRooTabs", message.bool ?? false)
1053+
await provider.postStateToWebview()
1054+
break
10471055
case "diagnosticsEnabled":
10481056
await updateGlobalState("diagnosticsEnabled", message.bool ?? true)
10491057
await provider.postStateToWebview()

‎src/integrations/editor/DiffViewProvider.ts‎

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { Task } from "../../core/task/Task"
1515
import { DEFAULT_WRITE_DELAY_MS } from "@roo-code/types"
1616

1717
import { DecorationController } from "./DecorationController"
18+
import { PostEditBehaviorUtils } from "./PostEditBehaviorUtils"
1819

1920
export const DIFF_VIEW_URI_SCHEME = "cline-diff"
2021
export const DIFF_VIEW_LABEL_CHANGES = "Original ↔ Roo's Changes"
@@ -36,15 +37,47 @@ export class DiffViewProvider {
3637
private activeLineController?: DecorationController
3738
private streamedLines: string[] = []
3839
private preDiagnostics: [vscode.Uri, vscode.Diagnostic[]][] = []
40+
private rooOpenedTabs: Set<string> = new Set()
41+
private preEditActiveEditor?: vscode.TextEditor
42+
private autoCloseRooTabs: boolean = false
43+
private autoCloseAllRooTabs: boolean = false
3944

4045
constructor(private cwd: string) {}
4146

47+
/**
48+
* Set the auto-close settings for this DiffViewProvider instance
49+
*/
50+
setAutoCloseSettings(autoCloseRooTabs: boolean, autoCloseAllRooTabs: boolean) {
51+
this.autoCloseRooTabs = autoCloseRooTabs
52+
this.autoCloseAllRooTabs = autoCloseAllRooTabs
53+
}
54+
55+
/**
56+
* Track a tab that Roo opened
57+
*/
58+
trackOpenedTab(filePath: string) {
59+
this.rooOpenedTabs.add(filePath)
60+
}
61+
62+
/**
63+
* Clear all tracked tabs
64+
*/
65+
clearTrackedTabs() {
66+
this.rooOpenedTabs.clear()
67+
}
68+
4269
async open(relPath: string): Promise<void> {
4370
this.relPath = relPath
4471
const fileExists = this.editType === "modify"
4572
const absolutePath = path.resolve(this.cwd, relPath)
4673
this.isEditing = true
4774

75+
// Store the currently active editor before we start making changes
76+
this.preEditActiveEditor = vscode.window.activeTextEditor
77+
78+
// Track that Roo is opening this file
79+
this.trackOpenedTab(absolutePath)
80+
4881
// If the file is already open, ensure it's not dirty before getting its
4982
// contents.
5083
if (fileExists) {
@@ -181,7 +214,10 @@ export class DiffViewProvider {
181214
}
182215
}
183216

184-
async saveChanges(diagnosticsEnabled: boolean = true, writeDelayMs: number = DEFAULT_WRITE_DELAY_MS): Promise<{
217+
async saveChanges(
218+
diagnosticsEnabled: boolean = true,
219+
writeDelayMs: number = DEFAULT_WRITE_DELAY_MS,
220+
): Promise<{
185221
newProblemsMessage: string | undefined
186222
userEdits: string | undefined
187223
finalContent: string | undefined
@@ -201,6 +237,17 @@ export class DiffViewProvider {
201237
await vscode.window.showTextDocument(vscode.Uri.file(absolutePath), { preview: false, preserveFocus: true })
202238
await this.closeAllDiffViews()
203239

240+
// Apply post-edit behaviors (tab closing)
241+
await PostEditBehaviorUtils.closeRooTabs(
242+
this.autoCloseRooTabs,
243+
this.autoCloseAllRooTabs,
244+
this.rooOpenedTabs,
245+
absolutePath,
246+
)
247+
248+
// Restore focus after closing tabs
249+
await PostEditBehaviorUtils.restoreFocus(this.preEditActiveEditor, absolutePath)
250+
204251
// Getting diagnostics before and after the file edit is a better approach than
205252
// automatically tracking problems in real-time. This method ensures we only
206253
// report new problems that are a direct result of this specific edit.
@@ -216,22 +263,22 @@ export class DiffViewProvider {
216263
// and can address them accordingly. If problems don't change immediately after
217264
// applying a fix, won't be notified, which is generally fine since the
218265
// initial fix is usually correct and it may just take time for linters to catch up.
219-
266+
220267
let newProblemsMessage = ""
221-
268+
222269
if (diagnosticsEnabled) {
223270
// Add configurable delay to allow linters time to process and clean up issues
224271
// like unused imports (especially important for Go and other languages)
225272
// Ensure delay is non-negative
226273
const safeDelayMs = Math.max(0, writeDelayMs)
227-
274+
228275
try {
229276
await delay(safeDelayMs)
230277
} catch (error) {
231278
// Log error but continue - delay failure shouldn't break the save operation
232279
console.warn(`Failed to apply write delay: ${error}`)
233280
}
234-
281+
235282
const postDiagnostics = vscode.languages.getDiagnostics()
236283

237284
const newProblems = await diagnosticsToProblemsString(
@@ -610,5 +657,7 @@ export class DiffViewProvider {
610657
this.activeLineController = undefined
611658
this.streamedLines = []
612659
this.preDiagnostics = []
660+
this.clearTrackedTabs()
661+
this.preEditActiveEditor = undefined
613662
}
614663
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import * as vscode from "vscode"
2+
import { arePathsEqual } from "../../utils/path"
3+
import { DIFF_VIEW_URI_SCHEME, DIFF_VIEW_LABEL_CHANGES } from "./DiffViewProvider"
4+
5+
export class PostEditBehaviorUtils {
6+
/**
7+
* Closes tabs based on the provided settings and tracked tabs
8+
* @param autoCloseRooTabs - Close only tabs that were not open prior to the current task
9+
* @param autoCloseAllRooTabs - Close all Roo tabs regardless of prior state
10+
* @param rooOpenedTabs - Set of file paths that Roo opened during the current task
11+
* @param editedFilePath - The path of the file that was just edited (optional)
12+
* @returns Promise that resolves when all tabs are closed
13+
*/
14+
static async closeRooTabs(
15+
autoCloseRooTabs: boolean,
16+
autoCloseAllRooTabs: boolean,
17+
rooOpenedTabs: Set<string>,
18+
editedFilePath?: string,
19+
): Promise<void> {
20+
if (!autoCloseRooTabs && !autoCloseAllRooTabs) {
21+
return
22+
}
23+
24+
const tabsToClose: vscode.Tab[] = []
25+
26+
// Iterate through all tab groups and their tabs
27+
for (const tabGroup of vscode.window.tabGroups.all) {
28+
for (const tab of tabGroup.tabs) {
29+
if (this.shouldCloseTab(tab, autoCloseRooTabs, autoCloseAllRooTabs, rooOpenedTabs, editedFilePath)) {
30+
tabsToClose.push(tab)
31+
}
32+
}
33+
}
34+
35+
// Close all identified tabs
36+
if (tabsToClose.length > 0) {
37+
await Promise.all(
38+
tabsToClose.map(async (tab) => {
39+
try {
40+
await vscode.window.tabGroups.close(tab)
41+
} catch (err) {
42+
console.error(`Failed to close tab: ${err}`)
43+
}
44+
}),
45+
)
46+
}
47+
}
48+
49+
/**
50+
* Determines if a tab should be closed based on the settings and tab properties
51+
*/
52+
private static shouldCloseTab(
53+
tab: vscode.Tab,
54+
autoCloseRooTabs: boolean,
55+
autoCloseAllRooTabs: boolean,
56+
rooOpenedTabs: Set<string>,
57+
editedFilePath?: string,
58+
): boolean {
59+
// Don't close dirty tabs
60+
if (tab.isDirty) {
61+
return false
62+
}
63+
64+
// Check if this is a diff view tab
65+
if (tab.input instanceof vscode.TabInputTextDiff) {
66+
// Check if it's a Roo diff view by URI scheme
67+
if (tab.input.original.scheme === DIFF_VIEW_URI_SCHEME) {
68+
return autoCloseAllRooTabs || autoCloseRooTabs
69+
}
70+
// Also check by label for compatibility
71+
if (tab.label.includes(DIFF_VIEW_LABEL_CHANGES)) {
72+
return autoCloseAllRooTabs || autoCloseRooTabs
73+
}
74+
}
75+
76+
// Check if this is a regular text tab
77+
if (tab.input instanceof vscode.TabInputText) {
78+
const tabPath = tab.input.uri.fsPath
79+
80+
// Skip the currently edited file to avoid closing it immediately
81+
if (editedFilePath && arePathsEqual(tabPath, editedFilePath)) {
82+
return false
83+
}
84+
85+
// If autoCloseAllRooTabs is enabled, close any tab that was tracked by Roo
86+
if (autoCloseAllRooTabs) {
87+
// Check if this tab's path exists in our tracked set
88+
for (const trackedPath of rooOpenedTabs) {
89+
if (arePathsEqual(tabPath, trackedPath)) {
90+
return true
91+
}
92+
}
93+
}
94+
95+
// If only autoCloseRooTabs is enabled, close tabs that Roo opened (not pre-existing)
96+
if (autoCloseRooTabs && !autoCloseAllRooTabs) {
97+
// This requires the tab to be in our tracked set
98+
for (const trackedPath of rooOpenedTabs) {
99+
if (arePathsEqual(tabPath, trackedPath)) {
100+
return true
101+
}
102+
}
103+
}
104+
}
105+
106+
return false
107+
}
108+
109+
/**
110+
* Restores focus to the appropriate editor after closing tabs
111+
* @param preEditActiveEditor - The editor that was active before the edit operation
112+
* @param editedFilePath - The path of the file that was edited
113+
*/
114+
static async restoreFocus(
115+
preEditActiveEditor: vscode.TextEditor | undefined,
116+
editedFilePath?: string,
117+
): Promise<void> {
118+
// Try to restore focus to the pre-edit active editor if it still exists
119+
if (preEditActiveEditor) {
120+
const stillExists = vscode.window.visibleTextEditors.some(
121+
(editor) => editor.document.uri.toString() === preEditActiveEditor.document.uri.toString(),
122+
)
123+
124+
if (stillExists) {
125+
await vscode.window.showTextDocument(preEditActiveEditor.document, {
126+
preserveFocus: false,
127+
preview: false,
128+
})
129+
return
130+
}
131+
}
132+
133+
// Otherwise, try to focus on the edited file
134+
if (editedFilePath) {
135+
try {
136+
await vscode.window.showTextDocument(vscode.Uri.file(editedFilePath), {
137+
preserveFocus: false,
138+
preview: false,
139+
})
140+
} catch (err) {
141+
// File might not exist or be accessible
142+
console.debug(`Could not restore focus to edited file: ${err}`)
143+
}
144+
}
145+
}
146+
}

0 commit comments

Comments
 (0)