Moved open and visible tab retrieval to a separate hostbridge module (#5150)

This commit is contained in:
Igor Tceglevskii authored and GitHub committed 2025-07-24 17:46:31 -07:00
1 parent af7e3a4d20
commit 7a9dce4f86
6 files changed
+281 -9

No files matched your search

+18
View File
@@ -14,6 +14,8 @@ service WindowService {
rpc showMessage(ShowMessageRequest) returns (SelectedResponse);
rpc showInputBox(ShowInputBoxRequest) returns (ShowInputBoxResponse);
rpc showSaveDialog(ShowSaveDialogRequest) returns (ShowSaveDialogResponse);
rpc getOpenTabs(GetOpenTabsRequest) returns (GetOpenTabsResponse);
rpc getVisibleTabs(GetVisibleTabsRequest) returns (GetVisibleTabsResponse);
}
message ShowTextDocumentRequest {
@@ -101,4 +103,20 @@ message ShowInputBoxRequest {
message ShowInputBoxResponse {
optional string response = 1;
}
message GetOpenTabsRequest {
// empty
}
message GetOpenTabsResponse {
repeated string paths = 1;
}
message GetVisibleTabsRequest {
// empty
}
message GetVisibleTabsResponse {
repeated string paths = 1;
}
+7 -9
View File
@@ -86,6 +86,7 @@ import { MessageStateHandler } from "./message-state"
import { TaskState } from "./TaskState"
import { ToolExecutor } from "./ToolExecutor"
import { updateApiReqMsg } from "./utils"
export const USE_EXPERIMENTAL_CLAUDE4_FEATURES = false
export type ToolResponse = string | Array<Anthropic.TextBlockParam | Anthropic.ImageBlockParam>
@@ -2525,10 +2526,9 @@ export class Task {
// It could be useful for cline to know if the user went from one or no file to another between messages, so we always include this context
details += "\n\n# VSCode Visible Files"
const visibleFilePaths = vscode.window.visibleTextEditors
?.map((editor) => editor.document?.uri?.fsPath)
.filter(Boolean)
.map((absolutePath) => path.relative(this.cwd, absolutePath))
const visibleFilePaths = (await HostProvider.window.getVisibleTabs({})).paths.map((absolutePath) =>
path.relative(this.cwd, absolutePath),
)
// Filter paths through clineIgnoreController
const allowedVisibleFiles = this.clineIgnoreController
@@ -2543,11 +2543,9 @@ export class Task {
}
details += "\n\n# VSCode Open Tabs"
const openTabPaths = vscode.window.tabGroups.all
.flatMap((group) => group.tabs)
.map((tab) => (tab.input as vscode.TabInputText)?.uri?.fsPath)
.filter(Boolean)
.map((absolutePath) => path.relative(this.cwd, absolutePath))
const openTabPaths = (await HostProvider.window.getOpenTabs({})).paths.map((absolutePath) =>
path.relative(this.cwd, absolutePath),
)
// Filter paths through clineIgnoreController
const allowedOpenTabs = this.clineIgnoreController
@@ -0,0 +1,90 @@
/* eslint-disable eslint-rules/no-direct-vscode-api */
import { describe, it, beforeEach, afterEach } from "mocha"
import { strict as assert } from "assert"
import * as vscode from "vscode"
import { getOpenTabs } from "@/hosts/vscode/hostbridge/window/getOpenTabs"
import { GetOpenTabsRequest } from "@/shared/proto/host/window"
describe("Hostbridge - Window - getOpenTabs", () => {
async function createAndOpenTestDocument(fileNumber: number, column: vscode.ViewColumn): Promise<void> {
const content = `// Test file ${fileNumber}\nconsole.log('Hello from file ${fileNumber}');`
// Create an untitled document with a custom name
const uri = vscode.Uri.parse(`untitled:test-file-${fileNumber}.js`)
const doc = await vscode.workspace.openTextDocument(uri)
// Set the content
const edit = new vscode.WorkspaceEdit()
edit.insert(uri, new vscode.Position(0, 0), content)
await vscode.workspace.applyEdit(edit)
await vscode.window.showTextDocument(doc, {
viewColumn: column,
preview: false,
})
}
beforeEach(async () => {
// Clean up any existing editors
await vscode.commands.executeCommand("workbench.action.closeAllEditors")
})
afterEach(async () => {
// Clean up test documents and editors
await vscode.commands.executeCommand("workbench.action.closeAllEditors")
})
it("should return empty array when no tabs are open", async () => {
// Ensure no tabs are open
await vscode.commands.executeCommand("workbench.action.closeAllEditors")
const request = GetOpenTabsRequest.create({})
const response = await getOpenTabs(request)
assert.strictEqual(
response.paths.length,
0,
`Should return empty array when no tabs are open. Found: ${JSON.stringify(response.paths)}`,
)
})
it("should return paths of open text document tabs", async () => {
// Open the documents in editors (this creates the tabs)
await createAndOpenTestDocument(1, vscode.ViewColumn.One)
await createAndOpenTestDocument(2, vscode.ViewColumn.Two)
// Wait a bit for tabs to be fully created
await new Promise((resolve) => setTimeout(resolve, 100))
const request = GetOpenTabsRequest.create({})
const response = await getOpenTabs(request)
// Should have 2 tabs open
assert.strictEqual(
response.paths.length,
2,
`Expected 2 tabs, got ${response.paths.length}. Found tabs: ${JSON.stringify(response.paths)}`,
)
})
it("should return all open tabs even when multiple files are opened in the same ViewColumn", async () => {
// Open all documents in the same column (only the last one will be visible, but all are open as tabs)
await createAndOpenTestDocument(1, vscode.ViewColumn.One)
await createAndOpenTestDocument(2, vscode.ViewColumn.One)
await createAndOpenTestDocument(3, vscode.ViewColumn.One)
// Wait a bit for tabs to be fully created
await new Promise((resolve) => setTimeout(resolve, 100))
const request = GetOpenTabsRequest.create({})
const response = await getOpenTabs(request)
// Should have all 3 tabs open, even though only 1 is visible
assert.strictEqual(
response.paths.length,
3,
`Expected 3 open tabs, got ${response.paths.length}. Found: ${JSON.stringify(response.paths)}`,
)
})
})
@@ -0,0 +1,11 @@
import { window, TabInputText } from "vscode"
import { GetOpenTabsRequest, GetOpenTabsResponse } from "@/shared/proto/host/window"
export async function getOpenTabs(_: GetOpenTabsRequest): Promise<GetOpenTabsResponse> {
const openTabPaths = window.tabGroups.all
.flatMap((group) => group.tabs)
.map((tab) => (tab.input as TabInputText)?.uri?.fsPath)
.filter(Boolean)
return GetOpenTabsResponse.create({ paths: openTabPaths })
}
@@ -0,0 +1,147 @@
/* eslint-disable eslint-rules/no-direct-vscode-api */
import { describe, it, beforeEach, afterEach } from "mocha"
import { strict as assert } from "assert"
import * as vscode from "vscode"
import { getVisibleTabs } from "@/hosts/vscode/hostbridge/window/getVisibleTabs"
import { GetVisibleTabsRequest } from "@/shared/proto/host/window"
describe("Hostbridge - Window - getVisibleTabs", () => {
/**
* Helper function to create and open a test document in a specific column
*/
async function createAndOpenTestDocument(fileNumber: number, column: vscode.ViewColumn): Promise<void> {
const content = `// Test file ${fileNumber}\nconsole.log('Hello from file ${fileNumber}');`
// Create an untitled document with a custom name
const uri = vscode.Uri.parse(`untitled:test-file-${fileNumber}.js`)
const doc = await vscode.workspace.openTextDocument(uri)
// Set the content
const edit = new vscode.WorkspaceEdit()
edit.insert(uri, new vscode.Position(0, 0), content)
await vscode.workspace.applyEdit(edit)
await vscode.window.showTextDocument(doc, {
viewColumn: column,
preview: false,
})
}
beforeEach(async () => {
// Clean up any existing editors
await vscode.commands.executeCommand("workbench.action.closeAllEditors")
})
afterEach(async () => {
// Clean up test documents and editors
await vscode.commands.executeCommand("workbench.action.closeAllEditors")
})
it("should return empty array when no visible editors are open", async () => {
// Ensure no editors are open
await vscode.commands.executeCommand("workbench.action.closeAllEditors")
const request = GetVisibleTabsRequest.create({})
const response = await getVisibleTabs(request)
assert.strictEqual(
response.paths.length,
0,
`Should return empty array when no visible editors are open. Found tabs: ${JSON.stringify(response.paths)}`,
)
})
it("should return paths of visible text editors", async () => {
// Open the first document in an editor (this makes it visible)
await createAndOpenTestDocument(1, vscode.ViewColumn.One)
// Wait a bit for editor to be fully created
await new Promise((resolve) => setTimeout(resolve, 100))
const request = GetVisibleTabsRequest.create({})
const response = await getVisibleTabs(request)
// Should have 1 visible editor
assert.strictEqual(
response.paths.length,
1,
`Expected 1 visible editor, got ${response.paths.length}. Found: ${JSON.stringify(response.paths)}`,
)
// Open the second document in a different column (both should now be visible)
await createAndOpenTestDocument(2, vscode.ViewColumn.Two)
// Wait a bit for editor to be fully created
await new Promise((resolve) => setTimeout(resolve, 100))
const response2 = await getVisibleTabs(request)
// Should have 2 visible editors
assert.strictEqual(
response2.paths.length,
2,
`Expected 2 visible editors, got ${response2.paths.length}. Found: ${JSON.stringify(response2.paths)}`,
)
})
it("should only return visible editors, not all open tabs", async () => {
// Open all documents in the same column (only the last one will be visible)
await createAndOpenTestDocument(1, vscode.ViewColumn.One)
await createAndOpenTestDocument(2, vscode.ViewColumn.One)
await createAndOpenTestDocument(3, vscode.ViewColumn.One)
// Wait a bit for editors to be fully created
await new Promise((resolve) => setTimeout(resolve, 100))
const request = GetVisibleTabsRequest.create({})
const response = await getVisibleTabs(request)
// Should have only 1 visible editor (the last one opened in the same column)
assert.strictEqual(
response.paths.length,
1,
`Expected 1 visible editor, got ${response.paths.length}. Found: ${JSON.stringify(response.paths)}`,
)
// Verify that we have the correct number of visible text editors
const actualVisibleEditors = vscode.window.visibleTextEditors.length
assert.strictEqual(
response.paths.length,
actualVisibleEditors,
`Response should match actual visible editors count: ${actualVisibleEditors}`,
)
})
it("should return only visible editors from multiple columns with multiple files", async () => {
// Open multiple documents in column one (only the last one will be visible in that column)
await createAndOpenTestDocument(1, vscode.ViewColumn.One)
await createAndOpenTestDocument(2, vscode.ViewColumn.One)
await createAndOpenTestDocument(3, vscode.ViewColumn.One)
// Open multiple documents in column two (only the last one will be visible in that column)
await createAndOpenTestDocument(4, vscode.ViewColumn.Two)
await createAndOpenTestDocument(5, vscode.ViewColumn.Two)
// Wait a bit for editors to be fully created
await new Promise((resolve) => setTimeout(resolve, 100))
const request = GetVisibleTabsRequest.create({})
const response = await getVisibleTabs(request)
// Should have only 2 visible editors (one from each column, despite having 5 total open tabs)
assert.strictEqual(
response.paths.length,
2,
`Expected 2 visible editors, got ${response.paths.length}. Found: ${JSON.stringify(response.paths)}`,
)
// Verify that we have the correct number of visible text editors
const actualVisibleEditors = vscode.window.visibleTextEditors.length
assert.strictEqual(
response.paths.length,
actualVisibleEditors,
`Response should match actual visible editors count: ${actualVisibleEditors}`,
)
})
})
@@ -0,0 +1,8 @@
import { window, TabInputText } from "vscode"
import { GetVisibleTabsRequest, GetVisibleTabsResponse } from "@/shared/proto/host/window"
export async function getVisibleTabs(_: GetVisibleTabsRequest): Promise<GetVisibleTabsResponse> {
const visibleTabPaths = window.visibleTextEditors?.map((editor) => editor.document?.uri?.fsPath).filter(Boolean)
return GetVisibleTabsResponse.create({ paths: visibleTabPaths })
}