Class: RBS::AST::Members::MethodDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree/rbs/members.rb

Instance Method Summary collapse

Instance Method Details

#format(q) ⇒ Object

Prints out a method definition, which looks like: def t: (T t) -> void



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/syntax_tree/rbs/members.rb', line 286

def format(q)
  SyntaxTree::RBS::Comment.maybe_format(q, comment)
  SyntaxTree::RBS::Annotations.maybe_format(q, annotations)

  q.group do
    q.text("#{visibility} ") if visibility
    q.text("def ")

    if kind == :singleton
      q.text("self.")
    elsif kind == :singleton_instance
      q.text("self?.")
    end

    q.text(Parser::KEYWORDS.include?(name.to_s) ? "`#{name}`" : name)
    q.text(":")

    if types.length == 1 && !overload?
      q.text(" ")
      SyntaxTree::RBS::MethodSignature.new(types.first).format(q)
    else
      separator =
        lambda do
          q.breakable
          q.text("| ")
        end

      q.group do
        q.indent do
          q.breakable
          q.seplist(types, separator) do |type|
            SyntaxTree::RBS::MethodSignature.new(type).format(q)
          end

          if overload?
            separator.call
            q.text("...")
          end
        end
      end
    end
  end
end

#pretty_print(q) ⇒ Object



330
331
332
333
334
335
336
337
338
339
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
# File 'lib/syntax_tree/rbs/members.rb', line 330

def pretty_print(q)
  q.group(2, "(method-definition", ")") do
    SyntaxTree::RBS::Comment.maybe_pretty_print(q, comment)
    SyntaxTree::RBS::Annotations.maybe_pretty_print(q, annotations)

    q.breakable
    q.text("kind=")
    q.pp(kind)

    q.breakable
    q.text("name=")
    q.pp(name)

    if visibility
      q.breakable
      q.text("visibility=")
      q.pp(visibility)
    end

    if overload?
      q.breakable
      q.text("overload")
    end

    q.breakable
    q.text("types=")
    q.group(2, "[", "]") do
      q.breakable("")
      q.seplist(types) do |type|
        q.pp(SyntaxTree::RBS::MethodSignature.new(type))
      end
      q.breakable("")
    end
  end
end