Skip to content

Commit e7d1249

Browse files
authored
feat(chat): add copy button in chat (#41)
1 parent 4e39df7 commit e7d1249

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

src/common/chat/utils/useHighlightedTextAsContext.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ Instructions:
5555
- If the answer expects any code examples, please provide examples.
5656
5757
Rules for code in response:
58-
- Suggest only changes.
58+
- Suggest only changes.
5959
- Provide only the necessary code.
6060
- Write as less as possible comments.
61+
- Wrap in Markdown with type of language.
6162
`;
6263

6364
export const humanMessageWithCodePrompt = new PromptTemplate({

webviews/src/components/ChatMessage/index.tsx

+25-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { memo } from "react";
2-
import { VSCodeDivider } from "@vscode/webview-ui-toolkit/react";
2+
import { VSCodeButton, VSCodeDivider } from "@vscode/webview-ui-toolkit/react";
33
import Markdown from "react-markdown";
44
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
55
import { vscDarkPlus as vscodeHighlightStyle } from "react-syntax-highlighter/dist/esm/styles/prism";
@@ -16,15 +16,31 @@ export const ChatMessage = memo((props: { role: string; content: string }) => {
1616
code(props) {
1717
const { children, className, node, ...rest } = props;
1818
const match = /language-(\w+)/.exec(className || "");
19+
1920
return match ? (
20-
// @ts-ignore
21-
<SyntaxHighlighter
22-
{...rest}
23-
PreTag="div"
24-
children={String(children).replace(/\n$/, "")}
25-
language={match[1]}
26-
style={vscodeHighlightStyle}
27-
/>
21+
<div className={styles["code-container"]}>
22+
<div className={styles["copy-container"]}>
23+
<VSCodeButton
24+
appearance="icon"
25+
onClick={() =>
26+
navigator.clipboard.writeText(
27+
String(children).replace(/\n$/, "")
28+
)
29+
}
30+
>
31+
<span className="codicon codicon-copy"></span>
32+
</VSCodeButton>
33+
</div>
34+
35+
{/* @ts-ignore */}
36+
<SyntaxHighlighter
37+
{...rest}
38+
PreTag="div"
39+
children={String(children).replace(/\n$/, "")}
40+
language={match[1]}
41+
style={vscodeHighlightStyle}
42+
/>
43+
</div>
2844
) : (
2945
<code {...rest} className={className}>
3046
{children}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
.chat-message {
2-
padding: 0px 20px;
3-
}
2+
padding: 0px 20px;
3+
}
4+
5+
.code-container {
6+
position: relative;
7+
}
8+
9+
.copy-container {
10+
position: absolute;
11+
right: 10px;
12+
top: 10px;
13+
}

0 commit comments

Comments
 (0)