Class: Y2R::AST::Ruby::BinaryOperator

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

Constant Summary collapse

OPS_TO_PRIORITIES =
{
  "**"  => Priority::POWER,
  "*"   => Priority::MULTIPLY,
  "/"   => Priority::MULTIPLY,
  "%"   => Priority::MULTIPLY,
  "+"   => Priority::ADD,
  "-"   => Priority::ADD,
  "<<"  => Priority::SHIFT,
  ">>"  => Priority::SHIFT,
  "&"   => Priority::BITWISE_AND,
  "|"   => Priority::BITWISE_OR,
  "^"   => Priority::BITWISE_OR,
  ">"   => Priority::COMPARE,
  ">="  => Priority::COMPARE,
  "<"   => Priority::COMPARE,
  "<="  => Priority::COMPARE,
  "<=>" => Priority::EQUAL,
  "=="  => Priority::EQUAL,
  "===" => Priority::EQUAL,
  "!="  => Priority::EQUAL,
  "=~"  => Priority::EQUAL,
  "!~"  => Priority::EQUAL,
  "&&"  => Priority::LOGICAL_AND,
  "||"  => Priority::LOGICAL_OR,
}

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)


1003
1004
1005
# File 'lib/y2r/ast/ruby.rb', line 1003

def ends_with_comment?
  comment_after || rhs.ends_with_comment?
end

#pass_trailer?Boolean

Returns:

  • (Boolean)


1007
1008
1009
# File 'lib/y2r/ast/ruby.rb', line 1007

def pass_trailer?
  true
end

#priorityObject



995
996
997
# File 'lib/y2r/ast/ruby.rb', line 995

def priority
  OPS_TO_PRIORITIES[op]
end

#single_line_width_base(context) ⇒ Object



982
983
984
985
986
987
988
989
990
991
992
993
# File 'lib/y2r/ast/ruby.rb', line 982

def single_line_width_base(context)
  if !has_line_breaking_comment?
    inner_context = context.with_priority(priority)

    lhs_width = lhs.single_line_width(inner_context)
    rhs_width = rhs.single_line_width(inner_context)

    lhs_width + 1 + op.size + 1 + rhs_width
  else
    Float::INFINITY
  end
end

#starts_with_comment?Boolean

Returns:

  • (Boolean)


999
1000
1001
# File 'lib/y2r/ast/ruby.rb', line 999

def starts_with_comment?
  comment_before || lhs.starts_with_comment?
end

#to_ruby_base(context) ⇒ Object



973
974
975
976
977
978
979
980
# File 'lib/y2r/ast/ruby.rb', line 973

def to_ruby_base(context)
  if (fits_current_line?(context) || rhs.hates_to_stand_alone?) &&
      !has_line_breaking_comment?
    to_ruby_base_single_line(context)
  else
    to_ruby_base_multi_line(context)
  end
end