Class: Y2R::AST::Ruby::MethodCall

Inherits:
Node
  • Object
show all
Defined in:
lib/y2r/ast/ruby.rb

Constant Summary

Constants inherited from Node

Node::INDENT_STEP

Instance Method Summary collapse

Methods inherited from Node

#ensure_separated, #has_comment?, #hates_to_stand_alone?, #single_line_width, #to_ruby

Instance Method Details

#ends_with_comment?Boolean

Returns:

  • (Boolean)


1213
1214
1215
1216
1217
1218
1219
# File 'lib/y2r/ast/ruby.rb', line 1213

def ends_with_comment?
  if parens
    comment_after || (block && block.ends_with_comment?)
  else
    comment_after   # Ignore deep comments, like we do for statements.
  end
end

#pass_trailer?Boolean

Returns:

  • (Boolean)


1221
1222
1223
1224
1225
1226
1227
# File 'lib/y2r/ast/ruby.rb', line 1221

def pass_trailer?
  if parens
    block
  else
    false    # Ignore deep comments, like we do for statements.
  end
end

#priorityObject



1201
1202
1203
# File 'lib/y2r/ast/ruby.rb', line 1201

def priority
  parens ? Priority::ATOMIC : Priority::NONE
end

#single_line_width_base(context) ⇒ Object



1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
# File 'lib/y2r/ast/ruby.rb', line 1183

def single_line_width_base(context)
  if !has_line_breaking_comment? && !args_have_comments?
    receiver_context = context.with_priority(Priority::ATOMIC)
    receiver_width   = if receiver
      receiver.single_line_width(receiver_context) + 1
    else
      0
    end

    args_context = context.with_priority(Priority::NONE)
    args_width   = args_single_line_width(args_context)

    receiver_width + name.size + args_width
  else
    Float::INFINITY
  end
end

#starts_with_comment?Boolean

Returns:

  • (Boolean)


1205
1206
1207
1208
1209
1210
1211
# File 'lib/y2r/ast/ruby.rb', line 1205

def starts_with_comment?
  if parens
    comment_before || (receiver && receiver.starts_with_comment?)
  else
    comment_before   # Ignore deep comments, like we do for statements.
  end
end

#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