path_coverage 0.2.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.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +172 -121
  3. data/lib/path_coverage/calls.rb +4 -11
  4. data/lib/path_coverage/card.rb +14 -0
  5. data/lib/path_coverage/card_tree.rb +186 -0
  6. data/lib/path_coverage/card_ways.rb +140 -0
  7. data/lib/path_coverage/condition.rb +11 -4
  8. data/lib/path_coverage/configuration.rb +1 -17
  9. data/lib/path_coverage/configuration_paths.rb +4 -2
  10. data/lib/path_coverage/console_summary.rb +15 -17
  11. data/lib/path_coverage/gaps.rb +12 -9
  12. data/lib/path_coverage/html/file.html +5 -5
  13. data/lib/path_coverage/html/index.html +3 -1
  14. data/lib/path_coverage/html/settings.js +50 -0
  15. data/lib/path_coverage/html/side.js +470 -432
  16. data/lib/path_coverage/html/style.css +345 -226
  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 +88 -0
  20. data/lib/path_coverage/html_cells.rb +71 -0
  21. data/lib/path_coverage/html_file.rb +82 -68
  22. data/lib/path_coverage/html_index.rb +28 -29
  23. data/lib/path_coverage/html_index_head.rb +10 -30
  24. data/lib/path_coverage/html_labels.rb +16 -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 +45 -49
  28. data/lib/path_coverage/html_statements.rb +186 -0
  29. data/lib/path_coverage/html_syntax.rb +167 -0
  30. data/lib/path_coverage/html_template.rb +6 -0
  31. data/lib/path_coverage/html_tree.rb +90 -88
  32. data/lib/path_coverage/json_reader.rb +21 -14
  33. data/lib/path_coverage/json_writer.rb +26 -13
  34. data/lib/path_coverage/kernel.rb +32 -51
  35. data/lib/path_coverage/kinds/block.rb +2 -1
  36. data/lib/path_coverage/kinds/case.rb +7 -9
  37. data/lib/path_coverage/kinds/choice.rb +3 -7
  38. data/lib/path_coverage/kinds/contains.rb +9 -5
  39. data/lib/path_coverage/kinds/safe.rb +6 -18
  40. data/lib/path_coverage/kinds/write.rb +0 -6
  41. data/lib/path_coverage/manifest.rb +5 -3
  42. data/lib/path_coverage/manifest_blocks.rb +3 -4
  43. data/lib/path_coverage/manifest_case.rb +2 -3
  44. data/lib/path_coverage/manifest_choices.rb +41 -20
  45. data/lib/path_coverage/manifest_folding.rb +48 -11
  46. data/lib/path_coverage/merge.rb +3 -3
  47. data/lib/path_coverage/method_statements.rb +305 -0
  48. data/lib/path_coverage/recorder.rb +12 -31
  49. data/lib/path_coverage/render.rb +19 -22
  50. data/lib/path_coverage/rewriter.rb +36 -55
  51. data/lib/path_coverage/site_paths.rb +8 -0
  52. data/lib/path_coverage/site_tree.rb +9 -0
  53. data/lib/path_coverage/statement_arms.rb +194 -0
  54. data/lib/path_coverage/summary.rb +11 -15
  55. data/lib/path_coverage/totals.rb +6 -12
  56. data/lib/path_coverage/version.rb +1 -1
  57. data/lib/path_coverage/within_methods.rb +177 -0
  58. metadata +19 -12
  59. data/lib/path_coverage/html/toggle.js +0 -12
  60. data/lib/path_coverage/html_paths.rb +0 -138
  61. data/lib/path_coverage/path_gaps.rb +0 -41
  62. data/lib/path_coverage/path_manifest.rb +0 -110
@@ -1,67 +1,67 @@
1
1
  module PathCoverage
2
- # The ways around a file's page: the header's lists of links to
3
- # each span, and the arrows between the spans with gaps.
2
+ # The ways around a file's page, from its cards, as Card gives
3
+ # them: the header's row of jumps to each card with gaps, and
4
+ # the arrows in the gutter between the cards of each status.
4
5
  module HtmlNav
5
- JUMP_LISTS = { 'covered' => 'Covered', 'gaps' => 'Gaps' }.freeze
6
+ # Each status's arrows, gaps first, and how a label names it.
7
+ KINDS = { 'gaps' => 'with gaps', 'covered' => 'covered' }.freeze
6
8
 
7
- # The header's lists of links to the first line of each span:
8
- # those covered, then those with gaps.
9
- def self.jumps(lines)
10
- firsts = lines.select { |line| first_of_span?(line) }
11
- lists = JUMP_LISTS.map do |status, label|
12
- spans = firsts.select { |line| line[:status].to_s == status }
13
- jump_list(status, label, spans.map { |line| line[:number] })
14
- end
15
- %(<nav class="jumps" aria-label="Sites">#{lists.join}</nav>)
16
- end
17
-
18
- # True for the line a span starts on.
19
- def self.first_of_span?(line)
20
- line[:span]&.begin == line[:number]
9
+ # The header's row of jumps, labelled Gaps, to the first line
10
+ # of each card with gaps, in line order; marked empty when
11
+ # there are none, so the header keeps its height.
12
+ def self.jumps(cards)
13
+ firsts = firsts(cards, 'gaps')
14
+ links = firsts.map { |first| jump(first) }
15
+ empty = links.empty? ? ' data-empty=""' : ''
16
+ name = '<span class="jump-label">Gaps</span>'
17
+ list = %(<div class="jump-list"#{empty}>#{name}#{links.join})
18
+ %(<nav class="jumps" aria-label="Cards">#{list}</div></nav>)
21
19
  end
22
20
 
23
- # One labelled list of links to lines.
24
- def self.jump_list(status, label, numbers)
25
- links = numbers.map { |number| jump(status, number) }
26
- name = %(<span class="jump-label">#{label}</span>)
27
- list = %(<div class="jump-list" data-jumps="#{status}">)
28
- "#{list}#{name}#{links.join}</div>"
21
+ # One jump to a card with gaps at line first.
22
+ def self.jump(first)
23
+ attributes = %(data-status="gaps" href="#L#{first}")
24
+ %(<a class="jump" #{attributes}>#{first}</a>)
29
25
  end
30
26
 
31
- # One link to a line.
32
- def self.jump(status, number)
33
- attributes = %(data-status="#{status}" href="#L#{number}")
34
- %(<a class="jump" #{attributes}>#{number}</a>)
27
+ # The first lines of the cards of status, in line order.
28
+ def self.firsts(cards, status)
29
+ cards.select { |card| card.status == status }.map(&:first).sort
35
30
  end
36
31
 
37
- # The first line of each span whose sites have gaps, given the
38
- # listing's blocks.
39
- def self.gap_lines(blocks)
40
- firsts = blocks.map(&:first)
41
- firsts.select { |line| line[:status] == :gaps }.map do |line|
42
- line[:number]
32
+ # The gutter of the row of numbers: by each card's first line,
33
+ # an arrow to the card of its status before it, and one to the
34
+ # one after, wrapping round at either end; empty on every
35
+ # other row, so the numbers stay aligned.
36
+ def self.cell(numbers, cards)
37
+ links = KINDS.keys.flat_map do |status|
38
+ firsts = firsts(cards, status)
39
+ (firsts & numbers).flat_map do |first|
40
+ arrows(firsts, first, status)
41
+ end
43
42
  end
43
+ %(<span class="nav" data-nav="">#{links.join}</span>)
44
44
  end
45
45
 
46
- # The cell left of a row's number: at a span with gaps, links
47
- # to the previous and next such span, wrapping round at the
48
- # ends; empty on every other row, so the numbers stay aligned.
49
- def self.cell(number, gaps)
50
- index = gaps.index(number)
51
- links = index ? arrows(gaps, index) : ''
52
- %(<span class="nav" data-nav="">#{links}</span>)
46
+ # The arrows by the card of status at line first to those
47
+ # either side of it in firsts, the first lines of the cards of
48
+ # its status.
49
+ def self.arrows(firsts, first, status)
50
+ index = firsts.index(first)
51
+ following = firsts[(index + 1) % firsts.size]
52
+ [arrow('prev', firsts[index - 1], status),
53
+ arrow('next', following, status)]
53
54
  end
54
55
 
55
- # The links to the spans with gaps either side of gaps[index].
56
- def self.arrows(gaps, index)
57
- previous = gaps[index - 1]
58
- following = gaps[(index + 1) % gaps.size]
59
- [
60
- %(<a data-prev="" href="#L#{previous}"),
61
- %( aria-label="Previous gap, line #{previous}">&#9650;</a>),
62
- %(<a data-next="" href="#L#{following}"),
63
- %( aria-label="Next gap, line #{following}">&#9660;</a>)
64
- ].join
56
+ # Each arrow's way and how its label names it.
57
+ WAYS = { 'prev' => 'Previous', 'next' => 'Next' }.freeze
58
+
59
+ # One arrow, way prev or next, to the card of status at line;
60
+ # CSS draws its glyph, in its status's colour.
61
+ def self.arrow(way, line, status)
62
+ label = "#{WAYS[way]} card #{KINDS[status]}, line #{line}"
63
+ %(<a data-#{way}="" data-status="#{status}" href="#L#{line}") +
64
+ %( aria-label="#{label}"></a>)
65
65
  end
66
66
  end
67
67
  end
@@ -1,86 +1,82 @@
1
1
  require 'fileutils'
2
+ require_relative 'html_cards'
2
3
  require_relative 'html_file'
3
4
  require_relative 'html_index'
4
- require_relative 'html_paths'
5
- require_relative 'path_manifest'
5
+ require_relative 'html_syntax'
6
6
  require_relative 'site_tree'
7
7
  require_relative 'source_lines'
8
8
  require_relative 'summary'
9
+ require_relative 'within_methods'
9
10
 
10
11
  module PathCoverage
11
12
  # The :html reporter: index.html, and files/<path>.html for
12
13
  # each covered file, written to the configuration's
13
14
  # output_dir.
14
15
  module HtmlReport
15
- # Writes every page. operands and site_paths are as Result
16
- # gives them, site_paths holding every site in files; files
17
- # is { full path => {source:, sites:} } for each covered
18
- # file, as the kernel parsed it. The files dir is emptied
19
- # first, so it holds no page for a file no longer covered.
20
- def self.write(operands, files, config, paths = nil, site_paths:)
16
+ # Writes every page. recorded is [site_paths, reached]:
17
+ # site_paths, like operands, as Result gives them, holding
18
+ # every site in files; reached as Recorder#reached gives it,
19
+ # nil when not known. files is { full path => {source:,
20
+ # sites:} } for each covered file, as the kernel parsed it.
21
+ # The files dir is emptied first, so it holds no page for a
22
+ # file no longer covered.
23
+ def self.write(operands, files, config, recorded)
24
+ site_paths, reached = recorded
21
25
  dir = config.output_dir
22
26
  FileUtils.rm_rf("#{dir}/files")
23
- index = index(files, config, paths, site_paths)
27
+ cards = WithinMethods.cards(files, config, site_paths, reached)
28
+ index = index(config, site_paths, cards)
24
29
  File.write("#{dir}/index.html", index)
30
+ write_pages(operands, files, config, recorded)
31
+ end
32
+
33
+ # Writes each covered file's page, recorded as write has it,
34
+ # with its methods' cards.
35
+ def self.write_pages(operands, files, config, recorded)
25
36
  files.each do |full_path, parsed|
26
37
  path = config.relative(full_path)
27
- out = "#{dir}/files/#{path}.html"
28
- # simplecov:disable
29
- grids = paths ? path_grids(path, parsed, paths) : ''
30
- # simplecov:enable
31
- write_page(out, path, parsed, [operands, site_paths], grids)
38
+ cards = HtmlCards.methods(path, parsed, recorded)
39
+ out = "#{config.output_dir}/files/#{path}.html"
40
+ write_page(out, path, parsed, [operands, recorded[0]], cards)
32
41
  end
33
42
  end
34
43
 
35
44
  # The index page: a row per file of its totals within each
36
- # decision, and, when paths are given, as they are when
37
- # methods are measured, within each method too;
38
- # then their Total row. With groups configured, each group,
39
- # and the files in none, get a tab of their own too. Any
40
- # operators config ignored are named after them.
41
- def self.index(files, config, paths, site_paths)
42
- methods = files.map do |full_path, parsed|
43
- path = config.relative(full_path)
44
- PathManifest.methods(parsed[:source], path)
45
- end
46
- all = methods.reduce({}, :merge)
47
- rows = Summary.files(site_paths, all, paths)
45
+ # decision, and, when cards are given, as they are when
46
+ # methods are measured, within each method too, its defs'
47
+ # cards added up; then their Total row. With groups
48
+ # configured, each group, and the files in none, get a tab
49
+ # of their own too. Any operators config ignored are named
50
+ # after them.
51
+ def self.index(config, site_paths, cards)
52
+ rows = Summary.files(site_paths, cards)
48
53
  groups = config.groups
49
54
  tabs = groups.empty? ? [] : Summary.groups(rows, groups)
50
55
  ignored = config.ignored
51
56
  HtmlIndex.page(rows, Summary.total(rows), tabs, ignored:)
52
57
  end
53
58
 
54
- # simplecov:disable
55
- # SPIKE: the hidden path grids of one file's methods.
56
- def self.path_grids(path, parsed, paths)
57
- source = parsed[:source]
58
- methods = PathManifest.methods(source, path)
59
- HtmlPaths.grids(source, methods, paths)
60
- end
61
- # simplecov:enable
62
-
63
59
  # Writes one file's page to out, from its sites' operand
64
- # counts and paths.
60
+ # counts and paths, with cards, its methods', then a card for
61
+ # each span of its sites at the top level.
65
62
  def self.write_page(out, path, parsed,
66
- (operands, site_paths), grids)
63
+ (operands, site_paths), cards)
67
64
  source, sites = parsed.values_at(:source, :sites)
68
- lines = SourceLines.of(source, sites, operands)
69
- trees = trees(source, sites, lines, site_paths)
65
+ plain = SourceLines.of(source, sites, operands)
66
+ lines = coloured(source, plain)
67
+ tops = HtmlCards.tops(path, parsed, [operands, site_paths])
68
+ page = HtmlFile.page(path, lines, cards + tops)
70
69
  FileUtils.mkdir_p(File.dirname(out))
71
- File.write(out, HtmlFile.page(path, lines, trees, grids))
70
+ File.write(out, page)
72
71
  end
73
72
 
74
- # { first line of a span => [SiteTree.of, ...] } for the
75
- # sites of one file, each under the span its line is in.
76
- def self.trees(source, sites, lines, site_paths)
77
- trees = Hash.new { |all, first| all[first] = [] }
78
- sites.each do |site, condition|
79
- first = lines[SourceLines.line_of(site) - 1][:span].begin
80
- tree = SiteTree.of(source, condition, site_paths.fetch(site))
81
- trees[first] << located(source, condition, tree)
73
+ # lines, as SourceLines.of gives them for source, each with
74
+ # its coloured tokens, as HtmlSyntax.of gives them.
75
+ def self.coloured(source, lines)
76
+ tokens = HtmlSyntax.of(source)
77
+ lines.map do |line|
78
+ line.merge(tokens: tokens.fetch(line[:number], []))
82
79
  end
83
- trees
84
80
  end
85
81
 
86
82
  # simplecov:disable
@@ -0,0 +1,186 @@
1
+ require 'cgi/escape'
2
+ require_relative 'html_syntax'
3
+ require_relative 'method_statements'
4
+
5
+ module PathCoverage
6
+ # One method's card in method mode, hidden in its file's page
7
+ # until side.js moves it into the rail: a title row of the
8
+ # method's name, its complexity, its size and the paths
9
+ # taken, a row titling the columns, then a row per statement,
10
+ # as MethodStatements gives them.
11
+ module HtmlStatements
12
+ # Example: ship, an if then a value, gives its title row,
13
+ # the column titles, then an if row and a value row.
14
+ # The card at site drawn from card, as the JSON report lists
15
+ # it, as WithinMethods.card gives it: its name, its last line,
16
+ # the ways through it and its rows. tree, when given, gives a
17
+ # row's site's tree rows, folded under its row.
18
+ def self.card(site, card, &)
19
+ first, column = site.split(':').last(2)
20
+ rows = card['rows']
21
+ attrs = [
22
+ %(id="paths-#{first}" data-first="#{first}"),
23
+ %(data-col="#{column}" data-last="#{card['last']}"),
24
+ %(data-status="#{status(card)}" hidden="hidden")
25
+ ].join(' ')
26
+ head = title_row(first, card, rows) + columns_row
27
+ %(<div class="#{GRID}" #{attrs}>) +
28
+ %(#{head}#{body(rows, &)}</div>)
29
+ end
30
+
31
+ # The classes of every card.
32
+ GRID = 'pathgrid statements'.freeze
33
+
34
+ # Example: FEE = (a ? 1 : 0) + (b ? 2 : 0) on line 1, scope
35
+ # file, gives a card titled file, a ?: row each.
36
+ # The card of the sites at the top level whose lines start at
37
+ # site, drawn from card as card draws a def's, its name its
38
+ # scope, eg file or its module, as a method's card is named
39
+ # by its method, its last its lines' last; and marked
40
+ # top-level, as it is no method's: a class no other part of
41
+ # the page has, so no style of another's, eg the header's top
42
+ # line's display, shows it when it is hidden.
43
+ def self.top_card(site, card, &)
44
+ card(site, card, &)
45
+ .sub(%(class="#{GRID}"), %(class="#{GRID} top-level"))
46
+ end
47
+
48
+ # Each row's, a site's with the rows tree gives for that site,
49
+ # headed as its row is, folded under it; rows as the JSON
50
+ # report lists a card's.
51
+ def self.body(rows, &tree)
52
+ rows.map do |row|
53
+ site = row['site']
54
+ folded = tree&.call(site, row['top']) if site
55
+ row_of(row, folds: !folded.nil?) + fold(site, folded)
56
+ end.join
57
+ end
58
+
59
+ # card's status: covered when every row's taken is known and
60
+ # is all of its paths, and its method was called, if it is a
61
+ # method's; gaps otherwise, as a row not known may have one,
62
+ # and a method never called, even of no rows, ran none of its
63
+ # code.
64
+ def self.status(card)
65
+ rows = card['rows']
66
+ done = rows.all? { |row| row['taken'] == row['paths'] } &&
67
+ card['calls'] != 0
68
+ done ? 'covered' : 'gaps'
69
+ end
70
+
71
+ # The row level with the def: card's name and how many times
72
+ # it was called, *its ways, then how many of its rows' paths
73
+ # were not taken, those of rows not known among them, and how
74
+ # many were.
75
+ def self.title_row(first, card, rows)
76
+ seen = rows.map { _1['taken'] }.compact.sum
77
+ paths = rows.map { _1['paths'] }
78
+ cells = [
79
+ "*#{card['ways']}",
80
+ (MethodStatements.size(paths) - seen).to_s, seen.to_s
81
+ ]
82
+ attrs = %(class="prow calls" data-line="#{first}")
83
+ row(attrs, title_label(card), cells)
84
+ end
85
+
86
+ # The label of card's title row: its name, escaped, then how
87
+ # many times its method was called.
88
+ def self.title_label(card)
89
+ CGI.escapeHTML(card['name'].to_s) + call_count(card['calls'])
90
+ end
91
+
92
+ # Example: 3 as <span class="call-count">&times;3</span>.
93
+ # How many times a method was called, after its name, a gap,
94
+ # never, for 0; none for a top card, or when not known.
95
+ def self.call_count(calls)
96
+ return '' if calls.nil?
97
+
98
+ css = calls.zero? ? 'call-count never' : 'call-count'
99
+ %(<span class="#{css}">&times;#{calls}</span>)
100
+ end
101
+
102
+ # The row naming the not taken and taken columns.
103
+ def self.columns_row
104
+ cells = ['', 'not taken', 'taken']
105
+ row('class="prow columns"', 'line:col', cells)
106
+ end
107
+
108
+ # One statement's row, as the JSON report lists it: its
109
+ # line:column, its at, its top, how many of its paths were
110
+ # not taken, and how many were, ? for both when not known. A
111
+ # site's row is marked as one, so a click on it can open its
112
+ # tree; when it folds a tree, its arrow, left of its
113
+ # line:column, shows whether the fold is open.
114
+ def self.row_of(row, folds: false)
115
+ count = row['taken']
116
+ counts = count ? [row['paths'] - count, count] : %w[? ?]
117
+ cells = [top(row['top']), *counts.map(&:to_s)]
118
+ css = row['site'] ? 'prow site' : 'prow'
119
+ at = row['at']
120
+ attrs = %(class="#{css}" data-line="#{Integer(at[/\d+/])}")
121
+ lead = folds ? arrow(at) : ''
122
+ row(attrs, at, cells, lead)
123
+ end
124
+
125
+ # Example: if, a site's top, as <span class="k">if</span>.
126
+ # A row's top, text, coloured as the code pane colours the
127
+ # code it quotes, eg if and return as keywords, as
128
+ # HtmlSyntax.label colours it.
129
+ def self.top(text)
130
+ HtmlSyntax.label(text)
131
+ end
132
+
133
+ # The arrow left of the line:column at of a row, its glyph,
134
+ # as style.css draws it, pointing down while its fold is shut
135
+ # and up while it is open.
136
+ def self.arrow(at)
137
+ [
138
+ '<button type="button" class="fold-arrow"',
139
+ ' aria-expanded="false"',
140
+ %( aria-label="Paths of #{at}"></button>)
141
+ ].join
142
+ end
143
+
144
+ # Example: 19:04, a statement on line 19 at column 4.
145
+ # statement's line:column, the column two digits so they
146
+ # line up.
147
+ def self.label(statement)
148
+ format('%<line>d:%<column>02d',
149
+ line: statement.line, column: statement.column)
150
+ end
151
+
152
+ # The fold under the row of site, hidden until the row is
153
+ # clicked: tree, site's rows as its tree draws them. Empty
154
+ # for a row with no tree.
155
+ def self.fold(site, tree)
156
+ return '' unless tree
157
+
158
+ [
159
+ %(<div class="fold" data-site="#{site}"),
160
+ ' hidden="hidden"><div class="site" data-site="">',
161
+ tree, '</div></div>'
162
+ ].join
163
+ end
164
+
165
+ # The class each of a row's cells adds after pcell: none for
166
+ # its top, then those colouring not taken and taken.
167
+ CELLS = ['', ' not-taken', ' was-taken'].freeze
168
+
169
+ # The class marking a not taken cell of 0, no gap, coloured
170
+ # blue as taken is; none for any other cell, as a count of
171
+ # paths not taken, or one not known, ?, is a gap.
172
+ def self.none(cell, css)
173
+ css == ' not-taken' && cell == '0' ? ' none' : ''
174
+ end
175
+
176
+ # A row div with attrs, lead, then its label, then a span
177
+ # per cell.
178
+ def self.row(attrs, label, cells, lead = '')
179
+ spans = cells.zip(CELLS).map do |cell, css|
180
+ %(<span class="pcell#{css}#{none(cell, css)}">#{cell}</span>)
181
+ end
182
+ %(<div #{attrs}>#{lead}<span class="plabel">#{label}</span>) +
183
+ %(#{spans.join}</div>)
184
+ end
185
+ end
186
+ end
@@ -0,0 +1,167 @@
1
+ require 'cgi/escape'
2
+ require 'prism'
3
+
4
+ module PathCoverage
5
+ # The colours of a file's code on its page: each token Prism
6
+ # lexes, as Ruby itself does, gets the class of its kind, eg k
7
+ # for a keyword, so the page's CSS colours it.
8
+ module HtmlSyntax
9
+ # The class of each coloured token type that is not a keyword
10
+ # or a number: c comment, s string, t constant, v instance
11
+ # variable, o an operator that decides a path.
12
+ CLASSES = {
13
+ COMMENT: 'c', CONSTANT: 't', INSTANCE_VARIABLE: 'v',
14
+ **%i[
15
+ AMPERSAND_AMPERSAND PIPE_PIPE AMPERSAND_DOT QUESTION_MARK
16
+ COLON AMPERSAND_AMPERSAND_EQUAL PIPE_PIPE_EQUAL
17
+ ].to_h { |type| [type, 'o'] },
18
+ **%i[
19
+ STRING_BEGIN STRING_CONTENT STRING_END SYMBOL_BEGIN LABEL
20
+ PERCENT_LOWER_W WORDS_SEP REGEXP_BEGIN REGEXP_END
21
+ HEREDOC_START HEREDOC_END
22
+ ].to_h { |type| [type, 's'] }
23
+ }.freeze
24
+
25
+ # { line number => [[from, to, class], ...] } for the lines of
26
+ # source that have coloured tokens, columns counting
27
+ # characters, a token over many lines having a range on each,
28
+ # touching ranges of one class joined.
29
+ def self.of(source)
30
+ layout = Layout.new(source)
31
+ pairs = classed(source).flat_map do |from, to, css|
32
+ layout.ranges(from, to).map do |number, first, last|
33
+ [number, [first, last, css]]
34
+ end
35
+ end
36
+ pairs.group_by(&:first).transform_values do |line|
37
+ joined(line.map(&:last).sort)
38
+ end
39
+ end
40
+
41
+ # A source's lines, by the byte offset each starts at.
42
+ class Layout
43
+ # The lines of source, line 1 starting at byte 0.
44
+ def initialize(source)
45
+ @source = source
46
+ @starts = [0]
47
+ source.each_line do |line|
48
+ @starts << (@starts.last + line.bytesize)
49
+ end
50
+ end
51
+
52
+ # [[line number, first column, last column], ...] for each
53
+ # line the bytes from...to reach, columns counting characters
54
+ # and kept within the line: from the line holding from to the
55
+ # last starting before to. The last start is the source's
56
+ # size, so both searches always find a line.
57
+ def ranges(from, to)
58
+ first = @starts.bsearch_index { |start| start > from } - 1
59
+ last = @starts.bsearch_index { |start| start >= to }
60
+ reached = (first...last).map { |n| columns(n, from, to) }
61
+ reached.compact
62
+ end
63
+
64
+ # [line number, first column, last column] of the bytes
65
+ # from...to on the index-th line; nil when none are on it.
66
+ def columns(index, from, to)
67
+ start = @starts[index]
68
+ size = @starts[index + 1] - start
69
+ text = @source.byteslice(start, size).chomp
70
+ first = chars(text, from - start)
71
+ last = chars(text, to - start)
72
+ first < last ? [index + 1, first, last] : nil
73
+ end
74
+
75
+ # How many characters of text its first bytes bytes hold.
76
+ def chars(text, bytes)
77
+ text.byteslice(0, bytes.clamp(0, text.bytesize)).length
78
+ end
79
+ end
80
+
81
+ # [[from, to, class], ...], the byte range and class of each
82
+ # coloured token of source; a symbol's name is coloured as
83
+ # its colon is.
84
+ def self.classed(source)
85
+ previous = nil
86
+ Prism.lex(source).value.filter_map do |token, _|
87
+ css = class_of(token.type, previous)
88
+ previous = token.type
89
+ location = token.location
90
+ css && [location.start_offset, location.end_offset, css]
91
+ end
92
+ end
93
+
94
+ # The class of a token of type, after one of type previous;
95
+ # nil for a token left uncoloured.
96
+ def self.class_of(type, previous)
97
+ return 's' if previous == :SYMBOL_BEGIN
98
+
99
+ name = type.to_s
100
+ return 'k' if name.start_with?('KEYWORD_')
101
+ return 'n' if name.start_with?('INTEGER', 'FLOAT')
102
+
103
+ CLASSES[type]
104
+ end
105
+
106
+ # ranges, in order, each touching one of the same class joined
107
+ # to it.
108
+ def self.joined(ranges)
109
+ ranges.each_with_object([]) do |(from, to, css), out|
110
+ last = out.last
111
+ if last && last[1] == from && last[2] == css
112
+ last[1] = to
113
+ else
114
+ out << [from, to, css]
115
+ end
116
+ end
117
+ end
118
+
119
+ # The names of the operators that decide a path, as a site
120
+ # tree's labels write them. Alone, some lex as other tokens,
121
+ # eg ?: as a character and || as two pipes, so each is
122
+ # coloured as an operator by name.
123
+ DECIDING = %w[&& || &. ?: &&= ||=].freeze
124
+
125
+ # text, a fragment of code on one line, eg a site tree's
126
+ # operator or operand, escaped and coloured as the code it is
127
+ # part of; one of DECIDING as the operator it names.
128
+ def self.fragment(text)
129
+ if DECIDING.include?(text)
130
+ return %(<span class="o">#{CGI.escapeHTML(text)}</span>)
131
+ end
132
+
133
+ html(text, of(text).fetch(1, []))
134
+ end
135
+
136
+ # Example: if/else as <span class="k">if</span>/<span
137
+ # class="k">else</span>.
138
+ # A row's or a fold's label, coloured as fragment colours it;
139
+ # but a label naming a choice and its else, eg if/else, each of
140
+ # its words, as code would read /else as a regexp's start.
141
+ def self.label(text)
142
+ return fragment(text) unless text.match?(%r{\A\w+/else\z})
143
+
144
+ text.split('/').map { fragment(_1) }.join('/')
145
+ end
146
+
147
+ # A ternary's arm's label, eg ? total > 100, as HTML: its ? or
148
+ # : coloured as an operator, the code after it as code.
149
+ def self.ternary_arm(label)
150
+ mark, code = label.split(' ', 2)
151
+ %(<span class="o">#{mark}</span> #{fragment(code.to_s)})
152
+ end
153
+
154
+ # text, escaped, each of tokens' ranges, [[from, to, class],
155
+ # ...], in a span of its class.
156
+ def self.html(text, tokens)
157
+ at = 0
158
+ parts = tokens.map do |from, to, css|
159
+ plain = CGI.escapeHTML(text[at...from])
160
+ at = to
161
+ token = CGI.escapeHTML(text[from...to])
162
+ %(#{plain}<span class="#{css}">#{token}</span>)
163
+ end
164
+ parts.join + CGI.escapeHTML(text[at..])
165
+ end
166
+ end
167
+ end
@@ -10,6 +10,12 @@ module PathCoverage
10
10
  File.read(File.join(DIR, name))
11
11
  end
12
12
 
13
+ # The page template named name, its {{style}} the report's
14
+ # style sheet, and each other {{name}} values[name].
15
+ def self.page(name, values)
16
+ fill(asset(name), values.merge('style' => asset('style.css')))
17
+ end
18
+
13
19
  # template with each {{name}} replaced by values[name].
14
20
  def self.fill(template, values)
15
21
  template.gsub(/\{\{(\w+)\}\}/) do