From 7fa299326765e9ed07dd305531509181873ed644 Mon Sep 17 00:00:00 2001 From: Test Date: Thu, 26 Sep 2024 20:40:57 -0700 Subject: [PATCH] gui: update styling for filename lins --- core/commands/slash/edit.ts | 4 +- core/commands/util.ts | 17 +- core/context/providers/FileContextProvider.ts | 8 +- gui/src/components/gui/StepContainer.tsx | 2 +- gui/src/components/index.ts | 2 - .../components/mainInput/ContextItemsPeek.tsx | 16 +- .../components/mainInput/ContinueInputBox.tsx | 10 +- gui/src/components/markdown/FilenameLink.tsx | 36 ++++ gui/src/components/markdown/LinkableCode.tsx | 170 ------------------ .../markdown/StyledMarkdownPreview.tsx | 45 +++-- gui/src/hooks/useChatHandler.ts | 4 + 11 files changed, 105 insertions(+), 209 deletions(-) create mode 100644 gui/src/components/markdown/FilenameLink.tsx delete mode 100644 gui/src/components/markdown/LinkableCode.tsx diff --git a/core/commands/slash/edit.ts b/core/commands/slash/edit.ts index a3c3ed91c..fd87ad32b 100644 --- a/core/commands/slash/edit.ts +++ b/core/commands/slash/edit.ts @@ -14,7 +14,7 @@ import { getMarkdownLanguageTagForFile, } from "../../util/"; import { - contextItemToRangeInFileWithContents, + ctxItemToRifWithContents, type RangeInFileWithContents, } from "../util"; @@ -250,7 +250,7 @@ const EditSlashCommand: SlashCommand = { } const rif: RangeInFileWithContents = - contextItemToRangeInFileWithContents(contextItemToEdit); + ctxItemToRifWithContents(contextItemToEdit); await ide.saveFile(rif.filepath); const fullFileContents = await ide.readFile(rif.filepath); diff --git a/core/commands/util.ts b/core/commands/util.ts index 931564839..a6be440e9 100644 --- a/core/commands/util.ts +++ b/core/commands/util.ts @@ -9,20 +9,29 @@ export interface RangeInFileWithContents { contents: string; } -export function contextItemToRangeInFileWithContents( +export function ctxItemToRifWithContents( item: ContextItemWithId, ): RangeInFileWithContents { - const lines = item.name.split("(")[1].split(")")[0].split("-"); + let startLine = 0; + let endLine = 0; + + const nameSplit = item.name.split("("); + + if (nameSplit.length > 1) { + const lines = nameSplit[1].split(")")[0].split("-"); + startLine = Number.parseInt(lines[0], 10); + endLine = Number.parseInt(lines[1], 10); + } const rif: RangeInFileWithContents = { filepath: item.uri?.value || "", range: { start: { - line: Number.parseInt(lines[0]), + line: startLine, character: 0, }, end: { - line: Number.parseInt(lines[1]), + line: endLine, character: 0, }, }, diff --git a/core/context/providers/FileContextProvider.ts b/core/context/providers/FileContextProvider.ts index be558c651..d1ac2d6e2 100644 --- a/core/context/providers/FileContextProvider.ts +++ b/core/context/providers/FileContextProvider.ts @@ -4,14 +4,14 @@ import { ContextProviderExtras, ContextSubmenuItem, LoadSubmenuItemsArgs, -} from "../../index.js"; -import { walkDir } from "../../indexing/walkDir.js"; +} from "../../"; +import { walkDir } from "../../indexing/walkDir"; import { getBasename, getUniqueFilePath, groupByLastNPathParts, -} from "../../util/index.js"; -import { BaseContextProvider } from "../index.js"; +} from "../../util/"; +import { BaseContextProvider } from "../"; const MAX_SUBMENU_ITEMS = 10_000; diff --git a/gui/src/components/gui/StepContainer.tsx b/gui/src/components/gui/StepContainer.tsx index c4d0b2597..94652bc7d 100644 --- a/gui/src/components/gui/StepContainer.tsx +++ b/gui/src/components/gui/StepContainer.tsx @@ -106,7 +106,7 @@ function StepContainer(props: StepContainerProps) { {shouldRenderActions && ( -
+
{truncatedEarly && (
{getContextItemIcon()} @@ -128,11 +128,11 @@ function ContextItemsPeekItem({ contextItem }: ContextItemsPeekItemProps) { {contextItem.name}
- {contextItem.description} + {contextItem.uri?.type === "file" + ? getBasename(contextItem.description) + : contextItem.description}
diff --git a/gui/src/components/mainInput/ContinueInputBox.tsx b/gui/src/components/mainInput/ContinueInputBox.tsx index 9778a6de9..40127037f 100644 --- a/gui/src/components/mainInput/ContinueInputBox.tsx +++ b/gui/src/components/mainInput/ContinueInputBox.tsx @@ -88,8 +88,6 @@ function ContinueInputBox(props: ContinueInputBoxProps) { [props.isMainInput], ); - console.log({ isGatheringContext, isGatheringContextStore }); - useEffect(() => { if (isGatheringContextStore && !isGatheringContext) { // 500ms delay when going from false -> true to prevent flashing loading indicator @@ -107,10 +105,8 @@ function ContinueInputBox(props: ContinueInputBoxProps) { }, [isGatheringContextStore]); return ( -
-
+
+
+ />
); } diff --git a/gui/src/components/markdown/FilenameLink.tsx b/gui/src/components/markdown/FilenameLink.tsx new file mode 100644 index 000000000..1ef434a7e --- /dev/null +++ b/gui/src/components/markdown/FilenameLink.tsx @@ -0,0 +1,36 @@ +import { RangeInFile } from "core"; +import { IdeMessengerContext } from "../../context/IdeMessenger"; +import { useContext } from "react"; +import { getBasename } from "core/util"; +import FileIcon from "../FileIcon"; + +interface FilenameLinkProps { + rif: RangeInFile; +} + +function FilenameLink({ rif }: FilenameLinkProps) { + const ideMessenger = useContext(IdeMessengerContext); + + function onClick() { + debugger; + ideMessenger.post("showLines", { + filepath: rif.filepath, + startLine: rif.range.start.line, + endLine: rif.range.end.line, + }); + } + + return ( +
+ + + {getBasename(rif.filepath)} + +
+ ); +} + +export default FilenameLink; diff --git a/gui/src/components/markdown/LinkableCode.tsx b/gui/src/components/markdown/LinkableCode.tsx deleted file mode 100644 index 9c4791265..000000000 --- a/gui/src/components/markdown/LinkableCode.tsx +++ /dev/null @@ -1,170 +0,0 @@ -import { RangeInFile } from "core"; -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")}; - word-wrap: break-word; - border-radius: ${defaultBorderRadius}; - background-color: ${vscInputBackground}; - - ${(props) => props.link && "cursor: pointer;"} - &:hover { - ${(props) => props.link && "text-decoration: underline;"} - } -`; - -function LinkableCode(props: any) { - const ideMessenger = useContext(IdeMessengerContext); - - const contextItems = useSelector(memoizedContextItemsSelector); - - const [linkingDone, setLinkingDone] = useState(false); - const [isLink, setIsLink] = useState(false); - const [rif, setRif] = useState(undefined); - - useEffect(() => { - if (linkingDone) return; - setLinkingDone(true); - - // Get content of the code tag - let content: string | undefined = undefined; - if (typeof props.children === "string") { - content = props.children; - } else if ( - Array.isArray(props.children) && - props.children.length > 0 && - typeof props.children[0] === "string" - ) { - content = props.children[0]; - } - if (!content) return; - - // Match filepath with context items - const contextItemFileMatch = contextItems.find( - (item) => - ["file", "codebase", "folder"].includes(item.id.providerTitle) && - item.description.split(" (")[0].endsWith(content), - ); - if (contextItemFileMatch) { - setIsLink(true); - if (contextItemFileMatch.description.includes(" (")) { - // Get line number from filename - const [startLine, _] = contextItemFileMatch.description - .split(" (")[1] - .split(")")[0] - .split("-"); - const filepath = contextItemFileMatch.description.split(" (")[0]; - setRif({ - filepath, - range: { - start: { - line: 0, - character: 0, - }, - end: { - line: 0, - character: 0, - }, - }, - }); - return; - } else { - // If no line number, just open the file - setRif({ - filepath: contextItemFileMatch.description.split(" (")[0], - range: { - start: { - line: 0, - character: 0, - }, - end: { - line: 0, - character: 0, - }, - }, - }); - return; - } - } - - // Try to match the content with a line in a file - if (content.length > 8) { - const contextItemContentMatch = contextItems.find( - (item) => - ["file", "codebase", "folder"].includes(item.id.providerTitle) && - item.content.includes(content), - ); - if (contextItemContentMatch) { - const line = contextItemContentMatch.content - .split("\n") - .slice(1) // code block fence - .findIndex((line) => line.includes(content)); - - const [filepath, rest] = - contextItemContentMatch.description.split(" ("); - - setIsLink(true); - - if (rest) { - const [startLine, endLine] = rest.split(")")[0]?.split("-") || [ - "0", - "0", - ]; - const start = parseInt(startLine); - const end = parseInt(endLine); - setRif({ - filepath, - range: { - start: { - line: start + line, - character: 0, - }, - end: { - line: start + line + 1, - character: 0, - }, - }, - }); - } else { - setRif({ - filepath, - range: { - start: { - line: 0, - character: 0, - }, - end: { - line: 0, - character: 0, - }, - }, - }); - } - } - } - }, [linkingDone, contextItems]); - - const onClick = () => { - if (!isLink) return; - if (!rif) return; - ideMessenger.post("showLines", { - filepath: rif.filepath, - startLine: rif.range.start.line, - endLine: rif.range.end.line, - }); - }; - - return ( - <> - {isLink} - - - ); -} - -export default LinkableCode; diff --git a/gui/src/components/markdown/StyledMarkdownPreview.tsx b/gui/src/components/markdown/StyledMarkdownPreview.tsx index 1e4be98db..1695b7d19 100644 --- a/gui/src/components/markdown/StyledMarkdownPreview.tsx +++ b/gui/src/components/markdown/StyledMarkdownPreview.tsx @@ -13,10 +13,13 @@ import { } from ".."; import { getFontSize } from "../../util"; import "./katex.css"; -import LinkableCode from "./LinkableCode"; +import FilenameLink from "./FilenameLink"; import "./markdown.css"; import PreWithToolbar from "./PreWithToolbar"; import { SyntaxHighlightedPre } from "./SyntaxHighlightedPre"; +import { useSelector } from "react-redux"; +import { memoizedContextItemsSelector } from "../../redux/slices/stateSlice"; +import { ctxItemToRifWithContents } from "core/commands/util"; const StyledMarkdown = styled.div<{ fontSize?: number; @@ -29,6 +32,7 @@ const StyledMarkdown = styled.div<{ overflow-x: scroll; overflow-y: hidden; + margin: 10px 0; padding: 6px 8px; } @@ -82,7 +86,7 @@ interface StyledMarkdownPreviewProps { const HLJS_LANGUAGE_CLASSNAME_PREFIX = "language-"; -const getLanuageFromClassName = (className: any): string | null => { +function getLanuageFromClassName(className: any): string | null { if (!className || typeof className !== "string") { return null; } @@ -93,11 +97,27 @@ const getLanuageFromClassName = (className: any): string | null => { ?.split("-")[1]; return language; -}; +} + +function getCodeChildrenContent(children: any) { + if (typeof children === "string") { + return children; + } else if ( + Array.isArray(children) && + children.length > 0 && + typeof children[0] === "string" + ) { + return children[0]; + } + + return undefined; +} const StyledMarkdownPreview = memo(function StyledMarkdownPreview( props: StyledMarkdownPreviewProps, ) { + const contextItems = useSelector(memoizedContextItemsSelector); + const [reactContent, setMarkdownSource] = useRemark({ remarkPlugins: [ remarkMath, @@ -167,15 +187,18 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( ); }, code: ({ node, ...codeProps }) => { - if ( - codeProps.className?.split(" ").includes("hljs") || - codeProps.children?.length > 1 - ) { - return {codeProps.children}; - } - return ( - {codeProps.children} + const content = getCodeChildrenContent(codeProps.children); + + const ctxItem = contextItems.find((ctxItem) => + ctxItem.uri?.value.includes(content), ); + + if (ctxItem) { + const rif = ctxItemToRifWithContents(ctxItem); + return ; + } + + return {codeProps.children}; }, }, }, diff --git a/gui/src/hooks/useChatHandler.ts b/gui/src/hooks/useChatHandler.ts index 9aff5dc79..655cfce96 100644 --- a/gui/src/hooks/useChatHandler.ts +++ b/gui/src/hooks/useChatHandler.ts @@ -230,6 +230,10 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) { itemId: currentFilePath, providerTitle: "file", }, + uri: { + type: "file", + value: currentFilePath, + }, }); } }