Skip to content

Commit

Permalink
feat: Clicking URLs in assistant now opens them
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanator committed Jul 7, 2023
1 parent 5073777 commit 9c9734d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
45 changes: 25 additions & 20 deletions src/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
15 changes: 11 additions & 4 deletions src/webview/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -206,20 +206,27 @@ 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(
'a[href="VALID_DIRECTORY"]'
);
folderLinks.forEach((link) => {
link.addEventListener("click", () => {
sendOpenPathRequest("directory", link.innerText);
sendOpenLinkRequest("directory", link.innerText);
});
});
};
Expand Down

0 comments on commit 9c9734d

Please sign in to comment.