codeviz 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1 +1,446 @@
1
- # codeviz
1
+ # CodeViz
2
+
3
+ **Interactive 2D codebase visualization with hierarchical semantic zoom**
4
+
5
+ [![npm version](https://img.shields.io/npm/v/codeviz.svg)](https://www.npmjs.com/package/codeviz)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
7
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.3+-blue.svg)](https://www.typescriptlang.org/)
8
+
9
+ CodeViz is a React component library for visualizing codebases as interactive 2D maps. It provides developers with an intuitive, map-like interface for understanding code structure, dependencies, and relationships at multiple levels of abstraction.
10
+
11
+ ## Features
12
+
13
+ - **Hierarchical Visualization** - Directories as nested containment regions (scopes)
14
+ - **Semantic Zoom** - 5 discrete zoom levels showing different information density
15
+ - **Multi-Language Support** - TypeScript, JavaScript, Python via tree-sitter parsing
16
+ - **Dependency Visualization** - Import and call graph relationships as edges
17
+ - **Overlay System** - Cursors, badges, annotations for collaboration
18
+ - **Theme Support** - Light and dark themes with full customization
19
+ - **Dual Renderers** - React Flow (default) or Sigma.js (WebGL) for large graphs
20
+ - **High Performance** - Supports codebases up to 10,000+ files with Sigma renderer
21
+
22
+ ## Quick Start
23
+
24
+ ```bash
25
+ npm install codeviz
26
+ ```
27
+
28
+ ```tsx
29
+ import { CodeMapComponent, useLayout, analyzeCodebase } from 'codeviz';
30
+
31
+ function App() {
32
+ const [codeGraph, setCodeGraph] = useState(null);
33
+
34
+ useEffect(() => {
35
+ analyzeCodebase('./src').then(result => {
36
+ setCodeGraph(result.codeGraph);
37
+ });
38
+ }, []);
39
+
40
+ const { codeMap, isComputing } = useLayout(codeGraph);
41
+
42
+ if (isComputing || !codeMap) {
43
+ return <div>Loading...</div>;
44
+ }
45
+
46
+ return (
47
+ <CodeMapComponent
48
+ codeMap={codeMap}
49
+ theme="dark"
50
+ onNodeClick={(node) => console.log('Clicked:', node)}
51
+ />
52
+ );
53
+ }
54
+ ```
55
+
56
+ ## Installation
57
+
58
+ ```bash
59
+ # npm
60
+ npm install codeviz
61
+
62
+ # yarn
63
+ yarn add codeviz
64
+
65
+ # pnpm
66
+ pnpm add codeviz
67
+ ```
68
+
69
+ ### Peer Dependencies
70
+
71
+ CodeViz requires React 18+:
72
+
73
+ ```bash
74
+ npm install react react-dom
75
+ ```
76
+
77
+ ## Usage
78
+
79
+ ### Analyzing a Codebase
80
+
81
+ Use `analyzeCodebase` to scan and parse source files:
82
+
83
+ ```tsx
84
+ import { analyzeCodebase } from 'codeviz';
85
+
86
+ const result = await analyzeCodebase('./src', {
87
+ extensions: ['.ts', '.tsx', '.js', '.jsx'],
88
+ ignorePatterns: ['node_modules', 'dist', '*.test.ts'],
89
+ maxDepth: 10,
90
+ });
91
+
92
+ // result.codeGraph contains:
93
+ // - files: FileNode[]
94
+ // - directories: DirectoryNode[]
95
+ // - symbols: SymbolNode[]
96
+ // - imports: ImportEdge[]
97
+ // - calls: CallEdge[]
98
+ ```
99
+
100
+ ### Computing Layout
101
+
102
+ Transform a `CodeGraph` into positioned `CodeMap`:
103
+
104
+ ```tsx
105
+ import { useLayout, computeLayout } from 'codeviz';
106
+
107
+ // React hook (recommended)
108
+ const { codeMap, isComputing, error } = useLayout(codeGraph, {
109
+ algorithm: 'force-directed',
110
+ clusteringEnabled: true,
111
+ });
112
+
113
+ // Or imperative API
114
+ const codeMap = await computeLayout(codeGraph, layoutConfig);
115
+ ```
116
+
117
+ ### Rendering the Map
118
+
119
+ ```tsx
120
+ import { CodeMapComponent } from 'codeviz';
121
+
122
+ <CodeMapComponent
123
+ codeMap={codeMap}
124
+ theme="dark"
125
+
126
+ // Interaction callbacks
127
+ onNodeClick={(node) => handleNodeClick(node)}
128
+ onNodeHover={(node) => handleNodeHover(node)}
129
+ onScopeToggle={(scopeId, expanded) => handleScopeToggle(scopeId, expanded)}
130
+ onViewportChange={(viewport) => handleViewportChange(viewport)}
131
+ onZoomLevelChange={(level) => handleZoomLevelChange(level)}
132
+
133
+ // Selection (controlled)
134
+ selectedNodes={['file-1', 'file-2']}
135
+ onSelectionChange={(nodeIds) => setSelectedNodes(nodeIds)}
136
+ />
137
+ ```
138
+
139
+ ### Using Overlays
140
+
141
+ Add cursors, badges, and annotations:
142
+
143
+ ```tsx
144
+ import { useOverlayPort, CodeMapComponent } from 'codeviz';
145
+
146
+ function CollaborativeMap({ codeMap }) {
147
+ const { port, overlays } = useOverlayPort();
148
+
149
+ // Add a cursor overlay
150
+ const cursorId = port.bind({
151
+ type: 'cursor',
152
+ position: { type: 'absolute', x: 100, y: 200 },
153
+ color: '#6366f1',
154
+ label: 'Alice',
155
+ });
156
+
157
+ // Add a badge to a node
158
+ port.bind({
159
+ type: 'badge',
160
+ position: { type: 'node', nodeId: 'file-1', anchor: 'top-right' },
161
+ variant: 'count',
162
+ value: 3,
163
+ });
164
+
165
+ // Update cursor position
166
+ port.update(cursorId, {
167
+ position: { type: 'absolute', x: 150, y: 250 },
168
+ });
169
+
170
+ return (
171
+ <CodeMapComponent
172
+ codeMap={codeMap}
173
+ overlayPort={port}
174
+ onOverlayClick={(id, overlay) => console.log('Overlay clicked:', id)}
175
+ />
176
+ );
177
+ }
178
+ ```
179
+
180
+ ## Sigma Renderer (WebGL)
181
+
182
+ For large codebases (1,000+ files), use the Sigma.js renderer for better performance:
183
+
184
+ ```tsx
185
+ import { CodeMap } from 'codeviz';
186
+
187
+ <CodeMap
188
+ codeMap={codeMap}
189
+ codeGraph={codeGraph} // Required for nexus view
190
+ renderer="sigma" // Use WebGL renderer
191
+ view="structure" // "structure" or "nexus"
192
+ theme="dark"
193
+ onNodeClick={(nodeId, node) => console.log('Clicked:', nodeId)}
194
+ />
195
+ ```
196
+
197
+ ### View Modes
198
+
199
+ | Mode | Description | Data Source | Best For |
200
+ |------|-------------|-------------|----------|
201
+ | `structure` | File system hierarchy with scopes | `CodeMap` | Understanding project organization |
202
+ | `nexus` | Flat relationship graph | `CodeGraph` | Exploring dependencies and imports |
203
+
204
+ ### When to Use Sigma vs React Flow
205
+
206
+ | Scenario | Recommended Renderer |
207
+ |----------|---------------------|
208
+ | < 500 nodes | React Flow (richer interactions) |
209
+ | 500 - 2,000 nodes | Either (test both) |
210
+ | 2,000+ nodes | Sigma (WebGL performance) |
211
+ | Need nested scopes | React Flow |
212
+ | Need force-directed layout | Sigma |
213
+
214
+ ### Sigma-Specific Features
215
+
216
+ ```tsx
217
+ import { SigmaRenderer } from 'codeviz';
218
+
219
+ // Direct Sigma renderer access (advanced)
220
+ <SigmaRenderer
221
+ graph={graphologyGraph}
222
+ view="nexus"
223
+ theme={darkTheme}
224
+
225
+ // Selection
226
+ selectedNodeId={selectedNode}
227
+ onSelectedNodeChange={setSelectedNode}
228
+
229
+ // Highlights (e.g., from AI/search)
230
+ highlightedNodeIds={['file-1', 'file-2']}
231
+
232
+ // Controls
233
+ showControls={true}
234
+ controlsPosition="bottom-right"
235
+
236
+ // Overlays work with Sigma too
237
+ overlays={overlays}
238
+ onOverlayClick={handleOverlayClick}
239
+ />
240
+ ```
241
+
242
+ ### Imperative API
243
+
244
+ ```tsx
245
+ import { useRef } from 'react';
246
+ import { SigmaRenderer, SigmaRendererHandle } from 'codeviz';
247
+
248
+ function MyComponent() {
249
+ const sigmaRef = useRef<SigmaRendererHandle>(null);
250
+
251
+ const handleFocus = () => {
252
+ sigmaRef.current?.focusNode('file-1');
253
+ };
254
+
255
+ const handleFitAll = () => {
256
+ sigmaRef.current?.fitToNodes(['file-1', 'file-2', 'file-3']);
257
+ };
258
+
259
+ return (
260
+ <>
261
+ <SigmaRenderer ref={sigmaRef} ... />
262
+ <button onClick={handleFocus}>Focus Node</button>
263
+ <button onClick={() => sigmaRef.current?.zoomIn()}>Zoom In</button>
264
+ <button onClick={() => sigmaRef.current?.resetView()}>Reset</button>
265
+ </>
266
+ );
267
+ }
268
+ ```
269
+
270
+ ## Zoom Levels
271
+
272
+ CodeViz uses 5 semantic zoom levels:
273
+
274
+ | Level | Name | Zoom Range | Content |
275
+ |-------|------|------------|---------|
276
+ | 0 | Overview | 0 - 0.25 | Top directories only, aggregated edges |
277
+ | 1 | Modules | 0.25 - 0.5 | Subdirectories + large files (>200 LOC) |
278
+ | 2 | Files | 0.5 - 1.0 | All files + exported symbols |
279
+ | 3 | Symbols | 1.0 - 2.0 | All symbols + call graph edges |
280
+ | 4 | Code | 2.0+ | Code preview panels, all details |
281
+
282
+ ## Configuration
283
+
284
+ ### Layout Configuration
285
+
286
+ ```tsx
287
+ import { computeLayout, DEFAULT_LAYOUT_CONFIG } from 'codeviz';
288
+
289
+ const layoutConfig = {
290
+ ...DEFAULT_LAYOUT_CONFIG,
291
+
292
+ // Clustering
293
+ clusteringEnabled: true,
294
+ clusteringResolution: 1.0,
295
+
296
+ // Force-directed layout
297
+ forceIterations: 100,
298
+ forceGravity: 0.1,
299
+ forceRepulsion: 1.0,
300
+
301
+ // Sizing
302
+ nodeSizeBase: 40,
303
+ nodeSizeScale: 1.5,
304
+ scopePadding: { top: 40, right: 20, bottom: 20, left: 20 },
305
+ };
306
+
307
+ const codeMap = await computeLayout(codeGraph, layoutConfig);
308
+ ```
309
+
310
+ ### Theme Customization
311
+
312
+ ```tsx
313
+ import { CodeMapComponent, darkTheme, mergeTheme } from 'codeviz';
314
+
315
+ const customTheme = mergeTheme(darkTheme, {
316
+ colors: {
317
+ nodes: {
318
+ file: { background: '#1e293b', border: '#334155' },
319
+ },
320
+ scopes: {
321
+ depth0: '#0f172a',
322
+ depth1: '#1e293b',
323
+ depth2: '#334155',
324
+ },
325
+ },
326
+ });
327
+
328
+ <CodeMapComponent codeMap={codeMap} theme={customTheme} />
329
+ ```
330
+
331
+ ## API Overview
332
+
333
+ ### Components
334
+
335
+ | Component | Description |
336
+ |-----------|-------------|
337
+ | `CodeMapComponent` | Main visualization component (React Flow renderer) |
338
+ | `CodeMap` | Unified component supporting both renderers |
339
+ | `SigmaRenderer` | WebGL-based Sigma.js renderer |
340
+ | `ThemeProvider` | Theme context provider |
341
+ | `OverlayLayer` | Overlay rendering layer |
342
+ | `GraphControls` | Zoom/pan controls for Sigma renderer |
343
+
344
+ ### Hooks
345
+
346
+ | Hook | Description |
347
+ |------|-------------|
348
+ | `useLayout` | Compute layout from CodeGraph |
349
+ | `useOverlayPort` | Manage overlay state |
350
+ | `useTheme` | Access current theme |
351
+
352
+ ### Core Functions
353
+
354
+ | Function | Description |
355
+ |----------|-------------|
356
+ | `analyzeCodebase` | Scan and parse source files |
357
+ | `computeLayout` | Transform CodeGraph to CodeMap |
358
+ | `createOverlayPort` | Create imperative overlay controller |
359
+
360
+ ### Types
361
+
362
+ ```tsx
363
+ import type {
364
+ // Input types
365
+ CodeGraph,
366
+ FileNode,
367
+ DirectoryNode,
368
+ SymbolNode,
369
+
370
+ // Output types
371
+ CodeMap,
372
+ MapNode,
373
+ MapScope,
374
+ MapEdge,
375
+
376
+ // Configuration
377
+ LayoutConfig,
378
+ ZoomLevelConfig,
379
+ Theme,
380
+
381
+ // Overlays
382
+ Overlay,
383
+ CursorOverlay,
384
+ BadgeOverlay,
385
+ AnnotationOverlay,
386
+ } from 'codeviz';
387
+ ```
388
+
389
+ ## Storybook
390
+
391
+ Explore components interactively:
392
+
393
+ ```bash
394
+ npm run storybook
395
+ ```
396
+
397
+ Visit http://localhost:6006 to see:
398
+ - CodeMap with sample data
399
+ - Theme comparisons (light/dark)
400
+ - Overlay demonstrations
401
+ - Hook integration examples
402
+
403
+ ## Development
404
+
405
+ ```bash
406
+ # Install dependencies
407
+ npm install
408
+
409
+ # Run tests
410
+ npm test
411
+
412
+ # Run tests once
413
+ npm run test:run
414
+
415
+ # Type checking
416
+ npm run typecheck
417
+
418
+ # Build library
419
+ npm run build
420
+
421
+ # Start Storybook
422
+ npm run storybook
423
+ ```
424
+
425
+ ## Contributing
426
+
427
+ 1. Fork the repository
428
+ 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
429
+ 3. Run tests (`npm test`)
430
+ 4. Commit your changes (`git commit -m 'Add amazing feature'`)
431
+ 5. Push to the branch (`git push origin feature/amazing-feature`)
432
+ 6. Open a Pull Request
433
+
434
+ ## License
435
+
436
+ MIT License - see [LICENSE](LICENSE) for details.
437
+
438
+ ## Acknowledgments
439
+
440
+ Built with:
441
+ - [React Flow](https://reactflow.dev/) - React library for node-based UIs
442
+ - [Sigma.js](https://www.sigmajs.org/) - WebGL graph visualization
443
+ - [tree-sitter](https://tree-sitter.github.io/) - Incremental parsing system
444
+ - [graphology](https://graphology.github.io/) - Graph data structure
445
+ - [ELK](https://www.eclipse.org/elk/) - Eclipse Layout Kernel
446
+ - [Lucide](https://lucide.dev/) - Icon library