Skip to content

Commit

Permalink
feat: Allow navigation to specific ranges in a document from coding a…
Browse files Browse the repository at this point in the history
…ssistant (#231)
  • Loading branch information
Hellebore authored Oct 4, 2023
1 parent 124100c commit d436d41
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions src/chat.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import * as vscode from "vscode";
import { randomBytes } from "crypto";
import { getCodingAssistantAssetsPath } from "./executable";
import * as path from "path";

export type Recipe = {
id: string;
name: string;
};

type DocumentPosition = {
line: number;
character: number;
};

type DocumentRange = {
start: DocumentPosition;
end: DocumentPosition;
};

// Requests handled by the extension
export type ExtensionMessage =
| {
Expand Down Expand Up @@ -119,31 +130,47 @@ export class ChatProvider implements vscode.WebviewViewProvider {
private handleOpenLinkRequest({
link,
linkType,
documentRange,
}: {
target: "extension";
request: "openLink";
linkType: "url" | "file" | "directory";
link: string;
documentRange: DocumentRange | null;
}) {
if (linkType === "url") {
vscode.env.openExternal(vscode.Uri.parse(link));
} else {
let path = vscode.Uri.file(link);

let filePath = vscode.Uri.file(link);
// Make the path relative to the workspace root
if (!link.startsWith("/")) {
if (!path.isAbsolute(link)) {
const workspaceRoot = vscode.workspace.workspaceFolders?.[0].uri;
if (workspaceRoot) {
path = vscode.Uri.joinPath(workspaceRoot, link);
filePath = vscode.Uri.joinPath(workspaceRoot, link);
}
}

if (linkType === "file") {
// Open the file in the editor
vscode.commands.executeCommand("vscode.open", path);
vscode.workspace.openTextDocument(filePath).then((doc) => {
vscode.window.showTextDocument(doc).then((editor) => {
if (documentRange) {
editor.selection = new vscode.Selection(
documentRange.start.line,
documentRange.start.character,
documentRange.end.line,
documentRange.end.character
);
editor.revealRange(
editor.selection,
vscode.TextEditorRevealType.InCenter
);
}
});
});
} else {
// Reveal the directory in the explorer
vscode.commands.executeCommand("revealInExplorer", path);
vscode.commands.executeCommand("revealInExplorer", link);
}
}
}
Expand Down

0 comments on commit d436d41

Please sign in to comment.