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 end\\-of\\-file",
                  # jruby 9.0.4.0
                  "formal argument must be local variable",
                  # jruby 9.2.0.0
                  "syntax error, unexpected (#{RUBY20_IO.join('|')})",
                  "syntax error, unexpected '\\W'"
                                                              ].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



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

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



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

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 —————



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

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)


125
126
127
# File 'lib/rib/core/multiline.rb', line 125

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

#multiline_promptObject



129
130
131
# File 'lib/rib/core/multiline.rb', line 129

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


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

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

#promptObject



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

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