diff --git a/core/protocol/ideWebview.ts b/core/protocol/ideWebview.ts index 55cb3a9b9..040f4445d 100644 --- a/core/protocol/ideWebview.ts +++ b/core/protocol/ideWebview.ts @@ -56,7 +56,12 @@ export interface EditModeArgs { highlightedCode: RangeInFileWithContents; } -export type EditStatus = "not-started" | "streaming" | "accepting" | "done"; +export type EditStatus = + | "not-started" + | "streaming" + | "accepting" + | "accepting:full-diff" + | "done"; export interface ApplyState { streamId: string; @@ -105,6 +110,6 @@ export type ToWebviewFromIdeProtocol = ToWebviewFromIdeOrCoreProtocol & { applyCodeFromChat: [undefined, void]; updateApplyState: [ApplyState, void]; startEditMode: [EditModeArgs, void]; - setEditStatus: [{ status: EditStatus }, void]; + setEditStatus: [{ status: EditStatus; fileAfterEdit?: string }, void]; exitEditMode: [undefined, void]; }; diff --git a/extensions/vscode/src/diff/vertical/manager.ts b/extensions/vscode/src/diff/vertical/manager.ts index 56be062ae..71af5e78b 100644 --- a/extensions/vscode/src/diff/vertical/manager.ts +++ b/extensions/vscode/src/diff/vertical/manager.ts @@ -267,13 +267,13 @@ export class VerticalDiffManager { onlyOneInsertion?: boolean, quickEdit?: string, range?: vscode.Range, - ) { + ): Promise { vscode.commands.executeCommand("setContext", "continue.diffVisible", true); let editor = vscode.window.activeTextEditor; if (!editor) { - return; + return undefined; } const filepath = editor.document.uri.fsPath; @@ -346,7 +346,7 @@ export class VerticalDiffManager { if (!diffHandler) { console.warn("Issue occured while creating new vertical diff handler"); - return; + return undefined; } let selectedRange = diffHandler.range; @@ -396,8 +396,10 @@ export class VerticalDiffManager { this.editDecorationManager.clear(); try { - this.logDiffs = await diffHandler.run( - streamDiffLines( + const streamedLines: string[] = []; + + async function* recordedStream() { + const stream = streamDiffLines( prefix, rangeContent, suffix, @@ -405,14 +407,26 @@ export class VerticalDiffManager { input, getMarkdownLanguageTagForFile(filepath), onlyOneInsertion, - ), - ); + ); + + for await (const line of stream) { + if (line.type === "new" || line.type === "same") { + streamedLines.push(line.line); + } + yield line; + } + } + + this.logDiffs = await diffHandler.run(recordedStream()); // enable a listener for user edits to file while diff is open this.enableDocumentChangeListener(); + + return `${prefix}${streamedLines.join("\n")}${suffix}`; } catch (e) { this.disableDocumentChangeListener(); vscode.window.showErrorMessage(`Error streaming diff: ${e}`); + return undefined; } finally { vscode.commands.executeCommand( "setContext", diff --git a/extensions/vscode/src/extension/VsCodeMessenger.ts b/extensions/vscode/src/extension/VsCodeMessenger.ts index 96ab1fa1a..b4e9749da 100644 --- a/extensions/vscode/src/extension/VsCodeMessenger.ts +++ b/extensions/vscode/src/extension/VsCodeMessenger.ts @@ -316,7 +316,7 @@ export class VsCodeMessenger { "getDefaultModelTitle", undefined, ); - await verticalDiffManager.streamEdit( + const fileAfterEdit = await verticalDiffManager.streamEdit( stripImages(prompt), modelTitle, "edit", @@ -330,6 +330,7 @@ export class VsCodeMessenger { this.webviewProtocol.request("setEditStatus", { status: "accepting", + fileAfterEdit, }); }); this.onWebview("edit/acceptReject", async (msg) => { diff --git a/gui/src/components/Layout.tsx b/gui/src/components/Layout.tsx index 0ab40cb03..42a1182e6 100644 --- a/gui/src/components/Layout.tsx +++ b/gui/src/components/Layout.tsx @@ -188,8 +188,8 @@ const Layout = () => { [navigate], ); - useWebviewListener("setEditStatus", async ({ status }) => { - dispatch(setEditStatus(status)); + useWebviewListener("setEditStatus", async ({ status, fileAfterEdit }) => { + dispatch(setEditStatus({ status, fileAfterEdit })); }); useWebviewListener("exitEditMode", async () => { diff --git a/gui/src/components/find/FindWidget.tsx b/gui/src/components/find/FindWidget.tsx index 3089803da..266a09cd8 100644 --- a/gui/src/components/find/FindWidget.tsx +++ b/gui/src/components/find/FindWidget.tsx @@ -1,19 +1,19 @@ -import { - useRef, - useEffect, - useState, - RefObject, - useCallback, - useMemo, -} from "react"; -import { RootState } from "../../redux/store"; -import { useSelector } from "react-redux"; -import { HeaderButton, Input } from ".."; import { ArrowDownIcon, ArrowUpIcon, XMarkIcon, } from "@heroicons/react/24/outline"; +import { + RefObject, + useCallback, + useEffect, + useMemo, + useRef, + useState, +} from "react"; +import { useSelector } from "react-redux"; +import { HeaderButton, Input } from ".."; +import { RootState } from "../../redux/store"; import HeaderButtonWithToolTip from "../gui/HeaderButtonWithToolTip"; interface SearchMatch { @@ -119,7 +119,7 @@ export const useFindWidget = (searchRef: RefObject) => { // Handle keyboard shortcuts for navigation useEffect(() => { const handleKeyDown = (event: KeyboardEvent) => { - if (event.metaKey && event.key.toLowerCase() === "f") { + if (event.metaKey && event.key.toLowerCase() === "f" && !event.shiftKey) { event.preventDefault(); event.stopPropagation(); openWidget(); diff --git a/gui/src/pages/edit/EditInputHeader.tsx b/gui/src/pages/edit/EditInputHeader.tsx new file mode 100644 index 000000000..b7b66de8c --- /dev/null +++ b/gui/src/pages/edit/EditInputHeader.tsx @@ -0,0 +1,114 @@ +import { CheckIcon, XMarkIcon } from "@heroicons/react/24/outline"; +import { getBasename } from "core/util"; +import { useContext, useState } from "react"; +import { useDispatch, useSelector } from "react-redux"; +import FileIcon from "../../components/FileIcon"; +import HeaderButtonWithToolTip from "../../components/gui/HeaderButtonWithToolTip"; +import { IdeMessengerContext } from "../../context/IdeMessenger"; +import { setEditDone, setEditStatus } from "../../redux/slices/editModeState"; +import { RootState } from "../../redux/store"; + +interface EditInputHeaderParams {} + +/** + * @description This is a class that represents an edit input header. + */ +export function EditInputHeader(editInputHeaderParams: EditInputHeaderParams) { + const dispatch = useDispatch(); + const ideMessenger = useContext(IdeMessengerContext); + const editModeState = useSelector((state: RootState) => state.editModeState); + + const [showCode, setShowCode] = useState(false); + + function onClick() { + ideMessenger.ide.showLines( + editModeState.highlightedCode.filepath, + editModeState.highlightedCode.range.start.line, + editModeState.highlightedCode.range.end.line, + ); + setShowCode(!showCode); + } + + async function showFullDiff() { + await ideMessenger.ide.showDiff( + editModeState.highlightedCode.filepath, + editModeState.fileAfterEdit, + 0, + ); + dispatch(setEditStatus({ status: "accepting:full-diff" })); + } + + return ( + <> +
+
+
+ + {getBasename(editModeState.highlightedCode.filepath)} ( + {editModeState.highlightedCode.range.start.line}- + {editModeState.highlightedCode.range.end.line}) +
+ {editModeState.editStatus === "accepting" || + editModeState.editStatus === "accepting:full-diff" ? ( +
+ {/* {editModeState.editStatus === "accepting" ? ( + <> + + + + + N diffs remaining + + + ) : ( +
hi
+ )} */} + +
+ + { + ideMessenger.request("edit/acceptReject", { + accept: false, + onlyFirst: false, + filepath: editModeState.highlightedCode.filepath, + }); + dispatch(setEditDone()); + }} + > + + + { + ideMessenger.request("edit/acceptReject", { + accept: true, + onlyFirst: false, + filepath: editModeState.highlightedCode.filepath, + }); + dispatch(setEditDone()); + }} + > + +
+
+ ) : null} +
+
+ + ); +} diff --git a/gui/src/pages/edit.tsx b/gui/src/pages/edit/index.tsx similarity index 62% rename from gui/src/pages/edit.tsx rename to gui/src/pages/edit/index.tsx index 9faf01578..0a361d6cd 100644 --- a/gui/src/pages/edit.tsx +++ b/gui/src/pages/edit/index.tsx @@ -2,28 +2,22 @@ import { ArrowLeftIcon } from "@heroicons/react/24/outline"; import { Editor, JSONContent } from "@tiptap/core"; import { InputModifiers } from "core"; import { stripImages } from "core/llm/images"; -import { getBasename } from "core/util"; -import { usePostHog } from "posthog-js/react"; -import { useContext, useEffect, useState } from "react"; +import { useContext, useEffect } from "react"; import { useDispatch, useSelector } from "react-redux"; import { useNavigate } from "react-router-dom"; import styled from "styled-components"; import { defaultBorderRadius, lightGray, - parseColorForHex, - VSC_DIFF_INSERTED_LINE_BACKGROUND_VAR, - VSC_DIFF_REMOVED_LINE_BACKGROUND_VAR, vscForeground, -} from "../components"; -import FileIcon from "../components/FileIcon"; -import { NewSessionButton } from "../components/mainInput/NewSessionButton"; -import resolveEditorContent from "../components/mainInput/resolveInput"; -import TipTapEditor from "../components/mainInput/TipTapEditor"; -import { IdeMessengerContext } from "../context/IdeMessenger"; -import { setEditDone, submitEdit } from "../redux/slices/editModeState"; -import { RootState } from "../redux/store"; -import { getMetaKeyLabel } from "../util"; +} from "../../components"; +import { NewSessionButton } from "../../components/mainInput/NewSessionButton"; +import resolveEditorContent from "../../components/mainInput/resolveInput"; +import TipTapEditor from "../../components/mainInput/TipTapEditor"; +import { IdeMessengerContext } from "../../context/IdeMessenger"; +import { setEditDone, submitEdit } from "../../redux/slices/editModeState"; +import { RootState } from "../../redux/store"; +import { EditInputHeader } from "./EditInputHeader"; const TopDiv = styled.div` overflow-y: auto; @@ -34,6 +28,13 @@ const TopDiv = styled.div` } `; +const ToolbarDiv = styled.div` + padding: 8px; + border-bottom: 1px solid ${lightGray}; + border-radius: ${defaultBorderRadius}; + display: flex; +`; + const AcceptRejectButton = styled.div<{ backgroundColor: string; disabled: boolean; @@ -85,7 +86,6 @@ const EDIT_DISALLOWED_CONTEXT_PROVIDERS = [ ]; function Edit() { - const posthog = usePostHog(); const dispatch = useDispatch(); const navigate = useNavigate(); const ideMessenger = useContext(IdeMessengerContext); @@ -95,8 +95,6 @@ function Edit() { ); useEffect(() => {}, []); - const [showCode, setShowCode] = useState(false); - // Reusing the applyState logic which was just the fastest way to get this working useEffect(() => { if (editModeState.editStatus === "done") { @@ -137,35 +135,7 @@ function Edit() {
-
{ - ideMessenger.ide.showLines( - editModeState.highlightedCode.filepath, - editModeState.highlightedCode.range.start.line, - editModeState.highlightedCode.range.end.line, - ); - setShowCode(!showCode); - }} - > - - {getBasename(editModeState.highlightedCode.filepath)} ( - {editModeState.highlightedCode.range.start.line}- - {editModeState.highlightedCode.range.end.line}) -
-
- } + header={} toolbarOptions={{ hideAddContext: true, hideImageUpload: true, @@ -223,43 +193,6 @@ function Edit() { - {["accepting", "streaming"].includes(editModeState.editStatus) && ( -
- { - ideMessenger.request("edit/acceptReject", { - accept: false, - onlyFirst: false, - filepath: editModeState.highlightedCode.filepath, - }); - dispatch(setEditDone()); - }} - > - {getMetaKeyLabel()}⇧⌫ Reject all - - { - ideMessenger.request("edit/acceptReject", { - accept: true, - onlyFirst: false, - filepath: editModeState.highlightedCode.filepath, - }); - dispatch(setEditDone()); - }} - > - {getMetaKeyLabel()}⇧⏎ Accept all - -
- )} -
{ diff --git a/gui/src/redux/slices/editModeState.ts b/gui/src/redux/slices/editModeState.ts index 4c5defeef..38091ccbf 100644 --- a/gui/src/redux/slices/editModeState.ts +++ b/gui/src/redux/slices/editModeState.ts @@ -1,12 +1,13 @@ import { createSlice, PayloadAction } from "@reduxjs/toolkit"; import { MessageContent } from "core"; import { RangeInFileWithContents } from "core/commands/util"; -import { EditModeArgs } from "core/protocol/ideWebview"; -type EditStatus = "not-started" | "streaming" | "accepting" | "done"; +import { EditModeArgs, EditStatus } from "core/protocol/ideWebview"; + interface EditModeState { highlightedCode?: RangeInFileWithContents; editStatus: EditStatus; previousInputs: MessageContent[]; + fileAfterEdit?: string; } const initialState: EditModeState = { @@ -22,22 +23,47 @@ export const editModeStateSlice = createSlice({ state.highlightedCode = payload.highlightedCode; state.editStatus = "not-started"; state.previousInputs = []; + state.fileAfterEdit = undefined; }, submitEdit: (state, { payload }: PayloadAction) => { state.previousInputs.push(payload); state.editStatus = "streaming"; }, - setEditStatus: (state, { payload }: PayloadAction) => { + setEditStatus: ( + state, + { + payload, + }: PayloadAction<{ status: EditStatus; fileAfterEdit?: string }>, + ) => { // Only allow valid transitions const currentStatus = state.editStatus; - if (currentStatus === "not-started" && payload === "streaming") { - state.editStatus = payload; - } else if (currentStatus === "streaming" && payload === "accepting") { - state.editStatus = payload; - } else if (currentStatus === "accepting" && payload === "done") { - state.editStatus = payload; - } else if (currentStatus === "done" && payload === "not-started") { - state.editStatus = payload; + if (currentStatus === "not-started" && payload.status === "streaming") { + state.editStatus = payload.status; + } else if ( + currentStatus === "streaming" && + payload.status === "accepting" + ) { + state.editStatus = payload.status; + state.fileAfterEdit = payload.fileAfterEdit; + } else if (currentStatus === "accepting" && payload.status === "done") { + state.editStatus = payload.status; + } else if ( + currentStatus === "accepting:full-diff" && + payload.status === "done" + ) { + state.editStatus = payload.status; + } else if ( + currentStatus === "accepting" && + payload.status === "accepting:full-diff" + ) { + state.editStatus = payload.status; + } else if ( + currentStatus === "accepting:full-diff" && + payload.status === "accepting" + ) { + state.editStatus = payload.status; + } else if (currentStatus === "done" && payload.status === "not-started") { + state.editStatus = payload.status; } }, addPreviousInput: (state, { payload }: PayloadAction) => {