Module: Rib::Multiline

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

Overview

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 \\$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"]      .join('|'))
end

Instance Attribute Summary

Attributes included from Plugin

#disabled

Instance Method Summary collapse

Methods included from Plugin

disable, disabled?, enable, enabled?, extended

Instance Method Details

#handle_interruptObject



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

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



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

def loop_eval input
  return super if Multiline.disabled?
  multiline_buffer << input
  super(multiline_buffer.join("\n"))
end

#loop_onceObject

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



65
66
67
68
69
70
71
72
73
# File 'lib/rib/core/multiline.rb', line 65

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)


113
114
115
# File 'lib/rib/core/multiline.rb', line 113

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

#multiline_promptObject



117
118
119
# File 'lib/rib/core/multiline.rb', line 117

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


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

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

#promptObject



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

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