Skip to content

feat(chat): add copy button in chat #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/common/chat/utils/useHighlightedTextAsContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ Instructions:
- If the answer expects any code examples, please provide examples.

Rules for code in response:
- Suggest only changes.
- Suggest only changes.
- Provide only the necessary code.
- Write as less as possible comments.
- Wrap in Markdown with type of language.
`;

export const humanMessageWithCodePrompt = new PromptTemplate({
Expand Down
34 changes: 25 additions & 9 deletions webviews/src/components/ChatMessage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { memo } from "react";
import { VSCodeDivider } from "@vscode/webview-ui-toolkit/react";
import { VSCodeButton, VSCodeDivider } from "@vscode/webview-ui-toolkit/react";
import Markdown from "react-markdown";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { vscDarkPlus as vscodeHighlightStyle } from "react-syntax-highlighter/dist/esm/styles/prism";
Expand All @@ -16,15 +16,31 @@ export const ChatMessage = memo((props: { role: string; content: string }) => {
code(props) {
const { children, className, node, ...rest } = props;
const match = /language-(\w+)/.exec(className || "");

return match ? (
// @ts-ignore
<SyntaxHighlighter
{...rest}
PreTag="div"
children={String(children).replace(/\n$/, "")}
language={match[1]}
style={vscodeHighlightStyle}
/>
<div className={styles["code-container"]}>
<div className={styles["copy-container"]}>
<VSCodeButton
appearance="icon"
onClick={() =>
navigator.clipboard.writeText(
String(children).replace(/\n$/, "")
)
}
>
<span className="codicon codicon-copy"></span>
</VSCodeButton>
</div>

{/* @ts-ignore */}
<SyntaxHighlighter
{...rest}
PreTag="div"
children={String(children).replace(/\n$/, "")}
language={match[1]}
style={vscodeHighlightStyle}
/>
</div>
) : (
<code {...rest} className={className}>
{children}
Expand Down
14 changes: 12 additions & 2 deletions webviews/src/components/ChatMessage/styles.module.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
.chat-message {
padding: 0px 20px;
}
padding: 0px 20px;
}

.code-container {
position: relative;
}

.copy-container {
position: absolute;
right: 10px;
top: 10px;
}