expressir 2.4.82 → 2.4.83

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 (32) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/lane-parity.yml +6 -1
  3. data/.github/workflows/rust-ext.yml +8 -1
  4. data/README.adoc +130 -1
  5. data/docs/_guides/formatter/formatter-architecture.adoc +169 -1
  6. data/docs/_guides/ruby-api/formatting-schemas.adoc +64 -0
  7. data/lib/expressir/express/builders/entity_decl_builder.rb +3 -0
  8. data/lib/expressir/express/builders/function_decl_builder.rb +1 -0
  9. data/lib/expressir/express/builders/procedure_decl_builder.rb +1 -0
  10. data/lib/expressir/express/builders/rule_decl_builder.rb +1 -0
  11. data/lib/expressir/express/builders/subtype_constraint_builder.rb +7 -1
  12. data/lib/expressir/express/formatter.rb +4 -3
  13. data/lib/expressir/express/formatters/data_types_formatter.rb +39 -21
  14. data/lib/expressir/express/formatters/declarations_formatter.rb +69 -57
  15. data/lib/expressir/express/formatters/remark_formatter.rb +53 -1
  16. data/lib/expressir/express/formatters/statements_formatter.rb +1 -0
  17. data/lib/expressir/express/formatters/supertype_expressions_formatter.rb +66 -1
  18. data/lib/expressir/express/line_map.rb +8 -0
  19. data/lib/expressir/express/node_position_index.rb +74 -4
  20. data/lib/expressir/express/pretty_formatter.rb +22 -5
  21. data/lib/expressir/express/remark_attacher.rb +665 -29
  22. data/lib/expressir/express/schema_compare.rb +2 -1
  23. data/lib/expressir/express/shtolo.rb +3 -0
  24. data/lib/expressir/model/declarations/entity.rb +5 -0
  25. data/lib/expressir/model/declarations/function.rb +4 -0
  26. data/lib/expressir/model/declarations/procedure.rb +4 -0
  27. data/lib/expressir/model/declarations/remark_item.rb +4 -0
  28. data/lib/expressir/model/declarations/rule.rb +4 -0
  29. data/lib/expressir/model/references/simple_reference.rb +1 -0
  30. data/lib/expressir/model/remark_placement.rb +6 -0
  31. data/lib/expressir/version.rb +1 -1
  32. metadata +2 -2
@@ -151,7 +151,7 @@ module Expressir
151
151
  "\n",
152
152
  indent([
153
153
  "(",
154
- enumeration_item_lines(node.items, item_indent),
154
+ list_item_lines(node.items, item_indent),
155
155
  ")",
156
156
  ].join),
157
157
  ].join
@@ -167,7 +167,7 @@ module Expressir
167
167
  "\n",
168
168
  indent([
169
169
  "(",
170
- enumeration_item_lines(node.items, item_indent),
170
+ list_item_lines(node.items, item_indent),
171
171
  ")",
172
172
  ].join),
173
173
  ].join
@@ -177,25 +177,47 @@ module Expressir
177
177
  ].join
178
178
  end
179
179
 
180
- # The items of an ENUMERATION, one a line. A remark that follows an
181
- # item on its line is written after the comma. The remark of the
182
- # last item is written after the `;` of the TYPE
183
- # ({#last_enumeration_item_remark}): before it, a tail remark would
184
- # take the `)` and the `;` with it.
185
- def enumeration_item_lines(items, item_indent)
180
+ # The items of an ENUMERATION or of a SELECT, one a line. A remark
181
+ # that follows an item on its line is written after the comma. The
182
+ # remark of the last item is written after the `;` of the TYPE
183
+ # ({#last_list_item_remark}): before it, a tail remark would take
184
+ # the `)` and the `;` with it.
185
+ def list_item_lines(items, item_indent)
186
186
  last = items.length - 1
187
187
  items.each_with_index.map do |item, i|
188
- i == last ? format(item) : "#{format(item)},#{format_inline_statement_remarks(item)}"
188
+ text = list_item_text(item, item_indent)
189
+ i == last ? text : "#{text},#{format_list_item_remarks(item)}"
189
190
  end.join("\n#{item_indent}")
190
191
  end
191
192
 
192
- # The remark that follows the last item of a TYPE's ENUMERATION.
193
- def last_enumeration_item_remark(type)
194
- enumeration = type.underlying_type
195
- return "" unless enumeration.is_a?(Model::DataTypes::Enumeration)
193
+ # An item with the remarks that stand on lines of their own above
194
+ # it. Each remark and the item start at the depth of the items; the
195
+ # lines inside a remark of several lines are left as they are.
196
+ def list_item_text(item, item_indent)
197
+ leading = format_leading_statement_remarks(item)
198
+ return format(item) if leading.empty?
196
199
 
197
- item = Array(enumeration.items).last
198
- item ? format_inline_statement_remarks(item) : ""
200
+ own = format(item).delete_prefix("#{leading.join("\n")}\n")
201
+ [*leading, own].join("\n#{item_indent}")
202
+ end
203
+
204
+ # The remark that follows the last item of a TYPE's ENUMERATION or
205
+ # SELECT.
206
+ def last_list_item_remark(type)
207
+ list = type.underlying_type
208
+ return "" unless list.is_a?(Model::DataTypes::Enumeration) ||
209
+ list.is_a?(Model::DataTypes::Select)
210
+
211
+ item = Array(list.items).last
212
+ item ? format_list_item_remarks(item) : ""
213
+ end
214
+
215
+ # An item of an ENUMERATION, or a reference: the items of a SELECT
216
+ # are references, and the attacher places an inline remark on a
217
+ # reference only there.
218
+ def list_item?(node)
219
+ node.is_a?(Model::DataTypes::EnumerationItem) ||
220
+ node.is_a?(Model::References::SimpleReference)
199
221
  end
200
222
 
201
223
  def format_data_types_enumeration_item(node)
@@ -320,9 +342,7 @@ module Expressir
320
342
  "\n",
321
343
  indent([
322
344
  "(",
323
- items.map do |x|
324
- format(x)
325
- end.join(",\n#{item_indent}"),
345
+ list_item_lines(items, item_indent),
326
346
  ")",
327
347
  ].join),
328
348
  ].join
@@ -335,9 +355,7 @@ module Expressir
335
355
  "\n",
336
356
  indent([
337
357
  "(",
338
- items.map do |x|
339
- format(x)
340
- end.join(",\n#{item_indent}"),
358
+ list_item_lines(items, item_indent),
341
359
  ")",
342
360
  ].join),
343
361
  ].join
@@ -60,7 +60,9 @@ module Expressir
60
60
  run.length > 1 && group_combinable?(run) ? run : nil
61
61
  end
62
62
  if members
63
- out << formatter_method.call(node, members.map(&:id))
63
+ # The combined declaration does not pass through format.
64
+ out << [*format_leading_statement_remarks(node),
65
+ formatter_method.call(node, members.map(&:id))].join("\n")
64
66
  i += members.length
65
67
  else
66
68
  out << format(node)
@@ -177,8 +179,7 @@ module Expressir
177
179
  "\n",
178
180
  indent([
179
181
  "ABSTRACT",
180
- " ",
181
- "SUPERTYPE",
182
+ *(node.abstract_entity ? [] : [" ", "SUPERTYPE"]),
182
183
  ].join),
183
184
  ].join
184
185
  end,
@@ -234,32 +235,37 @@ module Expressir
234
235
  *if explicit_attributes&.length&.positive?
235
236
  indent(format_grouped_declarations(explicit_attributes, method(:format_declarations_attribute)).join("\n"))
236
237
  end,
238
+ # A comment after the last member of a section closes it, before the
239
+ # keyword of the next section or END_ENTITY.
240
+ *format_closing_remarks(node, :attributes).map { |x| indent(x) },
237
241
  *if derived_attributes&.length&.positive?
238
242
  [
239
243
  "DERIVE",
240
244
  indent(derived_attributes.map { |x| format(x) }.join("\n")),
241
245
  ]
242
246
  end,
247
+ *format_closing_remarks(node, :derived_attributes).map { |x| indent(x) },
243
248
  *if inverse_attributes&.length&.positive?
244
249
  [
245
250
  "INVERSE",
246
251
  indent(inverse_attributes.map { |x| format(x) }.join("\n")),
247
252
  ]
248
253
  end,
254
+ *format_closing_remarks(node, :inverse_attributes).map { |x| indent(x) },
249
255
  *if node.unique_rules&.length&.positive?
250
256
  [
251
257
  "UNIQUE",
252
258
  indent(node.unique_rules.map { |x| format(x) }.join("\n")),
253
259
  ]
254
260
  end,
261
+ *format_closing_remarks(node, :unique_rules).map { |x| indent(x) },
255
262
  *if node.where_rules&.length&.positive?
256
263
  [
257
264
  "WHERE",
258
265
  indent(node.where_rules.map { |x| format(x) }.join("\n")),
259
266
  ]
260
267
  end,
261
- # A comment after the last attribute or rule closes the entity.
262
- *format_closing_remarks(node, :attributes, :where_rules).map { |x| indent(x) },
268
+ *format_closing_remarks(node, :where_rules).map { |x| indent(x) },
263
269
  *format_informal_propositions(node).map { |x| indent(x) },
264
270
  *format_remark_items(node).map { |x| indent(x) },
265
271
  [
@@ -282,6 +288,28 @@ module Expressir
282
288
  items.flatten.compact.uniq(&:object_id)
283
289
  end
284
290
 
291
+ # The declarations nested in a FUNCTION, PROCEDURE or RULE (#711,
292
+ # as for a schema): with a recorded declaration order they follow
293
+ # the source; a model without it keeps the grouped-by-kind layout.
294
+ def format_nested_declarations(node)
295
+ collections = [node.types, node.entities, node.subtype_constraints,
296
+ node.functions, node.procedures]
297
+ all = collections.flat_map(&:to_a)
298
+ # Each id of the record takes the first nested declaration of that
299
+ # id not written yet; what the record leaves out follows in the
300
+ # grouped-by-kind order, so every declaration is written once.
301
+ written = {}.compare_by_identity
302
+ ordered = Array(node.declaration_order).filter_map do |id|
303
+ hit = all.find { |x| !written.key?(x) && x.id == id }
304
+ written[hit] = true if hit
305
+ hit
306
+ end
307
+ ordered.concat(all.reject { |x| written.key?(x) })
308
+ return [] if ordered.empty?
309
+
310
+ [indent(ordered.map { |x| format(x) }.join("\n"))]
311
+ end
312
+
285
313
  def format_declarations_function(node)
286
314
  indent_char = self.class.const_get(:INDENT_CHAR)
287
315
  [
@@ -305,21 +333,7 @@ module Expressir
305
333
  format(node.return_type),
306
334
  ";",
307
335
  ].join,
308
- *if node.types&.length&.positive?
309
- indent(node.types.map { |x| format(x) }.join("\n"))
310
- end,
311
- *if node.entities&.length&.positive?
312
- indent(node.entities.map { |x| format(x) }.join("\n"))
313
- end,
314
- *if node.subtype_constraints&.length&.positive?
315
- indent(node.subtype_constraints.map { |x| format(x) }.join("\n"))
316
- end,
317
- *if node.functions&.length&.positive?
318
- indent(node.functions.map { |x| format(x) }.join("\n"))
319
- end,
320
- *if node.procedures&.length&.positive?
321
- indent(node.procedures.map { |x| format(x) }.join("\n"))
322
- end,
336
+ *format_nested_declarations(node),
323
337
  *if node.constants&.length&.positive?
324
338
  indent([
325
339
  "CONSTANT",
@@ -330,10 +344,15 @@ module Expressir
330
344
  ].join,
331
345
  ].join("\n"))
332
346
  end,
347
+ # A comment on its own line before LOCAL closes the declarations.
348
+ *format_trailing_region_remarks(node, :declarations).map { |x| indent(x) },
333
349
  *if node.variables&.length&.positive?
334
350
  indent([
335
351
  "LOCAL",
336
- indent(format_grouped_declarations(node.variables, method(:format_declarations_variable)).join("\n")),
352
+ indent([
353
+ *format_grouped_declarations(node.variables, method(:format_declarations_variable)),
354
+ *format_trailing_region_remarks(node, :variables),
355
+ ].join("\n")),
337
356
  [
338
357
  "END_LOCAL",
339
358
  ";",
@@ -431,21 +450,7 @@ module Expressir
431
450
  end,
432
451
  ";",
433
452
  ].join,
434
- *if node.types&.length&.positive?
435
- indent(node.types.map { |x| format(x) }.join("\n"))
436
- end,
437
- *if node.entities&.length&.positive?
438
- indent(node.entities.map { |x| format(x) }.join("\n"))
439
- end,
440
- *if node.subtype_constraints&.length&.positive?
441
- indent(node.subtype_constraints.map { |x| format(x) }.join("\n"))
442
- end,
443
- *if node.functions&.length&.positive?
444
- indent(node.functions.map { |x| format(x) }.join("\n"))
445
- end,
446
- *if node.procedures&.length&.positive?
447
- indent(node.procedures.map { |x| format(x) }.join("\n"))
448
- end,
453
+ *format_nested_declarations(node),
449
454
  *if node.constants&.length&.positive?
450
455
  indent([
451
456
  "CONSTANT",
@@ -456,10 +461,15 @@ module Expressir
456
461
  ].join,
457
462
  ].join("\n"))
458
463
  end,
464
+ # A comment on its own line before LOCAL closes the declarations.
465
+ *format_trailing_region_remarks(node, :declarations).map { |x| indent(x) },
459
466
  *if node.variables&.length&.positive?
460
467
  indent([
461
468
  "LOCAL",
462
- indent(format_grouped_declarations(node.variables, method(:format_declarations_variable)).join("\n")),
469
+ indent([
470
+ *format_grouped_declarations(node.variables, method(:format_declarations_variable)),
471
+ *format_trailing_region_remarks(node, :variables),
472
+ ].join("\n")),
463
473
  [
464
474
  "END_LOCAL",
465
475
  ";",
@@ -526,21 +536,7 @@ module Expressir
526
536
  ")",
527
537
  ";",
528
538
  ].join,
529
- *if node.types&.length&.positive?
530
- indent(node.types.map { |x| format(x) }.join("\n"))
531
- end,
532
- *if node.entities&.length&.positive?
533
- indent(node.entities.map { |x| format(x) }.join("\n"))
534
- end,
535
- *if node.subtype_constraints&.length&.positive?
536
- indent(node.subtype_constraints.map { |x| format(x) }.join("\n"))
537
- end,
538
- *if node.functions&.length&.positive?
539
- indent(node.functions.map { |x| format(x) }.join("\n"))
540
- end,
541
- *if node.procedures&.length&.positive?
542
- indent(node.procedures.map { |x| format(x) }.join("\n"))
543
- end,
539
+ *format_nested_declarations(node),
544
540
  *if node.constants&.length&.positive?
545
541
  indent([
546
542
  "CONSTANT",
@@ -551,10 +547,15 @@ module Expressir
551
547
  ].join,
552
548
  ].join("\n"))
553
549
  end,
550
+ # A comment on its own line before LOCAL closes the declarations.
551
+ *format_trailing_region_remarks(node, :declarations).map { |x| indent(x) },
554
552
  *if node.variables&.length&.positive?
555
553
  indent([
556
554
  "LOCAL",
557
- indent(format_grouped_declarations(node.variables, method(:format_declarations_variable)).join("\n")),
555
+ indent([
556
+ *format_grouped_declarations(node.variables, method(:format_declarations_variable)),
557
+ *format_trailing_region_remarks(node, :variables),
558
+ ].join("\n")),
558
559
  [
559
560
  "END_LOCAL",
560
561
  ";",
@@ -683,10 +684,18 @@ module Expressir
683
684
  # remark.exp byte-identically for the formatter golden.
684
685
  *format_schema_remarks(node).flat_map { |x| ["", x] },
685
686
  *format_remark_items(node).flat_map { |x| ["", x] },
687
+ # Comments after the last declaration, directly above END_SCHEMA.
688
+ # The blank line above them is already there after the last
689
+ # declaration, and is not after an item or in an empty schema.
690
+ *format_trailing_region_remarks(node, :declarations).then do |x|
691
+ blank = !schema_declarations&.length&.positive? ||
692
+ !(format_schema_remarks(node) + format_remark_items(node)).empty?
693
+ x.empty? || !blank ? x : ["", *x]
694
+ end,
686
695
  [
687
696
  "END_SCHEMA",
688
697
  ";",
689
- format_end_scope_remark(node),
698
+ format_schema_end_remark(node),
690
699
  ].join,
691
700
  ].join("\n")
692
701
  end
@@ -737,6 +746,7 @@ module Expressir
737
746
  indent([
738
747
  format(node.supertype_expression),
739
748
  ";",
749
+ last_supertype_operand_remarks(node.supertype_expression),
740
750
  ].join)
741
751
  end,
742
752
  *format_remark_items(node).map { |x| indent(x) },
@@ -759,16 +769,18 @@ module Expressir
759
769
  " ",
760
770
  format(node.underlying_type),
761
771
  ";",
762
- last_enumeration_item_remark(node),
772
+ last_list_item_remark(node),
763
773
  ].join,
774
+ # A comment after the definition closes it, before WHERE or END_TYPE;
775
+ # one after the last rule closes the type.
776
+ *format_closing_remarks(node, :underlying_type).map { |x| indent(x) },
764
777
  *if node.where_rules&.length&.positive?
765
778
  [
766
779
  "WHERE",
767
780
  indent(node.where_rules.map { |x| format(x) }.join("\n")),
768
781
  ]
769
782
  end,
770
- # A comment after the definition or the last rule closes the type.
771
- *format_closing_remarks(node, :underlying_type, :where_rules).map { |x| indent(x) },
783
+ *format_closing_remarks(node, :where_rules).map { |x| indent(x) },
772
784
  *format_remark_items(node).map { |x| indent(x) },
773
785
  [
774
786
  "END_TYPE",
@@ -125,6 +125,15 @@ module Expressir
125
125
  " #{lines.join("\n")}"
126
126
  end
127
127
 
128
+ # The remark that follows `END_SCHEMA;`. It stands after the schema:
129
+ # the plain form (`no_remarks` with `keep_untagged_remarks`) is the
130
+ # schema with its untagged remarks and ends at `END_SCHEMA;`.
131
+ def format_schema_end_remark(node)
132
+ return "" if @no_remarks
133
+
134
+ format_end_scope_remark(node)
135
+ end
136
+
128
137
  # The entity's informal propositions, in the ISO remark-tag form
129
138
  # so the source convention survives the round trip: `--IP1 text`,
130
139
  # or the bare declaration marker `--IP1` when the proposition
@@ -134,11 +143,20 @@ module Expressir
134
143
  # ride the proposition's remark item, and the plain form's keep
135
144
  # flag is for UNTAGGED comments only (#723) — a tagged
136
145
  # description must not reappear as a `--IPn:` line.
137
- return [] if @no_remarks
146
+ return [] if @no_remarks && !@keep_untagged_remarks
138
147
  return [] unless node.is_a?(Model::ModelElement)
139
148
  return [] unless node.respond_to?(:informal_propositions)
140
149
 
141
150
  Array(node.informal_propositions).filter_map do |ip|
151
+ # The plain form writes only the `--IPn:` comment lines of the
152
+ # source (`source_remarks`), never a tagged description's text.
153
+ if @no_remarks
154
+ lines = Array(ip.remark_items).flat_map { |ri| Array(ri.source_remarks) }
155
+ next if lines.empty?
156
+
157
+ next lines.map { |t| t.empty? ? "--#{ip.id}:" : "--#{ip.id}: #{t}" }.join("\n")
158
+ end
159
+
142
160
  # The attach stores the proposition's texts on its remark
143
161
  # item (create_or_find_informal_proposition returns it); the
144
162
  # ISO shorthand re-parses only with the colon. A multi-line
@@ -218,6 +236,23 @@ module Expressir
218
236
  end.join("\n")
219
237
  end
220
238
 
239
+ # The remarks that follow an item of an ENUMERATION or of a SELECT on
240
+ # its line. An item of a SELECT is a reference, which is no
241
+ # TakesInlineRemark (a reference takes such a remark nowhere else),
242
+ # so its remarks are read here. They are untagged remarks: the
243
+ # plain form (`keep_untagged_remarks`) keeps them.
244
+ def format_list_item_remarks(item)
245
+ return format_inline_statement_remarks(item) unless item.is_a?(Model::References::SimpleReference)
246
+ return "" if @no_remarks && !@keep_untagged_remarks
247
+
248
+ Array(item.untagged_remarks).filter_map do |remark|
249
+ next unless remark.inline? && remark.region.nil?
250
+
251
+ formatted = format_untagged_remark(remark)
252
+ " #{formatted}" unless formatted.empty?
253
+ end.join
254
+ end
255
+
221
256
  # Remarks that close one body of `node`, emitted just before that
222
257
  # body's closing keyword. Only explicit TRAILING remarks for the named
223
258
  # region qualify: legacy (nil) remarks keep their historical emission
@@ -288,6 +323,23 @@ module Expressir
288
323
  # `--` comment (#696).
289
324
  Array(node.untagged_remarks).filter_map do |remark|
290
325
  next unless remark.leading?
326
+ next if remark.region == Model::RemarkPlacement::BEFORE_LABEL_REGION
327
+
328
+ formatted = format_untagged_remark(remark)
329
+ formatted unless formatted.empty?
330
+ end
331
+ end
332
+
333
+ # The leading remarks of a CASE action's statement that were written
334
+ # before the label of the action; the CASE action writes them above
335
+ # the label.
336
+ def format_before_label_remarks(node)
337
+ return [] if @no_remarks && !@keep_untagged_remarks
338
+ return [] unless node.is_a?(Model::ModelElement)
339
+
340
+ Array(node.untagged_remarks).filter_map do |remark|
341
+ next unless remark.leading? &&
342
+ remark.region == Model::RemarkPlacement::BEFORE_LABEL_REGION
291
343
 
292
344
  formatted = format_untagged_remark(remark)
293
345
  formatted unless formatted.empty?
@@ -115,6 +115,7 @@ module Expressir
115
115
 
116
116
  def format_statements_case_action(node)
117
117
  [
118
+ *format_before_label_remarks(node.statement),
118
119
  [
119
120
  Array(node.labels).map { |x| format(x) }.join(", "),
120
121
  " ",
@@ -14,9 +14,14 @@ module Expressir
14
14
  # The minimal-paren policy (expressir#651): left child needs
15
15
  # parens only when strictly looser; right child when
16
16
  # same-or-looser (left-associative operators).
17
+ # An operand that carries the pairs of the source (`parenthesized`,
18
+ # as for expressions, #707) writes them itself; the policy
19
+ # applies to a model without that record.
17
20
  op1_higher_precedence = node.operand1.is_a?(Model::SupertypeExpressions::BinarySupertypeExpression) &&
21
+ !node.operand1.parenthesized.to_i.positive? &&
18
22
  (supertype_precedence[node.operand1.operator] > supertype_precedence[node.operator])
19
23
  op2_higher_precedence = node.operand2.is_a?(Model::SupertypeExpressions::BinarySupertypeExpression) &&
24
+ !node.operand2.parenthesized.to_i.positive? &&
20
25
  (supertype_precedence[node.operand2.operator] >= supertype_precedence[node.operator])
21
26
 
22
27
  [
@@ -47,10 +52,70 @@ module Expressir
47
52
  [
48
53
  "ONEOF",
49
54
  "(",
50
- Array(node.operands).map { |x| format(x) }.join(", "),
55
+ supertype_operand_list(Array(node.operands)),
51
56
  ")",
52
57
  ].join
53
58
  end
59
+
60
+ # The operands of a ONEOF, on one line; one a line when a remark
61
+ # stands at one of them. A remark that follows an operand on its
62
+ # line is written after the comma; the remark of the last operand
63
+ # of the whole expression is written after the `;`
64
+ # ({#last_supertype_operand_remarks}), as before the `)` a tail
65
+ # remark would take the `)` and the `;` with it.
66
+ def supertype_operand_list(operands)
67
+ unless operands.any? { |x| supertype_operand_remarks?(x) }
68
+ return operands.map { |x| format(x) }.join(", ")
69
+ end
70
+
71
+ item_indent = " " * 6 # the width of `ONEOF(`
72
+ last = operands.length - 1
73
+ operands.each_with_index.map do |operand, i|
74
+ text = supertype_operand_text(operand, item_indent)
75
+ i == last ? text : "#{text},#{supertype_operand_inline_remarks(operand)}"
76
+ end.join("\n#{item_indent}")
77
+ end
78
+
79
+ # An operand with the remarks that stand on lines of their own above
80
+ # it, each at the depth of the operands.
81
+ def supertype_operand_text(operand, item_indent)
82
+ leading = format_leading_statement_remarks(operand)
83
+ return format(operand) if leading.empty?
84
+
85
+ own = format(operand).delete_prefix("#{leading.join("\n")}\n")
86
+ [*leading, own].join("\n#{item_indent}")
87
+ end
88
+
89
+ def supertype_operand_remarks?(operand)
90
+ operand.is_a?(Model::References::SimpleReference) &&
91
+ (!format_leading_statement_remarks(operand).empty? ||
92
+ !supertype_operand_inline_remarks(operand).empty?)
93
+ end
94
+
95
+ # The remarks that follow an operand on its line. An operand is a
96
+ # reference, which is no TakesInlineRemark, so they are read here.
97
+ def supertype_operand_inline_remarks(operand)
98
+ return "" unless operand.is_a?(Model::References::SimpleReference)
99
+ return "" if @no_remarks && !@keep_untagged_remarks
100
+
101
+ Array(operand.untagged_remarks).filter_map do |remark|
102
+ next unless remark.inline? && remark.region.nil?
103
+
104
+ formatted = format_untagged_remark(remark)
105
+ " #{formatted}" unless formatted.empty?
106
+ end.join
107
+ end
108
+
109
+ # The remark that follows the last operand of the whole expression.
110
+ def last_supertype_operand_remarks(expression)
111
+ case expression
112
+ when Model::SupertypeExpressions::OneofSupertypeExpression
113
+ last_supertype_operand_remarks(Array(expression.operands).last)
114
+ when Model::SupertypeExpressions::BinarySupertypeExpression
115
+ last_supertype_operand_remarks(expression.operand2)
116
+ else supertype_operand_inline_remarks(expression)
117
+ end
118
+ end
54
119
  end
55
120
  end
56
121
  end
@@ -29,6 +29,14 @@ module Expressir
29
29
  idx || @offsets.size
30
30
  end
31
31
 
32
+ # Returns the byte position where the given 1-based line begins.
33
+ #
34
+ # @param line [Integer]
35
+ # @return [Integer, nil] nil when the line does not exist.
36
+ def line_start(line)
37
+ @offsets[line - 1] if line >= 1
38
+ end
39
+
32
40
  private
33
41
 
34
42
  # Builds the sorted array of byte positions where each line begins.
@@ -22,6 +22,19 @@ module Expressir
22
22
  # and NodePositionIndex via this alias for backward compatibility.
23
23
  COLLECTION_REGISTRY = Model::ModelElement.collection_registry
24
24
 
25
+ # The collection name the items of a SELECT list are indexed under.
26
+ SELECT_ITEMS = :select_items
27
+
28
+ # The collection names the references of a SUBTYPE_CONSTRAINT's
29
+ # supertype expression are indexed under: an operand that a comma
30
+ # follows in a ONEOF list, the last operand of the whole expression,
31
+ # and the others (the operand of an operator, the last operand of an
32
+ # inner ONEOF), which take no remark.
33
+ SUPERTYPE_LISTED = :supertype_listed
34
+ SUPERTYPE_FINAL = :supertype_final
35
+ SUPERTYPE_OTHER = :supertype_other
36
+ SUPERTYPE_OPERANDS = [SUPERTYPE_LISTED, SUPERTYPE_FINAL, SUPERTYPE_OTHER].freeze
37
+
25
38
  attr_reader :nodes
26
39
 
27
40
  # Semantic entries starting on `line` (node order).
@@ -155,13 +168,13 @@ module Expressir
155
168
  # preferring same-line starts/ends, then smallest containing span.
156
169
  # Skips nodes that cannot own such a remark; see {#remark_scope?}.
157
170
  def nearest_node_to(remark_line)
158
- same_start = starting_at(remark_line).select { |n| remark_scope?(n[:node]) }
171
+ same_start = starting_at(remark_line).select { |n| owns_own_line_remark?(n) }
159
172
  return same_start.last[:node] if same_start.any?
160
173
 
161
- same_end = ending_at(remark_line).select { |n| remark_scope?(n[:node]) }
174
+ same_end = ending_at(remark_line).select { |n| owns_own_line_remark?(n) }
162
175
  return same_end.last[:node] if same_end.any?
163
176
 
164
- containing = spanning(remark_line).select { |n| remark_scope?(n[:node]) }
177
+ containing = spanning(remark_line).select { |n| owns_own_line_remark?(n) }
165
178
 
166
179
  if containing.any?
167
180
  exp_file_node = containing.find { |n| n[:node].is_a?(Model::ExpFile) }
@@ -252,7 +265,16 @@ module Expressir
252
265
  # giving it to the clause would both misplace it and lose it, since
253
266
  # nothing renders remarks there.
254
267
  def remark_scope?(node)
255
- semantic?(node) && !node.is_a?(Model::Declarations::Interface)
268
+ semantic?(node) && !node.is_a?(Model::Declarations::Interface) &&
269
+ !@supertype_operands&.key?(node)
270
+ end
271
+
272
+ # Whether an indexed node can own a remark sitting on a line of its
273
+ # own. An item of a SELECT list cannot: it is written inside the
274
+ # list, and only a remark placed on it (inline or leading) is written
275
+ # there.
276
+ def owns_own_line_remark?(entry)
277
+ remark_scope?(entry[:node]) && entry[:collection] != SELECT_ITEMS
256
278
  end
257
279
 
258
280
  def build_sorted_nodes
@@ -341,9 +363,57 @@ module Expressir
341
363
  end
342
364
  end
343
365
 
366
+ collect_supertype_operands(node, result, visited)
367
+
344
368
  each_collection_on(node) do |item, attr|
345
369
  collect_nodes(item, result, visited, owner: node, collection: attr)
346
370
  end
371
+
372
+ # The items of a SELECT list are references, which no collection
373
+ # carries. They are indexed under their TYPE so that a remark
374
+ # inside the list finds the item it follows or precedes.
375
+ return unless node.is_a?(Model::Declarations::Type) &&
376
+ node.underlying_type.is_a?(Model::DataTypes::Select)
377
+
378
+ Array(node.underlying_type.items).each do |item|
379
+ collect_nodes(item, result, visited, owner: node, collection: SELECT_ITEMS)
380
+ end
381
+ end
382
+
383
+ # The references of a supertype expression, which no collection
384
+ # carries, are indexed under their SUBTYPE_CONSTRAINT so that a remark
385
+ # inside the expression finds the operand it follows or precedes. An
386
+ # operand encloses nothing, so it owns no remark on a line of its own.
387
+ def collect_supertype_operands(node, result, visited)
388
+ return unless node.is_a?(Model::Declarations::SubtypeConstraint)
389
+
390
+ @supertype_operands ||= {}.compare_by_identity
391
+ supertype_references(node.supertype_expression, true, false).each do |ref, collection|
392
+ @supertype_operands[ref] = true
393
+ collect_nodes(ref, result, visited, owner: node, collection: collection)
394
+ end
395
+ end
396
+
397
+ # [reference, collection] for each reference of `expression`. `last`:
398
+ # the rightmost operand so far; `listed`: followed by a comma.
399
+ def supertype_references(expression, last, listed)
400
+ case expression
401
+ when Model::SupertypeExpressions::OneofSupertypeExpression
402
+ operands = Array(expression.operands)
403
+ operands.each_with_index.flat_map do |operand, i|
404
+ supertype_references(operand, last && i == operands.length - 1, i < operands.length - 1)
405
+ end
406
+ when Model::SupertypeExpressions::BinarySupertypeExpression
407
+ supertype_references(expression.operand1, false, false) +
408
+ supertype_references(expression.operand2, last, false)
409
+ when Model::References::SimpleReference
410
+ collection = if listed then SUPERTYPE_LISTED
411
+ elsif last then SUPERTYPE_FINAL
412
+ else SUPERTYPE_OTHER
413
+ end
414
+ [[expression, collection]]
415
+ else []
416
+ end
347
417
  end
348
418
 
349
419
  # Yields each child in any declared collection on the node (with the