Module: Ripl::MultiLine

Defined in:
lib/ripl/multi_line/live_error.rb,
lib/ripl/multi_line.rb,
lib/ripl/multi_line/version.rb

Overview

# # # This multi-line implementation uses catches the syntax errors that are yielded by unfinsihed statements

works on:         2.0  1.9  1.8  jruby  rbx
analyze features: [:literal, :string]
                  [:literal, :regexp]
                  [:literal, :array]   (mri only)
                  [:literal, :hash]    (mri only)
                  [:statement]
                  [:forced]
notes:            rbx support buggy (depends on rubinius error messages)

Defined Under Namespace

Modules: ErrorCheck, Irb, LiveError, Ripper, RubyParser

Constant Summary collapse

VERSION =
'0.3.1'

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.engineObject

Returns the value of attribute engine.



7
8
9
# File 'lib/ripl/multi_line.rb', line 7

def engine
  @engine
end

Instance Method Details

#before_loopObject



10
11
12
13
14
15
16
17
18
19
# File 'lib/ripl/multi_line.rb', line 10

def before_loop
  @buffer = @buffer_info = nil
  # include CamelCased implementation
  require File.join( 'ripl', 'multi_line', config[:multi_line_engine].to_s )
  Ripl::MultiLine.engine = Ripl::MultiLine.const_get(
    config[:multi_line_engine].to_s.gsub(/(^|_)(\w)/){ $2.capitalize }
  )
  Ripl::Shell.include Ripl::MultiLine.engine
  super
end

#handle_interruptObject

remove last line from buffer MAYBE: terminal rewriting



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ripl/multi_line.rb', line 94

def handle_interrupt
  if @buffer
    @buffer.pop; @buffer_info.pop; history.pop
    if @buffer.empty?
      @buffer = @buffer_info = nil
      print '[buffer empty]'
      return super
    else
      puts "[previous line removed|#{@buffer.size}]"
      throw :multiline
    end
  else
    super
  end
end

#handle_multiline(type = [:statement]) ⇒ Object

MAYBE: add second arg for specific information



76
77
78
79
80
81
82
# File 'lib/ripl/multi_line.rb', line 76

def handle_multiline(type = [:statement]) # MAYBE: add second arg for specific information
  @buffer ||= []
  @buffer_info ||= []
  @buffer << @input
  @buffer_info << type
  throw :multiline
end

#loop_eval(input) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/ripl/multi_line.rb', line 84

def loop_eval(input)
  eval_string = if @buffer then @buffer*"\n" + "\n" + input else input end
  if type = multiline?(eval_string)
    handle_multiline(type)
  end
  super eval_string
end

#loop_onceObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ripl/multi_line.rb', line 34

def loop_once
  catch(:multiline) do
    super
    if config[:multi_line_history] && @buffer && @input
      (@buffer.size + 1).times{ history.pop }

      if config[:multi_line_history] == :compact
        history_entry = ''
        @buffer.zip(@buffer_info){ |str, type|
          history_entry << str
          history_entry << case
          when !type.is_a?(Array)
            "\n" # fallback to :block for unsure
          when type[0] == :statement
            '; '
          when type[0] == :literal && ( type[1] == :string || type[1] == :regexp )
            '\n'
          else
            ''
          end
        }
        history_entry << @input
        history << history_entry
      else # true or :block
        history << (@buffer << @input).join("\n")
      end
    end

    @buffer = @buffer_info = nil
  end
end

#multiline?(eval_string) ⇒ Boolean

This method is overwritten by a multi-line implementation in lib/multi_line/*.rb

It should return a true value for a string that is unfinished and a false one
 for complete expressions, which should get evaluated.
It's also possible (and encouraged) to return an array of symbols describing
 what's the reason for continuing the expression (this is used in the :compact
 history and gets passed to the prompt proc.

Returns:

  • (Boolean)


72
73
74
# File 'lib/ripl/multi_line.rb', line 72

def multiline?(eval_string)
  false
end

#promptObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ripl/multi_line.rb', line 21

def prompt
  if @buffer
    config[:multi_line_prompt].respond_to?(:call) ? 
        config[:multi_line_prompt].call( *@buffer_info[-1] ) :
        config[:multi_line_prompt]
  else
    super
  end
rescue StandardError, SyntaxError
  warn "ripl: Error while creating prompt:\n"+ format_error($!)
  Ripl::Shell::OPTIONS[:prompt]
end