diff --git a/src/remote-container.ts b/src/remote-container.ts index 1357edf..5228449 100644 --- a/src/remote-container.ts +++ b/src/remote-container.ts @@ -358,7 +358,7 @@ export default class RemoteContainer { } /** - * local env + * local env - 使用 Extension API 打开远程文件夹 */ async openRemoteFolder(host: string, port: number, _username: string, path: string, context: vscode.ExtensionContext): Promise { try { @@ -374,12 +374,18 @@ export default class RemoteContainer { } } - let terminal = vscode.window.activeTerminal || vscode.window.createTerminal(`Ext Terminal`); - terminal.show(true); + // 使用 VSCode Extension API 打开远程连接 + // 使用 SSH config 中的 Host 别名,让 SSH config 的用户配置生效 + // 格式: vscode-remote://ssh-remote+host/path + // 注意:不在 URI 中指定用户名,让 SSH config 中的 User root 配置生效 + const remoteUri = vscode.Uri.parse( + `vscode-remote://ssh-remote+${host}${path}` + ); - const command = `code --remote ssh-remote+root@${host}:${port} ${path} --reuse-window`; - - terminal.sendText(command); + // 尝试使用 vscode.openFolder 命令 + await vscode.commands.executeCommand('vscode.openFolder', remoteUri, { + forceNewWindow: false + }); } catch (error) { const errorMessage = error instanceof Error ? error.message : '未知错误'; @@ -468,15 +474,22 @@ export default class RemoteContainer { } /** - * 打开项目(无须插件登录) + * 打开项目(无须插件登录)- 使用 Extension API */ -export async function openProjectWithoutLogging(hostname: string, port: number, username: string, path: string): Promise { - const command = `code --remote ssh-remote+${username}@${hostname}:${port} ${path} --reuse-window`; - +export async function openProjectWithoutLogging(host: string, _port: number, _username: string, path: string): Promise { try { - let terminal = vscode.window.activeTerminal || vscode.window.createTerminal(`Ext Terminal`); - terminal.show(true); - terminal.sendText(command); + // 使用 VSCode Extension API 打开远程连接 + // 使用 SSH config 中的 Host 别名,让 SSH config 的用户配置生效 + // 格式: vscode-remote://ssh-remote+host/path + const remoteUri = vscode.Uri.parse( + `vscode-remote://ssh-remote+${host}${path}` + ); + + // 使用 vscode.openFolder 命令 + await vscode.commands.executeCommand('vscode.openFolder', remoteUri, { + forceNewWindow: false + }); + } catch (error) { const errorMessage = error instanceof Error ? error.message : '未知错误'; vscode.window.showErrorMessage(`无登录打开项目失败: ${errorMessage}`);