Module: Rib::Multiline

Extended by:
Plugin
Defined in:
lib/rib/core/multiline.rb

Constant Summary collapse

BINARY_OP =

test those: ruby -e ‘“’ ruby -e ‘{’ ruby -e ‘[’ ruby -e ‘(’ ruby -e ‘/’ ruby -e ‘class C’ ruby -e ‘def f’ ruby -e ‘begin’ ruby -e ‘eval ”1+1.to_i “’ ruby -e ‘eval ”11.to_i -“’ ruby -e ‘eval ”1+1.to_i *“’ ruby -e ‘eval ”1+1.to_i /“’ ruby -e ‘eval ”1+1.to_i &“’ ruby -e ‘eval ”1+1.to_i |“’ ruby -e ‘eval ”1+1.to_i ^“’

%w[tUPLUS tUMINUS tSTAR tREGEXP_BEG tAMPER]
RUBY20_IO =
%w[unary+ unary-  *     tREGEXP_BEG &].
map(&Regexp.method(:escape))
ERROR_REGEXP =
case engine
when 'ruby' ; Regexp.new(
                [ # string or regexp
                  "unterminated \\w+ meets end of file",
                  # mri and rubinius
                  "unexpected (#{BINARY_OP.join('|')}), expecting \\$end",
                  "syntax error, unexpected \\$end"    ,
                  # ruby 2.0
                  "syntax error, unexpected end-of-input",
                  "syntax error, unexpected (#{RUBY20_IO.join('|')}),"
                                                              ].join('|'))
when 'rbx'  ; Regexp.new(
                [ # string or regexp
                  "unterminated \\w+ meets end of file",
                  # mri and rubinius
                  "syntax error, unexpected \\$end"    ,
                  # rubinius
                  "expecting keyword_end"              ,
                  "expecting keyword_then"             ,
                  "expecting keyword_when"             ,
                  "expecting keyword_do_cond"          ,
                  "expecting \\$end"                   ,
                  "expecting '.+'( or '.+')*"          ,
                  "missing '.+' for '.+' started on line \\d+"].join('|'))
when 'jruby'; Regexp.new(
                [ # string or regexp
                  "unterminated \\w+ meets end of file",
                  # jruby
                  "syntax error, unexpected" \
                  " t(UPLUS|UMINUS|STAR|REGEXP_BEG|AMPER)",
                  "syntax error, unexpected end-of-file",
                  # jruby 9.0.4.0
                  "formal argument must be local variable"    ].join('|'))
end

Instance Attribute Summary

Attributes included from Plugin

#disabled

Instance Method Summary collapse

Methods included from Plugin

const_missing, disable, disabled?, enable, enabled?, extended

Instance Method Details

#handle_interruptObject



110
111
112
113
114
115
116
117
118
119
# File 'lib/rib/core/multiline.rb', line 110

def handle_interrupt
  return super if Multiline.disabled?
  if multiline_buffer.empty?
    super
  else
    print "[removed this line: #{multiline_buffer.pop}]"
    super
    throw :rib_multiline
  end
end

#loop_eval(input) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/rib/core/multiline.rb', line 81

def loop_eval input
  return super if Multiline.disabled?
  multiline_buffer << input
  if input =~ /\\\z/
    throw :rib_multiline
  else
    super(multiline_buffer.join("\n"))
  end
end

#loop_onceObject

————— Rib API —————



71
72
73
74
75
76
77
78
79
# File 'lib/rib/core/multiline.rb', line 71

def loop_once
  return super if Multiline.disabled?
  result = nil
  catch(:rib_multiline) do
    result = super
    multiline_buffer.clear
  end
  result
end

#multiline?(err) ⇒ Boolean

————— Plugin API —————

Returns:

  • (Boolean)


123
124
125
# File 'lib/rib/core/multiline.rb', line 123

def multiline? err
  err.is_a?(SyntaxError) && err.message =~ ERROR_REGEXP
end

#multiline_promptObject



127
128
129
# File 'lib/rib/core/multiline.rb', line 127

def multiline_prompt
  config[:multiline_prompt] ||= '| '
end


91
92
93
94
95
96
97
98
# File 'lib/rib/core/multiline.rb', line 91

def print_eval_error err
  return super if Multiline.disabled?
  if multiline?(err)
    throw :rib_multiline
  else
    super
  end
end

#promptObject



100
101
102
103
104
105
106
107
108
# File 'lib/rib/core/multiline.rb', line 100

def prompt
  return super if Multiline.disabled?
  if multiline_buffer.empty?
    super
  else
    mprompt = multiline_prompt[0, config[:prompt].size]
    "#{' '*(config[:prompt].size-mprompt.size)}#{mprompt}"
  end
end