From 9c9734ded5e5987f7eef5a6483112de752b173b9 Mon Sep 17 00:00:00 2001 From: Brendan Maginnis Date: Fri, 7 Jul 2023 13:37:07 +0100 Subject: [PATCH] feat: Clicking URLs in assistant now opens them --- src/chat.ts | 45 +++++++++++++++++++++++++-------------------- src/webview/chat.js | 15 +++++++++++---- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/src/chat.ts b/src/chat.ts index 48a26d96..1ed84f0f 100644 --- a/src/chat.ts +++ b/src/chat.ts @@ -54,10 +54,10 @@ export type CancelRequest = { type: "cancel_request"; }; -export type OpenPathRequest = { - type: "open_path_request"; - pathType: "file" | "directory"; - path: string; +export type OpenLinkRequest = { + type: "open_link_request"; + linkType: "url" | "file" | "directory"; + link: string; }; export class ChatProvider implements vscode.WebviewViewProvider { @@ -100,7 +100,7 @@ export class ChatProvider implements vscode.WebviewViewProvider { }); webviewView.webview.onDidReceiveMessage( - async (request: ChatRequest | CancelRequest | OpenPathRequest) => { + async (request: ChatRequest | CancelRequest | OpenLinkRequest) => { switch (request.type) { case "chat_request": { vscode.commands.executeCommand("sourcery.chat_request", request); @@ -110,23 +110,28 @@ export class ChatProvider implements vscode.WebviewViewProvider { vscode.commands.executeCommand("sourcery.chat_cancel_request"); break; } - case "open_path_request": { - let path = vscode.Uri.file(request.path); - - // Make the path relative to the workspace root - if (!request.path.startsWith("/")) { - const workspaceRoot = vscode.workspace.workspaceFolders?.[0].uri; - if (workspaceRoot) { - path = vscode.Uri.joinPath(workspaceRoot, request.path); + case "open_link_request": { + if (request.linkType === "url") { + vscode.env.openExternal(vscode.Uri.parse(request.link)); + } else { + let path = vscode.Uri.file(request.link); + + // Make the path relative to the workspace root + if (!request.link.startsWith("/")) { + const workspaceRoot = + vscode.workspace.workspaceFolders?.[0].uri; + if (workspaceRoot) { + path = vscode.Uri.joinPath(workspaceRoot, request.link); + } } - } - if (request.pathType === "file") { - // Open the file in the editor - vscode.commands.executeCommand("vscode.open", path); - } else { - // Reveal the directory in the explorer - vscode.commands.executeCommand("revealInExplorer", path); + if (request.linkType === "file") { + // Open the file in the editor + vscode.commands.executeCommand("vscode.open", path); + } else { + // Reveal the directory in the explorer + vscode.commands.executeCommand("revealInExplorer", path); + } } break; } diff --git a/src/webview/chat.js b/src/webview/chat.js index b93c49e0..57176ad9 100644 --- a/src/webview/chat.js +++ b/src/webview/chat.js @@ -89,8 +89,8 @@ const LINE_HEIGHT = 36; vscode.postMessage({ type: "cancel_request" }); } - function sendOpenPathRequest(pathType, path) { - vscode.postMessage({ type: "open_path_request", pathType, path }); + function sendOpenLinkRequest(linkType, link) { + vscode.postMessage({ type: "open_link_request", linkType, link }); } function clearAllMessages() { @@ -206,12 +206,19 @@ const LINE_HEIGHT = 36; let blocks = currentAssistantMessage.querySelectorAll("pre"); blocks.forEach(setupCopyButton); + let httpLinks = + currentAssistantMessage.querySelectorAll('a[href*="http"]'); + httpLinks.forEach((link) => { + link.addEventListener("click", () => { + sendOpenLinkRequest("url", link.href); + }); + }); let fileLinks = currentAssistantMessage.querySelectorAll( 'a[href="VALID_FILE"]' ); fileLinks.forEach((link) => { link.addEventListener("click", () => { - sendOpenPathRequest("file", link.innerText); + sendOpenLinkRequest("file", link.innerText); }); }); let folderLinks = currentAssistantMessage.querySelectorAll( @@ -219,7 +226,7 @@ const LINE_HEIGHT = 36; ); folderLinks.forEach((link) => { link.addEventListener("click", () => { - sendOpenPathRequest("directory", link.innerText); + sendOpenLinkRequest("directory", link.innerText); }); }); };