@@ -13,6 +13,7 @@ import { diagnosticsToProblemsString, getNewDiagnostics } from "../diagnostics"
1313import { ClineSayTool } from "../../shared/ExtensionMessage"
1414import { Task } from "../../core/task/Task"
1515import { DEFAULT_WRITE_DELAY_MS } from "@roo-code/types"
16+ import { ClineProvider } from "../../core/webview/ClineProvider"
1617
1718import { DecorationController } from "./DecorationController"
1819
@@ -36,8 +37,16 @@ export class DiffViewProvider {
3637 private activeLineController ?: DecorationController
3738 private streamedLines : string [ ] = [ ]
3839 private preDiagnostics : [ vscode . Uri , vscode . Diagnostic [ ] ] [ ] = [ ]
39-
40- constructor ( private cwd : string ) { }
40+ private providerRef ?: WeakRef < ClineProvider >
41+
42+ constructor (
43+ private cwd : string ,
44+ provider ?: ClineProvider ,
45+ ) {
46+ if ( provider ) {
47+ this . providerRef = new WeakRef ( provider )
48+ }
49+ }
4150
4251 async open ( relPath : string ) : Promise < void > {
4352 this . relPath = relPath
@@ -181,7 +190,10 @@ export class DiffViewProvider {
181190 }
182191 }
183192
184- async saveChanges ( diagnosticsEnabled : boolean = true , writeDelayMs : number = DEFAULT_WRITE_DELAY_MS ) : Promise < {
193+ async saveChanges (
194+ diagnosticsEnabled : boolean = true ,
195+ writeDelayMs : number = DEFAULT_WRITE_DELAY_MS ,
196+ ) : Promise < {
185197 newProblemsMessage : string | undefined
186198 userEdits : string | undefined
187199 finalContent : string | undefined
@@ -216,22 +228,22 @@ export class DiffViewProvider {
216228 // and can address them accordingly. If problems don't change immediately after
217229 // applying a fix, won't be notified, which is generally fine since the
218230 // initial fix is usually correct and it may just take time for linters to catch up.
219-
231+
220232 let newProblemsMessage = ""
221-
233+
222234 if ( diagnosticsEnabled ) {
223235 // Add configurable delay to allow linters time to process and clean up issues
224236 // like unused imports (especially important for Go and other languages)
225237 // Ensure delay is non-negative
226238 const safeDelayMs = Math . max ( 0 , writeDelayMs )
227-
239+
228240 try {
229241 await delay ( safeDelayMs )
230242 } catch ( error ) {
231243 // Log error but continue - delay failure shouldn't break the save operation
232244 console . warn ( `Failed to apply write delay: ${ error } ` )
233245 }
234-
246+
235247 const postDiagnostics = vscode . languages . getDiagnostics ( )
236248
237249 const newProblems = await diagnosticsToProblemsString (
@@ -461,6 +473,53 @@ export class DiffViewProvider {
461473 return editor
462474 }
463475
476+ // Determine the view column based on the openTabsInCorrectGroup setting
477+ let targetViewColumn = vscode . ViewColumn . Active
478+
479+ // Check if we should open in the same group as the original file
480+ const provider = this . providerRef ?. deref ( )
481+ if ( provider ) {
482+ const state = await provider . getState ( )
483+ const openTabsInCorrectGroup = state ?. openTabsInCorrectGroup ?? false
484+
485+ if ( openTabsInCorrectGroup ) {
486+ // Find which tab group contains the original file
487+ const originalFileTab = vscode . window . tabGroups . all
488+ . flatMap ( ( group ) => group . tabs . map ( ( tab ) => ( { tab, group } ) ) )
489+ . find (
490+ ( { tab } ) =>
491+ tab . input instanceof vscode . TabInputText && arePathsEqual ( tab . input . uri . fsPath , uri . fsPath ) ,
492+ )
493+
494+ if ( originalFileTab ) {
495+ // Use the view column of the group containing the original file
496+ targetViewColumn = originalFileTab . group . viewColumn
497+ } else {
498+ // If the original file isn't open, try to find the most logical group
499+ // This could be the group with the most related files (same directory)
500+ const fileDir = path . dirname ( uri . fsPath )
501+ const groupsWithRelatedFiles = vscode . window . tabGroups . all . map ( ( group ) => {
502+ const relatedFilesCount = group . tabs . filter ( ( tab ) => {
503+ if ( tab . input instanceof vscode . TabInputText ) {
504+ const tabDir = path . dirname ( tab . input . uri . fsPath )
505+ return tabDir === fileDir
506+ }
507+ return false
508+ } ) . length
509+ return { group, relatedFilesCount }
510+ } )
511+
512+ // Sort by most related files
513+ groupsWithRelatedFiles . sort ( ( a , b ) => b . relatedFilesCount - a . relatedFilesCount )
514+
515+ // Use the group with the most related files, or fall back to active
516+ if ( groupsWithRelatedFiles . length > 0 && groupsWithRelatedFiles [ 0 ] . relatedFilesCount > 0 ) {
517+ targetViewColumn = groupsWithRelatedFiles [ 0 ] . group . viewColumn
518+ }
519+ }
520+ }
521+ }
522+
464523 // Open new diff editor.
465524 return new Promise < vscode . TextEditor > ( ( resolve , reject ) => {
466525 const fileName = path . basename ( uri . fsPath )
@@ -523,7 +582,7 @@ export class DiffViewProvider {
523582 // Pre-open the file as a text document to ensure it doesn't open in preview mode
524583 // This fixes issues with files that have custom editor associations (like markdown preview)
525584 vscode . window
526- . showTextDocument ( uri , { preview : false , viewColumn : vscode . ViewColumn . Active , preserveFocus : true } )
585+ . showTextDocument ( uri , { preview : false , viewColumn : targetViewColumn , preserveFocus : true } )
527586 . then ( ( ) => {
528587 // Execute the diff command after ensuring the file is open as text
529588 return vscode . commands . executeCommand (
@@ -533,7 +592,7 @@ export class DiffViewProvider {
533592 } ) ,
534593 uri ,
535594 `${ fileName } : ${ fileExists ? `${ DIFF_VIEW_LABEL_CHANGES } ` : "New File" } (Editable)` ,
536- { preserveFocus : true } ,
595+ { preserveFocus : true , viewColumn : targetViewColumn } ,
537596 )
538597 } )
539598 . then (
0 commit comments