From 68bcb3f43210ec7d4908c3cc98ae05728285cc6d Mon Sep 17 00:00:00 2001 From: Nate Date: Sat, 11 Jan 2025 10:05:03 -0800 Subject: [PATCH] refactor out the symbol/rif matching --- .../StepContainerPreToolbar.tsx | 16 +++--- .../markdown/StyledMarkdownPreview.tsx | 53 ++++++++----------- gui/src/components/markdown/utils.ts | 38 +++++++++++++ 3 files changed, 67 insertions(+), 40 deletions(-) diff --git a/gui/src/components/markdown/StepContainerPreToolbar/StepContainerPreToolbar.tsx b/gui/src/components/markdown/StepContainerPreToolbar/StepContainerPreToolbar.tsx index 6ccedc74a..95361abb2 100644 --- a/gui/src/components/markdown/StepContainerPreToolbar/StepContainerPreToolbar.tsx +++ b/gui/src/components/markdown/StepContainerPreToolbar/StepContainerPreToolbar.tsx @@ -1,11 +1,18 @@ import { ChevronDownIcon } from "@heroicons/react/24/outline"; +import { inferResolvedUriFromRelativePath } from "core/util/ideUtils"; import { debounce } from "lodash"; import { useContext, useEffect, useRef, useState } from "react"; import styled from "styled-components"; import { v4 as uuidv4 } from "uuid"; -import { defaultBorderRadius, lightGray, vscEditorBackground } from "../.."; +import { lightGray, vscEditorBackground } from "../.."; import { IdeMessengerContext } from "../../../context/IdeMessenger"; import { useWebviewListener } from "../../../hooks/useWebviewListener"; +import { useAppSelector } from "../../../redux/hooks"; +import { selectDefaultModel } from "../../../redux/slices/configSlice"; +import { + selectApplyStateByStreamId, + selectIsInEditMode, +} from "../../../redux/slices/sessionSlice"; import { getFontSize } from "../../../util"; import { childrenToText, isTerminalCodeBlock } from "../utils"; import ApplyActions from "./ApplyActions"; @@ -13,13 +20,6 @@ import CopyButton from "./CopyButton"; import FileInfo from "./FileInfo"; import GeneratingCodeLoader from "./GeneratingCodeLoader"; import RunInTerminalButton from "./RunInTerminalButton"; -import { useAppSelector } from "../../../redux/hooks"; -import { selectDefaultModel } from "../../../redux/slices/configSlice"; -import { - selectApplyStateByStreamId, - selectIsInEditMode, -} from "../../../redux/slices/sessionSlice"; -import { inferResolvedUriFromRelativePath } from "core/util/ideUtils"; const TopDiv = styled.div` outline: 0.5px solid rgba(153, 153, 152); diff --git a/gui/src/components/markdown/StyledMarkdownPreview.tsx b/gui/src/components/markdown/StyledMarkdownPreview.tsx index c391b8a91..52d343fe4 100644 --- a/gui/src/components/markdown/StyledMarkdownPreview.tsx +++ b/gui/src/components/markdown/StyledMarkdownPreview.tsx @@ -6,30 +6,30 @@ import rehypeKatex from "rehype-katex"; import remarkMath from "remark-math"; import styled from "styled-components"; import { visit } from "unist-util-visit"; +import { v4 as uuidv4 } from "uuid"; import { defaultBorderRadius, vscBackground, vscEditorBackground, vscForeground, } from ".."; +import useUpdatingRef from "../../hooks/useUpdatingRef"; +import { useAppSelector } from "../../redux/hooks"; +import { selectUIConfig } from "../../redux/slices/configSlice"; +import { getContextItemsFromHistory } from "../../redux/thunks/updateFileSymbols"; import { getFontSize, isJetBrains } from "../../util"; +import { ToolTip } from "../gui/Tooltip"; import FilenameLink from "./FilenameLink"; import "./katex.css"; import "./markdown.css"; import StepContainerPreActionButtons from "./StepContainerPreActionButtons"; import StepContainerPreToolbar from "./StepContainerPreToolbar"; import SymbolLink from "./SymbolLink"; -import useUpdatingRef from "../../hooks/useUpdatingRef"; -import { remarkTables } from "./utils/remarkTables"; import { SyntaxHighlightedPre } from "./SyntaxHighlightedPre"; -import { patchNestedMarkdown } from "./utils/patchNestedMarkdown"; -import { useAppSelector } from "../../redux/hooks"; +import { isSymbolNotRif, matchCodeToSymbolOrFile } from "./utils"; import { fixDoubleDollarNewLineLatex } from "./utils/fixDoubleDollarLatex"; -import { selectUIConfig } from "../../redux/slices/configSlice"; -import { ToolTip } from "../gui/Tooltip"; -import { v4 as uuidv4 } from "uuid"; -import { ContextItemWithId, RangeInFileWithContents } from "core"; -import { getContextItemsFromHistory } from "../../redux/thunks/updateFileSymbols"; +import { patchNestedMarkdown } from "./utils/patchNestedMarkdown"; +import { remarkTables } from "./utils/remarkTables"; const StyledMarkdown = styled.div<{ fontSize?: number; @@ -301,30 +301,19 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( if (content) { const { symbols, rifs } = pastFileInfoRef.current; - // Insert file links for matching previous context items - // With some reasonable limitations on what might be a filename - if (rifs.length && content.includes(".") && content.length > 2) { - const match = rifs.find( - (rif) => rif.filepath.split("/").pop() === content, // Exact match for last segment of URI - ); - - if (match) { - return ; - } - } - - // Insert symbols for exact matches - const exactSymbol = symbols.find((s) => s.name === content); - if (exactSymbol) { - return ; - } - - // Partial matches - this is the case where the llm returns e.g. `subtract(number)` instead of `subtract` - const partialSymbol = symbols.find((s) => - content.startsWith(s.name), + const matchedSymbolOrFile = matchCodeToSymbolOrFile( + content, + symbols, + rifs, ); - if (partialSymbol) { - return ; + if (matchedSymbolOrFile) { + if (isSymbolNotRif(matchedSymbolOrFile)) { + return ( + + ); + } else { + return ; + } } } return {codeProps.children}; diff --git a/gui/src/components/markdown/utils.ts b/gui/src/components/markdown/utils.ts index d34044089..3961f11d0 100644 --- a/gui/src/components/markdown/utils.ts +++ b/gui/src/components/markdown/utils.ts @@ -1,3 +1,5 @@ +import { RangeInFileWithContents, SymbolWithRange } from "core"; + const terminalLanguages = ["bash", "sh"]; const commonTerminalCommands = [ "npm", @@ -52,3 +54,39 @@ function childToText(child: any): string { export function childrenToText(children: any) { return children.map((child: any) => childToText(child)).join(""); } + +export function matchCodeToSymbolOrFile( + content: string, + symbols: SymbolWithRange[], + rifs: RangeInFileWithContents[], +): SymbolWithRange | RangeInFileWithContents | undefined { + // Insert file links for matching previous context items + // With some reasonable limitations on what might be a filename + if (rifs.length && content.includes(".") && content.length > 2) { + const match = rifs.find( + (rif) => rif.filepath.split("/").pop() === content, // Exact match for last segment of URI + ); + + if (match) { + return match; + } + } + + // Insert symbols for exact matches + const exactSymbol = symbols.find((s) => s.name === content); + if (exactSymbol) { + return exactSymbol; + } + + // Partial matches - this is the case where the llm returns e.g. `subtract(number)` instead of `subtract` + const partialSymbol = symbols.find((s) => content.startsWith(s.name)); + if (partialSymbol) { + return partialSymbol; + } +} + +export function isSymbolNotRif( + item: SymbolWithRange | RangeInFileWithContents, +): item is SymbolWithRange { + return (item as SymbolWithRange).type !== undefined; +}