varar-core 0.8.0 → 0.9.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.
@@ -18,6 +18,7 @@ module Varar
18
18
  # the exception, so it never shows up in `inspect` output the way an
19
19
  # extra attribute would.
20
20
  ANCHOR_IVAR = :@varar_failure_anchor
21
+ DOC_PATH_IVAR = :@varar_failure_doc_path
21
22
 
22
23
  def failure_anchor(error, fallback)
23
24
  case error
@@ -41,6 +42,22 @@ module Varar
41
42
 
42
43
  error.instance_variable_get(ANCHOR_IVAR)
43
44
  end
45
+
46
+ # The document the anchor's offsets belong to, for a step a reference
47
+ # block spliced in from another oath (ADR 0016). Travels the same way and
48
+ # for the same reason as the anchor: the executor knows the step, and
49
+ # whoever builds the failure payload sees only the error.
50
+ def attach_doc_path(error, doc_path)
51
+ return unless error.respond_to?(:instance_variable_set)
52
+
53
+ error.instance_variable_set(DOC_PATH_IVAR, doc_path)
54
+ end
55
+
56
+ def attached_doc_path(error)
57
+ return nil unless error.respond_to?(:instance_variable_get)
58
+
59
+ error.instance_variable_get(DOC_PATH_IVAR)
60
+ end
44
61
  end
45
62
  end
46
63
  end
@@ -3,7 +3,7 @@
3
3
  module Varar
4
4
  module Core
5
5
  # FNV-1a (32-bit) change-detector over UTF-16 code units. Not a security
6
- # hash: tiny and byte-identical to the TS/Python/JVM ports so varar.lock.json
6
+ # hash: tiny and byte-identical to the TS/Python/JVM ports so run-result
7
7
  # fingerprints match everywhere. The "fnv1a:" prefix namespaces the algorithm.
8
8
  # Port of hash.ts.
9
9
  module Hash32
@@ -27,20 +27,17 @@ module Varar
27
27
  module Matcher
28
28
  module_function
29
29
 
30
- # A compiled, un-anchored pattern from the step's CucumberExpression.
31
- def unanchored_pattern(step)
32
- regexp = step.compiled.instance_variable_get(:@tree_regexp).regexp
33
- source = regexp.source
34
- source = source[1..] if source.start_with?('^')
35
- source = source[0...-1] if source.end_with?('$')
36
- Regexp.new(source, regexp.options)
37
- end
38
-
39
- # Every expression match found anywhere in +sentence+.
40
- def find_hits(sentence, registry)
30
+ # Every expression match found anywhere in +sentence+, in registration
31
+ # order. +index+ (a StepIndex) only narrows which steps are tried and
32
+ # never changes the answer: without one, every step is tried. A caller
33
+ # matching many sentences against one registry builds it once with
34
+ # StepIndex.create.
35
+ def find_hits(sentence, registry, index = nil)
41
36
  hits = []
42
- registry.steps.each do |step|
43
- pattern = unanchored_pattern(step)
37
+ positions = index ? index.candidates(registry.steps, sentence) : (0...registry.steps.length)
38
+ positions.each do |position|
39
+ step = registry.steps[position]
40
+ pattern = step.scan.pattern
44
41
  pos = 0
45
42
  while pos <= sentence.length
46
43
  m = pattern.match(sentence, pos)
@@ -11,7 +11,8 @@ module Varar
11
11
  module_function
12
12
 
13
13
  def parse(path, source, plugins = [])
14
- Structurer.structure(path, source, Scanner.scan(source, plugins))
14
+ idx = Offsets.index(source)
15
+ Structurer.structure(path, source, Scanner.scan(source, plugins, idx), idx)
15
16
  end
16
17
  end
17
18
  end
@@ -5,58 +5,110 @@ require 'varar/core/ast'
5
5
  require 'varar/core/cell_diff'
6
6
  require 'varar/core/diagnostics'
7
7
  require 'varar/core/matcher'
8
+ require 'varar/core/reference'
8
9
  require 'varar/core/sentences'
9
10
 
10
11
  module Varar
11
12
  module Core
12
13
  DocString = Data.define(:content, :content_type, :span)
13
14
 
14
- PlannedStep = Data.define(:text, :match_span, :param_spans, :step_def, :args, :formats, :data_table,
15
- :doc_string) do
16
- def initialize(text:, match_span:, param_spans:, step_def:, args:, formats: [], data_table: nil,
17
- doc_string: nil)
15
+ # param_texts: the notation each parameter matched, sliced at plan time from
16
+ # the document the step was WRITTEN in — consumers must use it rather than
17
+ # slicing the running oath's source, because a step a reference block
18
+ # spliced in (ADR 0016) has spans in a different document.
19
+ # doc_path: set only on such a spliced step — the document its spans belong to.
20
+ PlannedStep = Data.define(:text, :match_span, :param_spans, :param_texts, :step_def, :args, :formats,
21
+ :data_table, :doc_string, :doc_path) do
22
+ def initialize(text:, match_span:, param_spans:, step_def:, args:, param_texts: [], formats: [],
23
+ data_table: nil, doc_string: nil, doc_path: nil)
18
24
  super
19
25
  end
20
26
  end
21
27
 
22
28
  HeaderBinding = Data.define(:match_span, :param_spans, :step_def)
23
29
 
24
- PlannedExample = Data.define(:name, :scope_stack, :span, :steps, :header_binding, :row_checks,
30
+ # name: what a report shows (ADR 0017) — the innermost heading, `heading ›
31
+ # first sentence` when several examples share it, numbered when still
32
+ # equal. Presentation only, never an identity.
33
+ # key: the example's identity — its first paragraph's normalised text (see
34
+ # Plan.derive_example_key). Stable under heading edits and renumbering.
35
+ PlannedExample = Data.define(:name, :key, :scope_stack, :span, :steps, :header_binding, :row_checks,
25
36
  :expected_outcome, :expected_error_message) do
26
- def initialize(name:, scope_stack:, span:, steps:, header_binding: nil, row_checks: nil,
37
+ def initialize(name:, key:, scope_stack:, span:, steps:, header_binding: nil, row_checks: nil,
27
38
  expected_outcome: nil, expected_error_message: nil)
28
39
  super
29
40
  end
30
41
  end
31
42
 
32
- ExecutionPlan = Data.define(:doc, :examples, :diagnostics)
43
+ # `consumed` holds the spans of the candidate paragraphs left out because a
44
+ # reference block consumes the section they sit in (ADR 0016) — from this
45
+ # document or another. They run where they are referenced, not here, so
46
+ # drift treats them as neither live nor drifted (#117).
47
+ ExecutionPlan = Data.define(:doc, :examples, :diagnostics, :consumed)
33
48
 
34
49
  # Produce an ExecutionPlan from a Doc + Registry: match step expressions
35
50
  # against every text block, attach trailing tables/fences, detect
36
51
  # header-bound tables, and collect diagnostics. Port of plan.ts.
37
52
  module Plan
38
53
  BlockPlan = Data.define(:steps, :ambiguities)
54
+
55
+ # What one `plan` call derives once and every candidate then shares: the
56
+ # index that narrows which steps a sentence is tried against, and a
57
+ # source index per document spans are taken from — the planned one, plus
58
+ # any a reference block reaches into. Deriving either per sentence or per
59
+ # span is what made planning cost sentences × steps and spans × document
60
+ # length (varar-dev/varar#130).
61
+ Context = Data.define(:registry, :step_index, :indexes) do
62
+ def self.for(registry)
63
+ new(registry: registry, step_index: StepIndex.create(registry.steps), indexes: {}.compare_by_identity)
64
+ end
65
+
66
+ # The Offsets.index of +doc+'s source, built the first time it is asked for.
67
+ def index_of(doc)
68
+ indexes[doc] ||= Offsets.index(doc.source)
69
+ end
70
+ end
39
71
  Ambiguity = Data.define(:match_start, :match_end, :candidates)
40
72
 
41
73
  # A candidate paragraph, planned in isolation (Phase 1). Either a
42
74
  # header-bound table (standalone rows) or a step-bearing candidate the
43
75
  # grouping pass may merge into an open example.
44
76
  HeaderBoundUnit = Data.define(:rows)
45
- StepsUnit = Data.define(:matched, :preceded_by_delimiter, :name, :scope_stack, :span, :steps,
77
+ # A reference block: its whole text is a link to an oath section, whose
78
+ # steps are spliced in here (ADR 0016). Never prose, so it does not close
79
+ # the open example. Its span and scope stack are the referring
80
+ # document's: the example it opens lives here, not in the section.
81
+ ReferenceUnit = Data.define(:reference, :preceded_by_delimiter, :span, :scope_stack)
82
+ StepsUnit = Data.define(:matched, :preceded_by_delimiter, :key, :scope_stack, :span, :steps,
46
83
  :expected_outcome, :expected_error_message)
47
84
 
48
85
  # An open, merging example being built up across adjacent matching
49
86
  # candidates in Phase 2.
50
- MergedExample = Struct.new(:name, :scope_stack, :start_offset, :end_offset, :steps,
51
- :expected_outcome, :expected_error_message)
87
+ # key_from_reference: true while the key came from a spliced
88
+ # (referenced) paragraph and is waiting to be replaced by the example's
89
+ # own first matching paragraph.
90
+ MergedExample = Struct.new(:key, :scope_stack, :start_offset, :end_offset, :steps,
91
+ :expected_outcome, :expected_error_message, :key_from_reference)
52
92
 
53
93
  module_function
54
94
 
55
- def plan(doc, registry)
95
+ def plan(doc, registry, workspace)
56
96
  diagnostics = []
97
+ ctx = Context.for(registry)
98
+
99
+ # A section another oath references stops being a standalone example:
100
+ # it runs where it is referenced, not here (ADR 0016).
101
+ whole_file = Reference.section_key(doc.path, '')
102
+ consumed = lambda do |ex|
103
+ workspace.referenced.include?(whole_file) ||
104
+ ex.scope_stack.any? do |h|
105
+ workspace.referenced.include?(Reference.section_key(doc.path, Reference.slugify(h)))
106
+ end
107
+ end
57
108
 
58
109
  # Phase 1: plan each candidate paragraph independently into a "unit".
59
- units = doc.examples.map { |ex| plan_candidate(ex, doc, registry, diagnostics) }
110
+ units = doc.examples.reject { |ex| consumed.call(ex) }
111
+ .map { |ex| plan_candidate(ex, doc, ctx, diagnostics) }
60
112
 
61
113
  # Phase 2: group adjacent candidates into examples. A matching candidate
62
114
  # continues the open example when no delimiter (heading / `---`) precedes
@@ -66,7 +118,7 @@ module Varar
66
118
  examples = []
67
119
  open = nil
68
120
  flush = lambda do
69
- examples << finish_merged(open, doc.source) if open
121
+ examples << finish_merged(open, ctx.index_of(doc)) if open
70
122
  open = nil
71
123
  end
72
124
  units.each do |unit|
@@ -75,6 +127,31 @@ module Varar
75
127
  examples.concat(unit.rows)
76
128
  next
77
129
  end
130
+ if unit.is_a?(ReferenceUnit)
131
+ # Splice the referenced section's steps in at this position. Only
132
+ # the reference block itself is subject to the delimiter rule;
133
+ # everything it splices in belongs to the same sequence, so a
134
+ # section of several paragraphs stays one example.
135
+ resolve_reference(unit, doc, ctx, workspace, diagnostics, []).each_with_index do |spliced, i|
136
+ if open && (i.positive? || !unit.preceded_by_delimiter)
137
+ merge_into(open, spliced, from_reference: true)
138
+ else
139
+ flush.call
140
+ open = start_merged(spliced)
141
+ # An example that OPENS with a reference is keyed by its own
142
+ # first matching paragraph, not by the section it pulls in, and
143
+ # it sits under THIS document's headings, not the section's.
144
+ open.key_from_reference = true
145
+ open.scope_stack = unit.scope_stack
146
+ open.start_offset = unit.span.start_offset
147
+ end
148
+ # A spliced unit's span is in the referenced document; the
149
+ # example's span is in this one. It ends at the reference block
150
+ # until a later paragraph of the example's own extends it.
151
+ open.end_offset = unit.span.end_offset
152
+ end
153
+ next
154
+ end
78
155
  unless unit.matched
79
156
  # Prose paragraph — a delimiter. Drop it and end the open example.
80
157
  flush.call
@@ -89,15 +166,109 @@ module Varar
89
166
  end
90
167
  flush.call
91
168
 
92
- ExecutionPlan.new(doc: doc, examples: examples, diagnostics: diagnostics)
169
+ ExecutionPlan.new(doc: doc, examples: name_examples(examples), diagnostics: diagnostics,
170
+ consumed: doc.examples.select { |ex| consumed.call(ex) }.map(&:span))
171
+ end
172
+
173
+ # ADR 0017 §1. Name every example of one oath, in document order. A
174
+ # header-bound row keeps its cell name. Any other example is named by its
175
+ # innermost heading when it is the only example under that heading chain,
176
+ # by `heading › first sentence` when it shares the chain, and by its first
177
+ # sentence when no heading encloses it. Names still equal after that get
178
+ # ` (2)`, ` (3)` … in document order.
179
+ def name_examples(examples)
180
+ sharing = examples.reject(&:header_binding).map(&:scope_stack).tally
181
+ seen = Hash.new(0)
182
+ examples.map do |ex|
183
+ base = ex.header_binding ? ex.name : base_name(ex, sharing.fetch(ex.scope_stack, 0))
184
+ seen[base] += 1
185
+ n = seen[base]
186
+ ex.with(name: n == 1 ? base : "#{base} (#{n})")
187
+ end
188
+ end
189
+
190
+ def base_name(example, sharing_chain)
191
+ heading = example.scope_stack.last
192
+ return first_sentence(example.key) if heading.nil?
193
+
194
+ sharing_chain > 1 ? "#{heading} › #{first_sentence(example.key)}" : heading
195
+ end
196
+
197
+ # The first sentence of an example's key, split as steps are, without its
198
+ # terminator.
199
+ def first_sentence(key)
200
+ first = Sentences.split_sentences(key).first
201
+ (first ? first.text : key).sub(/[.!?]\z/, '')
202
+ end
203
+
204
+ # Resolve one reference block into the step-bearing units of the section
205
+ # it names, recursively: a referenced section may itself contain
206
+ # reference blocks, to any depth (ADR 0016 leaves depth to the author's
207
+ # judgement). `chain` carries the sections currently being resolved so a
208
+ # repeat is reported as a cycle instead of recursing forever.
209
+ def resolve_reference(unit, from_doc, ctx, workspace, diagnostics, chain)
210
+ ref = unit.reference
211
+ key = Reference.section_key(ref.path, ref.slug)
212
+ if chain.include?(key)
213
+ diagnostics << Diagnostics.reference_cycle(chain + [key], unit.span)
214
+ return []
215
+ end
216
+ # A same-file reference resolves against the document being planned,
217
+ # which is not necessarily in the workspace.
218
+ target = ref.path == from_doc.path ? from_doc : workspace.docs[ref.path]
219
+ if target.nil?
220
+ diagnostics << Diagnostics.reference_not_found(ref.text, ref.path, unit.span)
221
+ return []
222
+ end
223
+ # An anchor that names more than one heading could mean either section:
224
+ # report it rather than splicing both. A whole-file reference ('' slug)
225
+ # names no heading, so it is never ambiguous.
226
+ unless ref.slug.empty?
227
+ named = target.headings.select { |h| Reference.slugify(h.text) == ref.slug }
228
+ if named.length > 1
229
+ diagnostics << Diagnostics.ambiguous_anchor(ref.text, ref.path, ref.slug,
230
+ named.map { |h| h.span.start_line }, unit.span)
231
+ return []
232
+ end
233
+ end
234
+ out = []
235
+ Reference.section_candidates(target, ref.slug).each do |candidate|
236
+ planned = plan_candidate(candidate, target, ctx, diagnostics)
237
+ if planned.is_a?(ReferenceUnit)
238
+ out.concat(resolve_reference(planned, target, ctx, workspace, diagnostics, chain + [key]))
239
+ next
240
+ end
241
+ # A header-bound table produces one example per row, which a spliced
242
+ # step list cannot express; an `error` fence declares an outcome for
243
+ # an example, not for a reusable fragment. Both are left out.
244
+ next unless planned.is_a?(StepsUnit) && planned.matched
245
+
246
+ out << tag_with_doc(planned, target.path, from_doc.path)
247
+ end
248
+ diagnostics << Diagnostics.reference_empty(ref.text, ref.path, ref.slug, unit.span) if out.empty?
249
+ out
250
+ end
251
+
252
+ # Carry the source document's identity on every spliced step, so a failure
253
+ # in a referenced section reports spans against the file they were written
254
+ # in rather than the file being run.
255
+ def tag_with_doc(unit, doc_path, host_path)
256
+ return unit if doc_path == host_path
257
+
258
+ unit.with(steps: unit.steps.map { |step| step.with(doc_path: doc_path) })
93
259
  end
94
260
 
95
261
  def start_merged(unit)
96
- MergedExample.new(unit.name, unit.scope_stack, unit.span.start_offset, unit.span.end_offset,
97
- unit.steps.dup, unit.expected_outcome, unit.expected_error_message)
262
+ MergedExample.new(unit.key, unit.scope_stack, unit.span.start_offset, unit.span.end_offset,
263
+ unit.steps.dup, unit.expected_outcome, unit.expected_error_message, false)
98
264
  end
99
265
 
100
- def merge_into(open, unit)
266
+ def merge_into(open, unit, from_reference: false)
267
+ if open.key_from_reference && !from_reference
268
+ open.key = unit.key
269
+ open.scope_stack = unit.scope_stack
270
+ open.key_from_reference = false
271
+ end
101
272
  open.end_offset = unit.span.end_offset
102
273
  open.steps.concat(unit.steps)
103
274
  # Any error fence in a merged part marks the whole example
@@ -110,10 +281,12 @@ module Varar
110
281
  open.expected_error_message = unit.expected_error_message
111
282
  end
112
283
 
113
- def finish_merged(open, source)
114
- span = Offsets.span_from_offsets(source, open.start_offset, open.end_offset)
284
+ def finish_merged(open, idx)
285
+ span = Offsets.span_at(idx, open.start_offset, open.end_offset)
115
286
  PlannedExample.new(
116
- name: open.name,
287
+ # Named once the whole oath is planned (name_examples).
288
+ name: open.key,
289
+ key: open.key,
117
290
  scope_stack: open.scope_stack,
118
291
  span: span,
119
292
  steps: open.steps,
@@ -124,7 +297,19 @@ module Varar
124
297
 
125
298
  # Plan a single candidate paragraph (plus attached tables/fences) in
126
299
  # isolation. Emits ambiguity / error-fence diagnostics into +diagnostics+.
127
- def plan_candidate(ex, doc, registry, diagnostics)
300
+ def plan_candidate(ex, doc, ctx, diagnostics)
301
+ source_index = ctx.index_of(doc)
302
+ # A block whose whole text is a link to an oath section is a reference,
303
+ # not content: never matched against step definitions, never prose.
304
+ primary = ex.body.first
305
+ if primary.respond_to?(:text)
306
+ ref = Reference.reference_of(primary.text, doc.path)
307
+ if ref
308
+ return ReferenceUnit.new(reference: ref, preceded_by_delimiter: ex.preceded_by_delimiter,
309
+ span: ex.span, scope_stack: ex.scope_stack)
310
+ end
311
+ end
312
+
128
313
  had_ambiguous = false
129
314
  steps_by_block = {}
130
315
 
@@ -132,10 +317,10 @@ module Varar
132
317
  ex.body.each_with_index do |block, idx|
133
318
  next unless %w[paragraph list_item blockquote].include?(block.kind)
134
319
 
135
- result = plan_block(block.text, registry)
320
+ result = plan_block(block.text, ctx)
136
321
 
137
322
  result.ambiguities.each do |collision|
138
- span = lift_span(doc.source, block, collision.match_start, collision.match_end)
323
+ span = lift_span(source_index, block, collision.match_start, collision.match_end)
139
324
  cp_start = Offsets.cp_index_for_utf16(block.text, collision.match_start)
140
325
  cp_end = Offsets.cp_index_for_utf16(block.text, collision.match_end)
141
326
  diagnostics << Diagnostics.ambiguous_match(
@@ -159,8 +344,9 @@ module Varar
159
344
  steps_by_block[idx] = result.steps.map do |hit|
160
345
  PlannedStep.new(
161
346
  text: Offsets.utf16_slice(block.text, hit.match_start, hit.match_end),
162
- match_span: lift_span(doc.source, block, hit.match_start, hit.match_end),
163
- param_spans: hit.param_spans.map { |p| lift_span(doc.source, block, p.start, p.end) },
347
+ match_span: lift_span(source_index, block, hit.match_start, hit.match_end),
348
+ param_spans: hit.param_spans.map { |p| lift_span(source_index, block, p.start, p.end) },
349
+ param_texts: hit.param_spans.map { |p| Offsets.utf16_slice(block.text, p.start, p.end) },
164
350
  step_def: hit.step_def,
165
351
  args: hit.args,
166
352
  formats: hit.formats
@@ -169,7 +355,7 @@ module Varar
169
355
  end
170
356
 
171
357
  # Header-bound table detection.
172
- bound = had_ambiguous ? nil : detect_header_bound(ex, steps_by_block, doc.source)
358
+ bound = had_ambiguous ? nil : detect_header_bound(ex, steps_by_block, source_index)
173
359
  if bound
174
360
  table, binding_step, header_spans = bound
175
361
  header_binding = HeaderBinding.new(
@@ -182,14 +368,7 @@ module Varar
182
368
  table.header.cells.each_with_index do |cell_name, i|
183
369
  row_object[cell_name] = i < row.cells.length ? row.cells[i] : ''
184
370
  end
185
- row_step = PlannedStep.new(
186
- text: binding_step.text,
187
- match_span: row.span,
188
- param_spans: binding_step.param_spans,
189
- step_def: binding_step.step_def,
190
- args: binding_step.args + [row_object],
191
- formats: binding_step.formats
192
- )
371
+ row_step = binding_step.with(match_span: row.span, args: binding_step.args + [row_object])
193
372
  row_checks = table.header.cells.each_with_index.map do |cell_name, i|
194
373
  RowCheck.new(
195
374
  column: cell_name,
@@ -197,8 +376,10 @@ module Varar
197
376
  span: i < row.cell_spans.length ? row.cell_spans[i] : row.span
198
377
  )
199
378
  end
379
+ cells = row.cells.join(' / ')
200
380
  PlannedExample.new(
201
- name: row.cells.join(' / '),
381
+ name: cells,
382
+ key: cells,
202
383
  scope_stack: ex.scope_stack + [binding_step.text],
203
384
  span: row.span,
204
385
  steps: [row_step],
@@ -236,11 +417,7 @@ module Varar
236
417
  block_steps.each_with_index do |step, s_idx|
237
418
  if s_idx == block_steps.length - 1 && attach
238
419
  data_table, doc_string = attach
239
- final_steps << PlannedStep.new(
240
- text: step.text, match_span: step.match_span, param_spans: step.param_spans,
241
- step_def: step.step_def, args: step.args, formats: step.formats,
242
- data_table: data_table, doc_string: doc_string
243
- )
420
+ final_steps << step.with(data_table: data_table, doc_string: doc_string)
244
421
  else
245
422
  final_steps << step
246
423
  end
@@ -262,7 +439,7 @@ module Varar
262
439
  StepsUnit.new(
263
440
  matched: !runnable_steps.empty?,
264
441
  preceded_by_delimiter: ex.preceded_by_delimiter,
265
- name: derive_example_name(ex.body),
442
+ key: derive_example_key(ex.body),
266
443
  scope_stack: ex.scope_stack,
267
444
  span: ex.span,
268
445
  steps: runnable_steps,
@@ -271,12 +448,12 @@ module Varar
271
448
  )
272
449
  end
273
450
 
274
- def plan_block(text, registry)
451
+ def plan_block(text, ctx)
275
452
  all_steps = []
276
453
  all_ambiguities = []
277
454
 
278
455
  Sentences.split_sentences(text).each do |sentence|
279
- hits = Matcher.find_hits(sentence.text, registry)
456
+ hits = Matcher.find_hits(sentence.text, ctx.registry, ctx.step_index)
280
457
  adjusted = hits.map do |h|
281
458
  Hit.new(
282
459
  expression: h.expression,
@@ -310,7 +487,7 @@ module Varar
310
487
  m&.begin(0)
311
488
  end
312
489
 
313
- def detect_header_bound(ex, steps_by_block, source)
490
+ def detect_header_bound(ex, steps_by_block, source_index)
314
491
  body = ex.body
315
492
  (1...body.length).each do |idx|
316
493
  here = body[idx]
@@ -328,19 +505,24 @@ module Varar
328
505
 
329
506
  utf16_offsets = offsets.map { |o| Offsets.to_utf16_offset(above.text, o) }
330
507
  header_spans = header_cells.each_index.map do |i|
331
- lift_span(source, above, utf16_offsets[i], utf16_offsets[i] + Offsets.utf16_len(header_cells[i]))
508
+ lift_span(source_index, above, utf16_offsets[i], utf16_offsets[i] + Offsets.utf16_len(header_cells[i]))
332
509
  end
333
510
  return [here, steps.last, header_spans]
334
511
  end
335
512
  nil
336
513
  end
337
514
 
338
- def derive_example_name(body)
515
+ # The entire paragraph is the key — an example is often a paragraph where
516
+ # only some sentences match steps, and the narration around them is part
517
+ # of what identifies it. Whitespace runs collapse to single spaces (a key
518
+ # must be one line: varar.lock stores one per line), and a single
519
+ # trailing . ! ? is stripped.
520
+ def derive_example_key(body)
339
521
  primary = body.find { |b| %w[paragraph list_item blockquote].include?(b.kind) }
340
522
  return '' if primary.nil?
341
523
 
342
- name = primary.text.gsub(/\s+/, ' ').strip
343
- name.sub(/[.!?]$/, '')
524
+ key = primary.text.gsub(/\s+/, ' ').strip
525
+ key.sub(/[.!?]$/, '')
344
526
  end
345
527
 
346
528
  def lift_segment_offset(segment_map, text_offset)
@@ -351,12 +533,12 @@ module Varar
351
533
  best.source_offset + (text_offset - best.text_offset)
352
534
  end
353
535
 
354
- def lift_span(source, block, block_start, block_end)
536
+ def lift_span(source_index, block, block_start, block_end)
355
537
  return block.span unless %w[paragraph list_item blockquote].include?(block.kind)
356
538
 
357
539
  start_src = lift_segment_offset(block.segment_map, block_start)
358
540
  end_src = lift_segment_offset(block.segment_map, block_end)
359
- Offsets.span_from_offsets(source, start_src, end_src)
541
+ Offsets.span_at(source_index, start_src, end_src)
360
542
  end
361
543
  end
362
544
  end