refactor out the symbol/rif matching

This commit is contained in:
Nate 2025-01-11 10:05:03 -08:00
parent e3389aebc0
commit 68bcb3f432
3 changed files with 67 additions and 40 deletions

View file

@ -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);

View file

@ -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 <FilenameLink rif={match} />;
}
}
// Insert symbols for exact matches
const exactSymbol = symbols.find((s) => s.name === content);
if (exactSymbol) {
return <SymbolLink content={content} symbol={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),
const matchedSymbolOrFile = matchCodeToSymbolOrFile(
content,
symbols,
rifs,
);
if (partialSymbol) {
return <SymbolLink content={content} symbol={partialSymbol} />;
if (matchedSymbolOrFile) {
if (isSymbolNotRif(matchedSymbolOrFile)) {
return (
<SymbolLink content={content} symbol={matchedSymbolOrFile} />
);
} else {
return <FilenameLink rif={matchedSymbolOrFile} />;
}
}
}
return <code {...codeProps}>{codeProps.children}</code>;

View file

@ -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;
}