✨ code linking

This commit is contained in:
Nate Sesti 2024-06-11 00:04:39 -07:00
parent 9e13448b88
commit 0bbd9dcd0d
5 changed files with 73 additions and 13 deletions

12
gui/package-lock.json generated
View file

@ -50,6 +50,7 @@
"rehype-katex": "^7.0.0",
"rehype-wrap-all": "^1.1.0",
"remark-math": "^6.0.0",
"reselect": "^5.1.1",
"seti-file-icons": "^0.0.8",
"socket.io-client": "^4.7.2",
"styled-components": "^5.3.6",
@ -1180,6 +1181,11 @@
}
}
},
"node_modules/@reduxjs/toolkit/node_modules/reselect": {
"version": "4.1.8",
"resolved": "https://registry.npmjs.org/reselect/-/reselect-4.1.8.tgz",
"integrity": "sha512-ab9EmR80F/zQTMNeneUr4cv+jSwPJgIlvEmVwLerwrWVbpLlBuls9XHzIeTFy4cegU2NHBp3va0LKOzU5qFEYQ=="
},
"node_modules/@remirror/core-constants": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/@remirror/core-constants/-/core-constants-2.0.2.tgz",
@ -6917,9 +6923,9 @@
}
},
"node_modules/reselect": {
"version": "4.1.8",
"resolved": "https://registry.npmjs.org/reselect/-/reselect-4.1.8.tgz",
"integrity": "sha512-ab9EmR80F/zQTMNeneUr4cv+jSwPJgIlvEmVwLerwrWVbpLlBuls9XHzIeTFy4cegU2NHBp3va0LKOzU5qFEYQ=="
"version": "5.1.1",
"resolved": "https://registry.npmjs.org/reselect/-/reselect-5.1.1.tgz",
"integrity": "sha512-K/BG6eIky/SBpzfHZv/dd+9JBFiS4SWV7FIujVyJRux6e45+73RaUHXLmIR1f7WOMaQ0U1km6qwklRQxpJJY0w=="
},
"node_modules/resolve": {
"version": "1.22.8",

View file

@ -52,6 +52,7 @@
"rehype-katex": "^7.0.0",
"rehype-wrap-all": "^1.1.0",
"remark-math": "^6.0.0",
"reselect": "^5.1.1",
"seti-file-icons": "^0.0.8",
"socket.io-client": "^4.7.2",
"styled-components": "^5.3.6",

View file

@ -1,7 +1,9 @@
import { useContext, useEffect, useState } from "react";
import { useSelector } from "react-redux";
import styled from "styled-components";
import { defaultBorderRadius, vscInputBackground } from "..";
import { IdeMessengerContext } from "../../context/IdeMessenger";
import { memoizedContextItemsSelector } from "../../redux/slices/stateSlice";
const StyledCode = styled.code<{ link: boolean }>`
color: ${(props) => (props.link ? "#ff4343" : "#f78383")};
@ -18,37 +20,64 @@ const StyledCode = styled.code<{ link: boolean }>`
function LinkableCode(props: any) {
const ideMessenger = useContext(IdeMessengerContext);
const contextItems = useSelector(memoizedContextItemsSelector);
const [linkingDone, setLinkingDone] = useState(false);
const [isLink, setIsLink] = useState(false);
const [filepath, setFilepath] = useState("");
const [line, setLine] = useState(0);
useEffect(() => {
if (linkingDone) return;
setLinkingDone(true);
// Get filename from props.children
let filename: string | undefined = undefined;
let content: string | undefined = undefined;
if (typeof props.children === "string") {
filename = props.children;
content = props.children;
} else if (
Array.isArray(props.children) &&
props.children.length > 0 &&
typeof props.children[0] === "string"
) {
filename = props.children[0];
content = props.children[0];
}
if (!filename) return;
if (!content) return;
// Check if this is a real file
// TODO
let link = filename.slice(-4).includes(".");
const contextItemFileMatch = contextItems.find(
(item) =>
item.id.providerTitle === "file" && item.description.endsWith(content),
);
if (contextItemFileMatch) {
setIsLink(true);
setFilepath(contextItemFileMatch.description);
setLine(0);
}
// setIsLink(link);
setFilepath(filename);
}, [linkingDone]);
if (content.length > 6) {
const contextItemContentMatch = contextItems.find((item) =>
item.content.includes(content),
);
if (contextItemContentMatch) {
setIsLink(true);
setFilepath(contextItemContentMatch.description);
setLine(
contextItemContentMatch.content
.split("\n")
.findIndex((line) => line.includes(content)) + 1,
);
}
}
}, [linkingDone, contextItems]);
const onClick = () => {
ideMessenger.post("showFile", { filepath });
if (!isLink) return;
ideMessenger.post("showLines", {
filepath,
startLine: line,
endLine: line + 1,
});
};
return (

View file

@ -14,6 +14,7 @@ import {
} from "..";
import { RootState } from "../../redux/store";
import { getFontSize } from "../../util";
import LinkableCode from "./LinkableCode";
import PreWithToolbar from "./PreWithToolbar";
import { SyntaxHighlightedPre } from "./SyntaxHighlightedPre";
import "./katex.css";
@ -169,6 +170,17 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview(
<SyntaxHighlightedPre {...preProps}></SyntaxHighlightedPre>
);
},
code: ({ node, ...codeProps }) => {
if (
codeProps.className?.split(" ").includes("hljs") ||
codeProps.children?.length > 1
) {
return <code {...codeProps}>{codeProps.children}</code>;
}
return (
<LinkableCode {...codeProps}>{codeProps.children}</LinkableCode>
);
},
// pre: ({ node, ...preProps }) => {
// const codeString =
// preProps.children?.[0]?.props?.children?.[0].trim() || "";

View file

@ -11,7 +11,19 @@ import {
} from "core";
import { BrowserSerializedContinueConfig } from "core/config/load";
import { stripImages } from "core/llm/countTokens";
import { createSelector } from "reselect";
import { v4 } from "uuid";
import { RootState } from "../store";
export const memoizedContextItemsSelector = createSelector(
[(state: RootState) => state.state.history],
(history) => {
return history.reduce<ContextItemWithId[]>((acc, item) => {
acc.push(...item.contextItems);
return acc;
}, []);
},
);
const TEST_CONTEXT_ITEMS: ContextItemWithId[] = [
{