diff --git a/src/hosts/vscode/hostbridge/window/showTextDocument.ts b/src/hosts/vscode/hostbridge/window/showTextDocument.ts index 9bdbf320..eb70b346 100644 --- a/src/hosts/vscode/hostbridge/window/showTextDocument.ts +++ b/src/hosts/vscode/hostbridge/window/showTextDocument.ts @@ -1,9 +1,29 @@ import * as vscode from "vscode" import { ShowTextDocumentRequest, TextEditorInfo } from "@/shared/proto/host/window" +import { arePathsEqual } from "@/utils/path" export async function showTextDocument(request: ShowTextDocumentRequest): Promise { // Convert file path to URI const uri = vscode.Uri.file(request.path) + + // Check if the document is already open in a tab group that's not in the active editor's column. + // If it is, then close it (if not dirty) so that we don't duplicate tabs + try { + for (const group of vscode.window.tabGroups.all) { + const existingTab = group.tabs.find( + (tab) => tab.input instanceof vscode.TabInputText && arePathsEqual(tab.input.uri.fsPath, uri.fsPath), + ) + if (existingTab) { + const activeColumn = vscode.window.activeTextEditor?.viewColumn + const tabColumn = vscode.window.tabGroups.all.find((group) => group.tabs.includes(existingTab))?.viewColumn + if (activeColumn && activeColumn !== tabColumn && !existingTab.isDirty) { + await vscode.window.tabGroups.close(existingTab) + } + break + } + } + } catch {} // not essential, sometimes tab operations fail + const options: vscode.TextDocumentShowOptions = {} if (request.options?.preview !== undefined) { diff --git a/src/integrations/editor/DiffViewProvider.ts b/src/integrations/editor/DiffViewProvider.ts index ad38b309..d6cbf49c 100644 --- a/src/integrations/editor/DiffViewProvider.ts +++ b/src/integrations/editor/DiffViewProvider.ts @@ -9,6 +9,7 @@ import * as iconv from "iconv-lite" import { HostProvider } from "@/hosts/host-provider" import { DiagnosticSeverity, FileDiagnostics } from "@/shared/proto/index.cline" import { diagnosticsToProblemsString, getNewDiagnostics } from "@/integrations/diagnostics" +import { openFile } from "../misc/open-file" export abstract class DiffViewProvider { editType?: "create" | "modify" @@ -261,13 +262,7 @@ export abstract class DiffViewProvider { // get text after save in case there is any auto-formatting done by the editor const postSaveContent = (await this.getDocumentText()) || "" - await HostProvider.window.showTextDocument({ - path: this.absolutePath, - options: { - preview: false, - preserveFocus: true, - }, - }) + await openFile(this.absolutePath, true) await this.closeAllDiffViews() const newProblems = await this.getNewDiagnosticProblems() @@ -338,13 +333,7 @@ export abstract class DiffViewProvider { await this.saveDocument() console.log(`File ${this.absolutePath} has been reverted to its original content.`) if (this.documentWasOpen) { - await HostProvider.window.showTextDocument({ - path: this.absolutePath, - options: { - preview: false, - preserveFocus: true, - }, - }) + openFile(this.absolutePath, true) } await this.closeAllDiffViews() } diff --git a/src/integrations/misc/export-markdown.ts b/src/integrations/misc/export-markdown.ts index 3b235fbe..5176b9d5 100644 --- a/src/integrations/misc/export-markdown.ts +++ b/src/integrations/misc/export-markdown.ts @@ -4,6 +4,7 @@ import * as path from "path" import { HostProvider } from "@/hosts/host-provider" import { ShowMessageType } from "@/shared/proto/host/window" import { writeFile } from "@utils/fs" +import { openFile } from "./open-file" export async function downloadTask(dateTs: number, conversationHistory: Anthropic.MessageParam[]) { // File name @@ -42,10 +43,7 @@ export async function downloadTask(dateTs: number, conversationHistory: Anthropi try { // Write content to the selected location await writeFile(saveResponse.selectedPath, markdownContent) - await HostProvider.window.showTextDocument({ - path: saveResponse.selectedPath, - options: { preview: true }, - }) + await openFile(saveResponse.selectedPath, false, true) } catch (error) { await HostProvider.window.showMessage({ type: ShowMessageType.ERROR, diff --git a/src/integrations/misc/open-file.ts b/src/integrations/misc/open-file.ts index 3da04586..12b18e07 100644 --- a/src/integrations/misc/open-file.ts +++ b/src/integrations/misc/open-file.ts @@ -31,30 +31,11 @@ export async function openImage(dataUri: string) { } } -export async function openFile(absolutePath: string) { +export async function openFile(absolutePath: string, preserveFocus: boolean = false, preview: boolean = false) { try { - const uri = vscode.Uri.file(absolutePath) - - // Check if the document is already open in a tab group that's not in the active editor's column. If it is, then close it (if not dirty) so that we don't duplicate tabs - try { - for (const group of vscode.window.tabGroups.all) { - const existingTab = group.tabs.find( - (tab) => tab.input instanceof vscode.TabInputText && arePathsEqual(tab.input.uri.fsPath, uri.fsPath), - ) - if (existingTab) { - const activeColumn = vscode.window.activeTextEditor?.viewColumn - const tabColumn = vscode.window.tabGroups.all.find((group) => group.tabs.includes(existingTab))?.viewColumn - if (activeColumn && activeColumn !== tabColumn && !existingTab.isDirty) { - await vscode.window.tabGroups.close(existingTab) - } - break - } - } - } catch {} // not essential, sometimes tab operations fail - await HostProvider.window.showTextDocument({ - path: uri.fsPath, - options: { preview: false }, + path: absolutePath, + options: { preserveFocus, preview }, }) } catch (error) { HostProvider.window.showMessage({