diff --git a/core/protocol/ideWebview.ts b/core/protocol/ideWebview.ts index 91fa40395..cd79bcd21 100644 --- a/core/protocol/ideWebview.ts +++ b/core/protocol/ideWebview.ts @@ -56,7 +56,7 @@ export type ToIdeFromWebviewProtocol = ToIdeFromWebviewOrCoreProtocol & { { accept: boolean; onlyFirst: boolean; filepath: string }, void, ]; - "edit/escape": [undefined, void]; + "edit/exit": [{ shouldFocusEditor: boolean }, void]; }; export type ToWebviewFromIdeProtocol = ToWebviewFromIdeOrCoreProtocol & { diff --git a/extensions/vscode/src/commands.ts b/extensions/vscode/src/commands.ts index bd80fada6..c6acc5e14 100644 --- a/extensions/vscode/src/commands.ts +++ b/extensions/vscode/src/commands.ts @@ -582,7 +582,7 @@ const getCommandsMap: ( "continue.exitEditMode": async () => { captureCommandTelemetry("exitEditMode"); editDecorationManager.clear(); - await sidebar.webviewProtocol?.request("exitEditMode", undefined); + void sidebar.webviewProtocol?.request("exitEditMode", undefined); }, // "continue.quickEdit": async (args: QuickEditShowParams) => { // let linesOfCode = undefined; diff --git a/extensions/vscode/src/extension/VsCodeMessenger.ts b/extensions/vscode/src/extension/VsCodeMessenger.ts index 0f5a13cff..3460f5627 100644 --- a/extensions/vscode/src/extension/VsCodeMessenger.ts +++ b/extensions/vscode/src/extension/VsCodeMessenger.ts @@ -31,6 +31,7 @@ import { getFullyQualifiedPath } from "../util/util"; import { getExtensionUri } from "../util/vscode"; import { VsCodeIde } from "../VsCodeIde"; import { VsCodeWebviewProtocol } from "../webviewProtocol"; +import { hideGUI } from "../commands"; /** * A shared messenger class between Core and Webview @@ -400,8 +401,16 @@ export class VsCodeMessenger { vscode.commands.executeCommand("continue.rejectDiff", filepath); } }); - this.onWebview("edit/escape", async (msg) => { - this.editDecorationManager.clear(); + this.onWebview("edit/exit", async (msg) => { + if (msg.data.shouldFocusEditor) { + const activeEditor = vscode.window.activeTextEditor; + + if (activeEditor) { + vscode.window.showTextDocument(activeEditor.document); + } + } + + editDecorationManager.clear(); }); /** PASS THROUGH FROM WEBVIEW TO CORE AND BACK **/ diff --git a/gui/src/components/CodeToEditCard/CodeToEditListItem.tsx b/gui/src/components/CodeToEditCard/CodeToEditListItem.tsx index b457d3a69..ed46517e2 100644 --- a/gui/src/components/CodeToEditCard/CodeToEditListItem.tsx +++ b/gui/src/components/CodeToEditCard/CodeToEditListItem.tsx @@ -63,7 +63,11 @@ export default function CodeToEditListItem({ return (
  • setShowCodeSnippet((showCodeSnippet) => !showCodeSnippet)} + onClick={() => { + if (!isInsertion) { + setShowCodeSnippet((showCodeSnippet) => !showCodeSnippet); + } + }} >
    { useWebviewListener( "focusEdit", async () => { - await saveSession(); + await saveSession(false); + dispatch(newSession()); dispatch(focusEdit()); dispatch(setMode("edit")); }, @@ -167,7 +168,8 @@ const Layout = () => { useWebviewListener( "focusEditWithoutClear", async () => { - await saveSession(); + await saveSession(false); + dispatch(newSession()); dispatch(focusEdit()); dispatch(setMode("edit")); }, @@ -186,13 +188,6 @@ const Layout = () => { "setEditStatus", async ({ status, fileAfterEdit }) => { dispatch(setEditStatus({ status, fileAfterEdit })); - - if (status === "done") { - loadLastSession().catch((e) => - console.error(`Failed to load last session: ${e}`), - ); - dispatch(completeEdit()); - } }, [], ); @@ -202,7 +197,7 @@ const Layout = () => { console.error(`Failed to load last session: ${e}`), ); - dispatch(completeEdit()); + dispatch(exitEditMode()); }); useEffect(() => { diff --git a/gui/src/components/StepContainer/AcceptRejectAllButtons.tsx b/gui/src/components/StepContainer/AcceptRejectAllButtons.tsx index bc287c5c2..7fbbf07c6 100644 --- a/gui/src/components/StepContainer/AcceptRejectAllButtons.tsx +++ b/gui/src/components/StepContainer/AcceptRejectAllButtons.tsx @@ -4,7 +4,7 @@ import { CheckIcon, XMarkIcon } from "@heroicons/react/24/outline"; import { ApplyState } from "core"; import { getMetaKeyLabel } from "../../util"; import { useAppSelector } from "../../redux/hooks"; -import { selectIsSingleRangeEdit } from "../../redux/slices/sessionSlice"; +import { selectIsSingleRangeEditOrInsertion } from "../../redux/slices/sessionSlice"; export interface AcceptRejectAllButtonsProps { pendingApplyStates: ApplyState[]; @@ -16,7 +16,7 @@ export default function AcceptRejectAllButtons({ onAcceptOrReject, }: AcceptRejectAllButtonsProps) { const ideMessenger = useContext(IdeMessengerContext); - const isSingleRangeEdit = useAppSelector(selectIsSingleRangeEdit); + const isSingleRangeEdit = useAppSelector(selectIsSingleRangeEditOrInsertion); async function handleAcceptOrReject(status: "acceptDiff" | "rejectDiff") { for (const { filepath, streamId } of pendingApplyStates) { diff --git a/gui/src/components/StepContainer/EditActions.tsx b/gui/src/components/StepContainer/EditActions.tsx index 55ebcab20..99e1ce94a 100644 --- a/gui/src/components/StepContainer/EditActions.tsx +++ b/gui/src/components/StepContainer/EditActions.tsx @@ -6,7 +6,7 @@ import { renderChatMessage } from "core/util/messageContent"; import { CopyIconButton } from "../gui/CopyIconButton"; import { useAppDispatch, useAppSelector } from "../../redux/hooks"; import useHistory from "../../hooks/useHistory"; -import { completeEdit } from "../../redux/thunks/completeEdit"; +import { exitEditMode } from "../../redux/thunks/exitEditMode"; export interface EditActionsProps { index: number; @@ -63,7 +63,7 @@ export default function EditActions({ index, item }: EditActionsProps) { console.error(`Failed to load last session: ${e}`), ); - dispatch(completeEdit()); + dispatch(exitEditMode()); }} /> )} diff --git a/gui/src/components/mainInput/ContinueInputBox.tsx b/gui/src/components/mainInput/ContinueInputBox.tsx index 176c74dee..7625ca0d6 100644 --- a/gui/src/components/mainInput/ContinueInputBox.tsx +++ b/gui/src/components/mainInput/ContinueInputBox.tsx @@ -153,12 +153,7 @@ function ContinueInputBox(props: ContinueInputBoxProps) { > { - props.onEnter(...args); - if (props.isMainInput) { - args[2].commands.clearContent(true); - } - }} + onEnter={props.onEnter} placeholder={placeholder} isMainInput={props.isMainInput ?? false} availableContextProviders={filteredContextProviders} diff --git a/gui/src/components/mainInput/TipTapEditor.tsx b/gui/src/components/mainInput/TipTapEditor.tsx index 3d20cc7e3..479876f0f 100644 --- a/gui/src/components/mainInput/TipTapEditor.tsx +++ b/gui/src/components/mainInput/TipTapEditor.tsx @@ -70,7 +70,7 @@ import { clearCodeToEdit, selectIsInEditMode, } from "../../redux/slices/sessionSlice"; -import { completeEdit } from "../../redux/thunks"; +import { exitEditMode } from "../../redux/thunks"; const InputBoxDiv = styled.div<{ border?: string }>` resize: none; @@ -347,11 +347,10 @@ function TipTapEditor(props: TipTapEditorProps) { }, }), Placeholder.configure({ - placeholder: - props.placeholder ?? - (historyLengthRef.current === 0 - ? "Ask anything, '@' to add context" - : "Ask a follow-up"), + placeholder: getPlaceholderText( + props.placeholder, + historyLengthRef.current, + ), }), Paragraph.extend({ addKeyboardShortcuts() { @@ -419,14 +418,17 @@ function TipTapEditor(props: TipTapEditorProps) { } }, Escape: () => { - if (isInEditModeRef.current) { - loadLastSession().catch((e) => - console.error(`Failed to load last session: ${e}`), - ); - dispatch(completeEdit()); - return true; + if (inDropdownRef.current || !isInEditModeRef.current) { + return false; } - return false; + + loadLastSession().catch((e) => + console.error(`Failed to load last session: ${e}`), + ); + + dispatch(exitEditMode()); + + return true; }, ArrowDown: () => { if ( @@ -546,6 +548,38 @@ function TipTapEditor(props: TipTapEditorProps) { setShouldHideToolbar(value); }, 200); + function getPlaceholderText( + placeholder: TipTapEditorProps["placeholder"], + historyLength: number, + ) { + if (placeholder) { + return placeholder; + } + + return historyLength === 0 + ? "Ask anything, '@' to add context" + : "Ask a follow-up"; + } + + useEffect(() => { + const placeholder = getPlaceholderText( + props.placeholder, + historyLengthRef.current, + ); + + editor.extensionManager.extensions.filter( + (extension) => extension.name === "placeholder", + )[0].options["placeholder"] = placeholder; + + editor.view.dispatch(editor.state.tr); + }, [editor, props.placeholder, historyLengthRef.current]); + + useEffect(() => { + if (props.isMainInput) { + editor.commands.clearContent(true); + } + }, [isInEditMode, props.isMainInput]); + useEffect(() => { if (editor) { const handleFocus = () => { diff --git a/gui/src/components/markdown/StyledMarkdownPreview.tsx b/gui/src/components/markdown/StyledMarkdownPreview.tsx index 70be76bdc..c14ba3a11 100644 --- a/gui/src/components/markdown/StyledMarkdownPreview.tsx +++ b/gui/src/components/markdown/StyledMarkdownPreview.tsx @@ -256,9 +256,12 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( const content = getCodeChildrenContent(codeProps.children); if (content && contextItemsRef.current) { - const ctxItem = contextItemsRef.current.find((ctxItem) => - ctxItem.uri?.value.includes(content), + const ctxItem = contextItemsRef.current.find( + (ctxItem) => + ctxItem.uri?.value.includes(content) && + ctxItem.uri.type === "file", ); + if (ctxItem) { const rif = ctxItemToRifWithContents(ctxItem); return ; diff --git a/gui/src/pages/gui/Chat.tsx b/gui/src/pages/gui/Chat.tsx index 8630c5427..000a06f12 100644 --- a/gui/src/pages/gui/Chat.tsx +++ b/gui/src/pages/gui/Chat.tsx @@ -36,13 +36,11 @@ import { useTutorialCard } from "../../hooks/useTutorialCard"; import { useWebviewListener } from "../../hooks/useWebviewListener"; import { selectCurrentToolCall } from "../../redux/selectors/selectCurrentToolCall"; import { - clearCodeToEdit, clearLastEmptyResponse, newSession, selectIsInEditMode, - selectIsSingleRangeEdit, + selectIsSingleRangeEditOrInsertion, setInactive, - setMode, } from "../../redux/slices/sessionSlice"; import { streamResponseThunk } from "../../redux/thunks/streamResponse"; import { @@ -66,13 +64,12 @@ import { import { RootState } from "../../redux/store"; import FeedbackDialog from "../../components/dialogs/FeedbackDialog"; import getMultifileEditPrompt from "../../util/getMultifileEditPrompt"; -import { setEditDone, submitEdit } from "../../redux/slices/editModeState"; +import { submitEdit } from "../../redux/slices/editModeState"; import { stripImages } from "core/util/messageContent"; import resolveEditorContent from "../../components/mainInput/resolveInput"; -import CodeToEdit from "../../components/CodeToEditCard/CodeToEditCard"; import AcceptRejectAllButtons from "../../components/StepContainer/AcceptRejectAllButtons"; import CodeToEditCard from "../../components/CodeToEditCard"; -import { completeEdit } from "../../redux/thunks/completeEdit"; +import { exitEditMode } from "../../redux/thunks/exitEditMode"; const StopButton = styled.div` background-color: ${vscBackground}; @@ -161,7 +158,9 @@ export function Chat() { ); const hasPendingApplies = pendingApplyStates.length > 0; const isInEditMode = useAppSelector(selectIsInEditMode); - const isSingleRangeEdit = useAppSelector(selectIsSingleRangeEdit); + const isSingleRangeEditOrInsertion = useAppSelector( + selectIsSingleRangeEditOrInsertion, + ); const snapToBottom = useCallback(() => { if (!stepsDivRef.current) return; @@ -182,7 +181,7 @@ export function Chat() { setIsAtBottom(true); }, [stepsDivRef, setIsAtBottom]); - const returnToLastSessinButtonText = isInEditMode + const returnToLastSessionButtonText = isInEditMode ? "Back to Chat" : "Last Session"; @@ -228,8 +227,8 @@ export function Chat() { const { widget, highlights } = useFindWidget(stepsDivRef); - const sendChatInput = useCallback( - (editorState: JSONContent, modifiers: InputModifiers) => { + const sendInput = useCallback( + (editorState: JSONContent, modifiers: InputModifiers, editor: Editor) => { if (defaultModel?.provider === "free-trial") { const u = getLocalStorage("ftc"); if (u) { @@ -249,7 +248,18 @@ export function Chat() { } } - dispatch(streamResponseThunk({ editorState, modifiers })); + if (isSingleRangeEditOrInsertion) { + handleSingleRangeEditOrInsertion(editorState); + return; + } + + editor.commands.clearContent(true); + + const promptPreamble = isInEditMode + ? getMultifileEditPrompt(codeToEdit) + : undefined; + + dispatch(streamResponseThunk({ editorState, modifiers, promptPreamble })); // Increment localstorage counter for popup const currentCount = getLocalStorage("mainTextEntryCounter"); @@ -264,14 +274,16 @@ export function Chat() { setLocalStorage("mainTextEntryCounter", 1); } }, - [history, defaultModel, streamResponse], + [ + history, + defaultModel, + streamResponse, + isSingleRangeEditOrInsertion, + codeToEdit, + ], ); - async function handleSingleRangeEdit( - editorState: JSONContent, - modifiers: InputModifiers, - editor: Editor, - ) { + async function handleSingleRangeEditOrInsertion(editorState: JSONContent) { const [contextItems, __, userInstructions] = await resolveEditorContent( editorState, { @@ -294,27 +306,6 @@ export function Chat() { }); dispatch(submitEdit(prompt)); - editor.commands.selectTextblockEnd(); - } - - async function sendEditInput( - editorState: JSONContent, - modifiers: InputModifiers, - editor: Editor, - ) { - if (isSingleRangeEdit) { - handleSingleRangeEdit(editorState, modifiers, editor); - } else { - const promptPreamble = getMultifileEditPrompt(codeToEdit); - - dispatch( - streamResponseThunk({ - editorState, - modifiers, - promptPreamble, - }), - ); - } } useWebviewListener( @@ -322,7 +313,7 @@ export function Chat() { async () => { saveSession(); mainTextInputRef.current?.focus?.(); - dispatch(completeEdit()); + dispatch(exitEditMode()); }, [saveSession], ); @@ -362,13 +353,9 @@ export function Chat() { > {item.message.role === "user" ? ( <> - {isInEditMode && } + {isInEditMode && index === 0 && } { - dispatch( - streamResponseThunk({ editorState, modifiers, index }), - ); - }} + onEnter={sendInput} isLastUserInput={isLastUserInput(index)} isMainInput={false} editorState={item.editorState} @@ -466,18 +453,14 @@ export function Chat() { {toolCallState?.status === "generated" && } - {history.length === 0 && isInEditMode && } + {isInEditMode && history.length === 0 && } {isInEditMode && history.length > 0 ? null : ( { - isInEditMode - ? sendEditInput(editorContent, modifiers, editor) - : sendChatInput(editorContent, modifiers); - }} + onEnter={sendInput} /> )} @@ -499,13 +482,13 @@ export function Chat() { ); if (isInEditMode) { - dispatch(completeEdit()); + dispatch(exitEditMode()); } }} className="flex items-center gap-2" > - {returnToLastSessinButtonText} + {returnToLastSessionButtonText}
    ) : null} @@ -513,7 +496,7 @@ export function Chat() { - {hasPendingApplies && isSingleRangeEdit && ( + {hasPendingApplies && isSingleRangeEditOrInsertion && ( { @@ -521,7 +504,7 @@ export function Chat() { console.error(`Failed to load last session: ${e}`), ); - dispatch(completeEdit()); + dispatch(exitEditMode()); }} /> )} diff --git a/gui/src/redux/slices/sessionSlice.ts b/gui/src/redux/slices/sessionSlice.ts index ef712b7bf..d3d4f0da4 100644 --- a/gui/src/redux/slices/sessionSlice.ts +++ b/gui/src/redux/slices/sessionSlice.ts @@ -51,7 +51,22 @@ type SessionState = { }; function isCodeToEditEqual(a: CodeToEdit, b: CodeToEdit) { - return a.filepath === b.filepath && a.contents === b.contents; + if (a.filepath !== b.filepath || a.contents !== b.contents) { + return false; + } + + if ("range" in a && "range" in b) { + const rangeA = a.range; + const rangeB = b.range; + + return ( + rangeA.start.line === rangeB.start.line && + rangeA.end.line === rangeB.end.line + ); + } + + // If neither has a range, they are considered equal in this context + return !("range" in a) && !("range" in b); } function getBaseHistoryItem(): ChatHistoryItemWithMessageId { @@ -500,12 +515,16 @@ export const sessionSlice = createSlice({ selectIsInEditMode: (state) => { return state.mode === "edit"; }, - selectIsSingleRangeEdit: (state) => { - return ( - state.mode === "edit" && - (state.codeToEdit.length === 0 || - (state.codeToEdit.length === 1 && "range" in state.codeToEdit[0])) - ); + selectIsSingleRangeEditOrInsertion: (state) => { + if (state.mode !== "edit") { + return false; + } + + const isInsertion = state.codeToEdit.length === 0; + const selectIsSingleRangeEdit = + state.codeToEdit.length === 1 && "range" in state.codeToEdit[0]; + + return selectIsSingleRangeEdit || isInsertion; }, }, extraReducers: (builder) => { @@ -579,7 +598,7 @@ export const { export const { selectIsGatheringContext, selectIsInEditMode, - selectIsSingleRangeEdit, + selectIsSingleRangeEditOrInsertion, } = sessionSlice.selectors; export default sessionSlice.reducer; diff --git a/gui/src/redux/thunks/completeEdit.ts b/gui/src/redux/thunks/exitEditMode.ts similarity index 61% rename from gui/src/redux/thunks/completeEdit.ts rename to gui/src/redux/thunks/exitEditMode.ts index 57aee7c03..9faba123a 100644 --- a/gui/src/redux/thunks/completeEdit.ts +++ b/gui/src/redux/thunks/exitEditMode.ts @@ -2,16 +2,19 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; import { clearCodeToEdit, selectIsInEditMode, + selectIsSingleRangeEditOrInsertion, setMode, } from "../slices/sessionSlice"; import { ThunkApiType } from "../store"; import { setEditDone } from "../slices/editModeState"; -export const completeEdit = createAsyncThunk( +export const exitEditMode = createAsyncThunk( "edit/complete", async (_, { dispatch, extra, getState }) => { const state = getState(); const isInEditMode = selectIsInEditMode(state); + const isSingleRangeEditOrInsertion = + selectIsSingleRangeEditOrInsertion(state); if (!isInEditMode) { return; @@ -19,8 +22,10 @@ export const completeEdit = createAsyncThunk( dispatch(setMode("chat")); dispatch(clearCodeToEdit()); - setEditDone(); + dispatch(setEditDone()); - extra.ideMessenger.post("edit/escape", undefined); + extra.ideMessenger.post("edit/exit", { + shouldFocusEditor: isSingleRangeEditOrInsertion, + }); }, ); diff --git a/gui/src/redux/thunks/index.ts b/gui/src/redux/thunks/index.ts index fda8d375d..c35189de3 100644 --- a/gui/src/redux/thunks/index.ts +++ b/gui/src/redux/thunks/index.ts @@ -1,4 +1,4 @@ -export { completeEdit } from "./completeEdit"; +export { exitEditMode } from "./exitEditMode"; export { gatherContext } from "./gatherContext"; export { handleErrors } from "./handleErrors"; export { resetStateForNewMessage } from "./resetStateForNewMessage";