You need to sign in or sign up before continuing.

path_coverage 0.3.0 → 0.4.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fa9b979dc64b90e8ab047df2074c999067a5992bf62067c1e2975921a95a635b
4
- data.tar.gz: 540e4226258cdcff171e560defc1ade28089a9901c2ed0578758cbc0e56484ba
3
+ metadata.gz: bcbf082eb9c226b16a8fa03916aa7be19184fade6b6d762514713158b563222f
4
+ data.tar.gz: edab6c4a7cbc923e21fefc7f98292c5900d06c42ae6c0e889033ccb6f6c39728
5
5
  SHA512:
6
- metadata.gz: 822d8277455c05c7ac74af3e9e97625bca3884aa1f6d54fdaf625e112d28b210a929d8ade9c4941b9635f403daf2d1a87a1232741cf36d715dbfa568e36d39fc
7
- data.tar.gz: e0ca639c839185f833e1e99bc5b12ea1f7e6d45b8281d5a43764135c37c975509463fd7a342f0a8ec7e3b9d3fe4a3eeab041c9c8bdc47e2195f6df34b467618c
6
+ metadata.gz: 46cc81a9adaf89ed954e57715670f6a2c456e720c35490d0e19290712c963ba9bc5478291145a766cd1717af4c24fda4e3da36b1858e9b4fc130fb4d23d70eec
7
+ data.tar.gz: 6ed68e326285dd500eca2a5cb6553dfb921d6827dcd90cb7899722bc94d509198723a814b5334b3676b115aede150f9160690add5ad2e696100125add13f2ab1
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Gem Version](https://img.shields.io/gem/v/path_coverage)](https://rubygems.org/gems/path_coverage)
4
4
  [![Downloads](https://img.shields.io/gem/dt/path_coverage)](https://rubygems.org/gems/path_coverage)
5
- [![License](https://img.shields.io/github/license/cyber-dojo/path_coverage)](LICENSE.md)
5
+ [![License](https://img.shields.io/github/license/path-coverage/ruby)](LICENSE.md)
6
6
 
7
7
  Path coverage for Ruby, to run alongside SimpleCov's line and branch
8
8
  coverage.
@@ -16,7 +16,7 @@ safe-navigation chains. Method paths cover every site at a method's
16
16
  top level.
17
17
  More decisions hidden inside calls come next (see Roadmap).
18
18
 
19
- Demo: https://cyber-dojo.github.io/path_coverage/ shows the HTML report
19
+ Demo: https://path-coverage.github.io/ruby/ shows the HTML report
20
20
  for the mock source tree in `demo/`. `make demo` builds and opens the
21
21
  same report locally.
22
22
 
@@ -57,6 +57,10 @@ Clicking a row's `▼` opens it:
57
57
  - the symbols in a column line up with the tree's rows, and
58
58
  are explained below.
59
59
 
60
+ Hovering over code in the open card's method opens the tree holding
61
+ that code, its row lit, or, for a `return` or `raise`, lights its row.
62
+ A long piece of code scrolls within its row.
63
+
60
64
  ### Boolean: `T` `F`
61
65
 
62
66
  Many parts of a site, such as `name.size > 2`, are true or false.
@@ -81,15 +85,19 @@ In the second column `dee` was `T`, so because of `||`'s short-circuiting, `dum`
81
85
 
82
86
  ### Taken: `✓`
83
87
 
84
- An `if` takes one of its arms.
88
+ A `?:` takes one of its arms.
89
+
90
+ ![A file's page: line 2, paid ? 'paid' : 'due', its card open to its left](docs/example-taken.png)
85
91
 
86
- ![A file's page: line 2, if paid, its card open to its left](docs/example-taken.png)
92
+ - `✓` marks the column whose path goes to that arm
87
93
 
88
- - `✓` marks the column whose path took that "arm"
94
+ The `paid` row is the `?:`'s predicate, `T` or `F`. Below it is one row
95
+ per arm, `? 'paid'` and `: 'due'`. The first column, `paid` false, never
96
+ taken, goes to `: 'due'`; the second, `paid` true, to `? 'paid'`.
89
97
 
90
- The `paid` row is the `if`'s predicate, `T` or `F`. Below it is one row
91
- per arm, named `then` and `else`. The first column, `paid` false, took `else`; the
92
- second, `paid` true, took `then`.
98
+ An `if`, `unless`, `case` or `||=` shows no arm rows: which arm a path
99
+ takes follows from its predicate, and the code in an arm is a row of
100
+ its own on the card.
93
101
 
94
102
  ### Navigation: `∅` `●`
95
103
 
@@ -210,7 +218,8 @@ As the process exits:
210
218
  - `path_coverage.json` is always written to `coverage_dir`: every site
211
219
  of every covered file, with each operand's true and false counts under
212
220
  `sites`, each site's paths under `paths`, each method's card under
213
- `cards`, the statements reached under `reached`, and the paths taken
221
+ `cards`, how each card's rows nest under `tree`, the statements
222
+ reached under `reached`, and the paths taken
214
223
  of all possible under `totals`, as [taken, possible]: `decisions`,
215
224
  `methods`, and, with groups, each group's
216
225
  own under `groups`, eg `"groups": {"code": {"decisions": [481, 567]}}`.
@@ -237,8 +246,10 @@ source/kata.rb:4:24 never reached
237
246
  ### Decisions
238
247
 
239
248
  A site is named by the `path:line:column` where it starts, the path
240
- relative to `root`. A site in a covered file the run never loaded is
241
- still reported, as `never reached`.
249
+ relative to `root`; but an `if` or `unless` at its keyword, a `?:` at
250
+ its `?`, and a block method's call at its method, so no two sites
251
+ share a name. A site in a covered file the run never loaded is still
252
+ reported, as `never reached`.
242
253
 
243
254
  A path is the outcome of each leaf one evaluation of the site reached, in
244
255
  order; a leaf that short-circuiting skipped is not on it. Each leaf is
@@ -342,26 +353,67 @@ cannot be closed is the lesser error than hiding one that can.
342
353
  ### Methods
343
354
 
344
355
  Each method has a card, named by the `path:line:column` of its `def`,
345
- with a row per top-level statement. A row that is a site counts its
346
- paths; any other row is one path, taken when the statement is reached.
347
- A method's total adds up its rows. A file's code outside any method
348
- gets a card of its own.
349
-
350
- The JSON report's `cards` hold each card's rows:
356
+ with a row for each statement that shapes its paths: each site, and
357
+ each `return` or `raise`, which ends a path. Any other statement, eg
358
+ `x = 1`, runs exactly when what holds it does, so is no row. A site's
359
+ row counts its paths; a `return` or `raise` row is one path, taken
360
+ when it is reached. A method's total adds up its rows. Its title says
361
+ how many times it was called, so a method never called is a gap,
362
+ even one with no rows. A file's code outside any method gets a card
363
+ of its own.
364
+
365
+ The JSON report's `cards` hold each card's rows, its `ways` (the ways
366
+ one call can go through it) and its `calls`:
351
367
 
352
368
  ```json
353
369
  "cards": {
354
370
  "label.rb:1:0": {
355
- "name": "label", "first": 1, "last": 7,
371
+ "name": "label", "first": 1, "last": 7, "ways": 2, "calls": 1,
356
372
  "rows": [
357
373
  { "at": "2:02", "top": "if", "paths": 2, "taken": 1, "site": "label.rb:2:2" },
358
- { "at": "3:04", "top": "value", "paths": 1, "taken": 1, "site": null },
359
- { "at": "5:04", "top": "value", "paths": 1, "taken": 0, "site": null }
374
+ { "at": "3:04", "top": "return", "paths": 1, "taken": 1, "site": null },
375
+ { "at": "5:04", "top": "return", "paths": 1, "taken": 0, "site": null }
360
376
  ]
361
377
  }
362
378
  }
363
379
  ```
364
380
 
381
+ Its `tree` says how those rows nest, by file, then card, then row.
382
+ A row inside a site names that site as its `parent`, the arm it is
383
+ `in`, and `on`, the indices of the parent's paths that run it, in the
384
+ order `paths` lists them. A row at the method's top has none of these.
385
+
386
+ ```json
387
+ "tree": {
388
+ "label.rb": {
389
+ "1:0": {
390
+ "2:2": { "parent": null, "in": null, "on": null, "leaf": null, "gives": null },
391
+ "3:4": { "parent": "2:2", "in": "then", "on": [0], "leaf": null, "gives": null },
392
+ "5:4": { "parent": "2:2", "in": "else", "on": [1], "leaf": null, "gives": null }
393
+ }
394
+ }
395
+ }
396
+ ```
397
+
398
+ A row inside one of its parent's leaves names that `leaf`, by index.
399
+ A row that is the whole of that leaf, like the `any?` here, also
400
+ `gives` the leaf's outcome on each of its own paths, `null` where not
401
+ known:
402
+
403
+ ```ruby
404
+ def led?(xs)
405
+ xs.any? { |x| x.odd? } || xs.empty?
406
+ end
407
+ ```
408
+
409
+ ```json
410
+ "2:5": { "parent": "2:2", "in": null, "on": [0, 1, 2], "leaf": 0,
411
+ "gives": [false, true, false, true, false] }
412
+ ```
413
+
414
+ Its 5 paths are 0 items, then 1 item and 2+ items, each with a last
415
+ answer true or false.
416
+
365
417
  ### Reporters
366
418
 
367
419
  - `:html` writes `index.html` to `coverage_dir`: a sortable row per
@@ -0,0 +1,186 @@
1
+ require_relative 'manifest'
2
+ require_relative 'method_statements'
3
+ require_relative 'site_paths'
4
+ require_relative 'site_tree'
5
+
6
+ module PathCoverage
7
+ # How the rows of each card nest: for each row, the site it
8
+ # runs inside, the arm of that site it is in, and the paths of
9
+ # that site on which it runs, so a reader can tell which paths
10
+ # reach a row, and count the paths through a card as a whole.
11
+ module CardTree
12
+ # Part of a site's code that a row can be inside: an arm of
13
+ # statements, or a leaf; owner the key of the site's row,
14
+ # range its bytes, arm its arm's name, nil for a leaf, on
15
+ # the indices of the site's possible paths that run it, and
16
+ # leaf a leaf's index among the site's leaves, nil for an arm.
17
+ Region = Data.define(:owner, :range, :arm, :on, :leaf)
18
+
19
+ # The place of a row inside no other row's site.
20
+ TOP = {
21
+ 'parent' => nil, 'in' => nil, 'on' => nil,
22
+ 'leaf' => nil, 'gives' => nil
23
+ }.freeze
24
+
25
+ # Example: def f(a); if a then 1 end; end gives the value
26
+ # row 1 its parent, the if, in its then arm, on its path 0.
27
+ # { def site => { "line:column" => { 'parent' =>, 'in' =>,
28
+ # 'on' =>, 'leaf' =>, 'gives' => } } } for each def in
29
+ # source, a row keyed by where it is named. parent is the
30
+ # "line:column" of the site the row runs inside, in the name
31
+ # of its arm, on the indices of that site's possible paths,
32
+ # as SitePaths.possible orders them, that run the row, leaf
33
+ # the index of that site's leaf holding it, and gives, for a
34
+ # row whose site is the whole of that leaf, the leaf's
35
+ # outcome on each of the row's own possible paths, nil where
36
+ # not known; each nil where it does not apply.
37
+ def self.of(source, path, sites = nil)
38
+ sites = Manifest.sites(source, path) if sites.nil?
39
+ cards = MethodStatements.of(source, path, sites)
40
+ cards.transform_values { |rows| nested(source, sites, rows) }
41
+ end
42
+
43
+ # { "line:column" => its place } for each of rows, a card's,
44
+ # each placed in the innermost region of another row's site
45
+ # holding all of its own site's code, or, for a row that is
46
+ # no site, where it starts: a site starting where its child
47
+ # does, eg an || whose first operand is an any?, holds that
48
+ # child, and is not held by it.
49
+ def self.nested(source, sites, rows)
50
+ starts = line_starts(source)
51
+ regions = card_regions(source, sites, rows)
52
+ rows.to_h do |row|
53
+ at = starts[row.line - 1] + row.column
54
+ site = row.site ? sites.fetch(row.site) : nil
55
+ span = site ? site.extent : at...(at + 1)
56
+ key = key_of(row)
57
+ [key, placed(key, span, regions, site)]
58
+ end
59
+ end
60
+
61
+ # The regions of the sites of rows, a card's, sites being
62
+ # { site => Condition }.
63
+ def self.card_regions(source, sites, rows)
64
+ rows.select(&:site).flat_map do |row|
65
+ regions_of(source, key_of(row), sites.fetch(row.site))
66
+ end
67
+ end
68
+
69
+ # The place of the row keyed key, its code the bytes span,
70
+ # site its Condition, nil for a row that is no site: in the
71
+ # site owning the smallest region of another row holding
72
+ # span, as place gives it; TOP for a row in no such region.
73
+ def self.placed(key, span, regions, site)
74
+ holding = holding(key, span, regions)
75
+ inner = smallest(holding)
76
+ return TOP if inner.nil?
77
+
78
+ owner = inner.owner
79
+ own = holding.group_by(&:owner).fetch(owner)
80
+ place(owner, own).merge(leaf_place(own, site))
81
+ end
82
+
83
+ # The leaf and gives of a row's place, own being the regions
84
+ # of its parent holding it, site its Condition: the index of
85
+ # the innermost leaf holding it; and, when site is the whole
86
+ # of that leaf, site's value on each of its possible paths,
87
+ # as its kind gives them, the leaf's outcome there. Neither
88
+ # for a row that is no site, a return or a raise, which gives
89
+ # its parent nothing.
90
+ def self.leaf_place(own, site)
91
+ return { 'leaf' => nil, 'gives' => nil } if site.nil?
92
+
93
+ leaf = smallest(own.reject(&:arm))
94
+ whole = leaf && site.extent == leaf.range
95
+ gives = whole ? SitePaths.values(site) : nil
96
+ { 'leaf' => leaf&.leaf, 'gives' => gives }
97
+ end
98
+
99
+ # The regions of regions holding all of the bytes span, but
100
+ # for those of the row keyed key itself.
101
+ def self.holding(key, span, regions)
102
+ held = regions.group_by do |region|
103
+ region.owner != key && region.range.cover?(span)
104
+ end
105
+ held.fetch(true, [])
106
+ end
107
+
108
+ # A row's place in the site of the row keyed owner, own being
109
+ # the regions of that site holding the row: the innermost arm
110
+ # of them, nil for none, and the paths on which every one of
111
+ # them runs, as the row runs only where all of them do.
112
+ def self.place(owner, own)
113
+ arm = smallest(own.select(&:arm))
114
+ on = own.map(&:on).reduce(:&)
115
+ { 'parent' => owner, 'in' => arm&.arm, 'on' => on }
116
+ end
117
+
118
+ # The region of regions with the fewest bytes; nil for none.
119
+ def self.smallest(regions)
120
+ regions.min_by { |region| region.range.size }
121
+ end
122
+
123
+ # The regions of condition, the site of the row keyed owner:
124
+ # its arms', and its leaves'.
125
+ def self.regions_of(source, owner, condition)
126
+ possible = SitePaths.possible(condition)
127
+ node = SiteTree.node(source, condition, possible, (0..).each)
128
+ arm_regions(owner, condition, node) +
129
+ leaf_regions(owner, condition, possible)
130
+ end
131
+
132
+ # The regions of condition's leaves, in no arm, each run on
133
+ # the paths of possible with an outcome at its index.
134
+ def self.leaf_regions(owner, condition, possible)
135
+ condition.leaves.each_with_index.map do |range, index|
136
+ skipped = possible.each_index.group_by do |i|
137
+ possible[i][index].nil?
138
+ end
139
+ on = skipped.fetch(false)
140
+ Region.new(owner:, range:, arm: nil, on:, leaf: index)
141
+ end
142
+ end
143
+
144
+ # The regions of the arms in condition, node its tree, each
145
+ # operand's node at its own index: of its own arms, and of
146
+ # those of each condition nested in it, in an operand or in
147
+ # an arm's body.
148
+ def self.arm_regions(owner, condition, node)
149
+ built = condition.operands.zip(node[:operands])
150
+ built.flat_map do |operand, operand_node|
151
+ case operand
152
+ when Condition then arm_regions(owner, operand, operand_node)
153
+ when Arm then arm_region(owner, operand, operand_node)
154
+ else []
155
+ end
156
+ end
157
+ end
158
+
159
+ # The regions of arm, node its node: its own, named by its
160
+ # kind and run on the paths that take it, none for an arm
161
+ # with no statements, which holds no row; then its body's.
162
+ def self.arm_region(owner, arm, node)
163
+ body = arm.body ? arm_regions(owner, arm.body, node[:body]) : []
164
+ return body if arm.extent.nil?
165
+
166
+ cells = node[:cells]
167
+ on = cells.each_index.group_by { cells[_1] }.fetch(true, [])
168
+ own = Region.new(owner:, range: arm.extent, arm: arm.kind.to_s,
169
+ on:, leaf: nil)
170
+ [own, *body]
171
+ end
172
+
173
+ # The "line:column" key of row, from its name.
174
+ def self.key_of(row)
175
+ row.name.split(':').last(2).join(':')
176
+ end
177
+
178
+ # The byte offset where each line of source starts, first
179
+ # line first.
180
+ def self.line_starts(source)
181
+ source.each_line.reduce([0]) do |starts, line|
182
+ starts << (starts.last + line.bytesize)
183
+ end
184
+ end
185
+ end
186
+ end
@@ -0,0 +1,140 @@
1
+ require_relative 'card_tree'
2
+ require_relative 'manifest'
3
+ require_relative 'method_statements'
4
+ require_relative 'site_paths'
5
+
6
+ module PathCoverage
7
+ # The number of ways one call can go through each card, folded
8
+ # over its tree, as CardTree places its rows. Each row gives a
9
+ # pair, [ways that go on, ways that leave the method]: a return
10
+ # or a raise leaves, and so does each path of a site taking an
11
+ # arm that returns or raises, eg return 0 if a. Rows one after
12
+ # another multiply, but only the ways that go on reach the next
13
+ # row; a site adds up its paths, each with the rows it runs on
14
+ # that path one after another; and a row that is the whole of a
15
+ # leaf counts only those of its paths that give the leaf the
16
+ # outcome the path has, so no impossible way is counted where
17
+ # gives knows it. A block's body counts once.
18
+ class CardWays
19
+ # Example: { "k.rb:1:0" => 3 } for def f(a, b), return 0 if a,
20
+ # then a ?: of b: 1 way leaves, 2 go on.
21
+ # { def site => its ways } for each def in source, its sites
22
+ # those of no settings.
23
+ def self.of(source, path)
24
+ sites = Manifest.sites(source, path)
25
+ cards = MethodStatements.of(source, path, sites)
26
+ cards.transform_values { |rows| card(source, sites, rows) }
27
+ end
28
+
29
+ # The ways through a card of rows in source, sites being
30
+ # { site => Condition }, as Manifest.sites gives them.
31
+ def self.card(source, sites, rows)
32
+ places = CardTree.nested(source, sites, rows)
33
+ new(source, rows, places, sites).total
34
+ end
35
+
36
+ # The pair of rows one after another, from their pairs: the
37
+ # ways that go on through them all, and those that leave at
38
+ # one of them, having gone on through every one before it.
39
+ def self.sequence(pairs)
40
+ pairs.reduce([1, 0]) do |(on, left), (more, leaving)|
41
+ [on * more, left + (on * leaving)]
42
+ end
43
+ end
44
+
45
+ # The fold of rows, a card's in source, placed as places has
46
+ # them, each site row's Condition among sites.
47
+ def initialize(source, rows, places, sites)
48
+ @source = source
49
+ @rows = rows.to_h { |row| [CardTree.key_of(row), row] }
50
+ @places = places
51
+ @sites = sites
52
+ @children = places.keys.group_by { places[_1]['parent'] }
53
+ end
54
+
55
+ # The ways through the card: its top rows' pairs, one after
56
+ # another, those that go on and those that leave.
57
+ def total
58
+ tops = @children.fetch(nil, []).map { |key| pair_of(key) }
59
+ CardWays.sequence(tops).sum
60
+ end
61
+
62
+ # The pair of the row keyed key: a site's adds up its paths'
63
+ # pairs; any other row is a return or a raise, as no other is
64
+ # a row, so leaves.
65
+ def pair_of(key)
66
+ row = @rows[key]
67
+ return [0, 1] unless row.site
68
+
69
+ pairs = possible(row.site).each_index.map do |path|
70
+ pair_on(key, path)
71
+ end
72
+ added(pairs)
73
+ end
74
+
75
+ # The pair of the site row keyed key on its path at index
76
+ # path: the rows it runs on that path, one after another, each
77
+ # given that path's outcomes; every way of it leaving when the
78
+ # path takes an arm that leaves.
79
+ def pair_on(key, path)
80
+ outcomes = possible(@rows[key].site)[path]
81
+ pairs = running(key, path).map do |child|
82
+ child_pair(child, outcomes)
83
+ end
84
+ pair = CardWays.sequence(pairs)
85
+ leaving(key).include?(path) ? [0, pair.sum] : pair
86
+ end
87
+
88
+ # The keys of the rows the site row keyed key runs on its path
89
+ # at index path, in the order they are written.
90
+ def running(key, path)
91
+ runs = @children.fetch(key, []).group_by do |child|
92
+ @places[child]['on'].include?(path)
93
+ end
94
+ runs.fetch(true, [])
95
+ end
96
+
97
+ # The pair of the row keyed child, its parent's path having
98
+ # outcomes: all of its own, for a row that gives nothing; else
99
+ # those of only its paths that give its leaf the outcome that
100
+ # path has, or that give what is not known.
101
+ def child_pair(child, outcomes)
102
+ place = @places[child]
103
+ gives = place['gives']
104
+ return pair_of(child) if gives.nil?
105
+
106
+ fits = [nil, outcome_of(outcomes, place)]
107
+ pairs = gives.each_index.map do |path|
108
+ fits.include?(gives[path]) ? pair_on(child, path) : [0, 0]
109
+ end
110
+ added(pairs)
111
+ end
112
+
113
+ # The outcome on a parent's path, outcomes, of the leaf place
114
+ # names.
115
+ def outcome_of(outcomes, place)
116
+ outcomes[place['leaf']]
117
+ end
118
+
119
+ # The indices of the paths of the site row keyed key that take
120
+ # an arm whose code returns or raises.
121
+ def leaving(key)
122
+ site = @sites.fetch(@rows[key].site)
123
+ regions = CardTree.regions_of(@source, key, site)
124
+ left = regions.group_by do |region|
125
+ region.arm && @source.byteslice(region.range).match?(LEAVES)
126
+ end
127
+ left.fetch(true, []).flat_map(&:on)
128
+ end
129
+
130
+ # pairs added up, each part on its own.
131
+ def added(pairs)
132
+ pairs.reduce([0, 0]) { |(a, b), (c, d)| [a + c, b + d] }
133
+ end
134
+
135
+ # The possible paths of the site named site.
136
+ def possible(site)
137
+ SitePaths.possible(@sites.fetch(site))
138
+ end
139
+ end
140
+ end
@@ -2,9 +2,9 @@ module PathCoverage
2
2
  # The operators whose predicate picks an arm.
3
3
  CHOICES = %w[?: if unless].freeze
4
4
 
5
- # The choices whose arms are statements, so whose path is
6
- # decided by their predicate alone.
7
- STATEMENT_CHOICES = %w[if unless].freeze
5
+ # An arm whose code leaves the method, by its code's start: a
6
+ # return or a raise, captured.
7
+ LEAVES = /\A(return|raise)\b/
8
8
 
9
9
  # The operator of a safe-navigation chain, eg user&.account.
10
10
  SAFE = '&.'.freeze
@@ -382,6 +382,135 @@
382
382
  place();
383
383
  }
384
384
 
385
+ // Hovering code in the open card's lines unfolds that card's fold
386
+ // holding the innermost row whose code is under the pointer, alone,
387
+ // lighting that row; code in no fold, eg x = 42, lights its
388
+ // statement's row in the card instead, every fold folded. Each
389
+ // waits a pause, so sweeping the pointer along a line does not
390
+ // flicker. Leaving the code leaves what was lit lit. A card that
391
+ // is not open is left shut, as opening it would fold other
392
+ // methods' code away from under the pointer.
393
+ var HOVER_PAUSE = 80;
394
+ var hoverTimer = null;
395
+ var hoveredRow = null;
396
+ code.addEventListener('mousemove', function (event) {
397
+ clearTimeout(hoverTimer);
398
+ var x = event.clientX;
399
+ var y = event.clientY;
400
+ hoverTimer = setTimeout(function () { hoverAt(x, y); }, HOVER_PAUSE);
401
+ });
402
+ code.addEventListener('mouseleave', function () { clearTimeout(hoverTimer); });
403
+
404
+ // Shows, in the open card, the row of the code at x, y, if any.
405
+ function hoverAt(x, y) {
406
+ var at = caretAt(x, y);
407
+ var open = grids.filter(function (grid) { return !grid.hidden; })[0];
408
+ if (!at || !open) { return; }
409
+ var row = innermostRow(open, at.line, at.column);
410
+ if (row) {
411
+ if (row !== hoveredRow) { showRow(row); }
412
+ return;
413
+ }
414
+ var statement = statementRow(open, at.line, at.column);
415
+ if (statement && statement !== hoveredRow) { showStatement(statement); }
416
+ }
417
+
418
+ // The row of grid's statement holding line and column: of those
419
+ // starting on line, the last starting at or before column, or the
420
+ // first, eg the ?: row for the x = of x = a ? 1 : 2; null for none.
421
+ function statementRow(grid, line, column) {
422
+ var rows = grid.querySelectorAll('.prow[data-line="' + line + '"]:not(.calls)');
423
+ var best = rows[0] || null;
424
+ rows.forEach(function (row) {
425
+ var label = row.querySelector('.plabel').textContent;
426
+ if (Number(label.split(':')[1]) <= column) { best = row; }
427
+ });
428
+ return best;
429
+ }
430
+
431
+ // Lights row, a statement's, alone, every fold folded.
432
+ function showStatement(row) {
433
+ if (hoveredRow) { hoveredRow.classList.remove('hovered'); }
434
+ hoveredRow = row;
435
+ unfold(null);
436
+ row.classList.add('hovered');
437
+ }
438
+
439
+ // The line and column of the code character at x, y; null when
440
+ // no code is there.
441
+ function caretAt(x, y) {
442
+ var node;
443
+ var offset;
444
+ if (document.caretRangeFromPoint) {
445
+ var range = document.caretRangeFromPoint(x, y);
446
+ if (!range) { return null; }
447
+ node = range.startContainer;
448
+ offset = range.startOffset;
449
+ } else if (document.caretPositionFromPoint) {
450
+ var caret = document.caretPositionFromPoint(x, y);
451
+ if (!caret) { return null; }
452
+ node = caret.offsetNode;
453
+ offset = caret.offset;
454
+ } else {
455
+ return null;
456
+ }
457
+ var element = node.nodeType === 1 ? node : node.parentElement;
458
+ var src = element && element.closest('.src');
459
+ if (!src) { return null; }
460
+ var before = document.createRange();
461
+ before.setStart(src, 0);
462
+ before.setEnd(node, offset);
463
+ return { line: lineOf(src), column: before.toString().length };
464
+ }
465
+
466
+ // The number of the line whose code is src, a line's own or one of
467
+ // a loose site's lines.
468
+ function lineOf(src) {
469
+ var block = src.closest('.block');
470
+ if (!block) { return Number(src.parentElement.querySelector('.lineno').id.slice(1)); }
471
+ var index = Array.prototype.indexOf.call(block.querySelectorAll('.srcs > .src'), src);
472
+ return Number(block.querySelectorAll('.lineno')[index].id.slice(1));
473
+ }
474
+
475
+ // The row, of grid's folds, whose code, its data-at, or whose
476
+ // else keyword, its data-else, holds line and column and is the
477
+ // shortest so; null for none.
478
+ function innermostRow(grid, line, column) {
479
+ var best = null;
480
+ var shortest = Infinity;
481
+ grid.querySelectorAll('.fold .row[data-at], .fold .row[data-else]').forEach(function (row) {
482
+ ['data-at', 'data-else'].forEach(function (name) {
483
+ var span = row.getAttribute(name);
484
+ if (!span) { return; }
485
+ var at = span.split(',').map(Number);
486
+ if (!holds(at, line, column)) { return; }
487
+ var size = (at[2] - at[0]) * 100000 + (at[3] - at[1]);
488
+ if (size < shortest) { best = row; shortest = size; }
489
+ });
490
+ });
491
+ return best;
492
+ }
493
+
494
+ // True when at, [line, column, end line, end column], holds the
495
+ // character at line and column.
496
+ function holds(at, line, column) {
497
+ var from = line > at[0] || (line === at[0] && column >= at[1]);
498
+ var to = line < at[2] || (line === at[2] && column < at[3]);
499
+ return from && to;
500
+ }
501
+
502
+ // Unfolds row's fold alone and lights row, its text scrolled back
503
+ // to its start, unlighting the row lit before.
504
+ function showRow(row) {
505
+ if (hoveredRow) { hoveredRow.classList.remove('hovered'); }
506
+ hoveredRow = row;
507
+ var fold = row.closest('.fold');
508
+ if (fold.hidden) { unfold(fold); }
509
+ row.classList.add('hovered');
510
+ var text = row.querySelector('.text');
511
+ if (text) { text.scrollLeft = 0; }
512
+ }
513
+
385
514
  // Opening one card closes the others, and a click never closes
386
515
  // the one open, so the rail always shows one. A click on a def
387
516
  // line opens its method's card; one on a loose site's lines opens