Skip to content

Commit 9358255

Browse files
committed
Refactor GO_TO_FILE handling to accept a single file path that may include a workspace name prefix, simplifying the message structure and improving multi-root workspace file resolution logic
1 parent e3b3e5a commit 9358255

3 files changed

Lines changed: 27 additions & 18 deletions

File tree

‎packages/vscode/src/views/panel/backend/message-handlers/handle-go-to-file.ts‎

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { dictionary } from '@shared/constants/dictionary'
66
export const handle_go_to_file = async (
77
message: GoToFileMessage
88
): Promise<void> => {
9-
const { file_path, workspace_name } = message
9+
const { file_path } = message
1010
const workspace_folders = vscode.workspace.workspaceFolders
1111
if (!workspace_folders) {
1212
Logger.warn({
@@ -17,22 +17,31 @@ export const handle_go_to_file = async (
1717
}
1818

1919
let target_workspace: vscode.WorkspaceFolder | undefined
20+
let relative_file_path = file_path
2021

21-
if (workspace_name) {
22-
target_workspace = workspace_folders.find(
23-
(folder) => folder.name == workspace_name
24-
)
25-
} else if (workspace_folders.length == 1) {
26-
target_workspace = workspace_folders[0]
27-
} else {
22+
if (workspace_folders.length > 1) {
2823
for (const folder of workspace_folders) {
29-
const potential_uri = vscode.Uri.joinPath(folder.uri, file_path)
30-
try {
31-
await vscode.workspace.fs.stat(potential_uri)
24+
if (file_path.startsWith(`${folder.name}/`)) {
3225
target_workspace = folder
26+
relative_file_path = file_path.substring(folder.name.length + 1)
3327
break
34-
} catch {
35-
// file not in this workspace folder
28+
}
29+
}
30+
}
31+
32+
if (!target_workspace) {
33+
if (workspace_folders.length == 1) {
34+
target_workspace = workspace_folders[0]
35+
} else {
36+
for (const folder of workspace_folders) {
37+
const potential_uri = vscode.Uri.joinPath(folder.uri, file_path)
38+
try {
39+
await vscode.workspace.fs.stat(potential_uri)
40+
target_workspace = folder
41+
break
42+
} catch {
43+
// file not in this workspace folder
44+
}
3645
}
3746
}
3847
}
@@ -41,15 +50,15 @@ export const handle_go_to_file = async (
4150
Logger.error({
4251
function_name: 'handle_go_to_file',
4352
message: `Workspace not found for file: ${file_path}`,
44-
data: { file_path, workspace_name }
53+
data: { file_path }
4554
})
4655
vscode.window.showErrorMessage(
4756
dictionary.error_message.WORKSPACE_NOT_FOUND_FOR_FILE(file_path)
4857
)
4958
return
5059
}
5160

52-
const file_uri = vscode.Uri.joinPath(target_workspace.uri, file_path)
61+
const file_uri = vscode.Uri.joinPath(target_workspace.uri, relative_file_path)
5362

5463
try {
5564
const document = await vscode.workspace.openTextDocument(file_uri)

‎packages/vscode/src/views/panel/frontend/Panel.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,9 @@ export const Panel = () => {
360360
on_go_to_file={(file) => {
361361
post_message(vscode, {
362362
command: 'GO_TO_FILE',
363-
file_path: file.file_path,
364-
workspace_name: file.workspace_name
363+
file_path: file.workspace_name
364+
? `${file.workspace_name}/${file.file_path}`
365+
: file.file_path
365366
})
366367
}}
367368
on_toggle_file={(file) => {

‎packages/vscode/src/views/panel/types/messages.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ export interface FocusOnFileInPreviewMessage extends BaseMessage {
262262
export interface GoToFileMessage extends BaseMessage {
263263
command: 'GO_TO_FILE'
264264
file_path: string
265-
workspace_name?: string
266265
}
267266

268267
export interface ShowDiffMessage extends BaseMessage {

0 commit comments

Comments
 (0)