File size: 10,806 Bytes
05251fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/**
 * Edge semantics shared by the File Graph and the Call Graph.
 *
 * Both surfaces previously distinguished relationship direction by stroke colour
 * alone: emerald for incoming, indigo for outgoing. That is unreadable for anyone
 * with a red/green deficiency, and it left two relationships with no treatment at
 * all β€” a dependency cycle in the File Graph and mutual recursion in the Call
 * Graph both rendered as two ordinary arrows.
 *
 * Every relationship here is therefore carried by at least two channels: colour
 * plus stroke width, dash pattern, or arrowheads at both ends.
 *
 * Pure module β€” no React, no React Flow β€” so the rules can be unit tested. The
 * caller attaches its own `MarkerType`, since that enum lives in React Flow.
 */

/** Tones. Kept literal so a test can assert the mapping. */
export const EDGE_TONE = {
  /** A traced dependency path. */
  path: '#f43f5e',
  /** Something the focused entity reaches: a dependency, or a callee. */
  outgoing: '#818cf8',
  /** Something that reaches the focused entity: a dependent, or a caller. */
  incoming: '#34d399',
  /** A cycle, or mutual recursion. */
  cyclic: '#f59e0b',
  /** A call the analyser could not resolve to a single target. */
  ambiguous: '#f59e0b',
  /** Present but not part of the current focus. */
  inactive: '#27272a',
  /** No selection anywhere β€” the resting state of the topology. */
  idle: '#3f3f46',
} as const;

/** Dash patterns. Distinct enough to read apart at 1x zoom. */
export const EDGE_DASH = {
  solid: undefined as string | undefined,
  /** Incoming: something depends on / calls this. */
  incoming: '6 3',
  /** Cyclic: tight ticks, reads as "goes both ways". */
  cyclic: '2 2',
  /** Unresolved: sparse dots, reads as "uncertain". */
  ambiguous: '4 4',
  /** Recursion: fine dots. */
  recursive: '1 3',
} as const;

export interface EdgeVisual {
  stroke: string;
  strokeWidth: number;
  opacity: number;
  /** `strokeDasharray`, or undefined for a solid line. */
  dash?: string;
  /** True when the relationship runs both ways and needs two arrowheads. */
  bothEnds: boolean;
}

/**
 * Keys (`"source|target"`) for every edge whose reverse also exists.
 *
 * In the File Graph these are dependency cycles; in the Call Graph they are
 * mutual recursion. Derived entirely from the edge list already on screen β€” no
 * additional request and no invented relationship.
 */
export function buildMutualPairSet(
  edges: readonly { source: string; target: string }[],
): Set<string> {
  const present = new Set<string>();
  for (const e of edges) present.add(`${e.source}|${e.target}`);

  const mutual = new Set<string>();
  for (const e of edges) {
    if (e.source === e.target) continue; // a self-loop is not a mutual pair
    if (present.has(`${e.target}|${e.source}`)) {
      mutual.add(`${e.source}|${e.target}`);
    }
  }
  return mutual;
}

export function isMutual(
  mutualPairs: Set<string>,
  source: string,
  target: string,
): boolean {
  return mutualPairs.has(`${source}|${target}`);
}

/* ─────────────────────────────────────────────────────────────────────────────
 * Focus choreography.
 *
 * The two graphs report different dimensions, so they should not react to a
 * selection in the same rhythm:
 *
 *   architecture β€” a topology. Direction is a property of the structure, so both
 *                  directions resolve together and the unrelated graph recedes
 *                  slightly after them.
 *   execution    β€” a flow. What runs *before* the selected function illuminates
 *                  first, the function is already lit, and what it calls resolves
 *                  after β€” so the eye reads the traversal in the order it happens.
 *
 * Expressed as delays rather than as an animation: the values are handed to CSS
 * transitions on properties that were already transitioning, so a focus change
 * costs no extra frames, nothing loops, and the motion ends as soon as it has
 * communicated the ordering. Pure, so the cadence can be unit tested.
 * ────────────────────────────────────────────────────────────────────────── */

export type GraphSurface = 'architecture' | 'execution';

export type FocusRole =
  /** The selected entity itself. */
  | 'focus'
  /** Reaches the selection: a dependent, or a caller. */
  | 'incoming'
  /** Reached by the selection: a dependency, or a callee. */
  | 'outgoing'
  /** Present, but not part of the current focus. */
  | 'unrelated'
  /** Nothing is selected anywhere. */
  | 'idle';

export interface Choreography {
  /** Milliseconds before this element responds to the focus change. */
  delayMs: number;
  /** Milliseconds the response takes. */
  durationMs: number;
}

/** Longest delay any role can carry, so callers can size a settle timeout. */
export const CHOREOGRAPHY_SETTLE_MS = 760;

export function resolveFocusChoreography(
  role: FocusRole,
  surface: GraphSurface,
): Choreography {
  // The focus is the reason the change happened; it never waits.
  if (role === 'focus') return { delayMs: 0, durationMs: 260 };
  if (role === 'idle') return { delayMs: 0, durationMs: 320 };

  if (surface === 'execution') {
    if (role === 'incoming') return { delayMs: 0, durationMs: 260 };
    if (role === 'outgoing') return { delayMs: 210, durationMs: 300 };
    return { delayMs: 340, durationMs: 420 }; // unrelated retreats last
  }

  // Architecture: direction is structural, not temporal.
  if (role === 'incoming' || role === 'outgoing') {
    return { delayMs: 70, durationMs: 280 };
  }
  return { delayMs: 280, durationMs: 420 };
}

/** The role an edge plays relative to the current selection. */
export function edgeFocusRole(flags: {
  isIncoming: boolean;
  isOutgoing: boolean;
  hasActive: boolean;
}): FocusRole {
  if (!flags.hasActive) return 'idle';
  if (flags.isIncoming) return 'incoming';
  if (flags.isOutgoing) return 'outgoing';
  return 'unrelated';
}

/**
 * A CSS `transition` shorthand for the properties these graphs actually change
 * on a focus shift. Compositor-friendly where possible; `stroke` is not, but it
 * is a single paint on a handful of paths, not a per-frame recomputation.
 */
export function edgeTransition(c: Choreography): string {
  return (
    `stroke ${c.durationMs}ms ease ${c.delayMs}ms,` +
    ` stroke-width ${c.durationMs}ms ease ${c.delayMs}ms,` +
    ` opacity ${c.durationMs}ms ease ${c.delayMs}ms`
  );
}

export interface DependencyEdgeFlags {
  /** Part of an explicitly traced dependency path. */
  isPath: boolean;
  /** The focused file imports this edge's target. */
  isOutgoing: boolean;
  /** This edge's source imports the focused file. */
  isIncoming: boolean;
  /** The two files import each other. */
  isCyclic: boolean;
  /** Something is focused or hovered. */
  hasActive: boolean;
  /** Resting colour when nothing is focused, usually by source category. */
  idleTone?: string;
}

/**
 * File Graph: "what does this file depend on, and what depends on it?"
 *
 * Precedence is deliberate β€” a traced path outranks a cycle, which outranks
 * ordinary direction, because the trace is what the reader asked for.
 */
export function resolveDependencyEdgeStyle(flags: DependencyEdgeFlags): EdgeVisual {
  const { isPath, isOutgoing, isIncoming, isCyclic, hasActive, idleTone } = flags;

  if (isPath) {
    return {
      stroke: EDGE_TONE.path,
      strokeWidth: 2.75,
      opacity: 1,
      dash: EDGE_DASH.solid,
      bothEnds: false,
    };
  }

  if (isCyclic && (isOutgoing || isIncoming)) {
    return {
      stroke: EDGE_TONE.cyclic,
      strokeWidth: 2.25,
      opacity: 1,
      dash: EDGE_DASH.cyclic,
      bothEnds: true,
    };
  }

  if (isOutgoing) {
    return {
      stroke: EDGE_TONE.outgoing,
      strokeWidth: 2,
      opacity: 0.95,
      dash: EDGE_DASH.solid,
      bothEnds: false,
    };
  }

  if (isIncoming) {
    return {
      stroke: EDGE_TONE.incoming,
      strokeWidth: 2,
      opacity: 0.9,
      dash: EDGE_DASH.incoming,
      bothEnds: false,
    };
  }

  if (hasActive) {
    return {
      stroke: EDGE_TONE.inactive,
      strokeWidth: 1,
      opacity: 0.1,
      dash: EDGE_DASH.solid,
      bothEnds: false,
    };
  }

  return {
    stroke: idleTone ?? EDGE_TONE.idle,
    strokeWidth: 1.2,
    opacity: 0.42,
    dash: EDGE_DASH.solid,
    bothEnds: false,
  };
}

export interface CallEdgeFlags {
  /** The function calls itself. */
  isSelfCall: boolean;
  /** This function and the other call each other. */
  isMutualRecursion: boolean;
  /** The focused function calls this edge's target. */
  isOutgoing: boolean;
  /** This edge's source calls the focused function. */
  isIncoming: boolean;
  /** The analyser could not resolve the call target uniquely. */
  isAmbiguous: boolean;
  /** Something is focused. */
  hasActive: boolean;
}

/**
 * Call Graph: "what executes before and after this function?"
 *
 * Recursion outranks direction because it is a property of execution itself, and
 * an ambiguous call keeps its uncertainty treatment even inside the focus so a
 * reader never mistakes a guess for a resolved call.
 */
export function resolveCallEdgeStyle(flags: CallEdgeFlags): EdgeVisual {
  const {
    isSelfCall, isMutualRecursion, isOutgoing, isIncoming, isAmbiguous, hasActive,
  } = flags;

  if (isSelfCall || isMutualRecursion) {
    return {
      stroke: EDGE_TONE.path,
      strokeWidth: 2.25,
      opacity: 1,
      dash: EDGE_DASH.recursive,
      bothEnds: isMutualRecursion,
    };
  }

  if (isAmbiguous) {
    return {
      stroke: EDGE_TONE.ambiguous,
      // Still visible under focus, but never as strong as a resolved call.
      strokeWidth: isOutgoing || isIncoming ? 1.75 : 1.5,
      opacity: isOutgoing || isIncoming ? 0.85 : hasActive ? 0.3 : 0.65,
      dash: EDGE_DASH.ambiguous,
      bothEnds: false,
    };
  }

  if (isOutgoing) {
    return {
      stroke: EDGE_TONE.outgoing,
      strokeWidth: 2,
      opacity: 0.95,
      dash: EDGE_DASH.solid,
      bothEnds: false,
    };
  }

  if (isIncoming) {
    return {
      stroke: EDGE_TONE.incoming,
      strokeWidth: 2,
      opacity: 0.9,
      dash: EDGE_DASH.incoming,
      bothEnds: false,
    };
  }

  if (hasActive) {
    return {
      stroke: EDGE_TONE.inactive,
      strokeWidth: 1,
      opacity: 0.1,
      dash: EDGE_DASH.solid,
      bothEnds: false,
    };
  }

  return {
    stroke: EDGE_TONE.idle,
    strokeWidth: 1.2,
    opacity: 0.42,
    dash: EDGE_DASH.solid,
    bothEnds: false,
  };
}