From ec31735f557ba00edb455740947b4f4e0640f191 Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 26 Sep 2024 18:18:28 -0700 Subject: [PATCH 01/12] remove top codelens --- .../providers/VerticalPerLineCodeLensProvider.ts | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/extensions/vscode/src/lang-server/codeLens/providers/VerticalPerLineCodeLensProvider.ts b/extensions/vscode/src/lang-server/codeLens/providers/VerticalPerLineCodeLensProvider.ts index 94a68bac7..aec789bbe 100644 --- a/extensions/vscode/src/lang-server/codeLens/providers/VerticalPerLineCodeLensProvider.ts +++ b/extensions/vscode/src/lang-server/codeLens/providers/VerticalPerLineCodeLensProvider.ts @@ -43,21 +43,6 @@ export class VerticalDiffCodeLensProvider implements vscode.CodeLensProvider { start.translate(block.numGreen + block.numRed), ); - if (isFirstBlockInMultiBlockDocument) { - codeLenses.push( - new vscode.CodeLens(range, { - title: `Accept All (${getMetaKeyLabel()}⇧↩)`, - command: "continue.acceptDiff", - arguments: [filepath, i], - }), - new vscode.CodeLens(range, { - title: `Reject All (${getMetaKeyLabel()}⇧⌫)`, - command: "continue.rejectDiff", - arguments: [filepath, i], - }), - ); - } - codeLenses.push( new vscode.CodeLens(range, { title: `Accept${ From 2562dcb850efe001a334ede0ec8d0b15423a5efc Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 26 Sep 2024 18:26:23 -0700 Subject: [PATCH 02/12] fix debouncing to get correct value for codeblock toolbar --- .../components/markdown/PreWithToolbar.tsx | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/gui/src/components/markdown/PreWithToolbar.tsx b/gui/src/components/markdown/PreWithToolbar.tsx index 903d7cb7d..3f650740d 100644 --- a/gui/src/components/markdown/PreWithToolbar.tsx +++ b/gui/src/components/markdown/PreWithToolbar.tsx @@ -2,7 +2,7 @@ import { debounce } from "lodash"; import { useEffect, useState } from "react"; import { useSelector } from "react-redux"; import styled from "styled-components"; -import { defaultBorderRadius, lightGray } from ".."; +import { defaultBorderRadius } from ".."; import useUIConfig from "../../hooks/useUIConfig"; import { RootState } from "../../redux/store"; import CodeBlockToolBar from "./CodeBlockToolbar"; @@ -47,16 +47,20 @@ function PreWithToolbar(props: { ); useEffect(() => { - const debouncedEffect = debounce(() => { + if (copyValue === "") { setCopyValue(childrenToText(props.children.props.children)); - }, 100); + } else { + const debouncedEffect = debounce(() => { + setCopyValue(childrenToText(props.children.props.children)); + }, 100); - debouncedEffect(); + debouncedEffect(); - return () => { - debouncedEffect.cancel(); - }; - }, [props.children]); + return () => { + debouncedEffect.cancel(); + }; + } + }, [props.children, copyValue]); return ( Date: Thu, 26 Sep 2024 18:35:01 -0700 Subject: [PATCH 03/12] resolve abs path to allow relative paths to link from the gui --- extensions/vscode/src/util/ideUtils.ts | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/extensions/vscode/src/util/ideUtils.ts b/extensions/vscode/src/util/ideUtils.ts index a2f2e3f9f..b71f8e095 100644 --- a/extensions/vscode/src/util/ideUtils.ts +++ b/extensions/vscode/src/util/ideUtils.ts @@ -136,10 +136,30 @@ export class VsCodeIdeUtils { ); } - openFile(filepath: string, range?: vscode.Range) { + private async resolveAbsFilepathInWorkspace( + filepath: string, + ): Promise { + // If the filepath is already absolute, return it as is + if (this.path.isAbsolute(filepath)) { + return filepath; + } + + // Try to resolve for each workspace directory + const workspaceDirectories = this.getWorkspaceDirectories(); + for (const dir of workspaceDirectories) { + const resolvedPath = this.path.resolve(dir, filepath); + if (await this.fileExists(resolvedPath)) { + return resolvedPath; + } + } + + return filepath; + } + + async openFile(filepath: string, range?: vscode.Range) { // vscode has a builtin open/get open files return openEditorAndRevealRange( - filepath, + await this.resolveAbsFilepathInWorkspace(filepath), range, vscode.ViewColumn.One, false, From fee6a1e5ceb89c44a018b6a771065dea276268a9 Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 26 Sep 2024 18:47:18 -0700 Subject: [PATCH 04/12] remove bottom message div --- gui/src/components/Layout.tsx | 58 ++------------------- gui/src/redux/selectors/uiStateSelectors.ts | 5 -- gui/src/redux/slices/uiStateSlice.ts | 31 +---------- 3 files changed, 6 insertions(+), 88 deletions(-) delete mode 100644 gui/src/redux/selectors/uiStateSelectors.ts diff --git a/gui/src/components/Layout.tsx b/gui/src/components/Layout.tsx index d012194a3..a75fe834e 100644 --- a/gui/src/components/Layout.tsx +++ b/gui/src/components/Layout.tsx @@ -3,31 +3,22 @@ import { useContext, useEffect } from "react"; import { useDispatch, useSelector } from "react-redux"; import { Outlet, useLocation, useNavigate } from "react-router-dom"; import styled from "styled-components"; -import { - CustomScrollbarDiv, - defaultBorderRadius, - vscForeground, - vscInputBackground, -} from "."; +import { CustomScrollbarDiv, defaultBorderRadius, vscInputBackground } from "."; import { IdeMessengerContext } from "../context/IdeMessenger"; import { useWebviewListener } from "../hooks/useWebviewListener"; import { defaultModelSelector } from "../redux/selectors/modelSelectors"; +import { setShowDialog } from "../redux/slices/uiStateSlice"; import { RootState } from "../redux/store"; import { getFontSize, isMetaEquivalentKeyPressed } from "../util"; import { FREE_TRIAL_LIMIT_REQUESTS } from "../util/freeTrial"; import { getLocalStorage, setLocalStorage } from "../util/localStorage"; +import ButtonWithTooltip from "./ButtonWithTooltip"; import TextDialog from "./dialogs"; import ProgressBar from "./loaders/ProgressBar"; +import { useOnboardingCard } from "./OnboardingCard"; +import { isNewUserOnboarding } from "./OnboardingCard/utils"; import PostHogPageView from "./PosthogPageView"; import ProfileSwitcher from "./ProfileSwitcher"; -import { isNewUserOnboarding } from "./OnboardingCard/utils"; -import { useOnboardingCard } from "./OnboardingCard"; -import { - setBottomMessage, - setBottomMessageCloseTimeout, - setShowDialog, -} from "../redux/slices/uiStateSlice"; -import ButtonWithTooltip from "./ButtonWithTooltip"; const FOOTER_HEIGHT = "1.8em"; @@ -48,23 +39,6 @@ const LayoutTopDiv = styled(CustomScrollbarDiv)` } `; -const BottomMessageDiv = styled.div<{ displayOnBottom: boolean }>` - position: fixed; - bottom: ${(props) => (props.displayOnBottom ? "50px" : undefined)}; - top: ${(props) => (props.displayOnBottom ? undefined : "50px")}; - left: 0; - right: 0; - margin: 8px; - margin-top: 0; - background-color: ${vscInputBackground}; - color: ${vscForeground}; - border-radius: ${defaultBorderRadius}; - padding: 12px; - z-index: 100; - box-shadow: 0px 0px 2px 0px ${vscForeground}; - max-height: 35vh; -`; - const Footer = styled.footer` display: flex; flex-direction: row; @@ -120,13 +94,6 @@ const Layout = () => { const defaultModel = useSelector(defaultModelSelector); - const bottomMessage = useSelector( - (state: RootState) => state.uiState.bottomMessage, - ); - const displayBottomMessageOnBottom = useSelector( - (state: RootState) => state.uiState.displayBottomMessageOnBottom, - ); - const timeline = useSelector((state: RootState) => state.state.history); useEffect(() => { @@ -264,21 +231,6 @@ const Layout = () => { - - { - dispatch(setBottomMessageCloseTimeout(undefined)); - }} - onMouseLeave={(e) => { - if (!e.buttons) { - dispatch(setBottomMessage(undefined)); - } - }} - hidden={!bottomMessage} - > - {bottomMessage} -
state.uiState.bottomMessage; - -export { selectBottomMessage }; diff --git a/gui/src/redux/slices/uiStateSlice.ts b/gui/src/redux/slices/uiStateSlice.ts index d4c4f7390..94a5f2934 100644 --- a/gui/src/redux/slices/uiStateSlice.ts +++ b/gui/src/redux/slices/uiStateSlice.ts @@ -5,9 +5,6 @@ import { } from "../../components/OnboardingCard"; type UiState = { - bottomMessage: JSX.Element | undefined; - bottomMessageCloseTimeout: NodeJS.Timeout | undefined; - displayBottomMessageOnBottom: boolean; showDialog: boolean; dialogMessage: string | JSX.Element | undefined; dialogEntryOn: boolean; @@ -18,12 +15,9 @@ type UiState = { export const uiStateSlice = createSlice({ name: "uiState", initialState: { - bottomMessage: undefined, - bottomMessageCloseTimeout: undefined, showDialog: false, dialogMessage: "", dialogEntryOn: false, - displayBottomMessageOnBottom: true, nextCodeBlockToApplyIndex: 0, onboardingCard: defaultOnboardingCardState, } as UiState, @@ -34,21 +28,6 @@ export const uiStateSlice = createSlice({ ) => { state.onboardingCard = { ...state.onboardingCard, ...action.payload }; }, - setBottomMessage: ( - state, - action: PayloadAction, - ) => { - state.bottomMessage = action.payload; - }, - setBottomMessageCloseTimeout: ( - state, - action: PayloadAction, - ) => { - if (state.bottomMessageCloseTimeout) { - clearTimeout(state.bottomMessageCloseTimeout); - } - state.bottomMessageCloseTimeout = action.payload; - }, setDialogMessage: ( state, action: PayloadAction, @@ -64,12 +43,7 @@ export const uiStateSlice = createSlice({ setShowDialog: (state, action: PayloadAction) => { state.showDialog = action.payload; }, - setDisplayBottomMessageOnBottom: ( - state, - action: PayloadAction, - ) => { - state.displayBottomMessageOnBottom = action.payload; - }, + resetNextCodeBlockToApplyIndex: (state) => { state.nextCodeBlockToApplyIndex = 0; }, @@ -86,9 +60,6 @@ export const { setShowDialog, resetNextCodeBlockToApplyIndex, incrementNextCodeBlockToApplyIndex, - setBottomMessage, - setBottomMessageCloseTimeout, - setDisplayBottomMessageOnBottom, } = uiStateSlice.actions; export default uiStateSlice.reducer; From 55270279f6f7516769b2ec8c586f9a83d271ea2d Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 26 Sep 2024 20:07:25 -0700 Subject: [PATCH 05/12] apply status and corresponding ui --- core/protocol/ideWebview.ts | 8 +- extensions/vscode/src/commands.ts | 1 + .../vscode/src/diff/vertical/handler.ts | 6 ++ .../vscode/src/diff/vertical/manager.ts | 23 ++++- .../vscode/src/extension/VsCodeExtension.ts | 5 +- .../vscode/src/extension/VsCodeMessenger.ts | 8 +- .../VerticalPerLineCodeLensProvider.ts | 19 +--- .../src/quickEdit/QuickEditQuickPick.ts | 1 + gui/src/components/Layout.tsx | 10 ++- .../components/markdown/CodeBlockToolbar.tsx | 86 +++++++++++++++---- gui/src/redux/slices/uiStateSlice.ts | 17 ++++ 11 files changed, 147 insertions(+), 37 deletions(-) diff --git a/core/protocol/ideWebview.ts b/core/protocol/ideWebview.ts index 8ccc6d007..774da7ee5 100644 --- a/core/protocol/ideWebview.ts +++ b/core/protocol/ideWebview.ts @@ -15,7 +15,7 @@ export type ToIdeFromWebviewProtocol = ToIdeFromWebviewOrCoreProtocol & { }, ]; openUrl: [string, void]; - applyToCurrentFile: [{ text: string }, void]; + applyToCurrentFile: [{ text: string; streamId: string }, void]; showTutorial: [undefined, void]; showFile: [{ filepath: string }, void]; openConfigJson: [undefined, void]; @@ -32,6 +32,11 @@ export type ToIdeFromWebviewProtocol = ToIdeFromWebviewOrCoreProtocol & { rejectDiff: [{ filepath: string }, void]; }; +export interface ApplyState { + streamId: string; + status: "streaming" | "done" | "closed"; +} + export type ToWebviewFromIdeProtocol = ToWebviewFromIdeOrCoreProtocol & { setInactive: [undefined, void]; setTTSActive: [boolean, void]; @@ -65,4 +70,5 @@ export type ToWebviewFromIdeProtocol = ToWebviewFromIdeOrCoreProtocol & { incrementFtc: [undefined, void]; openOnboardingCard: [undefined, void]; applyCodeFromChat: [undefined, void]; + updateApplyState: [ApplyState, void]; }; diff --git a/extensions/vscode/src/commands.ts b/extensions/vscode/src/commands.ts index 6bec8b754..17140eaf8 100644 --- a/extensions/vscode/src/commands.ts +++ b/extensions/vscode/src/commands.ts @@ -223,6 +223,7 @@ const commandsMap: ( await verticalDiffManager.streamEdit( config.experimental?.contextMenuPrompts?.[promptName] ?? fallbackPrompt, modelTitle, + undefined, onlyOneInsertion, undefined, range, diff --git a/extensions/vscode/src/diff/vertical/handler.ts b/extensions/vscode/src/diff/vertical/handler.ts index 9259cb38e..5aa5b070b 100644 --- a/extensions/vscode/src/diff/vertical/handler.ts +++ b/extensions/vscode/src/diff/vertical/handler.ts @@ -1,4 +1,5 @@ import type { DiffLine } from "core"; +import { ApplyState } from "core/protocol/ideWebview"; import * as vscode from "vscode"; import { DecorationTypeRangeManager, @@ -12,6 +13,7 @@ import type { VerticalDiffCodeLens } from "./manager"; export interface VerticalDiffHandlerOptions { input?: string; instant?: boolean; + onStatusUpdate: (status: ApplyState["status"]) => void; } export class VerticalDiffHandler implements vscode.Disposable { @@ -268,6 +270,8 @@ export class VerticalDiffHandler implements vscode.Disposable { }, ); + this.options.onStatusUpdate("closed"); + this.cancelled = true; this.refreshCodeLens(); this.dispose(); @@ -352,6 +356,8 @@ export class VerticalDiffHandler implements vscode.Disposable { this.refreshCodeLens(); + this.options.onStatusUpdate("done"); + // Reject on user typing // const listener = vscode.workspace.onDidChangeTextDocument((e) => { // if (e.document.uri.fsPath === this.filepath) { diff --git a/extensions/vscode/src/diff/vertical/manager.ts b/extensions/vscode/src/diff/vertical/manager.ts index c84fa4397..2ecf6d596 100644 --- a/extensions/vscode/src/diff/vertical/manager.ts +++ b/extensions/vscode/src/diff/vertical/manager.ts @@ -5,6 +5,7 @@ import { getMarkdownLanguageTagForFile } from "core/util"; import { DiffLine } from "core"; import { streamDiffLines } from "core/edit/streamDiffLines"; import * as vscode from "vscode"; +import { VsCodeWebviewProtocol } from "../../webviewProtocol"; import { VerticalDiffHandler, VerticalDiffHandlerOptions } from "./handler"; export interface VerticalDiffCodeLens { @@ -22,7 +23,10 @@ export class VerticalDiffManager { private userChangeListener: vscode.Disposable | undefined; - constructor(private readonly configHandler: ConfigHandler) { + constructor( + private readonly configHandler: ConfigHandler, + private readonly webviewProtocol: VsCodeWebviewProtocol, + ) { this.userChangeListener = undefined; } @@ -176,6 +180,7 @@ export class VerticalDiffManager { async streamDiffLines( diffStream: AsyncGenerator, instant: boolean, + streamId: string, ) { vscode.commands.executeCommand("setContext", "continue.diffVisible", true); @@ -205,6 +210,11 @@ export class VerticalDiffManager { endLine, { instant, + onStatusUpdate: (status) => + this.webviewProtocol.request("updateApplyState", { + streamId, + status, + }), }, ); @@ -271,6 +281,7 @@ export class VerticalDiffManager { async streamEdit( input: string, modelTitle: string | undefined, + streamId?: string, onlyOneInsertion?: boolean, quickEdit?: string, range?: vscode.Range, @@ -339,7 +350,15 @@ export class VerticalDiffManager { filepath, startLine, endLine, - { input }, + { + input, + onStatusUpdate: (status) => + streamId && + this.webviewProtocol.request("updateApplyState", { + streamId, + status, + }), + }, ); if (!diffHandler) { diff --git a/extensions/vscode/src/extension/VsCodeExtension.ts b/extensions/vscode/src/extension/VsCodeExtension.ts index 68714b106..ce12a0a6b 100644 --- a/extensions/vscode/src/extension/VsCodeExtension.ts +++ b/extensions/vscode/src/extension/VsCodeExtension.ts @@ -124,7 +124,10 @@ export class VsCodeExtension { resolveConfigHandler?.(this.configHandler); this.configHandler.reloadConfig(); - this.verticalDiffManager = new VerticalDiffManager(this.configHandler); + this.verticalDiffManager = new VerticalDiffManager( + this.configHandler, + this.sidebar.webviewProtocol, + ); resolveVerticalDiffManager?.(this.verticalDiffManager); this.tabAutocompleteModel = new TabAutocompleteModel(this.configHandler); diff --git a/extensions/vscode/src/extension/VsCodeMessenger.ts b/extensions/vscode/src/extension/VsCodeMessenger.ts index 0e5b91b88..a50e389b5 100644 --- a/extensions/vscode/src/extension/VsCodeMessenger.ts +++ b/extensions/vscode/src/extension/VsCodeMessenger.ts @@ -191,10 +191,14 @@ export class VsCodeMessenger { ); const verticalDiffManager = await this.verticalDiffManagerPromise; if (instant) { - verticalDiffManager.streamDiffLines(diffLines, instant); + verticalDiffManager.streamDiffLines( + diffLines, + instant, + msg.data.streamId, + ); } else { const prompt = `The following code was suggested as an edit:\n\`\`\`\n${msg.data.text}\n\`\`\`\nPlease apply it to the previous code.`; - verticalDiffManager.streamEdit(prompt, llm.title); + verticalDiffManager.streamEdit(prompt, llm.title, msg.data.streamId); } }); diff --git a/extensions/vscode/src/lang-server/codeLens/providers/VerticalPerLineCodeLensProvider.ts b/extensions/vscode/src/lang-server/codeLens/providers/VerticalPerLineCodeLensProvider.ts index aec789bbe..a96cf5660 100644 --- a/extensions/vscode/src/lang-server/codeLens/providers/VerticalPerLineCodeLensProvider.ts +++ b/extensions/vscode/src/lang-server/codeLens/providers/VerticalPerLineCodeLensProvider.ts @@ -1,6 +1,6 @@ import * as vscode from "vscode"; import { VerticalDiffCodeLens } from "../../../diff/vertical/manager"; -import { getAltOrOption, getMetaKeyLabel } from "../../../util/util"; +import { getMetaKeyLabel } from "../../../util/util"; export class VerticalDiffCodeLensProvider implements vscode.CodeLensProvider { private _eventEmitter: vscode.EventEmitter = @@ -33,9 +33,6 @@ export class VerticalDiffCodeLensProvider implements vscode.CodeLensProvider { const codeLenses: vscode.CodeLens[] = []; for (let i = 0; i < blocks.length; i++) { - const isFirstBlockInMultiBlockDocument = - codeLenses.length === 0 && blocks.length > 1; - const block = blocks[i]; const start = new vscode.Position(block.start, 0); const range = new vscode.Range( @@ -45,26 +42,18 @@ export class VerticalDiffCodeLensProvider implements vscode.CodeLensProvider { codeLenses.push( new vscode.CodeLens(range, { - title: `Accept${ - codeLenses.length === 2 - ? ` (${getAltOrOption()}${getMetaKeyLabel()}Y)` - : "" - }`, + title: `Accept`, command: "continue.acceptVerticalDiffBlock", arguments: [filepath, i], }), new vscode.CodeLens(range, { - title: `Reject${ - codeLenses.length === 2 - ? ` (${getAltOrOption()}${getMetaKeyLabel()}N)` - : "" - }`, + title: `Reject`, command: "continue.rejectVerticalDiffBlock", arguments: [filepath, i], }), ); - if (codeLenses.length === 4) { + if (codeLenses.length === 2) { codeLenses.push( new vscode.CodeLens(range, { title: `${getMetaKeyLabel()}I to add instructions`, diff --git a/extensions/vscode/src/quickEdit/QuickEditQuickPick.ts b/extensions/vscode/src/quickEdit/QuickEditQuickPick.ts index 877d94ad3..0c0b276a8 100644 --- a/extensions/vscode/src/quickEdit/QuickEditQuickPick.ts +++ b/extensions/vscode/src/quickEdit/QuickEditQuickPick.ts @@ -296,6 +296,7 @@ export class QuickEdit { prompt, modelTitle, undefined, + undefined, this.previousInput, this.range, ); diff --git a/gui/src/components/Layout.tsx b/gui/src/components/Layout.tsx index a75fe834e..13cc1fc93 100644 --- a/gui/src/components/Layout.tsx +++ b/gui/src/components/Layout.tsx @@ -7,7 +7,7 @@ import { CustomScrollbarDiv, defaultBorderRadius, vscInputBackground } from "."; import { IdeMessengerContext } from "../context/IdeMessenger"; import { useWebviewListener } from "../hooks/useWebviewListener"; import { defaultModelSelector } from "../redux/selectors/modelSelectors"; -import { setShowDialog } from "../redux/slices/uiStateSlice"; +import { setShowDialog, updateApplyState } from "../redux/slices/uiStateSlice"; import { RootState } from "../redux/store"; import { getFontSize, isMetaEquivalentKeyPressed } from "../util"; import { FREE_TRIAL_LIMIT_REQUESTS } from "../util/freeTrial"; @@ -154,6 +154,14 @@ const Layout = () => { [], ); + useWebviewListener( + "updateApplyState", + async (state) => { + dispatch(updateApplyState(state)); + }, + [], + ); + useWebviewListener( "openOnboardingCard", async () => { diff --git a/gui/src/components/markdown/CodeBlockToolbar.tsx b/gui/src/components/markdown/CodeBlockToolbar.tsx index 510e70b2b..3175f5361 100644 --- a/gui/src/components/markdown/CodeBlockToolbar.tsx +++ b/gui/src/components/markdown/CodeBlockToolbar.tsx @@ -2,13 +2,15 @@ import { ArrowLeftEndOnRectangleIcon, CheckIcon, ClipboardIcon, - PlayIcon, CommandLineIcon, + PlayIcon, XMarkIcon, } from "@heroicons/react/24/outline"; -import { useContext, useState } from "react"; -import { useDispatch } from "react-redux"; +import { getBasename } from "core/util"; +import { useContext, useRef, useState } from "react"; +import { useDispatch, useSelector } from "react-redux"; import styled from "styled-components"; +import { v4 as uuidv4 } from "uuid"; import { defaultBorderRadius, lightGray, @@ -17,13 +19,16 @@ import { } from ".."; import { IdeMessengerContext } from "../../context/IdeMessenger"; import { useWebviewListener } from "../../hooks/useWebviewListener"; -import { incrementNextCodeBlockToApplyIndex } from "../../redux/slices/uiStateSlice"; +import { + incrementNextCodeBlockToApplyIndex, + updateApplyState, +} from "../../redux/slices/uiStateSlice"; +import { RootState } from "../../redux/store"; import { getFontSize, getMetaKeyLabel, isJetBrains } from "../../util"; -import FileIcon from "../FileIcon"; import ButtonWithTooltip from "../ButtonWithTooltip"; +import FileIcon from "../FileIcon"; import { CopyButton as CopyButtonHeader } from "./CopyButton"; import { ToolbarButtonWithTooltip } from "./ToolbarButtonWithTooltip"; -import { getBasename } from "core/util"; const ToolbarDiv = styled.div` display: flex; @@ -123,16 +128,28 @@ function CodeBlockToolBar(props: CodeBlockToolBarProps) { const ideMessenger = useContext(IdeMessengerContext); const dispatch = useDispatch(); const isTerminal = isTerminalCodeBlock(props.language, props.text); - const [showAcceptReject, setShowAcceptReject] = useState(false); const [isCopied, setIsCopied] = useState(false); const [isApplying, setIsApplying] = useState(false); + const streamIdRef = useRef(null); + if (streamIdRef.current === null) { + streamIdRef.current = uuidv4(); + } + + const applyState = useSelector( + (store: RootState) => + store.uiState.applyStates.find( + (state) => state.streamId === streamIdRef.current, + )?.status ?? "closed", + ); + // Handle apply keyboard shortcut useWebviewListener( "applyCodeFromChat", async () => { await ideMessenger.request("applyToCurrentFile", { text: props.text, + streamId: streamIdRef.current, }); dispatch(incrementNextCodeBlockToApplyIndex({})); }, @@ -157,10 +174,16 @@ function CodeBlockToolBar(props: CodeBlockToolBarProps) { if (isTerminal) { ideMessenger.ide.runCommand(getTerminalCommand(props.text)); } else { - ideMessenger.post("applyToCurrentFile", { text: props.text }); - setIsApplying(true); - setTimeout(() => setIsApplying(false), 2000); - setShowAcceptReject(true); + ideMessenger.post("applyToCurrentFile", { + text: props.text, + streamId: streamIdRef.current, + }); + dispatch( + updateApplyState({ + streamId: streamIdRef.current, + status: "streaming", + }), + ); } } @@ -173,12 +196,22 @@ function CodeBlockToolBar(props: CodeBlockToolBarProps) { function onClickAccept() { ideMessenger.post("acceptDiff", { filepath: props.filepath }); - setShowAcceptReject(false); + dispatch( + updateApplyState({ + streamId: streamIdRef.current, + status: "closed", + }), + ); } function onClickReject() { ideMessenger.post("rejectDiff", { filepath: props.filepath }); - setShowAcceptReject(false); + dispatch( + updateApplyState({ + streamId: streamIdRef.current, + status: "closed", + }), + ); } if (!props.filepath) { @@ -246,7 +279,7 @@ function CodeBlockToolBar(props: CodeBlockToolBarProps) { {!isJetBrains() && (
- {!showAcceptReject ? ( + {applyState === "closed" ? ( Apply
- ) : ( + ) : applyState === "done" ? ( <> + ) : ( +
+ + + + +
)}
)} diff --git a/gui/src/redux/slices/uiStateSlice.ts b/gui/src/redux/slices/uiStateSlice.ts index 94a5f2934..1625110eb 100644 --- a/gui/src/redux/slices/uiStateSlice.ts +++ b/gui/src/redux/slices/uiStateSlice.ts @@ -1,4 +1,5 @@ import { createSlice, PayloadAction } from "@reduxjs/toolkit"; +import { ApplyState } from "core/protocol/ideWebview"; import { defaultOnboardingCardState, OnboardingCardState, @@ -10,6 +11,7 @@ type UiState = { dialogEntryOn: boolean; nextCodeBlockToApplyIndex: number; onboardingCard: OnboardingCardState; + applyStates: ApplyState[]; }; export const uiStateSlice = createSlice({ @@ -20,6 +22,7 @@ export const uiStateSlice = createSlice({ dialogEntryOn: false, nextCodeBlockToApplyIndex: 0, onboardingCard: defaultOnboardingCardState, + applyStates: [], } as UiState, reducers: { setOnboardingCard: ( @@ -50,6 +53,19 @@ export const uiStateSlice = createSlice({ incrementNextCodeBlockToApplyIndex: (state, action) => { state.nextCodeBlockToApplyIndex++; }, + updateApplyState: (state, action: PayloadAction) => { + const { streamId, status } = action.payload; + const index = state.applyStates.findIndex( + (applyState) => applyState.streamId === streamId, + ); + if (status === "closed" && index !== -1) { + state.applyStates.splice(index, 1); + } else if (index === -1) { + state.applyStates.push(action.payload); + } else { + state.applyStates[index].status = status; + } + }, }, }); @@ -60,6 +76,7 @@ export const { setShowDialog, resetNextCodeBlockToApplyIndex, incrementNextCodeBlockToApplyIndex, + updateApplyState, } = uiStateSlice.actions; export default uiStateSlice.reducer; From 4d299ba2cd03a05046a56cf77c9d957ea7fcd41b Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 26 Sep 2024 20:18:05 -0700 Subject: [PATCH 06/12] apply to correct range --- .../vscode/src/extension/VsCodeMessenger.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/extensions/vscode/src/extension/VsCodeMessenger.ts b/extensions/vscode/src/extension/VsCodeMessenger.ts index a50e389b5..c0b62fdbc 100644 --- a/extensions/vscode/src/extension/VsCodeMessenger.ts +++ b/extensions/vscode/src/extension/VsCodeMessenger.ts @@ -198,7 +198,23 @@ export class VsCodeMessenger { ); } else { const prompt = `The following code was suggested as an edit:\n\`\`\`\n${msg.data.text}\n\`\`\`\nPlease apply it to the previous code.`; - verticalDiffManager.streamEdit(prompt, llm.title, msg.data.streamId); + const fullEditorRange = new vscode.Range( + 0, + 0, + editor.document.lineCount - 1, + editor.document.lineAt(editor.document.lineCount - 1).text.length, + ); + const rangeToApplyTo = editor.selection.isEmpty + ? fullEditorRange + : editor.selection; + verticalDiffManager.streamEdit( + prompt, + llm.title, + msg.data.streamId, + undefined, + undefined, + rangeToApplyTo, + ); } }); From 526fb7fa9388292475b1b4d3c264cac4293f8483 Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 26 Sep 2024 21:06:28 -0700 Subject: [PATCH 07/12] rename acc to replacements --- core/edit/lazy/deterministic.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/core/edit/lazy/deterministic.ts b/core/edit/lazy/deterministic.ts index aa03c086b..3fd4ffe73 100644 --- a/core/edit/lazy/deterministic.ts +++ b/core/edit/lazy/deterministic.ts @@ -135,10 +135,10 @@ export async function deterministicApplyLazyEdit( } } - const acc: AstReplacements = []; - findLazyBlockReplacements(oldTree.rootNode, newTree.rootNode, acc); + const replacements: AstReplacements = []; + findLazyBlockReplacements(oldTree.rootNode, newTree.rootNode, replacements); - const newFullFile = reconstructNewFile(oldFile, newLazyFile, acc); + const newFullFile = reconstructNewFile(oldFile, newLazyFile, replacements); const diff = myersDiff(oldFile, newFullFile); // If the diff is too messy and seems likely borked, we fall back to LLM strategy @@ -204,7 +204,7 @@ function nodesAreExact(a: Parser.SyntaxNode, b: Parser.SyntaxNode): boolean { function findLazyBlockReplacements( oldNode: Parser.SyntaxNode, newNode: Parser.SyntaxNode, - acc: AstReplacements, + replacements: AstReplacements, ): void { // Base case if (nodesAreExact(oldNode, newNode)) { @@ -254,7 +254,7 @@ function findLazyBlockReplacements( } // then recurse at the match - findLazyBlockReplacements(L, rightChildren[0], acc); + findLazyBlockReplacements(L, rightChildren[0], replacements); // then consume L and R leftChildren.shift(); @@ -263,7 +263,7 @@ function findLazyBlockReplacements( // Exit "lazy mode" if (isLazy) { // Record the replacement lines - acc.push({ + replacements.push({ nodeToReplace: currentLazyBlockNode!, replacementNodes: [...currentLazyBlockReplacementNodes], }); @@ -275,7 +275,7 @@ function findLazyBlockReplacements( } if (isLazy) { - acc.push({ + replacements.push({ nodeToReplace: currentLazyBlockNode!, replacementNodes: [...currentLazyBlockReplacementNodes, ...leftChildren], }); @@ -284,7 +284,7 @@ function findLazyBlockReplacements( // Cut out any extraneous lazy blocks for (const R of rightChildren) { if (isLazyBlock(R)) { - acc.push({ + replacements.push({ nodeToReplace: R, replacementNodes: [], }); From 14523ecbad36faa0e9e66f9ed6a9c068e32bd937 Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 26 Sep 2024 21:06:37 -0700 Subject: [PATCH 08/12] improve instructions for lazy comment --- core/llm/constructMessages.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/core/llm/constructMessages.ts b/core/llm/constructMessages.ts index d339134e3..13d538be8 100644 --- a/core/llm/constructMessages.ts +++ b/core/llm/constructMessages.ts @@ -5,9 +5,10 @@ const SYSTEM_MESSAGE = `When generating new code: 1. Always produce a single code block. 2. Never separate the code into multiple code blocks. 3. Only include the code that is being added. -4. Replace existing code with a "lazy" block like this: "// ... existing code ..." -5. You must always provide 1-2 lines of context above and below a "lazy" block -6. If the user submits a code block that contains a filename in the language specifier, always include the filename in any code block you generate based on that file. The filename should be on the same line as the language specifier in your code block. +4. Replace existing code with a "lazy" comment like this: "// ... existing code ..." +5. The "lazy" comment must always be a valid comment in the current context (e.g. "" for HTML, "// ... existing code ..." for JavaScript, "{/* ... existing code */}" for TSX, etc.) +6. You must always provide 1-2 lines of context above and below a "lazy" comment +7. If the user submits a code block that contains a filename in the language specifier, always include the filename in any code block you generate based on that file. The filename should be on the same line as the language specifier in your code block. Example 1: Input: From 9f44f829590c0f2af12bc88c087ecab3adad85a8 Mon Sep 17 00:00:00 2001 From: Nate Date: Fri, 27 Sep 2024 14:08:52 -0700 Subject: [PATCH 09/12] improved matching for tsx/html files --- core/edit/lazy/deterministic.test.ts | 4 + core/edit/lazy/deterministic.ts | 34 +++- .../test-examples/migration-page.tsx.diff | 166 ++++++++++++++++++ 3 files changed, 202 insertions(+), 2 deletions(-) create mode 100644 core/edit/lazy/test-examples/migration-page.tsx.diff diff --git a/core/edit/lazy/deterministic.test.ts b/core/edit/lazy/deterministic.test.ts index e3db3f280..7194c0083 100644 --- a/core/edit/lazy/deterministic.test.ts +++ b/core/edit/lazy/deterministic.test.ts @@ -154,4 +154,8 @@ describe("deterministicApplyLazyEdit(", () => { test("no lazy blocks in single top level class", async () => { await expectDiff("no-lazy-single-class.js"); }); + + test("should acknowledge jsx_expression lazy comments", async () => { + await expectDiff("migration-page.tsx"); + }); }); diff --git a/core/edit/lazy/deterministic.ts b/core/edit/lazy/deterministic.ts index 3fd4ffe73..e9cbdf921 100644 --- a/core/edit/lazy/deterministic.ts +++ b/core/edit/lazy/deterministic.ts @@ -155,9 +155,27 @@ export function isLazyLine(text: string): boolean { } function isLazyBlock(node: Parser.SyntaxNode): boolean { + // Special case for "{/* ... existing code ... */}" + if ( + node.type === "jsx_expression" && + node.namedChildCount === 1 && + isLazyBlock(node.namedChildren[0]) + ) { + return true; + } + return node.type.includes("comment") && isLazyLine(node.text); } +function stringsWithinLevDistThreshold( + a: string, + b: string, + threshold: number, +) { + const dist = distance(a, b); + return dist / Math.min(a.length, b.length) <= threshold; +} + /** * Determine whether two nodes are similar * @param a @@ -184,11 +202,23 @@ function nodesAreSimilar(a: Parser.SyntaxNode, b: Parser.SyntaxNode): boolean { return true; } + // Matching jsx_elements needs to be different because they have such a minimal first line + if ( + a.type === "jsx_element" && + b.type === "jsx_element" && + // Check that the tag names match + a.namedChildren[0]?.children[1]?.text === + b.namedChildren[0]?.children[1]?.text + ) { + if (stringsWithinLevDistThreshold(a.text, b.text, 0.3)) { + return true; + } + } + const lineOneA = a.text.split("\n")[0]; const lineOneB = b.text.split("\n")[0]; - const levDist = distance(lineOneA, lineOneB); - return levDist / Math.min(lineOneA.length, lineOneB.length) <= 0.2; + return stringsWithinLevDistThreshold(lineOneA, lineOneB, 0.2); } function nodesAreExact(a: Parser.SyntaxNode, b: Parser.SyntaxNode): boolean { diff --git a/core/edit/lazy/test-examples/migration-page.tsx.diff b/core/edit/lazy/test-examples/migration-page.tsx.diff new file mode 100644 index 000000000..3ff1810c5 --- /dev/null +++ b/core/edit/lazy/test-examples/migration-page.tsx.diff @@ -0,0 +1,166 @@ +import { useNavigate } from "react-router-dom"; +import ContinueButton from "../components/mainInput/ContinueButton"; + +function MigrationPage() { + const navigate = useNavigate(); + return ( +
+

+ Migration to config.json +

+ +

+ Continue now uses a .json config file. We hope that this takes the + guesswork out of setting up. +

+ +

+ Your configuration should have been automatically migrated, but we + recommend double-checking that everything looks correct. +

+ +

+ For a summary of what changed and examples of config.json, + please see the{" "} + + migration walkthrough + + , and if you have any questions please reach out to us on{" "} + + Discord + + . +

+ + + Note: If you are running the server manually and have not updated the + server, this message does not apply. + + + { + navigate("/"); + }} + disabled={false} + /> +
+ ); +} + +export default MigrationPage; + +--- + +import { useNavigate } from "react-router-dom"; +import ContinueButton from "../components/mainInput/ContinueButton"; + +function MigrationPage() { + const navigate = useNavigate(); + return ( +
+

+ Migration to config.json +

+ +

+ Continue now uses a .json config file. We hope that this takes the + guesswork out of setting up. +

+ +

+ Your configuration should have been automatically migrated, but we + recommend double-checking that everything looks correct. +

+ +

+ For a summary of what changed and examples of config.json, + please see the{" "} + + migration walkthrough + + , and if you have any questions please reach out to us on{" "} + + Discord + + . +

+ + + Note: If you are running the server manually and have not updated the + server, this message does not apply. + + + {/* ... existing code ... */} +
+ ); +} + +export default MigrationPage; + +--- + +import { useNavigate } from "react-router-dom"; +import ContinueButton from "../components/mainInput/ContinueButton"; + +function MigrationPage() { + const navigate = useNavigate(); + return ( +
+

+ Migration to config.json +

+ +-

++

+ Continue now uses a .json config file. We hope that this takes the + guesswork out of setting up. +

+ +-

++

+ Your configuration should have been automatically migrated, but we + recommend double-checking that everything looks correct. +

+ +-

++

+ For a summary of what changed and examples of config.json, + please see the{" "} + + migration walkthrough + + , and if you have any questions please reach out to us on{" "} + + Discord + + . +

+ +- ++ + Note: If you are running the server manually and have not updated the + server, this message does not apply. + + + { + navigate("/"); + }} + disabled={false} + /> +
+ ); +} + +export default MigrationPage; \ No newline at end of file From 88df5b63b9360b72a3e3ce9f10526a8382fa109f Mon Sep 17 00:00:00 2001 From: Nate Date: Fri, 27 Sep 2024 14:30:16 -0700 Subject: [PATCH 10/12] changelog update --- .changes/unreleased/Added-20240927-142940.yaml | 4 ++++ .changes/unreleased/Added-20240927-143005.yaml | 4 ++++ .changes/unreleased/Fixed-20240927-142924.yaml | 4 ++++ extensions/vscode/CHANGELOG.md | 4 ++++ extensions/vscode/package.json | 2 +- 5 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 .changes/unreleased/Added-20240927-142940.yaml create mode 100644 .changes/unreleased/Added-20240927-143005.yaml create mode 100644 .changes/unreleased/Fixed-20240927-142924.yaml diff --git a/.changes/unreleased/Added-20240927-142940.yaml b/.changes/unreleased/Added-20240927-142940.yaml new file mode 100644 index 000000000..3a2785783 --- /dev/null +++ b/.changes/unreleased/Added-20240927-142940.yaml @@ -0,0 +1,4 @@ +project: extensions/vscode +kind: Added +body: Improved loading/accept/reject UI for apply +time: 2024-09-27T14:29:40.283176-07:00 diff --git a/.changes/unreleased/Added-20240927-143005.yaml b/.changes/unreleased/Added-20240927-143005.yaml new file mode 100644 index 000000000..4dafa06cb --- /dev/null +++ b/.changes/unreleased/Added-20240927-143005.yaml @@ -0,0 +1,4 @@ +project: extensions/vscode +kind: Added +body: Polished chat sidebar, especially context dropdown +time: 2024-09-27T14:30:05.576347-07:00 diff --git a/.changes/unreleased/Fixed-20240927-142924.yaml b/.changes/unreleased/Fixed-20240927-142924.yaml new file mode 100644 index 000000000..9a799ee51 --- /dev/null +++ b/.changes/unreleased/Fixed-20240927-142924.yaml @@ -0,0 +1,4 @@ +project: extensions/vscode +kind: Fixed +body: Fixed a number of apply bugs +time: 2024-09-27T14:29:24.69568-07:00 diff --git a/extensions/vscode/CHANGELOG.md b/extensions/vscode/CHANGELOG.md index ffa85e2d1..ace7c5398 100644 --- a/extensions/vscode/CHANGELOG.md +++ b/extensions/vscode/CHANGELOG.md @@ -6,10 +6,14 @@ and is generated by [Changie](https://github.com/miniscruff/changie). Pre-release Changes +### Added +* Improved loading/accept/reject UI for apply +* Polished chat sidebar, especially context dropdown ### Changed * Updated tutorial file ### Fixed * Continue for teams auth bug fix +* Fixed a number of apply bugs ## 0.9.210 - 2024-09-15 diff --git a/extensions/vscode/package.json b/extensions/vscode/package.json index 653922315..5f3265799 100644 --- a/extensions/vscode/package.json +++ b/extensions/vscode/package.json @@ -2,7 +2,7 @@ "name": "continue", "icon": "media/icon.png", "author": "Continue Dev, Inc", - "version": "0.9.212", + "version": "0.9.213", "repository": { "type": "git", "url": "https://github.com/continuedev/continue" From 41779166a5418192deaf2d90d4335bc8afbf75a0 Mon Sep 17 00:00:00 2001 From: Nate Date: Fri, 27 Sep 2024 14:36:07 -0700 Subject: [PATCH 11/12] tweak docs for apply --- docs/docs/chat/how-to-use-it.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs/chat/how-to-use-it.md b/docs/docs/chat/how-to-use-it.md index 53cd85d17..509702b4f 100644 --- a/docs/docs/chat/how-to-use-it.md +++ b/docs/docs/chat/how-to-use-it.md @@ -28,7 +28,7 @@ If there is information from the codebase, documentation, IDE, or other tools th ## Apply generated code to your file -When the LLM replies with code, you can hover over the top right of the code block and select “Apply to current file” to update the existing code in the editor to this new version. +When the LLM replies with edits to a file, you can click the “Apply” button. This will update the existing code in the editor to reflect the suggested changes. ## Start a fresh session for a new task From 11fcb935b410816fb6310dcf4d1f3d99c0a45a88 Mon Sep 17 00:00:00 2001 From: Nate Date: Fri, 27 Sep 2024 15:37:13 -0700 Subject: [PATCH 12/12] Update plugin version to 0.0.70 --- extensions/intellij/gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/intellij/gradle.properties b/extensions/intellij/gradle.properties index 34fbfa779..6a7d5faf7 100644 --- a/extensions/intellij/gradle.properties +++ b/extensions/intellij/gradle.properties @@ -4,7 +4,7 @@ pluginGroup = com.github.continuedev.continueintellijextension pluginName = continue-intellij-extension pluginRepositoryUrl = https://github.com/continuedev/continue # SemVer format -> https://semver.org -pluginVersion = 0.0.69 +pluginVersion = 0.0.70 # Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html pluginSinceBuild = 223