Class: RBS::Annotate::RDocAnnotator

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/annotate/rdoc_annotator.rb,
sig/annotate/rdoc_annotater.rbs

Defined Under Namespace

Modules: _Annotated, _Commented, _PathTester, _WithRDocComment

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source:) ⇒ RDocAnnotator

Returns a new instance of RDocAnnotator.

Parameters:



9
10
11
12
13
14
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 9

def initialize(source:)
  @source = source

  @include_arg_lists = true
  @include_filename = true
end

Instance Attribute Details

#include_arg_listsBoolean

Returns the value of attribute include_arg_lists.

Returns:

  • (Boolean)


7
8
9
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 7

def include_arg_lists
  @include_arg_lists
end

#include_filenameBoolean

Returns the value of attribute include_filename.

Returns:

  • (Boolean)


7
8
9
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 7

def include_filename
  @include_filename
end

#sourceRDocSource (readonly)

Returns the value of attribute source.

Returns:



6
7
8
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 6

def source
  @source
end

Instance Method Details

#annotate_alias(typename, als, rewriter) ⇒ void

This method returns an undefined value.



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 281

def annotate_alias(typename, als, rewriter)
  annots = annotations(als)

  unless annots.skip?
    text = resolve_doc_source(annots.copy_annotation, tester: annots) do
      case als.kind
      when :instance
        doc_for_method(typename, instance_method: als.new_name, tester: annots)
      when :singleton
        doc_for_method(typename, singleton_method: als.new_name, tester: annots)
      end
    end
  end

  replace_comment(als, text, rewriter)
end

#annotate_attribute(typename, attr, rewriter) ⇒ void

This method returns an undefined value.



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 340

def annotate_attribute(typename, attr, rewriter)
  annots = annotations(attr)

  unless annots.skip?
    text = resolve_doc_source(annots.copy_annotation, tester: annots) do
      # @type var docs: Array[String?]
      docs = []

      case attr.kind
      when :instance
        if attr.is_a?(AST::Members::AttrReader) || attr.is_a?(AST::Members::AttrAccessor)
          docs << doc_for_method(typename, instance_method: attr.name, tester: annots)
        end
        if attr.is_a?(AST::Members::AttrWriter) || attr.is_a?(AST::Members::AttrAccessor)
          docs << doc_for_method(typename, instance_method: :"#{attr.name}=", tester: annots)
        end
      when :singleton
        if attr.is_a?(AST::Members::AttrReader) || attr.is_a?(AST::Members::AttrAccessor)
          docs << doc_for_method(typename, singleton_method: attr.name, tester: annots)
        end
        if attr.is_a?(AST::Members::AttrWriter) || attr.is_a?(AST::Members::AttrAccessor)
          docs << doc_for_method(typename, singleton_method: :"#{attr.name}=", tester: annots)
        end
      end
      join_docs(docs.uniq)
    end
  end

  replace_comment(attr, text, rewriter)
end

#annotate_class(decl, rewriter, outer:) ⇒ void

This method returns an undefined value.



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 244

def annotate_class(decl, rewriter, outer:)
  annots = annotations(decl)

  full_name = resolve_name(decl.name, outer: outer)
  unless annots.skip?
    text = resolve_doc_source(annots.copy_annotation, tester: annots) { doc_for_class(full_name, tester: annots) }
  end

  replace_comment(decl, text, rewriter)

  unless annots.skip_all?
    outer_ = outer + [decl.name.to_namespace]

    decl.each_member do |member|
      case member
      when AST::Members::MethodDefinition
        annotate_method(full_name, member, rewriter)
      when AST::Members::Alias
        annotate_alias(full_name, member, rewriter)
      when AST::Members::AttrReader, AST::Members::AttrAccessor, AST::Members::AttrWriter
        annotate_attribute(full_name, member, rewriter)
      end
    end

    annotate_decls(decl.each_decl.to_a, rewriter, outer: outer_)
  end
end

#annotate_constant(const, rewriter, outer:) ⇒ void

This method returns an undefined value.

Parameters:



272
273
274
275
276
277
278
279
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 272

def annotate_constant(const, rewriter, outer:)
  annots = Annotations.new([])

  full_name = resolve_name(const.name, outer: outer)
  text = doc_for_constant(full_name, tester: annots)

  replace_comment(const, text, rewriter)
end

#annotate_decls(decls, rewriter, outer: []) ⇒ void

This method returns an undefined value.

Parameters:

  • (Array[AST::Declarations::t])
  • (Rewriter)
  • outer: (Array[Namespace]) (defaults to: [])


25
26
27
28
29
30
31
32
33
34
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 25

def annotate_decls(decls, rewriter, outer: [])
  decls.each do |decl|
    case decl
    when AST::Declarations::Class, AST::Declarations::Module
      annotate_class(decl, rewriter, outer: outer)
    when AST::Declarations::Constant
      annotate_constant(decl, rewriter, outer: outer)
    end
  end
end

#annotate_file(arg0) ⇒ void #annotate_filevoid

Overloads:

  • #annotate_file(arg0) ⇒ void

    This method returns an undefined value.

    Parameters:

    • arg0 (Pathname)
  • #annotate_filevoid

    This method returns an undefined value.



16
17
18
19
20
21
22
23
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 16

def annotate_file(path, preserve: true)
  buffer, _, decls = Parser.parse_signature(path.read())

  rewriter = Rewriter.new(buffer)
  annotate_decls(decls, rewriter)

  path.write(rewriter.string)
end

#annotate_method(typename, method, rewriter) ⇒ void

This method returns an undefined value.



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 311

def annotate_method(typename, method, rewriter)
  annots = annotations(method)

  unless annots.skip?
    text = resolve_doc_source(annots.copy_annotation, tester: annots) {
      case method.kind
      when :singleton
        doc_for_method(typename, singleton_method: method.name, tester: annots)
      when :instance
        if method.name == :initialize
          doc_for_method(typename, instance_method: :initialize, tester: annots) ||
            doc_for_method(typename, singleton_method: :new, tester: annots)
        else
          doc_for_method(typename, instance_method: method.name, tester: annots)
        end
      when :singleton_instance
        join_docs(
          [
            doc_for_method(typename, singleton_method: method.name, tester: annots),
            doc_for_method(typename, instance_method: method.name, tester: annots)
          ].uniq
        )
      end
    }
  end

  replace_comment(method, text, rewriter)
end

#annotations(annots) ⇒ Annotations

Parameters:

Returns:



391
392
393
394
395
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 391

def annotations(annots)
  # @type var as: Array[Annotations::t]
  as = _ = annots.annotations.map {|annot| Annotations.parse(annot) }.compact
  Annotations.new(as)
end

#doc_for_alias(typename, name:, singleton:, tester:) ⇒ String?

Parameters:

Returns:

  • (String, nil)


186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 186

def doc_for_alias(typename, name:, singleton:, tester:)
  if as =
    if singleton
      source.find_method(typename, singleton_method: name)
    else
      source.find_method(typename, instance_method: name)
    end

    formatter = Formatter.new

    each_part(as, tester: tester) do |doc, obj|
      # @type var method: RDoc::AnyMethod
      method = _ = obj

      if method.is_alias_for
        text = Formatter.translate(doc) or next

        unless text.empty?
          formatter << "<!-- rdoc-file=#{doc.file} -->" if include_filename
          formatter << text
        end
      end
    end

    formatter.format(newline_at_end: true)
  end
end

#doc_for_attribute(typename, attr_name, require: nil, singleton:, tester:) ⇒ String?

Having require: nil means any attribute is okay. Having require: "R" means read attr is required. Having require: "W" means write attr_ is required.

Parameters:

  • (TypeName)
  • (Symbol)
  • singleton: (Boolean)
  • require: (nil, "R", "W") (defaults to: nil)
  • tester: (_PathTester)

Returns:

  • (String, nil)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'sig/annotate/rdoc_annotater.rbs', line 46

def doc_for_attribute(typename, attr_name, require: nil, singleton:, tester:)
  if as = source.find_attribute(typename, attr_name, singleton: singleton)
    as = as.select do |attr|
      case require
      when "R"
        attr.rw == "R" || attr.rw == "RW"
      when "W"
        attr.rw == "W" || attr.rw == "RW"
      else
        true
      end
    end

    return if as.empty?

    formatter = Formatter.new()

    each_part(as, tester: tester) do |doc, obj|
      if text = Formatter.translate(doc)
        unless text.empty?
          formatter << "<!-- rdoc-file=#{doc.file} -->" if include_filename
          formatter << text
        end
      end
    end

    formatter.format(newline_at_end: true)
  end
end

#doc_for_class(name, tester:) ⇒ String?

Parameters:

Returns:

  • (String, nil)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 65

def doc_for_class(name, tester:)
  if clss = source.find_class(name)
    formatter = Formatter.new()

    each_part(clss, tester: tester) do |doc, _|
      text = Formatter.translate(doc) or next

      unless text.empty?
        if include_filename
          formatter << "<!-- rdoc-file=#{doc.file} -->"
        end
        formatter << text

        formatter.margin
      end
    end

    formatter.format(newline_at_end: true)
  end
end

#doc_for_constant(name, tester:) ⇒ String?

Parameters:

Returns:

  • (String, nil)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 86

def doc_for_constant(name, tester:)
  if constants = source.find_const(name)
    formatter = Formatter.new

    each_part(constants, tester: tester) do |doc, _|
      text = Formatter.translate(doc) or next

      unless text.empty?
        if include_filename
          formatter << "<!-- rdoc-file=#{doc.file} -->"
        end

        formatter << text

        formatter.margin
      end
    end

    formatter.format(newline_at_end: true)
  end
end

#doc_for_method(arg0, instance_method:, tester:) ⇒ String? #doc_for_method(arg0, singleton_method:, tester:) ⇒ String?

Returns the formatted document of given method.

Expands attribute documents, or alias documents if needed.

Overloads:

  • #doc_for_method(arg0, instance_method:, tester:) ⇒ String?

    Parameters:

    Returns:

    • (String, nil)
  • #doc_for_method(arg0, singleton_method:, tester:) ⇒ String?

    Parameters:

    Returns:

    • (String, nil)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'sig/annotate/rdoc_annotater.rbs', line 35

def doc_for_method(typename, instance_method: nil, singleton_method: nil, tester:)
  formatter = Formatter.new()

  case
  when method = instance_method
    doc = doc_for_alias(typename, name: method, singleton: false, tester: tester)
    doc = doc_for_method0(typename, instance_method: method, tester: tester) if !doc || doc.empty?
    if !doc || doc.empty?
      if (s = method.to_s) =~ /\A[a-zA-Z_]/
        # may be attribute
        doc =
          if s.end_with?("=")
            doc_for_attribute(typename, s.delete_suffix("=").to_sym, require: "W", singleton: false, tester: tester)
          else
            doc_for_attribute(typename, s.to_sym, require: "R", singleton: false, tester: tester)
          end
      end
    end
  when method = singleton_method
    doc = doc_for_alias(typename, name: method, singleton: true, tester: tester)
    doc = doc_for_method0(typename, singleton_method: method, tester: tester) if !doc || doc.empty?
    if !doc || doc.empty?
      if (s = method.to_s) =~ /\A[a-zA-Z_]/
        # may be attribute
        doc =
          if s.end_with?("=")
            doc_for_attribute(typename, s.delete_suffix("=").to_sym, require: "W", singleton: true, tester: tester)
          else
            doc_for_attribute(typename, s.to_sym, require: "R", singleton: true, tester: tester)
          end
      end
    end
  else
    raise
  end

  if doc
    formatter << doc
    formatter.format(newline_at_end: true)
  end
end

#doc_for_method0(arg0, instance_method:, tester:) ⇒ String? #doc_for_method0(arg0, singleton_method:, tester:) ⇒ String?

Overloads:

  • #doc_for_method0(arg0, instance_method:, tester:) ⇒ String?

    Parameters:

    Returns:

    • (String, nil)
  • #doc_for_method0(arg0, singleton_method:, tester:) ⇒ String?

    Parameters:

    Returns:

    • (String, nil)


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 108

def doc_for_method0(typename, instance_method: nil, singleton_method: nil, tester:)
  ms = source.find_method(typename, instance_method: instance_method) if instance_method
  ms = source.find_method(typename, singleton_method: singleton_method) if singleton_method

  if ms
    formatter = Formatter.new

    each_part(ms, tester: tester) do |doc, method|
      text = Formatter.translate(doc) or next
      # @type var as: String?
      as = (_ = method).arglists

      if include_arg_lists && as
        formatter << "<!--"
        formatter << "  rdoc-file=#{doc.file}" if include_filename
        as.chomp.split("\n").each do |line|
          formatter << "  - #{line.strip}"
        end
        formatter << "-->"
      else
        if include_filename
          formatter << "<!-- rdoc-file=#{doc.file} -->"
        end
      end

      unless text.empty?
        formatter << text
      end

      formatter.margin(separator: "----")
    end

    formatter.format(newline_at_end: false)
  end
end

#each_part(arg0, tester:) ⇒ void #each_part(arg0, tester:) ⇒ Enumerator[[RDoc::Markup::Document, Object & _WithRDocComment], void]

Overloads:

Yields:

Yield Parameters:

Yield Returns:

  • (void)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 36

def each_part(subjects, tester:)
  if block_given?
    subjects.each do |subject, docs|
      comment = subject.comment
      raise if comment.is_a?(String)
      Formatter.each_part(comment.parse) do |doc|
        if tester.test_path(doc.file || raise)
          yield [doc, subject]
        end
      end
    end
  else
    enum_for :each_part, tester: tester
  end
end

#join_docs(docs, separator: "----") ⇒ String?

Parameters:

  • (Array[String?])
  • separator: (String) (defaults to: "----")

Returns:

  • (String, nil)


298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 298

def join_docs(docs, separator: "----")
  formatter = Formatter.new()

  docs.each do |doc|
    formatter << doc
    formatter.margin(separator: separator)
  end

  unless formatter.empty?
    formatter.format(newline_at_end: true)
  end
end

#replace_comment(commented, string, rewriter) ⇒ void

This method returns an undefined value.

  • If a string is given as comment, the content is attached to the object as a comment.
  • If empty string is given as comment, it deletes the original comment.
  • If nil is given as comment, it keeps the original comment.

Parameters:



74
75
76
77
78
79
80
81
82
83
84
# File 'sig/annotate/rdoc_annotater.rbs', line 74

def replace_comment(commented, string, rewriter)
  if string
    if string.empty?
      rewriter.delete_comment(commented.comment) if commented.comment
    elsif commented.comment
      rewriter.replace_comment(commented.comment, content: string)
    else
      rewriter.add_comment(commented.location || raise, *commented.annotations.filter_map(&:location), content: string)
    end
  end
end

#resolve_doc_source(copy, tester:) { ... } ⇒ String?

Parameters:

Yields:

Yield Returns:

  • (String, nil)

Returns:

  • (String, nil)


52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 52

def resolve_doc_source(copy, tester:)
  case
  when copy && (mn = copy.method_name) && copy.singleton?
    doc_for_method(copy.type_name, singleton_method: mn, tester: tester)
  when copy && (mn = copy.method_name) && !copy.singleton?
    doc_for_method(copy.type_name, instance_method: mn, tester: tester)
  when copy
    doc_for_class(copy.type_name, tester: tester) || doc_for_constant(copy.type_name, tester: tester)
  else
    yield
  end
end

#resolve_name(name, outer:) ⇒ TypeName

Parameters:

Returns:



383
384
385
386
387
388
389
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 383

def resolve_name(name, outer:)
  namespace = outer.inject(RBS::Namespace.root) do |ns1, ns2|
    ns1 + ns2
  end

  name.with_prefix(namespace).relative!
end