✨ insert at cursor button

This commit is contained in:
Nate Sesti 2024-03-29 11:05:47 -07:00
parent 2ddfb60240
commit c7bd822634
3 changed files with 60 additions and 30 deletions

View file

@ -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 = {

View file

@ -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<T extends keyof ReverseWebviewProtocol>(

View file

@ -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) {
<TopDiv>
<SecondDiv bottom={props.bottom || false}>
{isJetBrains() || (
<HeaderButtonWithText
text={
terminalLanguages.includes(props.language)
? "Run in terminal"
: applying
? "Applying..."
: "Apply to current file"
}
disabled={applying}
style={{ backgroundColor: vscEditorBackground }}
onClick={(e) => {
if (terminalLanguages.includes(props.language)) {
let text = props.text;
if (text.startsWith("$ ")) {
text = text.slice(2);
}
new WebviewIde().runCommand(text);
<>
<HeaderButtonWithText
text={
terminalLanguages.includes(props.language)
? "Run in terminal"
: applying
? "Applying..."
: "Apply to current file"
}
disabled={applying}
style={{ backgroundColor: vscEditorBackground }}
onClick={() => {
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 ? (
<CheckIcon className="w-4 h-4 text-green-500" />
) : (
<PlayIcon className="w-4 h-4" />
)}
</HeaderButtonWithText>
if (applying) return;
postToIde("applyToCurrentFile", { text: props.text });
setApplying(true);
setTimeout(() => setApplying(false), 2000);
}}
>
{applying ? (
<CheckIcon className="w-4 h-4 text-green-500" />
) : (
<PlayIcon className="w-4 h-4" />
)}
</HeaderButtonWithText>
<HeaderButtonWithText
text="Insert at cursor"
style={{ backgroundColor: vscEditorBackground }}
onClick={() => {
postToIde("insertAtCursor", { text: props.text });
}}
>
<ArrowLeftEndOnRectangleIcon className="w-4 h-4" />
</HeaderButtonWithText>
</>
)}
<CopyButton text={props.text} />
</SecondDiv>
</TopDiv>