Elffuss BraidHub

kikocis/continue public

0bc8a18563fc · 5.4 KiBRawPermalink
1import { ChevronRightIcon, XMarkIcon } from "@heroicons/react/24/outline";
2import { SetCodeToEditPayload } from "core";
3import { getMarkdownLanguageTagForFile } from "core/util";
4import { getUriPathBasename } from "core/util/uri";
5import { useContext, useMemo, useState } from "react";
6import { useDispatch } from "react-redux";
7import styled from "styled-components";
8import { IdeMessengerContext } from "../../context/IdeMessenger";
9import { useAppSelector } from "../../redux/hooks";
10import { clearCodeToEdit } from "../../redux/slices/editModeState";
11import { getMetaKeyLabel } from "../../util";
12import FileIcon from "../FileIcon";
13import StyledMarkdownPreview from "../StyledMarkdownPreview";
14import { useFontSize } from "../ui/font";
15
16const NoPaddingWrapper = styled.div`
17 > * {
18 margin: 0 !important;
19 padding: 0 !important;
20 }
21
22 pre {
23 margin: 0 !important;
24 padding: 0px 0px 0px 10px !important;
25 }
26`;
27
28interface CodeToEditItemProps {
29 code: SetCodeToEditPayload;
30}
31
32function CodeToEditItem({ code }: CodeToEditItemProps) {
33 const dispatch = useDispatch();
34 const ideMessenger = useContext(IdeMessengerContext);
35 const [showCodeSnippet, setShowCodeSnippet] = useState(false);
36
37 function onDelete() {
38 dispatch(clearCodeToEdit());
39 ideMessenger.post("edit/clearDecorations", undefined);
40 }
41
42 const fileName = getUriPathBasename(code.filepath);
43
44 let isInsertion = false;
45 let title = fileName;
46
47 if ("range" in code) {
48 const start = code.range.start.line + 1;
49 const end = code.range.end.line + 1;
50
51 isInsertion = start === end;
52
53 title += isInsertion
54 ? ` - Inserting at line ${start}`
55 : ` (${start} - ${end})`;
56 }
57
58 const source =
59 "```" +
60 getMarkdownLanguageTagForFile(code.filepath) +
61 "\n" +
62 code.contents +
63 "\n" +
64 "```";
65
66 async function onClickFilename() {
67 if ("range" in code) {
68 await ideMessenger.ide.showLines(
69 code.filepath,
70 code.range.start.line,
71 code.range.end.line,
72 );
73 } else {
74 await ideMessenger.ide.openFile(code.filepath);
75 }
76 }
77
78 const smallFont = useFontSize(-3);
79
80 const iconClass =
81 "text-lightgray hover:bg-lightgray hover:text-vsc-foreground h-3.5 w-3.5 cursor-pointer rounded-sm p-0.5 hover:bg-opacity-20";
82
83 return (
84 <div className="flex flex-col gap-1 py-0.5">
85 <div className="group flex flex-row items-center justify-start gap-1.5 px-1">
86 <span
87 className="hidden flex-shrink-0 pl-2 font-semibold text-gray-400 sm:flex"
88 style={{
89 fontSize: smallFont,
90 }}
91 >
92 Code to edit
93 </span>
94 {/* <CodeBracketIcon className="h-3 w-3 flex-shrink-0 text-gray-400 sm:hidden" /> */}
95 <div
96 className={`hover:bg-lightgray flex flex-1 cursor-pointer items-center justify-between rounded transition-colors hover:bg-opacity-20 ${showCodeSnippet && "bg-lightgray bg-opacity-20"}`}
97 onClick={() => {
98 if (!isInsertion) {
99 setShowCodeSnippet((showCodeSnippet) => !showCodeSnippet);
100 }
101 }}
102 >
103 <div className="flex items-center gap-0.5">
104 <FileIcon filename={code.filepath} height={"18px"} width={"18px"} />
105 <span
106 className="line-clamp-1 text-xs hover:underline"
107 onClick={(e) => {
108 e.stopPropagation();
109 void onClickFilename();
110 }}
111 >
112 {title}
113 </span>
114 </div>
115
116 <div className="invisible flex items-center group-hover:visible">
117 <ChevronRightIcon
118 onClick={(e) => {
119 e.stopPropagation();
120 setShowCodeSnippet((val) => !val);
121 }}
122 className={`text-lightgray hover:bg-lightgray hover:text-vsc-foreground h-3.5 w-3.5 cursor-pointer rounded-sm p-0.5 hover:bg-opacity-20 ${showCodeSnippet ? "rotate-90" : ""} ${isInsertion ? "hidden" : ""}`}
123 />
124 <div className="flex items-center">
125 <XMarkIcon
126 onClick={(e) => {
127 e.stopPropagation();
128 void onDelete();
129 }}
130 className={iconClass}
131 />
132 </div>
133 </div>
134 </div>
135 </div>
136 {showCodeSnippet && (
137 <div className="!m-0 max-h-48 overflow-y-auto !p-0">
138 <NoPaddingWrapper>
139 <StyledMarkdownPreview source={source} />
140 </NoPaddingWrapper>
141 </div>
142 )}
143 </div>
144 );
145}
146
147export default function CodeToEditCard() {
148 // Array from previous multi-file edit
149 const codeToEditArray = useAppSelector(
150 (state) => state.editModeState.codeToEdit,
151 );
152 const codeToEdit = useMemo(() => {
153 return codeToEditArray[0];
154 }, [codeToEditArray]);
155
156 const metaKeyLabel = useMemo(() => {
157 return getMetaKeyLabel();
158 }, []);
159
160 const tinyFont = useFontSize(-3);
161
162 return (
163 <div className="bg-vsc-editor-background mx-3 flex flex-col rounded-t-lg py-1">
164 {codeToEdit ? (
165 <CodeToEditItem code={codeToEdit} />
166 ) : (
167 <>
168 <span
169 className="text-balance px-3 text-center font-semibold text-gray-400"
170 style={{
171 fontSize: tinyFont,
172 }}
173 >
174 {`To edit code, highlight it and press `}
175 <kbd>{metaKeyLabel}</kbd> <kbd>I</kbd>
176 </span>
177 </>
178 )}
179 </div>
180 );
181}