Class: SyntaxTree::CommandCall

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

Overview

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

object.method argument

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

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

Returns a new instance of CommandCall.



3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
# File 'lib/syntax_tree/node.rb', line 3141

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)

nil | Args

the arguments going along with the message



3136
3137
3138
# File 'lib/syntax_tree/node.rb', line 3136

def arguments
  @arguments
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3139
3140
3141
# File 'lib/syntax_tree/node.rb', line 3139

def comments
  @comments
end

#messageObject (readonly)

Const | Ident | Op

the message being send



3133
3134
3135
# File 'lib/syntax_tree/node.rb', line 3133

def message
  @message
end

#operatorObject (readonly)

:“::” | Op | Period

the operator used to send the message



3130
3131
3132
# File 'lib/syntax_tree/node.rb', line 3130

def operator
  @operator
end

#receiverObject (readonly)

untyped

the receiver of the message



3127
3128
3129
# File 'lib/syntax_tree/node.rb', line 3127

def receiver
  @receiver
end

Instance Method Details

#accept(visitor) ⇒ Object



3157
3158
3159
# File 'lib/syntax_tree/node.rb', line 3157

def accept(visitor)
  visitor.visit_command_call(self)
end

#child_nodesObject Also known as: deconstruct



3161
3162
3163
# File 'lib/syntax_tree/node.rb', line 3161

def child_nodes
  [receiver, message, arguments]
end

#deconstruct_keys(_keys) ⇒ Object



3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
# File 'lib/syntax_tree/node.rb', line 3167

def deconstruct_keys(_keys)
  {
    receiver: receiver,
    operator: operator,
    message: message,
    arguments: arguments,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
# File 'lib/syntax_tree/node.rb', line 3178

def format(q)
  q.group do
    doc =
      q.nest(0) do
        q.format(receiver)

        # If there are leading comments on the message then we know we have
        # a newline in the source that is forcing these things apart. In
        # this case we will have to use a trailing operator.
        if message.comments.any?(&:leading?)
          q.format(CallOperatorFormatter.new(operator), stackable: false)
          q.indent do
            q.breakable_empty
            q.format(message)
          end
        else
          q.format(CallOperatorFormatter.new(operator), stackable: false)
          q.format(message)
        end
      end

    # Format the arguments for this command call here. If there are no
    # arguments, then print nothing.
    if arguments
      parts = arguments.parts

      if parts.length == 1 && parts.first.is_a?(IfOp)
        q.if_flat { q.text(" ") }
        q.format(arguments)
      else
        q.text(" ")
        q.nest(argument_alignment(q, doc)) { q.format(arguments) }
      end
    end
  end
end