File size: 12,330 Bytes
42ae0b9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | import { useState, useEffect, useMemo, useCallback } from "react";
import { ChevronRight, ChevronDown, Search, X } from "lucide-react";
import type { AstNode } from "@/lib/api";
// ββ Node type β CSS class mapping βββββββββββββββββββββββββββββββββββββ
function getNodeClass(type: string): string {
const t = type.toLowerCase();
if (t.includes("keyword") || t.includes("statement") || t.includes("clause")) return "ast-keyword";
if (t.includes("literal") || t.includes("quoted") || t.includes("string")) return "ast-literal";
if (t.includes("numeric") || t.includes("integer") || t.includes("float")) return "ast-numeric";
if (t.includes("comment")) return "ast-comment";
if (t.includes("function") || t.includes("func")) return "ast-function";
if (t.includes("operator") || t.includes("comparison") || t.includes("arithmetic")) return "ast-operator";
if (t.includes("type") || t.includes("datatype")) return "ast-type";
if (t.includes("whitespace") || t.includes("newline") || t.includes("indent")) return "ast-whitespace";
return "ast-default";
}
function getNodeBadgeStyle(type: string): React.CSSProperties {
const t = type.toLowerCase();
if (t.includes("keyword") || t.includes("statement") || t.includes("clause"))
return { background: "oklch(0.72 0.18 270 / 0.15)", color: "oklch(0.72 0.18 270)", border: "1px solid oklch(0.72 0.18 270 / 0.3)" };
if (t.includes("literal") || t.includes("quoted") || t.includes("string"))
return { background: "oklch(0.75 0.16 145 / 0.15)", color: "oklch(0.75 0.16 145)", border: "1px solid oklch(0.75 0.16 145 / 0.3)" };
if (t.includes("numeric") || t.includes("integer") || t.includes("float"))
return { background: "oklch(0.78 0.18 55 / 0.15)", color: "oklch(0.78 0.18 55)", border: "1px solid oklch(0.78 0.18 55 / 0.3)" };
if (t.includes("comment"))
return { background: "oklch(0.50 0.010 264 / 0.15)", color: "oklch(0.55 0.010 264)", border: "1px solid oklch(0.50 0.010 264 / 0.3)" };
if (t.includes("function") || t.includes("func"))
return { background: "oklch(0.78 0.16 210 / 0.15)", color: "oklch(0.78 0.16 210)", border: "1px solid oklch(0.78 0.16 210 / 0.3)" };
if (t.includes("operator") || t.includes("comparison"))
return { background: "oklch(0.82 0.010 264 / 0.10)", color: "oklch(0.82 0.010 264)", border: "1px solid oklch(0.82 0.010 264 / 0.3)" };
if (t.includes("whitespace") || t.includes("newline"))
return { background: "oklch(0.20 0.008 264 / 0.5)", color: "oklch(0.38 0.008 264)", border: "1px solid oklch(0.25 0.008 264 / 0.3)" };
return { background: "oklch(0.20 0.010 264 / 0.5)", color: "oklch(0.72 0.010 264)", border: "1px solid oklch(0.28 0.010 264 / 0.3)" };
}
// ββ Search helpers βββββββββββββββββββββββββββββββββββββββββββββββββββββ
function collectMatchIds(node: AstNode, query: string, matchIds: Set<string>, ancestorIds: Set<string>, path: string[]): boolean {
const q = query.toLowerCase();
const matches =
node.type.toLowerCase().includes(q) ||
node.name.toLowerCase().includes(q) ||
(node.raw?.toLowerCase().includes(q) ?? false);
let childMatched = false;
for (const child of node.children) {
if (collectMatchIds(child, query, matchIds, ancestorIds, [...path, node.id])) {
childMatched = true;
}
}
if (matches) {
matchIds.add(node.id);
path.forEach((id) => ancestorIds.add(id));
}
return matches || childMatched;
}
function highlightText(text: string, query: string): React.ReactNode {
if (!query) return text;
const idx = text.toLowerCase().indexOf(query.toLowerCase());
if (idx === -1) return text;
return (
<>
{text.slice(0, idx)}
<mark style={{ background: "oklch(0.68 0.16 210 / 0.35)", color: "inherit", borderRadius: "2px" }}>
{text.slice(idx, idx + query.length)}
</mark>
{text.slice(idx + query.length)}
</>
);
}
// ββ Single tree node βββββββββββββββββββββββββββββββββββββββββββββββββββ
interface NodeProps {
node: AstNode;
depth: number;
query: string;
matchIds: Set<string>;
ancestorIds: Set<string>;
expandedIds: Set<string>;
onToggle: (id: string) => void;
onJumpToLine?: (line: number) => void;
}
function TreeNode({ node, depth, query, matchIds, ancestorIds, expandedIds, onToggle, onJumpToLine }: NodeProps) {
const isMatch = matchIds.has(node.id);
const isAncestor = ancestorIds.has(node.id);
const isExpanded = expandedIds.has(node.id);
const hasChildren = node.children.length > 0;
// Skip pure whitespace nodes when no search
const isWhitespace = node.type.toLowerCase().includes("whitespace") || node.type.toLowerCase().includes("newline");
if (isWhitespace && !query && !node.raw?.trim()) return null;
const indent = depth * 16;
const nodeClass = getNodeClass(node.type);
const badgeStyle = getNodeBadgeStyle(node.type);
return (
<div>
<div
className={`flex items-center gap-1.5 py-0.5 px-2 rounded cursor-pointer group transition-colors duration-100 ${
isMatch
? "bg-[oklch(0.68_0.16_210_/_0.12)] border border-[oklch(0.68_0.16_210_/_0.3)]"
: "hover:bg-[oklch(0.18_0.010_264)]"
}`}
style={{ paddingLeft: `${indent + 8}px` }}
onClick={() => {
if (hasChildren) onToggle(node.id);
if (node.start_line && onJumpToLine) onJumpToLine(node.start_line);
}}
>
{/* Expand/collapse chevron */}
<span className="w-4 h-4 flex items-center justify-center flex-shrink-0 text-[oklch(0.45_0.010_264)]">
{hasChildren ? (
isExpanded ? <ChevronDown size={12} /> : <ChevronRight size={12} />
) : (
<span className="w-1 h-1 rounded-full bg-[oklch(0.30_0.010_264)]" />
)}
</span>
{/* Node type badge */}
<span
className="text-[10px] font-mono font-medium px-1.5 py-0.5 rounded flex-shrink-0"
style={badgeStyle}
>
{highlightText(node.type, query)}
</span>
{/* Raw value for leaf nodes */}
{node.is_leaf && node.raw && node.raw.trim() && (
<span className={`text-xs font-mono truncate max-w-[200px] ${nodeClass}`}>
{highlightText(JSON.stringify(node.raw), query)}
</span>
)}
{/* Position info */}
{node.start_line && (
<span className="text-[10px] text-[oklch(0.38_0.010_264)] ml-auto flex-shrink-0 opacity-0 group-hover:opacity-100 transition-opacity">
L{node.start_line}:{node.start_pos}
</span>
)}
</div>
{/* Children */}
{hasChildren && isExpanded && (
<div>
{node.children.map((child) => (
<TreeNode
key={child.id}
node={child}
depth={depth + 1}
query={query}
matchIds={matchIds}
ancestorIds={ancestorIds}
expandedIds={expandedIds}
onToggle={onToggle}
onJumpToLine={onJumpToLine}
/>
))}
</div>
)}
</div>
);
}
// ββ Main component βββββββββββββββββββββββββββββββββββββββββββββββββββββ
interface AstTreeViewProps {
tree: AstNode | null;
tokenCount?: number;
depth?: number;
onJumpToLine?: (line: number) => void;
}
export function AstTreeView({ tree, tokenCount, depth, onJumpToLine }: AstTreeViewProps) {
const [query, setQuery] = useState("");
const [expandedIds, setExpandedIds] = useState<Set<string>>(new Set());
// Auto-expand root on first load
useEffect(() => {
if (tree) {
setExpandedIds(new Set([tree.id, ...tree.children.map((c) => c.id)]));
}
}, [tree]);
const { matchIds, ancestorIds } = useMemo(() => {
if (!tree || !query.trim()) return { matchIds: new Set<string>(), ancestorIds: new Set<string>() };
const matchIds = new Set<string>();
const ancestorIds = new Set<string>();
collectMatchIds(tree, query.trim(), matchIds, ancestorIds, []);
return { matchIds, ancestorIds };
}, [tree, query]);
// Auto-expand ancestors when searching
useEffect(() => {
if (query.trim() && ancestorIds.size > 0) {
setExpandedIds((prev) => {
const next = new Set(prev);
ancestorIds.forEach((id) => next.add(id));
matchIds.forEach((id) => next.add(id));
return next;
});
}
}, [ancestorIds, matchIds, query]);
const handleToggle = useCallback((id: string) => {
setExpandedIds((prev) => {
const next = new Set(prev);
if (next.has(id)) next.delete(id);
else next.add(id);
return next;
});
}, []);
const expandAll = useCallback(() => {
if (!tree) return;
const ids = new Set<string>();
const collect = (n: AstNode) => { ids.add(n.id); n.children.forEach(collect); };
collect(tree);
setExpandedIds(ids);
}, [tree]);
const collapseAll = useCallback(() => {
if (!tree) return;
setExpandedIds(new Set([tree.id]));
}, [tree]);
if (!tree) {
return (
<div className="flex flex-col items-center justify-center h-full gap-3 text-[oklch(0.45_0.010_264)]">
<div className="text-4xl opacity-30">β¨/β©</div>
<p className="text-sm">Run analysis to view the AST</p>
</div>
);
}
return (
<div className="flex flex-col h-full">
{/* Toolbar */}
<div className="flex items-center gap-2 px-3 py-2 border-b border-[oklch(0.22_0.010_264)] flex-shrink-0">
<div className="relative flex-1">
<Search size={12} className="absolute left-2.5 top-1/2 -translate-y-1/2 text-[oklch(0.45_0.010_264)]" />
<input
value={query}
onChange={(e) => setQuery(e.target.value)}
placeholder="Filter by node typeβ¦"
className="h-7 pl-7 pr-7 text-xs w-full rounded"
style={{ background: "var(--bg-elevated)", border: "1px solid var(--border)", color: "var(--text-primary)", fontFamily: "var(--font-mono)", outline: "none", paddingLeft: "28px", paddingRight: "28px" }}
/>
{query && (
<button
onClick={() => setQuery("")}
className="absolute right-2 top-1/2 -translate-y-1/2 text-[oklch(0.45_0.010_264)] hover:text-foreground"
>
<X size={12} />
</button>
)}
</div>
<button onClick={expandAll} className="h-7 px-2 text-xs rounded transition-colors" style={{ color: "var(--text-secondary)", background: "transparent" }}>Expand</button>
<button onClick={collapseAll} className="h-7 px-2 text-xs rounded transition-colors" style={{ color: "var(--text-secondary)", background: "transparent" }}>Collapse</button>
</div>
{/* Stats */}
<div className="flex items-center gap-3 px-3 py-1.5 border-b border-[oklch(0.22_0.010_264)] flex-shrink-0">
<span className="text-[10px] text-[oklch(0.45_0.010_264)]">
<span className="text-[oklch(0.68_0.16_210)]">{tokenCount}</span> tokens
</span>
<span className="text-[10px] text-[oklch(0.45_0.010_264)]">
depth <span className="text-[oklch(0.68_0.16_210)]">{depth}</span>
</span>
{query && matchIds.size > 0 && (
<span className="text-[10px] text-[oklch(0.75_0.16_145)]">
{matchIds.size} match{matchIds.size !== 1 ? "es" : ""}
</span>
)}
{query && matchIds.size === 0 && (
<span className="text-[10px] text-[oklch(0.60_0.20_25)]">No matches</span>
)}
</div>
{/* Tree */}
<div className="flex-1 overflow-auto py-1 text-xs">
<TreeNode
node={tree}
depth={0}
query={query}
matchIds={matchIds}
ancestorIds={ancestorIds}
expandedIds={expandedIds}
onToggle={handleToggle}
onJumpToLine={onJumpToLine}
/>
</div>
</div>
);
}
|