| 1 | import { SymbolWithRange } from "core"; |
| 2 | import { useContext, useMemo } from "react"; |
| 3 | import { v4 as uuidv4 } from "uuid"; |
| 4 | import { IdeMessengerContext } from "../../context/IdeMessenger"; |
| 5 | import { ToolTip } from "../gui/Tooltip"; |
| 6 | |
| 7 | interface SymbolLinkProps { |
| 8 | symbol: SymbolWithRange; |
| 9 | content: string; |
| 10 | } |
| 11 | |
| 12 | function SymbolLink({ symbol, content }: SymbolLinkProps) { |
| 13 | const ideMessenger = useContext(IdeMessengerContext); |
| 14 | |
| 15 | function onClick() { |
| 16 | ideMessenger.post("showLines", { |
| 17 | filepath: symbol.filepath, |
| 18 | startLine: symbol.range.start.line, |
| 19 | endLine: symbol.range.end.line, |
| 20 | }); |
| 21 | } |
| 22 | |
| 23 | const processedContent = useMemo(() => { |
| 24 | let content = symbol.content; |
| 25 | // TODO Normalize indentation |
| 26 | // let lines = symbol.content.split("\n"); |
| 27 | // if (lines.length > 1) { |
| 28 | // const firstLineIndentation = lines[0].match(/^\s*/)?.[0].length || 0; |
| 29 | // content = lines |
| 30 | // .map((line) => line.slice(firstLineIndentation)) |
| 31 | // .join("\n"); |
| 32 | // } |
| 33 | |
| 34 | // Truncate |
| 35 | return content.length > 200 ? content.slice(0, 196) + "\n..." : content; |
| 36 | }, [symbol]); |
| 37 | |
| 38 | const id = uuidv4(); |
| 39 | return ( |
| 40 | <> |
| 41 | <span |
| 42 | className="mx-[0.1em] mb-[0.15em] inline-flex cursor-pointer flex-row items-center gap-[0.2rem] rounded-md align-middle hover:ring-1" |
| 43 | onClick={onClick} |
| 44 | data-tooltip-id={id} |
| 45 | data-tooltip-delay-show={500} |
| 46 | > |
| 47 | <code className="text-link align-middle">{content}</code> |
| 48 | </span> |
| 49 | <ToolTip id={id} place="top" className="m-0 p-0"> |
| 50 | <pre className="text-left">{processedContent ?? symbol.filepath}</pre> |
| 51 | </ToolTip> |
| 52 | </> |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | export default SymbolLink; |