Method: Y2R::AST::Ruby::MethodCall#to_ruby_base

Defined in:
lib/y2r/ast/ruby.rb

#to_ruby_base(context) ⇒ Object



1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
# File 'lib/y2r/ast/ruby.rb', line 1140

def to_ruby_base(context)
  # The algorithm for deciding whether the call should be split into
  # multile lines is based on seeing if the arguments fit into the
  # current line. That means we ignore the block part. This could lead
  # to some cases where a block starts beyond the right margin (when the
  # arguments fit, but just barely). I decided to ignore these cases, as
  # their impact on readability is minimal and handling them would
  # complicate already complex code.

  receiver_context = context.with_trailer(".").with_priority(Priority::ATOMIC)
  receiver_code    = receiver ? "#{receiver.to_ruby(receiver_context)}" : ""

  if has_line_breaking_comment?
    context = context.indented(INDENT_STEP)
  end

  args_shift   = receiver_code.size + name.size
  args_context = context.
    shifted(args_shift).
    without_trailer.
    with_priority(Priority::NONE)
  args_code    = emit_args(context, args_context)

  if Code.multi_line?(args_code)
    block_context = context.shifted(1).with_priority(Priority::NONE)
  else
    block_shift   = args_shift + args_code.size
    block_context = context.
      shifted(block_shift).
      with_priority(Priority::NONE)
  end
  block_code    = block ? " #{block.to_ruby(block_context)}" : ""

  if !has_line_breaking_comment?
    "#{receiver_code}#{name}#{args_code}#{block_code}"
  else
    combine do |parts|
      parts << receiver_code
      parts << indent("#{name}#{args_code}#{block_code}")
    end
  end
end