Class: RBS::Annotate::RDocAnnotator
- Inherits:
-
Object
- Object
- RBS::Annotate::RDocAnnotator
- Defined in:
- lib/rbs/annotate/rdoc_annotator.rb
Instance Attribute Summary collapse
-
#include_arg_lists ⇒ Object
Returns the value of attribute include_arg_lists.
-
#include_filename ⇒ Object
Returns the value of attribute include_filename.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #annotate_alias(typename, als) ⇒ Object
- #annotate_attribute(typename, attr) ⇒ Object
- #annotate_class(decl, outer:) ⇒ Object
- #annotate_constant(const, outer:) ⇒ Object
- #annotate_decls(decls, outer: []) ⇒ Object
- #annotate_file(path, preserve:) ⇒ Object
- #annotate_method(typename, method) ⇒ Object
- #annotations(annots) ⇒ Object
- #doc_for_alias(typename, name:, singleton:, tester:) ⇒ Object
- #doc_for_attribute(typename, attr_name, require: nil, singleton:, tester:) ⇒ Object
- #doc_for_class(name, tester:) ⇒ Object
- #doc_for_constant(name, tester:) ⇒ Object
- #doc_for_method(typename, instance_method: nil, singleton_method: nil, tester:) ⇒ Object
- #doc_for_method0(typename, instance_method: nil, singleton_method: nil, tester:) ⇒ Object
- #each_part(subjects, tester:) ⇒ Object
-
#initialize(source:) ⇒ RDocAnnotator
constructor
A new instance of RDocAnnotator.
- #join_docs(docs, separator: "----") ⇒ Object
- #replace_comment(commented, string) ⇒ Object
- #resolve_doc_source(copy, tester:) ⇒ Object
- #resolve_name(name, outer:) ⇒ Object
Constructor Details
#initialize(source:) ⇒ RDocAnnotator
Returns a new instance of RDocAnnotator.
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_lists ⇒ Object
Returns the value of attribute include_arg_lists.
7 8 9 |
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 7 def include_arg_lists @include_arg_lists end |
#include_filename ⇒ Object
Returns the value of attribute include_filename.
7 8 9 |
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 7 def include_filename @include_filename end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
6 7 8 |
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 6 def source @source end |
Instance Method Details
#annotate_alias(typename, als) ⇒ Object
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 282 def annotate_alias(typename, als) 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) end |
#annotate_attribute(typename, attr) ⇒ Object
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 370 |
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 341 def annotate_attribute(typename, attr) 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) end |
#annotate_class(decl, outer:) ⇒ Object
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 271 |
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 245 def annotate_class(decl, 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) 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) when AST::Members::Alias annotate_alias(full_name, member) when AST::Members::AttrReader, AST::Members::AttrAccessor, AST::Members::AttrWriter annotate_attribute(full_name, member) end end annotate_decls(decl.each_decl.to_a, outer: outer_) end end |
#annotate_constant(const, outer:) ⇒ Object
273 274 275 276 277 278 279 280 |
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 273 def annotate_constant(const, outer:) annots = Annotations.new([]) full_name = resolve_name(const.name, outer: outer) text = doc_for_constant(full_name, tester: annots) replace_comment(const, text) end |
#annotate_decls(decls, outer: []) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 28 def annotate_decls(decls, outer: []) decls.each do |decl| case decl when AST::Declarations::Class, AST::Declarations::Module annotate_class(decl, outer: outer) when AST::Declarations::Constant annotate_constant(decl, outer: outer) end end end |
#annotate_file(path, preserve:) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 16 def annotate_file(path, preserve:) content = path.read() _, _, decls = Parser.parse_signature(content) annotate_decls(decls) path.open("w") do |io| Writer.new(out: io).preserve!(preserve: preserve).write(decls) end end |
#annotate_method(typename, method) ⇒ Object
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 339 |
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 312 def annotate_method(typename, method) 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) end |
#annotations(annots) ⇒ Object
393 394 395 396 397 |
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 393 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:) ⇒ Object
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 213 |
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 187 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:) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 215 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:) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 66 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:) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 87 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(typename, instance_method: nil, singleton_method: nil, tester:) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 145 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(typename, instance_method: nil, singleton_method: nil, tester:) ⇒ Object
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 143 |
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 109 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(subjects, tester:) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 39 def each_part(subjects, tester:) if block_given? subjects.each do |subject, docs| Formatter.each_part(subject.comment) 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: "----") ⇒ Object
299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 299 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) ⇒ Object
372 373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 372 def replace_comment(commented, string) if string if string.empty? commented.instance_variable_set(:@comment, nil) else commented.instance_variable_set( :@comment, AST::Comment.new(location: nil, string: string) ) end end end |
#resolve_doc_source(copy, tester:) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rbs/annotate/rdoc_annotator.rb', line 53 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 |