Class: SyntaxTree::CommandCall

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

Overview

CommandCall represents a method call on an object with arguments and no parentheses.

object.method argument

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(receiver:, operator:, message:, arguments:, location:, comments: []) ⇒ CommandCall

Returns a new instance of CommandCall.



3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
# File 'lib/syntax_tree.rb', line 3496

def initialize(
  receiver:,
  operator:,
  message:,
  arguments:,
  location:,
  comments: []
)
  @receiver = receiver
  @operator = operator
  @message = message
  @arguments = arguments
  @location = location
  @comments = comments
end

Instance Attribute Details

#argumentsObject (readonly)

Args

the arguments going along with the message



3488
3489
3490
# File 'lib/syntax_tree.rb', line 3488

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3494
3495
3496
# File 'lib/syntax_tree.rb', line 3494

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



3491
3492
3493
# File 'lib/syntax_tree.rb', line 3491

def location
  @location
end

#messageObject (readonly)

Const | Ident | Op

the message being send



3485
3486
3487
# File 'lib/syntax_tree.rb', line 3485

def message
  @message
end

#operatorObject (readonly)

:“::” | Op | Period

the operator used to send the message



3482
3483
3484
# File 'lib/syntax_tree.rb', line 3482

def operator
  @operator
end

#receiverObject (readonly)

untyped

the receiver of the message



3479
3480
3481
# File 'lib/syntax_tree.rb', line 3479

def receiver
  @receiver
end

Instance Method Details

#child_nodesObject



3512
3513
3514
# File 'lib/syntax_tree.rb', line 3512

def child_nodes
  [receiver, message, arguments]
end

#format(q) ⇒ Object



3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
# File 'lib/syntax_tree.rb', line 3516

def format(q)
  q.group do
    doc = q.format(receiver)
    q.format(CallOperatorFormatter.new(operator))
    q.format(message)
    q.text(" ")

    width = doc_width(doc)
    if width > (q.maxwidth / 2) || width < 2
      q.indent { q.format(arguments) }
    else
      q.nest(width) { q.format(arguments) }
    end
  end
end

#pretty_print(q) ⇒ Object



3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
# File 'lib/syntax_tree.rb', line 3532

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("command_call")

    q.breakable
    q.pp(receiver)

    q.breakable
    q.pp(operator)

    q.breakable
    q.pp(message)

    q.breakable
    q.pp(arguments)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
# File 'lib/syntax_tree.rb', line 3552

def to_json(*opts)
  {
    type: :command_call,
    receiver: receiver,
    op: operator,
    message: message,
    args: arguments,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end