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