path_coverage 0.1.0 → 0.3.0

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.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +315 -107
  3. data/lib/path_coverage/calls.rb +55 -23
  4. data/lib/path_coverage/card.rb +14 -0
  5. data/lib/path_coverage/condition.rb +33 -3
  6. data/lib/path_coverage/configuration.rb +43 -22
  7. data/lib/path_coverage/configuration_paths.rb +58 -0
  8. data/lib/path_coverage/console_summary.rb +75 -0
  9. data/lib/path_coverage/gaps.rb +32 -13
  10. data/lib/path_coverage/html/file.html +5 -5
  11. data/lib/path_coverage/html/index.html +6 -18
  12. data/lib/path_coverage/html/settings.js +50 -0
  13. data/lib/path_coverage/html/side.js +341 -413
  14. data/lib/path_coverage/html/sort.js +24 -21
  15. data/lib/path_coverage/html/style.css +339 -207
  16. data/lib/path_coverage/html/tabs.js +15 -0
  17. data/lib/path_coverage/html/theme.js +27 -0
  18. data/lib/path_coverage/html_branch.rb +30 -0
  19. data/lib/path_coverage/html_cards.rb +111 -0
  20. data/lib/path_coverage/html_cells.rb +66 -0
  21. data/lib/path_coverage/html_file.rb +82 -68
  22. data/lib/path_coverage/html_index.rb +95 -41
  23. data/lib/path_coverage/html_index_head.rb +29 -0
  24. data/lib/path_coverage/html_labels.rb +23 -0
  25. data/lib/path_coverage/html_methods.rb +66 -0
  26. data/lib/path_coverage/html_nav.rb +50 -50
  27. data/lib/path_coverage/html_report.rb +69 -56
  28. data/lib/path_coverage/html_statements.rb +166 -0
  29. data/lib/path_coverage/html_syntax.rb +156 -0
  30. data/lib/path_coverage/html_tabs.rb +26 -0
  31. data/lib/path_coverage/html_template.rb +6 -0
  32. data/lib/path_coverage/html_tree.rb +59 -74
  33. data/lib/path_coverage/json_reader.rb +101 -0
  34. data/lib/path_coverage/json_writer.rb +52 -7
  35. data/lib/path_coverage/kernel.rb +55 -53
  36. data/lib/path_coverage/kinds/base.rb +71 -0
  37. data/lib/path_coverage/kinds/block.rb +137 -0
  38. data/lib/path_coverage/kinds/case.rb +92 -0
  39. data/lib/path_coverage/kinds/chain.rb +45 -0
  40. data/lib/path_coverage/kinds/choice.rb +68 -0
  41. data/lib/path_coverage/kinds/contains.rb +113 -0
  42. data/lib/path_coverage/kinds/safe.rb +153 -0
  43. data/lib/path_coverage/kinds/write.rb +54 -0
  44. data/lib/path_coverage/kinds.rb +37 -0
  45. data/lib/path_coverage/manifest.rb +60 -20
  46. data/lib/path_coverage/manifest_blocks.rb +48 -10
  47. data/lib/path_coverage/manifest_case.rb +2 -3
  48. data/lib/path_coverage/manifest_chains.rb +76 -0
  49. data/lib/path_coverage/manifest_choices.rb +32 -18
  50. data/lib/path_coverage/manifest_folding.rb +91 -0
  51. data/lib/path_coverage/manifest_ignoring.rb +35 -0
  52. data/lib/path_coverage/manifest_writes.rb +67 -0
  53. data/lib/path_coverage/merge.rb +19 -3
  54. data/lib/path_coverage/method_statements.rb +300 -0
  55. data/lib/path_coverage/post.rb +8 -8
  56. data/lib/path_coverage/recorder.rb +20 -79
  57. data/lib/path_coverage/recorder_blocks.rb +68 -0
  58. data/lib/path_coverage/recorder_cases.rb +23 -0
  59. data/lib/path_coverage/recorder_chains.rb +40 -0
  60. data/lib/path_coverage/recorder_writes.rb +26 -0
  61. data/lib/path_coverage/render.rb +56 -47
  62. data/lib/path_coverage/result.rb +19 -0
  63. data/lib/path_coverage/rewriter.rb +69 -107
  64. data/lib/path_coverage/rewriter_nested.rb +62 -0
  65. data/lib/path_coverage/site_paths.rb +9 -58
  66. data/lib/path_coverage/site_tree.rb +21 -57
  67. data/lib/path_coverage/statement_arms.rb +161 -0
  68. data/lib/path_coverage/summary.rb +41 -17
  69. data/lib/path_coverage/totals.rb +6 -12
  70. data/lib/path_coverage/version.rb +1 -1
  71. data/lib/path_coverage/within_methods.rb +92 -0
  72. metadata +40 -11
  73. data/lib/path_coverage/html/toggle.js +0 -12
  74. data/lib/path_coverage/html_paths.rb +0 -138
  75. data/lib/path_coverage/path_gaps.rb +0 -41
  76. data/lib/path_coverage/path_manifest.rb +0 -110
@@ -1,53 +1,82 @@
1
- // SPIKE: puts a file page's trees in a rail to the left of the
2
- // code, each level with its site, and its path grids in a rail
3
- // to the right. Each method whose paths are counted gets a bar
4
- // on the code's right edge, from its def to its end, that opens
5
- // its grid. One tree or grid is open at a time; opening one
6
- // widens its rail, sliding the code away from it.
1
+ // The behaviour of a file's page: its cards sit in the rail left
2
+ // of the code, one open at a time, level with its first line. A
3
+ // method's def number opens its card, unfolding its body; a loose
4
+ // site's button, outside every method, opens its lines' top card.
5
+ // The page's markup, from the Ruby, already marks every method's
6
+ // lines, numbers and rows; this only opens, closes and places.
7
7
  (function () {
8
8
  var code = document.querySelector('.code');
9
9
  if (!code) { return; }
10
10
  var box = code.parentElement;
11
- var trees = Array.prototype.slice.call(document.querySelectorAll('.tree'));
11
+ var rail = document.querySelector('.rail');
12
+ var divider = document.querySelector('.divider');
12
13
  var grids = Array.prototype.slice.call(document.querySelectorAll('.pathgrid'));
13
- var ccRail = document.createElement('div');
14
- ccRail.className = 'rail cc';
15
- var pcRail = document.createElement('div');
16
- pcRail.className = 'rail pc';
17
- var bars = grids.map(function (grid) {
18
- var bar = document.createElement('button');
19
- bar.type = 'button';
20
- bar.className = 'pathbar';
21
- bar.setAttribute('data-status', grid.getAttribute('data-status'));
22
- bar.setAttribute('aria-controls', grid.id);
23
- bar.setAttribute('aria-expanded', 'false');
24
- bar.title = grid.querySelector('.calls').title;
25
- return bar;
26
- });
27
- // Each method's def line, lit from its def to its bar, so the
28
- // two read as one bracket around the method.
29
- var bands = grids.map(function (grid) {
30
- var band = document.createElement('div');
31
- band.className = 'methodband';
32
- band.setAttribute('data-status', grid.getAttribute('data-status'));
33
- return band;
34
- });
35
14
 
36
- // The line-number button that opens tree.
37
- function buttonOf(tree) {
38
- return document.querySelector('button.lnum[aria-controls="' + tree.id + '"]');
15
+ // True for a top card, of sites outside every method.
16
+ function isTop(grid) {
17
+ return grid.classList.contains('top-level');
39
18
  }
40
19
 
41
- // The bar that opens grid.
42
- function barOf(grid) {
43
- return bars[grids.indexOf(grid)];
44
- }
20
+ // A card's first and last lines.
21
+ function firstOf(grid) { return Number(grid.getAttribute('data-first')); }
22
+ function lastOf(grid) { return Number(grid.getAttribute('data-last')); }
45
23
 
46
24
  // The number span of line n.
47
25
  function lineno(n) {
48
26
  return document.getElementById('L' + n);
49
27
  }
50
28
 
29
+ // The row, a line or a loose site's block, holding line n.
30
+ function rowOf(n) {
31
+ return lineno(n).closest('.line, .block');
32
+ }
33
+
34
+ // The code span of line n, whether a line's own or one of a
35
+ // loose site's lines.
36
+ function srcOf(n) {
37
+ var number = lineno(n);
38
+ if (!number) { return null; }
39
+ var button = number.closest('button.lnum');
40
+ if (!button) { return number.parentElement.querySelector('.src'); }
41
+ var index = Array.prototype.indexOf.call(button.querySelectorAll('.lineno'), number);
42
+ return button.closest('.block').querySelectorAll('.srcs > .src')[index];
43
+ }
44
+
45
+ // The code of each method's lines, from its def to its end,
46
+ // which thickens its stripe while its card is open; none for a
47
+ // top card.
48
+ var methodSrcs = grids.map(function (grid) {
49
+ if (isTop(grid)) { return []; }
50
+ var srcs = [];
51
+ for (var n = firstOf(grid); n <= lastOf(grid); n++) { srcs.push(srcOf(n)); }
52
+ return srcs;
53
+ });
54
+
55
+ // The rows of each method's body, after its def line to its end,
56
+ // folded under its def line while its card is closed; a row
57
+ // holding its def line too is never folded. None for a top card.
58
+ var methodRows = grids.map(function (grid) {
59
+ if (isTop(grid)) { return []; }
60
+ var def = rowOf(firstOf(grid));
61
+ var rows = [];
62
+ for (var n = firstOf(grid) + 1; n <= lastOf(grid); n++) {
63
+ var row = rowOf(n);
64
+ if (row !== def && rows.indexOf(row) < 0) { rows.push(row); }
65
+ }
66
+ return rows;
67
+ });
68
+
69
+ // The def number of each method's card, which opens it; null for
70
+ // a top card, which its site's button opens.
71
+ var defs = grids.map(function (grid) {
72
+ return isTop(grid) ? null : lineno(firstOf(grid));
73
+ });
74
+
75
+ // The loose button opening each top card; null for a method's.
76
+ var buttons = grids.map(function (grid) {
77
+ return isTop(grid) ? document.querySelector('button.lnum[aria-controls="' + grid.id + '"]') : null;
78
+ });
79
+
51
80
  // Element's top, middle and bottom relative to within's top.
52
81
  function spot(element, within) {
53
82
  var rect = element.getBoundingClientRect();
@@ -55,270 +84,136 @@
55
84
  return { top: top, middle: top + rect.height / 2, bottom: top + rect.height };
56
85
  }
57
86
 
58
- // Opens panel, a tree or a grid, alone; null closes all. Any
59
- // path shown or pinned goes with the grid it was in.
60
- function openOnly(panel) {
61
- pin(null);
87
+ // Opens grid alone, null closing all: a method's card opens its
88
+ // body, every other method's folded; its def number, or its
89
+ // site's button, says it is open.
90
+ function openOnly(open) {
62
91
  clear();
63
- trees.forEach(function (tree) {
64
- tree.hidden = tree !== panel;
65
- buttonOf(tree).setAttribute('aria-expanded', tree === panel ? 'true' : 'false');
66
- });
67
92
  grids.forEach(function (grid, i) {
68
- grid.hidden = grid !== panel;
69
- barOf(grid).setAttribute('aria-expanded', grid === panel ? 'true' : 'false');
70
- bands[i].classList.toggle('open', grid === panel);
93
+ var shown = grid === open;
94
+ grid.hidden = !shown;
95
+ methodSrcs[i].forEach(function (src) { src.classList.toggle('method-open', shown); });
96
+ (defs[i] || buttons[i]).setAttribute('aria-expanded', String(shown));
71
97
  });
72
- var side = !panel ? '' : panel.classList.contains('tree') ? 'cc' : 'pc';
73
- document.body.setAttribute('data-open', side);
74
- slide(panel);
75
- }
76
-
77
- // Scrolls the page just enough to show all of panel when it
78
- // is a tree, eg one opened from a line near the bottom of the
79
- // window, so its last rows are not below the fold. Its first
80
- // row stays level with its site's line.
81
- function showWhole(panel) {
82
- if (panel && panel.classList.contains('tree')) {
83
- panel.scrollIntoView({ block: 'nearest', behavior: 'smooth' });
98
+ foldBodies(grids.indexOf(open));
99
+ document.body.toggleAttribute('data-open', open !== null);
100
+ place();
101
+ stickDef();
102
+ }
103
+
104
+ // True when the method of grids[inner] is defined inside that of
105
+ // grids[outer], eg a def inside a def.
106
+ function nests(inner, outer) {
107
+ return firstOf(grids[outer]) < firstOf(grids[inner]) && lastOf(grids[inner]) <= lastOf(grids[outer]);
108
+ }
109
+
110
+ // Folds every method's body but that of grids[open], none for -1:
111
+ // its rows show, but for those of any method inside it, which stay
112
+ // folded under their def lines; and its def line shows, with those
113
+ // of the methods it is inside, though they lie in folded bodies.
114
+ function foldBodies(open) {
115
+ var shown = [];
116
+ if (open >= 0) {
117
+ shown = methodRows[open].filter(function (row) {
118
+ return !grids.some(function (grid, j) {
119
+ return nests(j, open) && methodRows[j].indexOf(row) >= 0;
120
+ });
121
+ });
122
+ grids.forEach(function (grid, j) {
123
+ if (j === open || nests(open, j)) { shown.push(rowOf(firstOf(grid))); }
124
+ });
84
125
  }
126
+ methodRows.forEach(function (rows) {
127
+ rows.forEach(function (row) { row.classList.toggle('method-folded', shown.indexOf(row) < 0); });
128
+ });
85
129
  }
86
130
 
87
- // How long a rail takes to open or close, as style.css has it.
88
- var SLIDE_MS = 250;
89
-
90
- // Places everything on every frame while the rails slide, as
91
- // the bars and bands follow the code across, then shows all
92
- // of panel, the one just opened, once it is in its place.
93
- function slide(panel) {
94
- var end = performance.now() + SLIDE_MS + 50;
95
- function frame(now) {
96
- place();
97
- if (now < end) { requestAnimationFrame(frame); } else { showWhole(panel); }
98
- }
99
- requestAnimationFrame(frame);
100
- }
131
+ // The narrowest the rail drags to, and the narrowest the code
132
+ // beside it is left.
133
+ var RAIL_MIN = 240;
134
+ var CODE_MIN = 320;
135
+ var RAIL_KEY = 'path_coverage.rail';
136
+
137
+ // Sets the rail to width pixels, kept within its bounds, and
138
+ // places the open card again beside it.
139
+ function setRail(width) {
140
+ var most = box.getBoundingClientRect().width - CODE_MIN;
141
+ var wide = Math.round(Math.max(RAIL_MIN, Math.min(most, width)));
142
+ document.body.style.setProperty('--rail', wide + 'px');
143
+ place();
144
+ return wide;
145
+ }
146
+
147
+ // Dragging the divider sets the rail's width, kept in the
148
+ // browser, where it can be, for the next page.
149
+ divider.addEventListener('pointerdown', function (event) {
150
+ divider.setPointerCapture(event.pointerId);
151
+ divider.classList.add('dragging');
152
+ document.body.classList.add('dragging');
153
+ event.preventDefault();
154
+ });
155
+ divider.addEventListener('pointermove', function (event) {
156
+ if (!divider.classList.contains('dragging')) { return; }
157
+ setRail(event.clientX - box.getBoundingClientRect().left - 6);
158
+ });
159
+ divider.addEventListener('pointerup', function (event) {
160
+ divider.releasePointerCapture(event.pointerId);
161
+ divider.classList.remove('dragging');
162
+ document.body.classList.remove('dragging');
163
+ var wide = parseInt(document.body.style.getPropertyValue('--rail'), 10);
164
+ try { window.localStorage.setItem(RAIL_KEY, String(wide)); } catch (e) { /* this page only */ }
165
+ });
101
166
 
102
- // Sets an open tree's top so its first site's row, naming its
103
- // operator and path counts, is level with the first line of
104
- // its site's code.
105
- function placeTree(tree) {
106
- var block = buttonOf(tree).closest('.block');
107
- tree.style.top = '0px';
108
- var line = spot(block.querySelector('.src'), ccRail).middle;
109
- var row = spot(tree.querySelector('.site .row'), tree).middle;
110
- tree.style.top = (line - row) + 'px';
167
+ // The rail's width kept from an earlier page, if any.
168
+ function keptRail() {
169
+ try { return parseInt(window.localStorage.getItem(RAIL_KEY), 10); } catch (e) { return NaN; }
111
170
  }
112
171
 
113
- // Sets an open grid's top level with its def, its calls row
114
- // first and its decision rows straight after, with no gaps;
115
- // each row's label names its decision's line.
116
- // When the def has scrolled up under the sticky header, the
117
- // grid slides down to stay just below it, but never past its
118
- // method's last line.
172
+ // Sets an open card's top so its title row is level with its
173
+ // first line; its rows, and any fold open among them, flow down
174
+ // it. When its first line has scrolled up under the sticky
175
+ // header, the card slides down to stay just below it, but never
176
+ // past its last line. The rail stretches to the card's bottom,
177
+ // as the card, placed absolutely, adds no height of its own, so
178
+ // the box grows to show a card taller than the code.
119
179
  function placeGrid(grid) {
120
- var rows = Array.prototype.slice.call(grid.querySelectorAll('.prow'));
121
- var first = spot(lineno(grid.getAttribute('data-first')), pcRail);
122
- var last = spot(lineno(grid.getAttribute('data-last')), pcRail);
123
- var height = rows[0].getBoundingClientRect().height + 2;
124
- var tall = rows.length * height;
180
+ var first = spot(lineno(firstOf(grid)), rail);
181
+ var last = spot(lineno(lastOf(grid)), rail);
182
+ var title = spot(grid.querySelector('.prow'), grid).middle;
183
+ var tall = grid.getBoundingClientRect().height;
125
184
  var head = document.querySelector('.head').getBoundingClientRect().bottom;
126
- var below = head - pcRail.getBoundingClientRect().top + 8;
127
- var top = first.middle - height / 2;
185
+ var below = head - rail.getBoundingClientRect().top + 8;
186
+ var top = first.middle - title;
128
187
  top = Math.max(top, Math.min(below, last.bottom - tall));
129
188
  grid.style.top = top + 'px';
130
- grid.style.height = tall + 'px';
131
- rows.forEach(function (row, i) {
132
- row.style.top = (i * height) + 'px';
133
- });
189
+ rail.style.minHeight = (top + tall) + 'px';
134
190
  }
135
191
 
136
- // Places panel, a tree or a grid.
137
- function placeOne(panel) {
138
- if (panel.classList.contains('tree')) { placeTree(panel); } else { placeGrid(panel); }
139
- }
140
-
141
- // Sets each bar beside the lines from its def to its end, on
142
- // the code pane's right edge, half into the gap before the
143
- // rail.
144
- // How many methods' lines enclose grid's: its bar sits one
145
- // lane further in for each, like nested brackets.
146
- function laneOf(grid) {
147
- var first = Number(grid.getAttribute('data-first'));
148
- var last = Number(grid.getAttribute('data-last'));
149
- return grids.filter(function (other) {
150
- var f = Number(other.getAttribute('data-first'));
151
- var l = Number(other.getAttribute('data-last'));
152
- return other !== grid && f <= first && last <= l;
153
- }).length;
154
- }
155
-
156
- // The x of a bar's middle, from the code pane's right edge,
157
- // for a method in lane.
158
- function barInset(lane) {
159
- return lane * 20;
160
- }
161
-
162
- // Sets each method's band over its def line, from the def's
163
- // column to its bar; it scrolls with the code, so is placed
164
- // again whenever the code scrolls.
165
- function placeBands() {
166
- var pane = code.getBoundingClientRect();
167
- bands.forEach(function (band, i) {
168
- var grid = grids[i];
169
- var src = srcOf(grid.getAttribute('data-first'));
170
- var text = src.firstChild;
171
- var col = Number(grid.getAttribute('data-col'));
172
- var range = document.createRange();
173
- range.setStart(text, Math.min(col, text.length));
174
- range.collapse(true);
175
- var left = range.getBoundingClientRect().left - pane.left + code.scrollLeft;
176
- var right = code.scrollLeft + code.clientWidth - barInset(laneOf(grid));
177
- var line = src.getBoundingClientRect();
178
- band.style.left = (left - 4) + 'px';
179
- band.style.width = Math.max(0, right - left + 4) + 'px';
180
- band.style.top = (line.top - pane.top + code.scrollTop) + 'px';
181
- band.style.height = line.height + 'px';
182
- });
183
- }
184
-
185
- function placeBars() {
186
- var edge = code.getBoundingClientRect().right - box.getBoundingClientRect().left;
187
- placeBands();
188
- bars.forEach(function (bar, i) {
189
- bar.style.left = (edge - 8 - barInset(laneOf(grids[i]))) + 'px';
190
- var first = spot(lineno(grids[i].getAttribute('data-first')), box);
191
- var last = spot(lineno(grids[i].getAttribute('data-last')), box);
192
- bar.style.top = first.top + 'px';
193
- bar.style.height = (last.bottom - first.top) + 'px';
194
- });
195
- }
196
-
197
- // Places the bars and whichever tree or grid is open.
192
+ // Places whichever card is open; with none open the rail is
193
+ // only as tall as the code.
198
194
  function place() {
199
- placeBars();
200
- trees.concat(grids).forEach(function (panel) {
201
- if (!panel.hidden) { placeOne(panel); }
202
- });
203
- }
204
-
205
- // The trees in the left rail, the grids in the right, the bars
206
- // on the code's right edge, nothing open.
207
- function beside() {
208
- box.insertBefore(ccRail, code);
209
- box.appendChild(pcRail);
210
- trees.forEach(function (tree) { ccRail.appendChild(tree); });
211
- grids.forEach(function (grid) { pcRail.appendChild(grid); });
212
- bars.forEach(function (bar) { box.appendChild(bar); });
213
- bands.forEach(function (band) { code.appendChild(band); });
214
- code.addEventListener('scroll', placeBands);
215
- openOnly(null);
216
- placeBars();
217
- }
218
-
219
- // The code span of line n, whether a plain line or one of a
220
- // site's lines.
221
- function srcOf(n) {
222
- var number = lineno(n);
223
- if (!number) { return null; }
224
- var button = number.closest('button.lnum');
225
- if (!button) { return number.parentElement.querySelector('.src'); }
226
- var index = Array.prototype.indexOf.call(button.querySelectorAll('.lineno'), number);
227
- return button.closest('.block').querySelectorAll('.srcs > .src')[index];
228
- }
229
-
230
- // [first, last] from a "first,last" attribute, null for "".
231
- function span(text) {
232
- return text ? text.split(',').map(Number) : null;
195
+ rail.style.minHeight = '';
196
+ grids.forEach(function (grid) { if (!grid.hidden) { placeGrid(grid); } });
233
197
  }
234
198
 
199
+ // The code spans lit for the card row hovered.
235
200
  var lit = [];
236
- var chips = [];
237
- var hot = [];
238
201
 
239
202
  // Lights the code of every line in span, [first, last].
240
203
  function light(lines) {
241
- if (!lines) { return; }
242
204
  for (var n = lines[0]; n <= lines[1]; n++) {
243
205
  var src = srcOf(n);
244
206
  if (src) { src.classList.add('lit'); lit.push(src); }
245
207
  }
246
208
  }
247
209
 
248
- // Puts a T or F chip in the gutter of line n's row.
249
- function chip(n, text) {
250
- var number = lineno(n);
251
- if (!number) { return; }
252
- var nav = number.closest('.line, .block').querySelector('.nav');
253
- nav.setAttribute('data-chip', text);
254
- chips.push(nav);
255
- }
256
-
257
- // Undoes the lights and chips of the last path shown.
210
+ // Unlights the code lit.
258
211
  function clear() {
259
212
  lit.forEach(function (src) { src.classList.remove('lit'); });
260
- chips.forEach(function (nav) { nav.removeAttribute('data-chip'); });
261
- hot.forEach(function (cell) { cell.classList.remove('hot'); });
262
213
  lit = [];
263
- chips = [];
264
- hot = [];
265
214
  }
266
215
 
267
- // Shows in the code the path in column col of grid: for each
268
- // decision it makes, its if lit with the arm taken and its
269
- // end, T lighting the then arm, F the else (none without
270
- // one), and a T or F chip in the gutter. Decisions after a
271
- // path leaves early stay unlit.
272
- function show(grid, col) {
273
- clear();
274
- grid.querySelectorAll('.prow').forEach(function (row) {
275
- var cell = row.querySelectorAll('.pcell')[col];
276
- cell.classList.add('hot');
277
- hot.push(cell);
278
- if (row.classList.contains('calls') || cell.classList.contains('ended')) { return; }
279
- var taken = cell.classList.contains('true');
280
- light(span(row.getAttribute('data-head')));
281
- light(span(row.getAttribute(taken ? 'data-then' : 'data-else')));
282
- light(span(row.getAttribute('data-end')));
283
- chip(Number(row.getAttribute('data-line')), taken ? 'T' : 'F');
284
- });
285
- }
286
-
287
- // The pinned path, { grid, col }, shown whenever no other is
288
- // hovered; null when none is pinned.
289
- var pinned = null;
290
-
291
- // The grid and column of a cell.
292
- function columnOf(cell) {
293
- var cells = Array.prototype.slice.call(cell.parentElement.querySelectorAll('.pcell'));
294
- return { grid: cell.closest('.pathgrid'), col: cells.indexOf(cell) };
295
- }
296
-
297
- // Pins column, or unpins with null, outlining the pinned
298
- // column's cells.
299
- function pin(column) {
300
- document.querySelectorAll('.pcell.pinned').forEach(function (cell) {
301
- cell.classList.remove('pinned');
302
- });
303
- pinned = column;
304
- if (!column) { return; }
305
- column.grid.querySelectorAll('.prow').forEach(function (row) {
306
- row.querySelectorAll('.pcell')[column.col].classList.add('pinned');
307
- });
308
- }
309
-
310
- // Shows the pinned path, or nothing when none is pinned.
311
- function showPinned() {
312
- if (pinned) { show(pinned.grid, pinned.col); } else { clear(); }
313
- }
314
-
315
- pcRail.addEventListener('mouseover', function (event) {
316
- var cell = event.target.closest('.pathgrid .pcell');
317
- if (!cell) { return; }
318
- var column = columnOf(cell);
319
- show(column.grid, column.col);
320
- });
321
- pcRail.addEventListener('mouseleave', showPinned);
216
+ rail.addEventListener('mouseleave', clear);
322
217
 
323
218
  // The code spans whose characters are marked, with the text
324
219
  // each had before.
@@ -329,62 +224,12 @@
329
224
  return text.match(/^\s*/)[0].length;
330
225
  }
331
226
 
332
- // Puts back every span marked as it was, site tints and all.
227
+ // Puts back every span marked as it was.
333
228
  function unmarkChars() {
334
229
  markedSrcs.forEach(function (entry) { entry.src.innerHTML = entry.html; });
335
230
  markedSrcs = [];
336
231
  }
337
232
 
338
- // Tints each site's own characters by its status, gaps when
339
- // any of its paths was never taken, rather than its whole
340
- // lines; where sites overlap, gaps wins.
341
- function tintSites() {
342
- var perLine = {};
343
- document.querySelectorAll('.tree .site').forEach(function (site) {
344
- var row = site.querySelector('.row[data-at]');
345
- if (!row) { return; }
346
- var at = row.getAttribute('data-at').split(',').map(Number);
347
- var gaps = site.querySelector('.count.never') !== null;
348
- for (var n = at[0]; n <= at[2]; n++) {
349
- (perLine[n] = perLine[n] || []).push({
350
- from: n === at[0] ? at[1] : -1, to: n === at[2] ? at[3] : Infinity,
351
- status: gaps ? 'gaps' : 'covered'
352
- });
353
- }
354
- });
355
- Object.keys(perLine).forEach(function (n) {
356
- var src = srcOf(n);
357
- if (!src) { return; }
358
- var text = src.textContent;
359
- var marks = text.split('').map(function () { return null; });
360
- perLine[n].forEach(function (span) {
361
- // A continuation line (from -1) is tinted from its first
362
- // character after the indent.
363
- var from = span.from < 0 ? indentOf(text) : span.from;
364
- for (var i = from; i < Math.min(span.to, text.length); i++) {
365
- if (marks[i] !== 'gaps') { marks[i] = span.status; }
366
- }
367
- });
368
- src.textContent = '';
369
- var i = 0;
370
- while (i < text.length) {
371
- var j = i;
372
- while (j < text.length && marks[j] === marks[i]) { j++; }
373
- var part = text.slice(i, j);
374
- if (marks[i]) {
375
- var mark = document.createElement('mark');
376
- mark.className = 'site';
377
- mark.setAttribute('data-status', marks[i]);
378
- mark.textContent = part;
379
- src.appendChild(mark);
380
- } else {
381
- src.appendChild(document.createTextNode(part));
382
- }
383
- i = j;
384
- }
385
- });
386
- }
387
-
388
233
  // Marks the characters from line l1, column c1 to line l2,
389
234
  // column c2, each line's part in a mark of its own.
390
235
  function markChars(l1, c1, l2, c2) {
@@ -396,16 +241,67 @@
396
241
  var from = n === l1 ? c1 : indentOf(text);
397
242
  var to = n === l2 ? c2 : text.length;
398
243
  markedSrcs.push({ src: src, html: src.innerHTML });
399
- var mark = document.createElement('mark');
400
- mark.className = 'chars';
401
- mark.textContent = text.slice(from, to);
402
- src.textContent = text.slice(0, from);
403
- src.appendChild(mark);
404
- src.appendChild(document.createTextNode(text.slice(to)));
244
+ paint(src, function (i) { return i >= from && i < to ? 'chars' : null; }, function () {
245
+ var mark = document.createElement('mark');
246
+ mark.className = 'chars';
247
+ return mark;
248
+ });
405
249
  }
406
250
  reveal(document.querySelectorAll('mark.chars'));
407
251
  }
408
252
 
253
+ // The colour class of each of src's characters, the class of the
254
+ // token span it is in, null for one in none.
255
+ function tokensOf(src) {
256
+ var tokens = [];
257
+ var walker = document.createTreeWalker(src, NodeFilter.SHOW_TEXT);
258
+ for (var node = walker.nextNode(); node; node = walker.nextNode()) {
259
+ var parent = node.parentNode;
260
+ var css = parent !== src && parent.tagName === 'SPAN' ? parent.className : null;
261
+ for (var i = 0; i < node.length; i++) { tokens.push(css); }
262
+ }
263
+ return tokens;
264
+ }
265
+
266
+ // Rebuilds src's code, each run of characters keyAt gives one key
267
+ // to in the element wrap makes of that key, the others bare, every
268
+ // character still in a span of its token's colour class.
269
+ function paint(src, keyAt, wrap) {
270
+ var text = src.textContent;
271
+ var tokens = tokensOf(src);
272
+ src.textContent = '';
273
+ var i = 0;
274
+ while (i < text.length) {
275
+ var j = i;
276
+ var key = keyAt(i);
277
+ while (j < text.length && keyAt(j) === key) { j++; }
278
+ var holder = key ? wrap(key) : src;
279
+ appendTokens(holder, text, tokens, i, j);
280
+ if (key) { src.appendChild(holder); }
281
+ i = j;
282
+ }
283
+ }
284
+
285
+ // Appends text's characters i to j to holder, each run of one
286
+ // token class in a span of that class, the rest bare.
287
+ function appendTokens(holder, text, tokens, i, j) {
288
+ var k = i;
289
+ while (k < j) {
290
+ var m = k;
291
+ while (m < j && tokens[m] === tokens[k]) { m++; }
292
+ var part = text.slice(k, m);
293
+ if (tokens[k]) {
294
+ var span = document.createElement('span');
295
+ span.className = tokens[k];
296
+ span.textContent = part;
297
+ holder.appendChild(span);
298
+ } else {
299
+ holder.appendChild(document.createTextNode(part));
300
+ }
301
+ k = m;
302
+ }
303
+ }
304
+
409
305
  // The width of the code pane's left columns (gap arrows and
410
306
  // line numbers), which code scrolls under.
411
307
  var GUTTER = 108;
@@ -428,115 +324,147 @@
428
324
  if (shift !== 0) { code.scrollBy({ left: shift, behavior: 'smooth' }); }
429
325
  }
430
326
 
431
- // Hovering a tree's row marks in the code the characters of
432
- // its operand, or of its operator's whole condition.
327
+ // Hovering a row of a fold in a card marks in the code the
328
+ // characters of its operand, or of its operator's whole
329
+ // condition.
433
330
  var markedRow = null;
434
- ccRail.addEventListener('mouseover', function (event) {
435
- var row = event.target.closest('.tree .row[data-at]');
331
+ rail.addEventListener('mouseover', function (event) {
332
+ var row = event.target.closest('.fold .row[data-at]');
436
333
  if (row === markedRow) { return; }
437
334
  markedRow = row;
438
335
  if (!row) { unmarkChars(); return; }
439
336
  var at = row.getAttribute('data-at').split(',').map(Number);
440
337
  markChars(at[0], at[1], at[2], at[3]);
441
338
  });
442
- ccRail.addEventListener('mouseleave', function () {
339
+ rail.addEventListener('mouseleave', function () {
443
340
  markedRow = null;
444
341
  unmarkChars();
445
342
  });
446
343
 
447
- // A click on a cell pins its column, or unpins it when it is
448
- // the one pinned.
449
- pcRail.addEventListener('click', function (event) {
450
- var cell = event.target.closest('.pathgrid .pcell');
451
- if (!cell) { return; }
452
- var column = columnOf(cell);
453
- var same = pinned && pinned.grid === column.grid && pinned.col === column.col;
454
- pin(same ? null : column);
455
- show(column.grid, column.col);
344
+ // Hovering a statement's row in a card lights its line in the
345
+ // code, scrolling the page only when that line is out of view.
346
+ var litRow = null;
347
+ rail.addEventListener('mouseover', function (event) {
348
+ var row = event.target.closest('.statements .prow[data-line]:not(.calls)');
349
+ if (row === litRow) { return; }
350
+ litRow = row;
351
+ clear();
352
+ if (!row) { return; }
353
+ var n = Number(row.getAttribute('data-line'));
354
+ light([n, n]);
355
+ // A number's scroll margin keeps it clear of the sticky header
356
+ // and the def line stuck below it.
357
+ lineno(n).scrollIntoView({ block: 'nearest', behavior: 'smooth' });
456
358
  });
457
-
458
- document.addEventListener('keydown', function (event) {
459
- if (event.key === 'Escape' && pinned) { pin(null); clear(); }
359
+ rail.addEventListener('mouseleave', function () { litRow = null; });
360
+
361
+ // A click on a site's row in a card unfolds that site's tree in
362
+ // the row's place, folding up any other open; a click on the row
363
+ // of the one open, or on its fold's first row, which stands in
364
+ // the row's place, folds it back.
365
+ rail.addEventListener('click', function (event) {
366
+ if (event.target.closest('.fold .site > .row:first-child')) { unfold(null); return; }
367
+ var row = event.target.closest('.statements .prow.site');
368
+ var fold = row && row.nextElementSibling;
369
+ if (!fold || !fold.classList.contains('fold')) { return; }
370
+ unfold(fold.hidden ? fold : null);
460
371
  });
461
372
 
462
- // Opening one tree or grid closes the others; a click on a
463
- // site's code acts as one on its button; a gap arrow opens
464
- // the tree of the line it jumps to.
373
+ // Shows fold alone, null folding them all up, the arrow of each
374
+ // site's row pointing up while its fold shows; then places the
375
+ // open card again, as its height has changed.
376
+ function unfold(fold) {
377
+ rail.querySelectorAll('.fold').forEach(function (each) {
378
+ each.hidden = each !== fold;
379
+ var arrow = each.previousElementSibling.querySelector('.fold-arrow');
380
+ arrow.setAttribute('aria-expanded', String(each === fold));
381
+ });
382
+ place();
383
+ }
384
+
385
+ // Opening one card closes the others, and a click never closes
386
+ // the one open, so the rail always shows one. A click on a def
387
+ // line opens its method's card; one on a loose site's lines opens
388
+ // their top card; a gutter arrow or a header jump opens the card
389
+ // at the line it jumps to.
465
390
  document.addEventListener('click', function (event) {
466
- var bar = event.target.closest('.pathbar');
467
- if (bar) {
468
- var grid = document.getElementById(bar.getAttribute('aria-controls'));
469
- openOnly(grid.hidden ? grid : null);
470
- return;
471
- }
472
- var line = event.target.closest('.line');
473
- if (line && !event.target.closest('button, a')) {
474
- // A click on a method's def line acts as one on its bar.
475
- var number = line.querySelector('.lineno').getAttribute('data-line');
476
- var method = grids.filter(function (g) { return g.getAttribute('data-first') === number; })[0];
477
- if (method) { openOnly(method.hidden ? method : null); }
478
- return;
479
- }
480
- var block = event.target.closest('.block');
481
- if (block && !event.target.closest('button, a')) {
482
- block.querySelector('button[aria-controls]').click();
483
- return;
484
- }
485
- var button = event.target.closest('button.lnum');
486
- if (button) {
487
- var tree = document.getElementById(button.getAttribute('aria-controls'));
488
- openOnly(tree.hidden ? null : tree);
489
- return;
490
- }
491
- var arrow = event.target.closest('.nav a, a.jump');
492
- if (arrow) {
493
- var target = document.getElementById('tree-' + arrow.hash.slice(2));
494
- if (target) { openOnly(target); }
495
- }
391
+ var link = event.target.closest('.nav a, a.jump');
392
+ if (link) { openOnly(document.getElementById('paths-' + link.hash.slice(2))); return; }
393
+ if (event.target.closest('a')) { return; }
394
+ var loose = event.target.closest('.block');
395
+ var button = loose && loose.querySelector('button.lnum.loose');
396
+ if (button) { openOnly(document.getElementById(button.getAttribute('aria-controls'))); return; }
397
+ var row = event.target.closest('.line, .block');
398
+ var number = row && row.querySelector('.lineno.mdef');
399
+ if (number) { openOnly(grids[defs.indexOf(number)]); }
496
400
  });
497
401
 
498
402
  window.addEventListener('resize', place);
499
- window.addEventListener('scroll', function () {
500
- grids.forEach(function (grid) { if (!grid.hidden) { placeGrid(grid); } });
501
- });
502
403
 
503
- // Puts every site's jump in the header into one row, labelled
504
- // Conditions, in line order; each chip's edge shows its status.
505
- function oneJumpRow() {
506
- var lists = document.querySelectorAll('.jump-list');
507
- if (lists.length === 0) { return; }
508
- var jumps = Array.prototype.slice.call(document.querySelectorAll('a.jump'));
509
- jumps.sort(function (a, b) { return Number(a.textContent) - Number(b.textContent); });
510
- var row = lists[0];
511
- row.removeAttribute('data-jumps');
512
- row.querySelector('.jump-label').textContent = 'Conditions';
513
- jumps.forEach(function (jump) { row.appendChild(jump); });
514
- for (var i = 1; i < lists.length; i++) { lists[i].remove(); }
515
- }
516
-
517
- // The cell kind that draws each of a tree's branch glyphs.
518
- var BRANCH_CELLS = { '│': 'v', '├': 't', '└': 'l', '─': 'h' };
519
-
520
- // Redraws each tree's branch glyphs as cells a whole row tall,
521
- // whose lines run edge to edge, so the branches join from row
522
- // to row as the glyphs, shorter than a row, cannot.
523
- function drawBranches() {
524
- document.querySelectorAll('.tree .branch').forEach(function (branch) {
525
- var text = branch.textContent;
526
- branch.setAttribute('aria-hidden', 'true');
527
- branch.textContent = '';
528
- text.split('').forEach(function (ch) {
529
- var cell = document.createElement('span');
530
- cell.className = 'bx';
531
- cell.setAttribute('data-bx', BRANCH_CELLS[ch] || 's');
532
- branch.appendChild(cell);
533
- });
404
+ // The card of the innermost method whose def to end hold line n,
405
+ // or of the top card holding it, so a linked line in a body is
406
+ // never left folded away; null when none holds n.
407
+ function cardOf(n) {
408
+ var holding = grids.filter(function (grid) { return firstOf(grid) <= n && n <= lastOf(grid); });
409
+ return holding.length ? holding[holding.length - 1] : null;
410
+ }
411
+
412
+ // On a page at a linked line, eg #L8, opens the card holding it.
413
+ // Otherwise opens the first card with gaps, or, with none, the
414
+ // first card, its first line scrolled into view, so one is always
415
+ // open, every other method's body folded under its def line;
416
+ // opens nothing only on a page with no card at all.
417
+ function openFirst() {
418
+ var linked = /^#L(\d+)$/.exec(window.location.hash);
419
+ var own = linked && cardOf(Number(linked[1]));
420
+ if (own) { openOnly(own); return; }
421
+ var jump = document.querySelector('.jump-list a.jump');
422
+ var card = jump ? cardOf(Number(jump.hash.slice(2))) : grids[0];
423
+ if (!card) { openOnly(null); return; }
424
+ lineno(firstOf(card)).scrollIntoView({ block: 'center' });
425
+ openOnly(card);
426
+ }
427
+
428
+ // Keeps the header, the open card and its def line in view as the
429
+ // page scrolls, and as a pinch-zoomed view pans or zooms. Pinch
430
+ // zoom pans a lens over the page, which CSS sticky ignores, so
431
+ // the header is shifted down by the lens's pan; the card and def
432
+ // line then follow the header's drawn bottom.
433
+ function followView() {
434
+ var pan = window.visualViewport ? window.visualViewport.offsetTop : 0;
435
+ document.querySelector('.head').style.transform = pan > 0 ? 'translateY(' + pan + 'px)' : '';
436
+ place();
437
+ stickDef();
438
+ }
439
+ window.addEventListener('scroll', followView);
440
+ window.addEventListener('resize', stickDef);
441
+ if (window.visualViewport) {
442
+ window.visualViewport.addEventListener('scroll', followView);
443
+ window.visualViewport.addEventListener('resize', followView);
444
+ }
445
+
446
+ // Keeps the def line of the open method in view: once it would
447
+ // scroll up under the sticky header, it is shifted down to sit
448
+ // just below it, over the body lines passing beneath, but never
449
+ // past the method's last line. The code pane scrolls sideways, so
450
+ // CSS sticky, which would stick within the pane, cannot do this.
451
+ function stickDef() {
452
+ grids.forEach(function (grid) {
453
+ var row = rowOf(firstOf(grid));
454
+ row.style.transform = '';
455
+ row.classList.remove('def-stuck');
456
+ if (grid.hidden) { return; }
457
+ var head = document.querySelector('.head').getBoundingClientRect().bottom;
458
+ var rect = row.getBoundingClientRect();
459
+ var last = lineno(lastOf(grid)).getBoundingClientRect();
460
+ var shift = Math.min(head - rect.top, last.bottom - rect.bottom);
461
+ if (shift <= 0) { return; }
462
+ row.style.transform = 'translateY(' + shift + 'px)';
463
+ row.classList.add('def-stuck');
534
464
  });
535
465
  }
536
466
 
537
- document.body.setAttribute('data-layout', 'beside');
538
- drawBranches();
539
- oneJumpRow();
540
- tintSites();
541
- beside();
467
+ var kept = keptRail();
468
+ if (kept > 0) { setRail(kept); }
469
+ openFirst();
542
470
  })();