nicer edit accept buttons
This commit is contained in:
parent
41dc2f02ef
commit
f5e22b1bfe
8 changed files with 212 additions and 119 deletions
|
|
@ -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];
|
||||
};
|
||||
|
|
|
|||
|
|
@ -267,13 +267,13 @@ export class VerticalDiffManager {
|
|||
onlyOneInsertion?: boolean,
|
||||
quickEdit?: string,
|
||||
range?: vscode.Range,
|
||||
) {
|
||||
): Promise<string | undefined> {
|
||||
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",
|
||||
|
|
|
|||
|
|
@ -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) => {
|
||||
|
|
|
|||
|
|
@ -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 () => {
|
||||
|
|
|
|||
|
|
@ -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<HTMLDivElement>) => {
|
|||
// 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();
|
||||
|
|
|
|||
114
gui/src/pages/edit/EditInputHeader.tsx
Normal file
114
gui/src/pages/edit/EditInputHeader.tsx
Normal file
|
|
@ -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 (
|
||||
<>
|
||||
<div
|
||||
className="select-none p-1"
|
||||
style={{
|
||||
backgroundColor: "#fff2",
|
||||
}}
|
||||
>
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex cursor-pointer" onClick={onClick}>
|
||||
<FileIcon
|
||||
filename={editModeState.highlightedCode.filepath}
|
||||
height={"18px"}
|
||||
width={"18px"}
|
||||
></FileIcon>
|
||||
{getBasename(editModeState.highlightedCode.filepath)} (
|
||||
{editModeState.highlightedCode.range.start.line}-
|
||||
{editModeState.highlightedCode.range.end.line})
|
||||
</div>
|
||||
{editModeState.editStatus === "accepting" ||
|
||||
editModeState.editStatus === "accepting:full-diff" ? (
|
||||
<div className="float-right flex items-center gap-2">
|
||||
{/* {editModeState.editStatus === "accepting" ? (
|
||||
<>
|
||||
<HeaderButtonWithToolTip text="View full diff">
|
||||
<DocumentPlusIcon
|
||||
className="h-4 w-4 cursor-pointer"
|
||||
onClick={showFullDiff}
|
||||
></DocumentPlusIcon>
|
||||
</HeaderButtonWithToolTip>
|
||||
<span className="text-xs text-gray-400">
|
||||
N diffs remaining
|
||||
</span>
|
||||
</>
|
||||
) : (
|
||||
<div>hi</div>
|
||||
)} */}
|
||||
|
||||
<div className="flex">
|
||||
<HeaderButtonWithToolTip text="Accept">
|
||||
<XMarkIcon
|
||||
className="h-4 w-4 cursor-pointer px-2"
|
||||
color="red"
|
||||
onClick={() => {
|
||||
ideMessenger.request("edit/acceptReject", {
|
||||
accept: false,
|
||||
onlyFirst: false,
|
||||
filepath: editModeState.highlightedCode.filepath,
|
||||
});
|
||||
dispatch(setEditDone());
|
||||
}}
|
||||
></XMarkIcon>
|
||||
</HeaderButtonWithToolTip>
|
||||
<HeaderButtonWithToolTip text="Reject">
|
||||
<CheckIcon
|
||||
className="h-4 w-4 cursor-pointer px-2 text-green-500"
|
||||
onClick={() => {
|
||||
ideMessenger.request("edit/acceptReject", {
|
||||
accept: true,
|
||||
onlyFirst: false,
|
||||
filepath: editModeState.highlightedCode.filepath,
|
||||
});
|
||||
dispatch(setEditDone());
|
||||
}}
|
||||
></CheckIcon>
|
||||
</HeaderButtonWithToolTip>
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
|
@ -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() {
|
|||
|
||||
<div className="relative mb-1 mt-3 flex px-2">
|
||||
<TipTapEditor
|
||||
header={
|
||||
<div
|
||||
className="select-none p-1"
|
||||
style={{
|
||||
backgroundColor: "#fff2",
|
||||
}}
|
||||
>
|
||||
<div
|
||||
className="flex cursor-pointer"
|
||||
onClick={() => {
|
||||
ideMessenger.ide.showLines(
|
||||
editModeState.highlightedCode.filepath,
|
||||
editModeState.highlightedCode.range.start.line,
|
||||
editModeState.highlightedCode.range.end.line,
|
||||
);
|
||||
setShowCode(!showCode);
|
||||
}}
|
||||
>
|
||||
<FileIcon
|
||||
filename={editModeState.highlightedCode.filepath}
|
||||
height={"18px"}
|
||||
width={"18px"}
|
||||
></FileIcon>
|
||||
{getBasename(editModeState.highlightedCode.filepath)} (
|
||||
{editModeState.highlightedCode.range.start.line}-
|
||||
{editModeState.highlightedCode.range.end.line})
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
header={<EditInputHeader />}
|
||||
toolbarOptions={{
|
||||
hideAddContext: true,
|
||||
hideImageUpload: true,
|
||||
|
|
@ -223,43 +193,6 @@ function Edit() {
|
|||
</div>
|
||||
</div>
|
||||
|
||||
{["accepting", "streaming"].includes(editModeState.editStatus) && (
|
||||
<div className="my-2 flex w-full cursor-pointer">
|
||||
<AcceptRejectButton
|
||||
disabled={editModeState.editStatus !== "accepting"}
|
||||
backgroundColor={parseColorForHex(
|
||||
VSC_DIFF_REMOVED_LINE_BACKGROUND_VAR,
|
||||
)}
|
||||
onClick={() => {
|
||||
ideMessenger.request("edit/acceptReject", {
|
||||
accept: false,
|
||||
onlyFirst: false,
|
||||
filepath: editModeState.highlightedCode.filepath,
|
||||
});
|
||||
dispatch(setEditDone());
|
||||
}}
|
||||
>
|
||||
<code>{getMetaKeyLabel()}⇧⌫</code> Reject all
|
||||
</AcceptRejectButton>
|
||||
<AcceptRejectButton
|
||||
disabled={editModeState.editStatus !== "accepting"}
|
||||
backgroundColor={parseColorForHex(
|
||||
VSC_DIFF_INSERTED_LINE_BACKGROUND_VAR,
|
||||
)}
|
||||
onClick={() => {
|
||||
ideMessenger.request("edit/acceptReject", {
|
||||
accept: true,
|
||||
onlyFirst: false,
|
||||
filepath: editModeState.highlightedCode.filepath,
|
||||
});
|
||||
dispatch(setEditDone());
|
||||
}}
|
||||
>
|
||||
<code>{getMetaKeyLabel()}⇧⏎</code> Accept all
|
||||
</AcceptRejectButton>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="mt-2">
|
||||
<NewSessionButton
|
||||
onClick={async () => {
|
||||
|
|
@ -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<MessageContent>) => {
|
||||
state.previousInputs.push(payload);
|
||||
state.editStatus = "streaming";
|
||||
},
|
||||
setEditStatus: (state, { payload }: PayloadAction<EditStatus>) => {
|
||||
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<MessageContent>) => {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue