Skip to content
refs

feat(map): code navigator on the repository and pull request pages

Amancommitted Dec 1, 202686c0d19parent 9ae5e36

The Map tab shows a commit's TypeScript and JavaScript code as blocks inside blocks: folders, files, declarations and class members. Zoom goes from Architecture to Code, where the blocks show the code. Selecting a name in a code card adds its definition to a trace below the map, drawn as the refs line, so a reader follows a path through the code without jumping around it. On a pull request, the map marks each changed file and declaration with its added and removed lines. The Changes list and the ] and [ keys step through every change, and Diff only folds the unchanged lines in each card. refs builds the map of a commit on push, for the default branch and the branches of open pull requests, and on first view of any other commit. Migration 0032 stores one gzipped map per commit.

6 decisions

Index TypeScript and JavaScript with a small lexical outliner that runs in the Worker.

Why

  • The repository uses TypeScript 7, which has no JavaScript compiler API.
  • The TypeScript 5 compiler is too large and too slow for a Worker request or queue message.
  • The outliner indexes this repository (167 files, 1290 symbols) in about 4 seconds, including the reads from Artifacts.

Paths considered

  1. Chosen
  2. typescript-compiler-apiIt does not fit the Worker size and memory limits.
  3. tree-sitter-wasmIt adds a WebAssembly build and a grammar per language for names the outliner already finds.

Assumes

  • Uses through `ns.member` and method calls on objects of unknown type are not linked. Readers accept that for now.

Touches

  • lexer.ts
  • outline.ts
  • graph.ts

Store one gzipped code map per commit in D1, and serve it from a route keyed by commit with an immutable cache.

Why

  • A commit never changes, so its map never changes and the browser can keep it for good.
  • The page loader then sends only the map's state, which cut the page from 987 KB to 15 KB.

Paths considered

  1. Chosen
  2. per-symbol-rowsThousands of rows per commit for data that is always read whole.
  3. map-in-loaderIt made every visit download and decode the whole map with the page.

Assumes

  • A map over 1.9 MB gzipped is stored as too large. No current repository is near that.

Touches

  • 0032_code_maps.sql
  • store.ts
  • repo-code-graph.ts
  • repo-code-file.ts

Build a commit's map on push for the default branch and open pull request branches, and on first view for any other commit.

Why

  • Maps for the pages people open most are ready before anyone opens them.
  • A claim row makes each commit build once, even when a push and a view race.

Paths considered

  1. Chosen
  2. every-pushed-branchTask branches without a pull request are rarely mapped and would cost a build per push.

Touches

  • code-maps.ts
  • pushes.ts

Lay out each block's children as a storyline, callers left and the code they use right, wrapping into rows when a block gets much wider than it is tall.

Why

  • The order of reading follows the order of calls, as in a story.
  • Without wrapping, the map of this repository was a thin band that used a third of the screen.

Touches

  • layout.ts

Draw folder and file names and change counts in screen space, and leave out a label that would cover another one.

Why

  • Names scaled with the map were unreadable when zoomed out and overlapped each other.
  • Labels hide while the map moves and come back when it rests, so a zoom moves one layer only.

Touches

  • map-view.tsx
  • app.css

Give the Map pages the full width and the full height of the window.

Why

  • The owner asked for the map to take the full page.
  • A canvas gains nothing from the 1080 px reading column.

Touches

  • shell.tsx
  • repo.tsx
  • fill.ts
38 files changed · +3335 −22
README.md+8 −0
apps/backend/code-index/build.ts+125 −0
apps/backend/code-index/graph.ts+167 −0
apps/backend/code-index/lexer.ts+148 −0
apps/backend/code-index/outline.ts+377 −0
apps/backend/code-index/store.ts+75 −0
apps/backend/code-maps.ts+80 −0
apps/backend/contract/handles.ts+9 −0
apps/backend/contract/views.ts+70 −0
apps/backend/handles/pull.ts+15 −2
apps/backend/handles/repo.ts+17 −0
apps/backend/pushes.ts+2 −0
apps/backend/test/code-maps.test.ts+149 −0
apps/frontend/DESIGN.md+1 −0
apps/frontend/app.css+56 −0
apps/frontend/components/code-map/changes-panel.tsx+66 −0
apps/frontend/components/code-map/code-card.tsx+167 −0
apps/frontend/components/code-map/code-navigator.tsx+352 −0
apps/frontend/components/code-map/fill.ts+33 −0
apps/frontend/components/code-map/layout.ts+140 −0
apps/frontend/components/code-map/map-states.tsx+82 −0
apps/frontend/components/code-map/map-view.tsx+542 −0
apps/frontend/components/code-map/model.ts+332 −0
apps/frontend/components/code-map/sources.ts+84 −0
apps/frontend/components/code-map/trace-line.tsx+31 −0
apps/frontend/components/code-map/use-code-graph.ts+38 −0
apps/frontend/components/shell.tsx+2 −2
apps/frontend/routes.ts+4 −0
apps/frontend/routes/pull-layout.tsx+4 −4
apps/frontend/routes/pull-map.tsx+37 −0
apps/frontend/routes/repo-code-file.ts+24 −0
apps/frontend/routes/repo-code-graph.ts+21 −0
apps/frontend/routes/repo-map.tsx+31 −0
apps/frontend/routes/repo.tsx+2 −2
migrations/0032_code_maps.sql+12 −0
packages/design-system/components/repo.tsx+3 −2
packages/design-system/components/shell.tsx+13 −10
packages/design-system/icons.tsx+16 −0