varar-core 0.8.1 → 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.
@@ -27,21 +27,47 @@ module Varar
27
27
 
28
28
  HeaderBinding = Data.define(:match_span, :param_spans, :step_def)
29
29
 
30
- 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,
31
36
  :expected_outcome, :expected_error_message) do
32
- 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,
33
38
  expected_outcome: nil, expected_error_message: nil)
34
39
  super
35
40
  end
36
41
  end
37
42
 
38
- 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)
39
48
 
40
49
  # Produce an ExecutionPlan from a Doc + Registry: match step expressions
41
50
  # against every text block, attach trailing tables/fences, detect
42
51
  # header-bound tables, and collect diagnostics. Port of plan.ts.
43
52
  module Plan
44
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
45
71
  Ambiguity = Data.define(:match_start, :match_end, :candidates)
46
72
 
47
73
  # A candidate paragraph, planned in isolation (Phase 1). Either a
@@ -53,21 +79,22 @@ module Varar
53
79
  # the open example. Its span and scope stack are the referring
54
80
  # document's: the example it opens lives here, not in the section.
55
81
  ReferenceUnit = Data.define(:reference, :preceded_by_delimiter, :span, :scope_stack)
56
- StepsUnit = Data.define(:matched, :preceded_by_delimiter, :name, :scope_stack, :span, :steps,
82
+ StepsUnit = Data.define(:matched, :preceded_by_delimiter, :key, :scope_stack, :span, :steps,
57
83
  :expected_outcome, :expected_error_message)
58
84
 
59
85
  # An open, merging example being built up across adjacent matching
60
86
  # candidates in Phase 2.
61
- # name_from_reference: true while the name came from a spliced
87
+ # key_from_reference: true while the key came from a spliced
62
88
  # (referenced) paragraph and is waiting to be replaced by the example's
63
89
  # own first matching paragraph.
64
- MergedExample = Struct.new(:name, :scope_stack, :start_offset, :end_offset, :steps,
65
- :expected_outcome, :expected_error_message, :name_from_reference)
90
+ MergedExample = Struct.new(:key, :scope_stack, :start_offset, :end_offset, :steps,
91
+ :expected_outcome, :expected_error_message, :key_from_reference)
66
92
 
67
93
  module_function
68
94
 
69
95
  def plan(doc, registry, workspace)
70
96
  diagnostics = []
97
+ ctx = Context.for(registry)
71
98
 
72
99
  # A section another oath references stops being a standalone example:
73
100
  # it runs where it is referenced, not here (ADR 0016).
@@ -81,7 +108,7 @@ module Varar
81
108
 
82
109
  # Phase 1: plan each candidate paragraph independently into a "unit".
83
110
  units = doc.examples.reject { |ex| consumed.call(ex) }
84
- .map { |ex| plan_candidate(ex, doc, registry, diagnostics) }
111
+ .map { |ex| plan_candidate(ex, doc, ctx, diagnostics) }
85
112
 
86
113
  # Phase 2: group adjacent candidates into examples. A matching candidate
87
114
  # continues the open example when no delimiter (heading / `---`) precedes
@@ -91,7 +118,7 @@ module Varar
91
118
  examples = []
92
119
  open = nil
93
120
  flush = lambda do
94
- examples << finish_merged(open, doc.source) if open
121
+ examples << finish_merged(open, ctx.index_of(doc)) if open
95
122
  open = nil
96
123
  end
97
124
  units.each do |unit|
@@ -105,16 +132,16 @@ module Varar
105
132
  # the reference block itself is subject to the delimiter rule;
106
133
  # everything it splices in belongs to the same sequence, so a
107
134
  # section of several paragraphs stays one example.
108
- resolve_reference(unit, doc, registry, workspace, diagnostics, []).each_with_index do |spliced, i|
135
+ resolve_reference(unit, doc, ctx, workspace, diagnostics, []).each_with_index do |spliced, i|
109
136
  if open && (i.positive? || !unit.preceded_by_delimiter)
110
137
  merge_into(open, spliced, from_reference: true)
111
138
  else
112
139
  flush.call
113
140
  open = start_merged(spliced)
114
- # An example that OPENS with a reference is named by its own
141
+ # An example that OPENS with a reference is keyed by its own
115
142
  # first matching paragraph, not by the section it pulls in, and
116
143
  # it sits under THIS document's headings, not the section's.
117
- open.name_from_reference = true
144
+ open.key_from_reference = true
118
145
  open.scope_stack = unit.scope_stack
119
146
  open.start_offset = unit.span.start_offset
120
147
  end
@@ -139,7 +166,39 @@ module Varar
139
166
  end
140
167
  flush.call
141
168
 
142
- 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/, '')
143
202
  end
144
203
 
145
204
  # Resolve one reference block into the step-bearing units of the section
@@ -147,7 +206,7 @@ module Varar
147
206
  # reference blocks, to any depth (ADR 0016 leaves depth to the author's
148
207
  # judgement). `chain` carries the sections currently being resolved so a
149
208
  # repeat is reported as a cycle instead of recursing forever.
150
- def resolve_reference(unit, from_doc, registry, workspace, diagnostics, chain)
209
+ def resolve_reference(unit, from_doc, ctx, workspace, diagnostics, chain)
151
210
  ref = unit.reference
152
211
  key = Reference.section_key(ref.path, ref.slug)
153
212
  if chain.include?(key)
@@ -174,9 +233,9 @@ module Varar
174
233
  end
175
234
  out = []
176
235
  Reference.section_candidates(target, ref.slug).each do |candidate|
177
- planned = plan_candidate(candidate, target, registry, diagnostics)
236
+ planned = plan_candidate(candidate, target, ctx, diagnostics)
178
237
  if planned.is_a?(ReferenceUnit)
179
- out.concat(resolve_reference(planned, target, registry, workspace, diagnostics, chain + [key]))
238
+ out.concat(resolve_reference(planned, target, ctx, workspace, diagnostics, chain + [key]))
180
239
  next
181
240
  end
182
241
  # A header-bound table produces one example per row, which a spliced
@@ -200,15 +259,15 @@ module Varar
200
259
  end
201
260
 
202
261
  def start_merged(unit)
203
- MergedExample.new(unit.name, unit.scope_stack, unit.span.start_offset, unit.span.end_offset,
262
+ MergedExample.new(unit.key, unit.scope_stack, unit.span.start_offset, unit.span.end_offset,
204
263
  unit.steps.dup, unit.expected_outcome, unit.expected_error_message, false)
205
264
  end
206
265
 
207
266
  def merge_into(open, unit, from_reference: false)
208
- if open.name_from_reference && !from_reference
209
- open.name = unit.name
267
+ if open.key_from_reference && !from_reference
268
+ open.key = unit.key
210
269
  open.scope_stack = unit.scope_stack
211
- open.name_from_reference = false
270
+ open.key_from_reference = false
212
271
  end
213
272
  open.end_offset = unit.span.end_offset
214
273
  open.steps.concat(unit.steps)
@@ -222,10 +281,12 @@ module Varar
222
281
  open.expected_error_message = unit.expected_error_message
223
282
  end
224
283
 
225
- def finish_merged(open, source)
226
- 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)
227
286
  PlannedExample.new(
228
- name: open.name,
287
+ # Named once the whole oath is planned (name_examples).
288
+ name: open.key,
289
+ key: open.key,
229
290
  scope_stack: open.scope_stack,
230
291
  span: span,
231
292
  steps: open.steps,
@@ -236,7 +297,8 @@ module Varar
236
297
 
237
298
  # Plan a single candidate paragraph (plus attached tables/fences) in
238
299
  # isolation. Emits ambiguity / error-fence diagnostics into +diagnostics+.
239
- def plan_candidate(ex, doc, registry, diagnostics)
300
+ def plan_candidate(ex, doc, ctx, diagnostics)
301
+ source_index = ctx.index_of(doc)
240
302
  # A block whose whole text is a link to an oath section is a reference,
241
303
  # not content: never matched against step definitions, never prose.
242
304
  primary = ex.body.first
@@ -255,10 +317,10 @@ module Varar
255
317
  ex.body.each_with_index do |block, idx|
256
318
  next unless %w[paragraph list_item blockquote].include?(block.kind)
257
319
 
258
- result = plan_block(block.text, registry)
320
+ result = plan_block(block.text, ctx)
259
321
 
260
322
  result.ambiguities.each do |collision|
261
- 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)
262
324
  cp_start = Offsets.cp_index_for_utf16(block.text, collision.match_start)
263
325
  cp_end = Offsets.cp_index_for_utf16(block.text, collision.match_end)
264
326
  diagnostics << Diagnostics.ambiguous_match(
@@ -282,8 +344,8 @@ module Varar
282
344
  steps_by_block[idx] = result.steps.map do |hit|
283
345
  PlannedStep.new(
284
346
  text: Offsets.utf16_slice(block.text, hit.match_start, hit.match_end),
285
- match_span: lift_span(doc.source, block, hit.match_start, hit.match_end),
286
- 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) },
287
349
  param_texts: hit.param_spans.map { |p| Offsets.utf16_slice(block.text, p.start, p.end) },
288
350
  step_def: hit.step_def,
289
351
  args: hit.args,
@@ -293,7 +355,7 @@ module Varar
293
355
  end
294
356
 
295
357
  # Header-bound table detection.
296
- 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)
297
359
  if bound
298
360
  table, binding_step, header_spans = bound
299
361
  header_binding = HeaderBinding.new(
@@ -314,8 +376,10 @@ module Varar
314
376
  span: i < row.cell_spans.length ? row.cell_spans[i] : row.span
315
377
  )
316
378
  end
379
+ cells = row.cells.join(' / ')
317
380
  PlannedExample.new(
318
- name: row.cells.join(' / '),
381
+ name: cells,
382
+ key: cells,
319
383
  scope_stack: ex.scope_stack + [binding_step.text],
320
384
  span: row.span,
321
385
  steps: [row_step],
@@ -375,7 +439,7 @@ module Varar
375
439
  StepsUnit.new(
376
440
  matched: !runnable_steps.empty?,
377
441
  preceded_by_delimiter: ex.preceded_by_delimiter,
378
- name: derive_example_name(ex.body),
442
+ key: derive_example_key(ex.body),
379
443
  scope_stack: ex.scope_stack,
380
444
  span: ex.span,
381
445
  steps: runnable_steps,
@@ -384,12 +448,12 @@ module Varar
384
448
  )
385
449
  end
386
450
 
387
- def plan_block(text, registry)
451
+ def plan_block(text, ctx)
388
452
  all_steps = []
389
453
  all_ambiguities = []
390
454
 
391
455
  Sentences.split_sentences(text).each do |sentence|
392
- hits = Matcher.find_hits(sentence.text, registry)
456
+ hits = Matcher.find_hits(sentence.text, ctx.registry, ctx.step_index)
393
457
  adjusted = hits.map do |h|
394
458
  Hit.new(
395
459
  expression: h.expression,
@@ -423,7 +487,7 @@ module Varar
423
487
  m&.begin(0)
424
488
  end
425
489
 
426
- def detect_header_bound(ex, steps_by_block, source)
490
+ def detect_header_bound(ex, steps_by_block, source_index)
427
491
  body = ex.body
428
492
  (1...body.length).each do |idx|
429
493
  here = body[idx]
@@ -441,19 +505,24 @@ module Varar
441
505
 
442
506
  utf16_offsets = offsets.map { |o| Offsets.to_utf16_offset(above.text, o) }
443
507
  header_spans = header_cells.each_index.map do |i|
444
- 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]))
445
509
  end
446
510
  return [here, steps.last, header_spans]
447
511
  end
448
512
  nil
449
513
  end
450
514
 
451
- 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)
452
521
  primary = body.find { |b| %w[paragraph list_item blockquote].include?(b.kind) }
453
522
  return '' if primary.nil?
454
523
 
455
- name = primary.text.gsub(/\s+/, ' ').strip
456
- name.sub(/[.!?]$/, '')
524
+ key = primary.text.gsub(/\s+/, ' ').strip
525
+ key.sub(/[.!?]$/, '')
457
526
  end
458
527
 
459
528
  def lift_segment_offset(segment_map, text_offset)
@@ -464,12 +533,12 @@ module Varar
464
533
  best.source_offset + (text_offset - best.text_offset)
465
534
  end
466
535
 
467
- def lift_span(source, block, block_start, block_end)
536
+ def lift_span(source_index, block, block_start, block_end)
468
537
  return block.span unless %w[paragraph list_item blockquote].include?(block.kind)
469
538
 
470
539
  start_src = lift_segment_offset(block.segment_map, block_start)
471
540
  end_src = lift_segment_offset(block.segment_map, block_end)
472
- Offsets.span_from_offsets(source, start_src, end_src)
541
+ Offsets.span_at(source_index, start_src, end_src)
473
542
  end
474
543
  end
475
544
  end
@@ -3,13 +3,16 @@
3
3
  require 'cucumber/cucumber_expressions/cucumber_expression'
4
4
  require 'cucumber/cucumber_expressions/parameter_type'
5
5
  require 'cucumber/cucumber_expressions/parameter_type_registry'
6
+ require 'varar/core/step_index'
6
7
 
7
8
  module Varar
8
9
  module Core
9
- # One registered step definition. `compiled` is the CucumberExpression.
10
+ # One registered step definition. `compiled` is the CucumberExpression;
11
+ # `scan` (a StepScan) is derived from it when the step is registered:
12
+ # what the matcher scans sentences with.
10
13
  StepRegistration = Data.define(
11
14
  :expression, :expression_source_file, :expression_source_line,
12
- :handler, :compiled, :kind
15
+ :handler, :compiled, :kind, :scan
13
16
  )
14
17
 
15
18
  # A registry of step definitions plus the shared cucumber ParameterTypeRegistry
@@ -74,7 +77,8 @@ module Varar
74
77
  expression_source_line: expression_source_line,
75
78
  handler: handler,
76
79
  compiled: compiled,
77
- kind: kind
80
+ kind: kind,
81
+ scan: StepIndex.step_scan(expression, compiled)
78
82
  )
79
83
  registry.with(steps: registry.steps + [reg])
80
84
  end
@@ -26,8 +26,9 @@ module Varar
26
26
 
27
27
  module_function
28
28
 
29
- # Scan +source+ into an immutable Array of Block nodes.
30
- def scan(source, plugins = [])
29
+ # Scan +source+ into an immutable Array of Block nodes. +idx+ is the
30
+ # source's Offsets.index, built here unless the caller already has one.
31
+ def scan(source, plugins = [], idx = Offsets.index(source))
31
32
  blocks = []
32
33
  lines = split_lines(source)
33
34
 
@@ -39,56 +40,56 @@ module Varar
39
40
  next
40
41
  end
41
42
 
42
- matched = run_plugins(source, lines, i, plugins)
43
+ matched = run_plugins(idx, lines, i, plugins)
43
44
  if matched
44
45
  blocks << matched[0]
45
46
  i = matched[1]
46
47
  next
47
48
  end
48
49
 
49
- fence_result = try_fence(source, lines, i)
50
+ fence_result = try_fence(idx, lines, i)
50
51
  if fence_result
51
52
  blocks << fence_result[0]
52
53
  i = fence_result[1]
53
54
  next
54
55
  end
55
56
 
56
- table_result = try_table(source, lines, i)
57
+ table_result = try_table(idx, lines, i)
57
58
  if table_result
58
59
  blocks << table_result[0]
59
60
  i = table_result[1]
60
61
  next
61
62
  end
62
63
 
63
- thematic = try_thematic(source, line)
64
+ thematic = try_thematic(idx, line)
64
65
  if thematic
65
66
  blocks << thematic
66
67
  i += 1
67
68
  next
68
69
  end
69
70
 
70
- bq_result = try_blockquote(source, lines, i)
71
+ bq_result = try_blockquote(idx, lines, i)
71
72
  if bq_result
72
73
  blocks << bq_result[0]
73
74
  i = bq_result[1]
74
75
  next
75
76
  end
76
77
 
77
- heading = try_heading(source, line)
78
+ heading = try_heading(idx, line)
78
79
  if heading
79
80
  blocks << heading
80
81
  i += 1
81
82
  next
82
83
  end
83
84
 
84
- list_item = try_list_item(source, line)
85
+ list_item = try_list_item(idx, line)
85
86
  if list_item
86
87
  blocks << list_item
87
88
  i += 1
88
89
  next
89
90
  end
90
91
 
91
- paragraph, next_i = consume_paragraph(source, lines, i, plugins)
92
+ paragraph, next_i = consume_paragraph(idx, lines, i, plugins)
92
93
  blocks << paragraph
93
94
  i = next_i
94
95
  end
@@ -96,9 +97,9 @@ module Varar
96
97
  blocks
97
98
  end
98
99
 
99
- def run_plugins(source, lines, start_idx, plugins)
100
+ def run_plugins(idx, lines, start_idx, plugins)
100
101
  plugins.each do |p|
101
- r = p.try_scan(source: source, lines: lines, start_idx: start_idx)
102
+ r = p.try_scan(source: idx.source, lines: lines, start_idx: start_idx)
102
103
  return r if r
103
104
  end
104
105
  nil
@@ -106,6 +107,8 @@ module Varar
106
107
 
107
108
  # Split +source+ into RawLines with UTF-16 start/end offsets.
108
109
  def split_lines(source)
110
+ return split_ascii_lines(source) if source.ascii_only?
111
+
109
112
  out = []
110
113
  start_u16 = 0
111
114
  current_u16 = 0
@@ -124,24 +127,38 @@ module Varar
124
127
  out
125
128
  end
126
129
 
127
- def try_thematic(source, line)
130
+ # split_lines for a source whose characters are each one UTF-16 unit:
131
+ # String#split finds the newlines without visiting every character from
132
+ # Ruby. A source with no newline (even an empty one) is one line.
133
+ def split_ascii_lines(source)
134
+ texts = source.split("\n", -1)
135
+ texts = [''] if texts.empty?
136
+ start = 0
137
+ texts.map do |text|
138
+ line = RawLine.new(text: text, start_offset: start, end_offset: start + text.length)
139
+ start += text.length + 1
140
+ line
141
+ end
142
+ end
143
+
144
+ def try_thematic(idx, line)
128
145
  return nil unless THEMATIC_RE.match?(line.text)
129
146
 
130
- ThematicBreak.new(span: Offsets.span_from_offsets(source, line.start_offset, line.end_offset))
147
+ ThematicBreak.new(span: Offsets.span_at(idx, line.start_offset, line.end_offset))
131
148
  end
132
149
 
133
- def try_heading(source, line)
150
+ def try_heading(idx, line)
134
151
  m = HEADING_RE.match(line.text)
135
152
  return nil unless m
136
153
 
137
154
  Heading.new(
138
155
  level: m[1].length,
139
156
  text: (m[2] || '').strip,
140
- span: Offsets.span_from_offsets(source, line.start_offset, line.end_offset)
157
+ span: Offsets.span_at(idx, line.start_offset, line.end_offset)
141
158
  )
142
159
  end
143
160
 
144
- def try_list_item(source, line)
161
+ def try_list_item(idx, line)
145
162
  if (ul = UL_RE.match(line.text))
146
163
  text = ul[3] || ''
147
164
  marker_start = line.start_offset + Offsets.utf16_len(ul[1] || '')
@@ -150,10 +167,10 @@ module Varar
150
167
  text_start = line.start_offset + Offsets.to_utf16_offset(line.text, cp_idx)
151
168
  return ListItem.new(
152
169
  text: text,
153
- span: Offsets.span_from_offsets(source, line.start_offset, line.end_offset),
170
+ span: Offsets.span_at(idx, line.start_offset, line.end_offset),
154
171
  segment_map: [SegmentOffset.new(text_offset: 0, source_offset: text_start)],
155
172
  ordered: false,
156
- marker_span: Offsets.span_from_offsets(source, marker_start, marker_end)
173
+ marker_span: Offsets.span_at(idx, marker_start, marker_end)
157
174
  )
158
175
  end
159
176
 
@@ -165,17 +182,17 @@ module Varar
165
182
  text_start = line.start_offset + Offsets.to_utf16_offset(line.text, cp_idx)
166
183
  return ListItem.new(
167
184
  text: text,
168
- span: Offsets.span_from_offsets(source, line.start_offset, line.end_offset),
185
+ span: Offsets.span_at(idx, line.start_offset, line.end_offset),
169
186
  segment_map: [SegmentOffset.new(text_offset: 0, source_offset: text_start)],
170
187
  ordered: true,
171
- marker_span: Offsets.span_from_offsets(source, marker_start, marker_end)
188
+ marker_span: Offsets.span_at(idx, marker_start, marker_end)
172
189
  )
173
190
  end
174
191
 
175
192
  nil
176
193
  end
177
194
 
178
- def try_blockquote(source, lines, start_idx)
195
+ def try_blockquote(idx, lines, start_idx)
179
196
  return nil if start_idx >= lines.length
180
197
 
181
198
  first = lines[start_idx]
@@ -216,14 +233,14 @@ module Varar
216
233
  [
217
234
  Blockquote.new(
218
235
  text: segments.join("\n"),
219
- span: Offsets.span_from_offsets(source, first.start_offset, end_offset),
236
+ span: Offsets.span_at(idx, first.start_offset, end_offset),
220
237
  segment_map: segment_map
221
238
  ),
222
239
  i
223
240
  ]
224
241
  end
225
242
 
226
- def consume_paragraph(source, lines, start_idx, plugins)
243
+ def consume_paragraph(idx, lines, start_idx, plugins)
227
244
  raise 'invariant: start_idx out of range' if start_idx >= lines.length
228
245
 
229
246
  first = lines[start_idx]
@@ -239,7 +256,7 @@ module Varar
239
256
  break if FENCE_RE.match?(candidate.text)
240
257
  break if ROW_RE.match?(candidate.text)
241
258
  break if THEMATIC_RE.match?(candidate.text)
242
- break if run_plugins(source, lines, candidate_idx, plugins)
259
+ break if run_plugins(idx, lines, candidate_idx, plugins)
243
260
 
244
261
  end_idx += 1
245
262
  end
@@ -249,15 +266,15 @@ module Varar
249
266
  end_offset = last.end_offset
250
267
  [
251
268
  Paragraph.new(
252
- text: Offsets.utf16_slice(source, start_offset, end_offset),
253
- span: Offsets.span_from_offsets(source, start_offset, end_offset),
269
+ text: Offsets.slice_at(idx, start_offset, end_offset),
270
+ span: Offsets.span_at(idx, start_offset, end_offset),
254
271
  segment_map: [SegmentOffset.new(text_offset: 0, source_offset: start_offset)]
255
272
  ),
256
273
  end_idx + 1
257
274
  ]
258
275
  end
259
276
 
260
- def try_fence(source, lines, start_idx)
277
+ def try_fence(idx, lines, start_idx)
261
278
  return nil if start_idx >= lines.length
262
279
 
263
280
  start = lines[start_idx]
@@ -284,22 +301,22 @@ module Varar
284
301
  i += 1
285
302
  end
286
303
 
287
- body = body_start.nil? || body_end.nil? ? '' : Offsets.utf16_slice(source, body_start, body_end)
304
+ body = body_start.nil? || body_end.nil? ? '' : Offsets.slice_at(idx, body_start, body_end)
288
305
 
289
306
  fallback = start.end_offset
290
- body_span = Offsets.span_from_offsets(source, body_start || fallback, body_end || fallback)
307
+ body_span = Offsets.span_at(idx, body_start || fallback, body_end || fallback)
291
308
  [
292
309
  Fence.new(
293
310
  info: info,
294
311
  body: body,
295
312
  body_span: body_span,
296
- span: Offsets.span_from_offsets(source, start.start_offset, end_offset)
313
+ span: Offsets.span_at(idx, start.start_offset, end_offset)
297
314
  ),
298
315
  i + 1
299
316
  ]
300
317
  end
301
318
 
302
- def try_table(source, lines, start_idx)
319
+ def try_table(idx, lines, start_idx)
303
320
  return nil if start_idx + 1 >= lines.length
304
321
 
305
322
  header_line = lines[start_idx]
@@ -308,11 +325,11 @@ module Varar
308
325
  return nil unless DELIM_RE.match?(delim_line.text)
309
326
 
310
327
  header_cells, header_cell_spans = TableCells.parse_row_cells(header_line.text, header_line.start_offset,
311
- source)
328
+ idx)
312
329
  header = Row.new(
313
330
  cells: header_cells,
314
331
  cell_spans: header_cell_spans,
315
- span: Offsets.span_from_offsets(source, header_line.start_offset, header_line.end_offset)
332
+ span: Offsets.span_at(idx, header_line.start_offset, header_line.end_offset)
316
333
  )
317
334
 
318
335
  rows = []
@@ -321,11 +338,11 @@ module Varar
321
338
  ln = lines[i]
322
339
  break unless ROW_RE.match?(ln.text)
323
340
 
324
- cells, cell_spans = TableCells.parse_row_cells(ln.text, ln.start_offset, source)
341
+ cells, cell_spans = TableCells.parse_row_cells(ln.text, ln.start_offset, idx)
325
342
  rows << Row.new(
326
343
  cells: cells,
327
344
  cell_spans: cell_spans,
328
- span: Offsets.span_from_offsets(source, ln.start_offset, ln.end_offset)
345
+ span: Offsets.span_at(idx, ln.start_offset, ln.end_offset)
329
346
  )
330
347
  i += 1
331
348
  end
@@ -334,7 +351,7 @@ module Varar
334
351
  end_offset = last_row ? last_row.span.end_offset : delim_line.end_offset
335
352
  [
336
353
  Table.new(
337
- span: Offsets.span_from_offsets(source, header_line.start_offset, end_offset),
354
+ span: Offsets.span_at(idx, header_line.start_offset, end_offset),
338
355
  header: header,
339
356
  rows: rows
340
357
  ),