From 38b0ede53a551292748243722741990382deffd9 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Fri, 13 Dec 2024 20:43:54 -0800 Subject: [PATCH] add missing vscodemessenger message handlers and fix apply --- core/util/ideUtils.ts | 15 ++- core/util/uri.ts | 5 +- extensions/vscode/src/VsCodeIde.ts | 14 +- .../vscode/src/extension/VsCodeMessenger.ts | 123 ++++++++++++------ .../CodeToEditCard/CodeToEditListItem.tsx | 3 +- gui/src/components/mainInput/TipTapEditor.tsx | 8 +- .../StepContainerPreActionButtons.tsx | 2 +- .../StepContainerPreToolbar/FileInfo.tsx | 19 ++- .../StepContainerPreToolbar.tsx | 36 +++-- .../markdown/StyledMarkdownPreview.tsx | 23 ++-- gui/src/context/IdeMessenger.ts | 6 +- 11 files changed, 162 insertions(+), 92 deletions(-) diff --git a/core/util/ideUtils.ts b/core/util/ideUtils.ts index 26b4b75ab..8172167d8 100644 --- a/core/util/ideUtils.ts +++ b/core/util/ideUtils.ts @@ -38,7 +38,6 @@ export async function inferResolvedUriFromRelativePath( throw new Error("inferResolvedUriFromRelativePath: no dirs provided"); } const segments = pathToUriPathSegment(path).split("/"); - // Generate all possible suffixes from shortest to longest const suffixes: string[] = []; for (let i = segments.length - 1; i >= 0; i--) { @@ -51,13 +50,17 @@ export async function inferResolvedUriFromRelativePath( dir, partialUri: joinPathsToUri(dir, suffix), })); - const existenceChecks = await Promise.all( - uris.map(async ({ partialUri, dir }) => ({ + console.log("here"); + const promises = uris.map(async ({ partialUri, dir }) => { + const exists = await ide.fileExists(partialUri); + return { dir, partialUri, - exists: await ide.fileExists(partialUri), - })), - ); + exists, + }; + }); + console.log("INFERRING", suffix, promises); + const existenceChecks = await Promise.all(promises); const existingUris = existenceChecks.filter(({ exists }) => exists); diff --git a/core/util/uri.ts b/core/util/uri.ts index d2ab8476d..7cb17a5cd 100644 --- a/core/util/uri.ts +++ b/core/util/uri.ts @@ -9,7 +9,10 @@ export function pathToUriPathSegment(path: string) { let clean = path.replace(/[\\]/g, "/"); // backslashes -> forward slashes clean = clean.replace(/^\//, ""); // remove start slash clean = clean.replace(/\/$/, ""); // remove end slash - return encodeURIComponent(clean); + return clean + .split("/") + .map((segment) => encodeURIComponent(segment)) + .join("/"); } export function findUriInDirs( diff --git a/extensions/vscode/src/VsCodeIde.ts b/extensions/vscode/src/VsCodeIde.ts index 2dc769ffd..a812fe86d 100644 --- a/extensions/vscode/src/VsCodeIde.ts +++ b/extensions/vscode/src/VsCodeIde.ts @@ -39,10 +39,16 @@ class VsCodeIde implements IDE { } async fileExists(uri: string): Promise { - return vscode.workspace.fs.stat(vscode.Uri.parse(uri)).then( - () => true, - () => false, - ); + console.log("VSCODE FILE EXISTS"); + try { + await vscode.workspace.fs.stat(vscode.Uri.parse(uri)); + return true; + } catch (error) { + if (error instanceof vscode.FileSystemError) { + return false; + } + throw error; + } } async gotoDefinition(location: Location): Promise { diff --git a/extensions/vscode/src/extension/VsCodeMessenger.ts b/extensions/vscode/src/extension/VsCodeMessenger.ts index 9fe86d5b4..7fa90e2c8 100644 --- a/extensions/vscode/src/extension/VsCodeMessenger.ts +++ b/extensions/vscode/src/extension/VsCodeMessenger.ts @@ -98,18 +98,6 @@ export class VsCodeMessenger { ); }); - this.onWebview("readRangeInFile", async (msg) => { - return await vscode.workspace - .openTextDocument(msg.data.filepath) - .then((document) => { - const start = new vscode.Position(0, 0); - const end = new vscode.Position(5, 0); - const range = new vscode.Range(start, end); - - const contents = document.getText(range); - return contents; - }); - }); this.onWebview("toggleDevTools", (msg) => { vscode.commands.executeCommand("workbench.action.toggleDevTools"); vscode.commands.executeCommand("continue.viewLogs"); @@ -123,35 +111,22 @@ export class VsCodeMessenger { this.onWebview("toggleFullScreen", (msg) => { vscode.commands.executeCommand("continue.toggleFullScreen"); }); - // History - this.onWebview("saveFile", async (msg) => { - return await ide.saveFile(msg.data.filepath); - }); - this.onWebview("readFile", async (msg) => { - return await ide.readFile(msg.data.filepath); + + this.onWebview("acceptDiff", async ({ data: { filepath, streamId } }) => { + await vscode.commands.executeCommand( + "continue.acceptDiff", + filepath, + streamId, + ); }); - webviewProtocol.on( - "acceptDiff", - async ({ data: { filepath, streamId } }) => { - await vscode.commands.executeCommand( - "continue.acceptDiff", - filepath, - streamId, - ); - }, - ); - - webviewProtocol.on( - "rejectDiff", - async ({ data: { filepath, streamId } }) => { - await vscode.commands.executeCommand( - "continue.rejectDiff", - filepath, - streamId, - ); - }, - ); + this.onWebview("rejectDiff", async ({ data: { filepath, streamId } }) => { + await vscode.commands.executeCommand( + "continue.rejectDiff", + filepath, + streamId, + ); + }); this.onWebview("applyToFile", async ({ data }) => { webviewProtocol.request("updateApplyState", { @@ -259,10 +234,6 @@ export class VsCodeMessenger { await showTutorial(this.ide); }); - this.onWebview("openUrl", (msg) => { - vscode.env.openExternal(vscode.Uri.parse(msg.data)); - }); - this.onWebview( "overwriteFile", async ({ data: { prevFileContent, filepath } }) => { @@ -389,6 +360,19 @@ export class VsCodeMessenger { // None right now /** BOTH CORE AND WEBVIEW **/ + this.onWebviewOrCore("readRangeInFile", async (msg) => { + return await vscode.workspace + .openTextDocument(msg.data.filepath) + .then((document) => { + const start = new vscode.Position(0, 0); + const end = new vscode.Position(5, 0); + const range = new vscode.Range(start, end); + + const contents = document.getText(range); + return contents; + }); + }); + this.onWebviewOrCore("getIdeSettings", async (msg) => { return ide.getIdeSettings(); }); @@ -471,5 +455,58 @@ export class VsCodeMessenger { false, ); }); + this.onWebviewOrCore("saveFile", async (msg) => { + return await ide.saveFile(msg.data.filepath); + }); + this.onWebviewOrCore("readFile", async (msg) => { + return await ide.readFile(msg.data.filepath); + }); + this.onWebviewOrCore("openUrl", (msg) => { + vscode.env.openExternal(vscode.Uri.parse(msg.data)); + }); + + this.onWebviewOrCore("fileExists", async (msg) => { + return await ide.fileExists(msg.data.filepath); + }); + + this.onWebviewOrCore("gotoDefinition", async (msg) => { + return await ide.gotoDefinition(msg.data.location); + }); + + this.onWebviewOrCore("getLastModified", async (msg) => { + return await ide.getLastModified(msg.data.files); + }); + + this.onWebviewOrCore("getGitRootPath", async (msg) => { + return await ide.getGitRootPath(msg.data.dir); + }); + + this.onWebviewOrCore("listDir", async (msg) => { + return await ide.listDir(msg.data.dir); + }); + + this.onWebviewOrCore("getRepoName", async (msg) => { + return await ide.getRepoName(msg.data.dir); + }); + + this.onWebviewOrCore("getTags", async (msg) => { + return await ide.getTags(msg.data); + }); + + this.onWebviewOrCore("getIdeInfo", async (msg) => { + return await ide.getIdeInfo(); + }); + + this.onWebviewOrCore("isTelemetryEnabled", async (msg) => { + return await ide.isTelemetryEnabled(); + }); + + this.onWebviewOrCore("getWorkspaceConfigs", async (msg) => { + return await ide.getWorkspaceConfigs(); + }); + + this.onWebviewOrCore("getUniqueId", async (msg) => { + return await ide.getUniqueId(); + }); } } diff --git a/gui/src/components/CodeToEditCard/CodeToEditListItem.tsx b/gui/src/components/CodeToEditCard/CodeToEditListItem.tsx index 4935d129d..46380f084 100644 --- a/gui/src/components/CodeToEditCard/CodeToEditListItem.tsx +++ b/gui/src/components/CodeToEditCard/CodeToEditListItem.tsx @@ -4,12 +4,11 @@ import { ChevronRightIcon, XMarkIcon, } from "@heroicons/react/24/outline"; -import { useMemo, useState } from "react"; +import { useState } from "react"; import StyledMarkdownPreview from "../markdown/StyledMarkdownPreview"; import { getMarkdownLanguageTagForFile } from "core/util"; import styled from "styled-components"; import { CodeToEdit } from "core"; -import { rifWithContentsToContextItem } from "core/commands/util"; import { getLastNUriRelativePathParts, getUriPathBasename, diff --git a/gui/src/components/mainInput/TipTapEditor.tsx b/gui/src/components/mainInput/TipTapEditor.tsx index 759373cc7..f77cbb671 100644 --- a/gui/src/components/mainInput/TipTapEditor.tsx +++ b/gui/src/components/mainInput/TipTapEditor.tsx @@ -782,13 +782,7 @@ function TipTapEditor(props: TipTapEditorProps) { editor.commands.focus("end"); }, 20); }, - [ - editor, - props.isMainInput, - historyLength, - props.isMainInput, - onEnterRef.current, - ], + [editor, props.isMainInput, historyLength, onEnterRef.current], ); useWebviewListener( diff --git a/gui/src/components/markdown/StepContainerPreActionButtons.tsx b/gui/src/components/markdown/StepContainerPreActionButtons.tsx index 3e42c5872..1afbe2a27 100644 --- a/gui/src/components/markdown/StepContainerPreActionButtons.tsx +++ b/gui/src/components/markdown/StepContainerPreActionButtons.tsx @@ -83,7 +83,7 @@ export default function StepContainerPreActionButtons({ const defaultModel = useAppSelector(selectDefaultModel); function onClickApply() { - if (!defaultModel) { + if (!defaultModel || !streamIdRef.current) { return; } ideMessenger.post("applyToFile", { diff --git a/gui/src/components/markdown/StepContainerPreToolbar/FileInfo.tsx b/gui/src/components/markdown/StepContainerPreToolbar/FileInfo.tsx index 05d1eed57..c2f09a24a 100644 --- a/gui/src/components/markdown/StepContainerPreToolbar/FileInfo.tsx +++ b/gui/src/components/markdown/StepContainerPreToolbar/FileInfo.tsx @@ -1,19 +1,24 @@ import FileIcon from "../../FileIcon"; import { useContext } from "react"; import { IdeMessengerContext } from "../../../context/IdeMessenger"; -import { getUriPathBasename } from "core/util/uri"; +import { getLastNPathParts, getUriPathBasename } from "core/util/uri"; +import { inferResolvedUriFromRelativePath } from "core/util/ideUtils"; export interface FileInfoProps { - filepath: string; + relativeFilepath: string; range?: string; } -const FileInfo = ({ filepath, range }: FileInfoProps) => { +const FileInfo = ({ relativeFilepath, range }: FileInfoProps) => { const ideMessenger = useContext(IdeMessengerContext); - function onClickFileName() { + async function onClickFileName() { + const fileUri = await inferResolvedUriFromRelativePath( + relativeFilepath, + ideMessenger.ide, + ); ideMessenger.post("showFile", { - filepath, + filepath: fileUri, }); } @@ -23,9 +28,9 @@ const FileInfo = ({ filepath, range }: FileInfoProps) => { className="mr-0.5 flex w-full min-w-0 cursor-pointer items-center gap-0.5" onClick={onClickFileName} > - + - {getUriPathBasename(filepath)} + {getLastNPathParts(relativeFilepath, 1)} {range && ` ${range}`} diff --git a/gui/src/components/markdown/StepContainerPreToolbar/StepContainerPreToolbar.tsx b/gui/src/components/markdown/StepContainerPreToolbar/StepContainerPreToolbar.tsx index 45c5a335c..7339bd83a 100644 --- a/gui/src/components/markdown/StepContainerPreToolbar/StepContainerPreToolbar.tsx +++ b/gui/src/components/markdown/StepContainerPreToolbar/StepContainerPreToolbar.tsx @@ -19,6 +19,7 @@ import { selectApplyStateByStreamId, selectIsInEditMode, } from "../../../redux/slices/sessionSlice"; +import { inferResolvedUriFromRelativePath } from "core/util/ideUtils"; const TopDiv = styled.div` outline: 1px solid rgba(153, 153, 152); @@ -44,7 +45,7 @@ const ToolbarDiv = styled.div<{ isExpanded: boolean }>` export interface StepContainerPreToolbarProps { codeBlockContent: string; language: string; - fileUri: string; + relativeFilepath: string; isGeneratingCodeBlock: boolean; codeBlockIndex: number; // To track which codeblock we are applying range?: string; @@ -84,17 +85,23 @@ export default function StepContainerPreToolbar( : props.isGeneratingCodeBlock; const isNextCodeBlock = nextCodeBlockIndex === props.codeBlockIndex; - const hasFileExtension = /\.[0-9a-z]+$/i.test(props.fileUri); + const hasFileExtension = /\.[0-9a-z]+$/i.test(props.relativeFilepath); const defaultModel = useAppSelector(selectDefaultModel); - function onClickApply() { + async function onClickApply() { if (!defaultModel) { return; } + console.log(props.relativeFilepath); + const fileUri = await inferResolvedUriFromRelativePath( + props.relativeFilepath, + ideMessenger.ide, + ); + console.log(fileUri); ideMessenger.post("applyToFile", { streamId: streamIdRef.current, - filepath: props.fileUri, + filepath: fileUri, text: codeBlockContent, curSelectedModelTitle: defaultModel.title, }); @@ -137,16 +144,24 @@ export default function StepContainerPreToolbar( wasGeneratingRef.current = isGeneratingCodeBlock; }, [isGeneratingCodeBlock]); - function onClickAcceptApply() { + async function onClickAcceptApply() { + const fileUri = await inferResolvedUriFromRelativePath( + props.relativeFilepath, + ideMessenger.ide, + ); ideMessenger.post("acceptDiff", { - filepath: props.fileUri, + filepath: fileUri, streamId: streamIdRef.current, }); } - function onClickRejectApply() { + async function onClickRejectApply() { + const fileUri = await inferResolvedUriFromRelativePath( + props.relativeFilepath, + ideMessenger.ide, + ); ideMessenger.post("rejectDiff", { - filepath: props.fileUri, + filepath: fileUri, streamId: streamIdRef.current, }); } @@ -172,7 +187,10 @@ export default function StepContainerPreToolbar( }`} />
- +
diff --git a/gui/src/components/markdown/StyledMarkdownPreview.tsx b/gui/src/components/markdown/StyledMarkdownPreview.tsx index 3a72f1645..ebaf1e4f9 100644 --- a/gui/src/components/markdown/StyledMarkdownPreview.tsx +++ b/gui/src/components/markdown/StyledMarkdownPreview.tsx @@ -201,9 +201,8 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( if (node.meta) { let meta = node.meta.split(" "); - node.data.hProperties["data-fileuri"] = - inferResolvedUriFromRelativePath(meta[0], ideMessenger.ide); - node.data.hProperties.range = meta[1]; // E.g + node.data.hProperties["data-relativefilepath"] = meta[0]; + node.data.hProperties.range = meta[1]; } }); }, @@ -224,7 +223,7 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( return (tree) => { visit(tree, { tagName: "pre" }, (node: any) => { // Add an index (0, 1, 2, etc...) to each code block. - node.properties = { codeBlockIndex }; + node.properties = { "data-codeblockindex": codeBlockIndex }; codeBlockIndex++; }); }; @@ -241,9 +240,11 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( ); }, pre: ({ node, ...preProps }) => { - const preChildProps = preProps?.children?.[0]?.props; - const { className, range } = preChildProps ?? {}; - const fileUri = preChildProps["data-fileuri"]; + const codeBlockIndex = preProps["data-codeblockindex"]; + + const preChildProps = preProps?.children?.[0]?.props ?? {}; + const { className, range } = preChildProps; + const relativeFilePath = preChildProps["data-relativefilepath"]; const codeBlockContent = preChildProps["data-codeblockcontent"]; const isGeneratingCodeBlock = preChildProps["data-isgeneratingcodeblock"]; @@ -258,12 +259,12 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( // that is just action buttons on hover. // We also use this in JB since we haven't yet implemented // the logic forfileUri lazy apply. - if (!fileUri || isJetBrains()) { + if (!relativeFilePath || isJetBrains()) { return ( @@ -274,9 +275,9 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( return ( diff --git a/gui/src/context/IdeMessenger.ts b/gui/src/context/IdeMessenger.ts index 8b47fbf9b..8bb236289 100644 --- a/gui/src/context/IdeMessenger.ts +++ b/gui/src/context/IdeMessenger.ts @@ -65,7 +65,11 @@ export class IdeMessenger implements IIdeMessenger { ); } - private _postToIde(messageType: string, data: any, messageId?: string) { + private _postToIde( + messageType: string, + data: FromWebviewProtocol[T][0], + messageId?: string, + ) { if (typeof vscode === "undefined") { if (isJetBrains()) { if (window.postIntellijMessage === undefined) {