From c7bd8226347df6e3f637987016b95c8a317fb595 Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Fri, 29 Mar 2024 11:05:47 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20insert=20at=20cursor=20button?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/web/webviewProtocol.ts | 1 + extensions/vscode/src/webviewProtocol.ts | 13 ++++ .../components/markdown/CodeBlockToolbar.tsx | 76 +++++++++++-------- 3 files changed, 60 insertions(+), 30 deletions(-) diff --git a/core/web/webviewProtocol.ts b/core/web/webviewProtocol.ts index 217bdf856..547f34a3a 100644 --- a/core/web/webviewProtocol.ts +++ b/core/web/webviewProtocol.ts @@ -85,6 +85,7 @@ export type WebviewProtocol = Protocol & toggleFullScreen: [undefined, void]; "stats/getTokensPerDay": [undefined, { day: string; tokens: number }[]]; "stats/getTokensPerModel": [undefined, { model: string; tokens: number }[]]; + insertAtCursor: [{ text: string }, void]; }; export type ReverseWebviewProtocol = { diff --git a/extensions/vscode/src/webviewProtocol.ts b/extensions/vscode/src/webviewProtocol.ts index befbb6b72..64d69c221 100644 --- a/extensions/vscode/src/webviewProtocol.ts +++ b/extensions/vscode/src/webviewProtocol.ts @@ -549,6 +549,19 @@ export class VsCodeWebviewProtocol { const rows = await DevDataSqliteDb.getTokensPerModel(); return rows; }); + this.on("insertAtCursor", async (msg) => { + const editor = vscode.window.activeTextEditor; + if (editor === undefined || !editor.selection) { + return; + } + + editor.edit((editBuilder) => { + editBuilder.replace( + new vscode.Range(editor.selection.start, editor.selection.end), + msg.data.text, + ); + }); + }); } public request( diff --git a/gui/src/components/markdown/CodeBlockToolbar.tsx b/gui/src/components/markdown/CodeBlockToolbar.tsx index 0dd1fa4c2..a7a1e53e1 100644 --- a/gui/src/components/markdown/CodeBlockToolbar.tsx +++ b/gui/src/components/markdown/CodeBlockToolbar.tsx @@ -1,4 +1,8 @@ -import { CheckIcon, PlayIcon } from "@heroicons/react/24/outline"; +import { + ArrowLeftEndOnRectangleIcon, + CheckIcon, + PlayIcon, +} from "@heroicons/react/24/outline"; import { useState } from "react"; import styled from "styled-components"; import { defaultBorderRadius, vscEditorBackground } from ".."; @@ -45,38 +49,50 @@ function CodeBlockToolBar(props: CodeBlockToolBarProps) { {isJetBrains() || ( - { - if (terminalLanguages.includes(props.language)) { - let text = props.text; - if (text.startsWith("$ ")) { - text = text.slice(2); - } - new WebviewIde().runCommand(text); + <> + { + if (terminalLanguages.includes(props.language)) { + let text = props.text; + if (text.startsWith("$ ")) { + text = text.slice(2); + } + new WebviewIde().runCommand(text); + } - if (applying) return; - postToIde("applyToCurrentFile", { text: props.text }); - setApplying(true); - setTimeout(() => setApplying(false), 2000); - }} - > - {applying ? ( - - ) : ( - - )} - + if (applying) return; + postToIde("applyToCurrentFile", { text: props.text }); + setApplying(true); + setTimeout(() => setApplying(false), 2000); + }} + > + {applying ? ( + + ) : ( + + )} + + { + postToIde("insertAtCursor", { text: props.text }); + }} + > + + + )} +