dietrb 0.5.2 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -53,20 +53,20 @@ namespace :macruby do
53
53
  end
54
54
  end
55
55
 
56
- # begin
57
- # require 'rubygems'
58
- # require 'jeweler'
59
- # require File.expand_path('../lib/irb/version', __FILE__)
60
- # Jeweler::Tasks.new do |gemspec|
61
- # gemspec.name = "dietrb"
62
- # gemspec.version = IRB::VERSION::STRING
63
- # gemspec.summary = gemspec.description = "IRB on a diet, for MacRuby / Ruby 1.9"
64
- # gemspec.email = "eloy.de.enige@gmail.com"
65
- # gemspec.homepage = "http://github.com/alloy/dietrb"
66
- # gemspec.authors = ["Eloy Duran"]
67
- #
68
- # gemspec.required_ruby_version = ::Gem::Requirement.new("~> 1.9")
69
- # gemspec.files.reject! { |file| file =~ /^(extensions|\.gitignore)/ }
70
- # end
71
- # rescue LoadError
72
- # end
56
+ begin
57
+ require 'rubygems'
58
+ require 'jeweler'
59
+ require File.expand_path('../lib/irb/version', __FILE__)
60
+ Jeweler::Tasks.new do |gemspec|
61
+ gemspec.name = "dietrb"
62
+ gemspec.version = IRB::VERSION::STRING
63
+ gemspec.summary = gemspec.description = "IRB on a diet, for MacRuby / Ruby 1.9"
64
+ gemspec.email = "eloy.de.enige@gmail.com"
65
+ gemspec.homepage = "http://github.com/alloy/dietrb"
66
+ gemspec.authors = ["Eloy Duran"]
67
+
68
+ gemspec.required_ruby_version = ::Gem::Requirement.new("~> 1.9")
69
+ gemspec.files.reject! { |file| file =~ /^(extensions|\.gitignore|\w+_prototype\.rb)/ }
70
+ end
71
+ rescue LoadError
72
+ end
data/TODO CHANGED
@@ -1,4 +1,3 @@
1
- * Complete file paths in strings (for require etc).
2
1
  * Write docs for using a as library. Probably right after creating a Cocoa client.
3
2
  * Write a not useless README.
4
3
  * Add specs for irb/driver/socket, and possibly for bin/dietrb.
@@ -6,3 +5,9 @@
6
5
  * Make sure the following formatters work: hirb, and looksee
7
6
  * Make sure the majority of the utils in utility_belt work
8
7
  * Possibly add copy-paste support as an ext
8
+
9
+ = Config
10
+
11
+ Add a global config. Modules such as the formatter can register options with it.
12
+ The modules must always check the global config, unless the option has been
13
+ explicitely overriden on the module instance. See config_prototype.rb
data/dietrb.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{dietrb}
8
- s.version = "0.5.2"
8
+ s.version = "0.6.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Eloy Duran"]
12
- s.date = %q{2010-10-28}
12
+ s.date = %q{2010-11-02}
13
13
  s.default_executable = %q{dietrb}
14
14
  s.description = %q{IRB on a diet, for MacRuby / Ruby 1.9}
15
15
  s.email = %q{eloy.de.enige@gmail.com}
data/lib/irb/context.rb CHANGED
@@ -11,13 +11,14 @@ module IRB
11
11
  class Context
12
12
  IGNORE_RESULT = :irb_ignore_result
13
13
 
14
- attr_reader :object, :binding, :line, :source
14
+ attr_reader :object, :binding, :line, :level, :source
15
15
  attr_accessor :formatter
16
16
 
17
17
  def initialize(object, explicit_binding = nil)
18
18
  @object = object
19
19
  @binding = explicit_binding || object.instance_eval { binding }
20
20
  @line = 1
21
+ @level = 0
21
22
  clear_buffer
22
23
 
23
24
  @last_result_assigner = __evaluate__("_ = nil; proc { |val| _ = val }")
@@ -59,10 +60,17 @@ module IRB
59
60
  # But at code block indentation level 0, `quit' means exit the runloop:
60
61
  #
61
62
  # process_line("quit") # => false
63
+ #
64
+ # If re-indenting the line results in a new line, the reformatted line and
65
+ # prompt are yielded to the optional block. This happens *before* the line
66
+ # is actually processed, so the caller (driver) has the opportunity to
67
+ # update the last printed line.
62
68
  def process_line(line)
63
- @source << line
69
+ prompt_and_line = formatter.reindent_last_line(self) { @source << line }
70
+ yield(*prompt_and_line) if prompt_and_line && block_given?
71
+
64
72
  return false if @source.terminate?
65
-
73
+
66
74
  if @source.syntax_error?
67
75
  output(formatter.syntax_error(@line, @source.syntax_error))
68
76
  @source.pop
@@ -71,22 +79,27 @@ module IRB
71
79
  clear_buffer
72
80
  end
73
81
  @line += 1
82
+ @level = source.level
74
83
 
75
84
  true
76
85
  end
77
86
 
87
+ def driver
88
+ IRB::Driver.current
89
+ end
90
+
78
91
  # Output is directed to the IRB::Driver.current driver’s output if a
79
92
  # current driver is available. Otherwise it’s simply printed to $stdout.
80
93
  def output(string)
81
- if driver = IRB::Driver.current
94
+ if driver = self.driver
82
95
  driver.output.puts(string)
83
96
  else
84
97
  puts(string)
85
98
  end
86
99
  end
87
100
 
88
- def prompt
89
- formatter.prompt(self)
101
+ def prompt(ignore_auto_indent = false)
102
+ formatter.prompt(self, ignore_auto_indent)
90
103
  end
91
104
 
92
105
  def input_line(line)
@@ -1,5 +1,6 @@
1
1
  module IRB
2
2
  def self.deprecated(message, caller)
3
+ return unless $DEBUG
3
4
  caller = caller.first.split(':')[0..-2].join(':')
4
5
  warn "[!] Deprecation warning from #{caller}: #{message}"
5
6
  end
@@ -31,6 +32,9 @@ module IRB
31
32
  when :PROMPT_MODE
32
33
  message = "use `IRB.formatter.prompt = :#{value.downcase}'"
33
34
  IRB.formatter.prompt = "#{value.to_s.downcase}".to_sym
35
+ when :AUTO_INDENT
36
+ message = "use `IRB.formatter.auto_indent = #{value}'"
37
+ IRB.formatter.auto_indent = value
34
38
  when :USE_READLINE
35
39
  message = "for now DietRB only has a readline module"
36
40
  when :SAVE_HISTORY
@@ -40,4 +44,4 @@ module IRB
40
44
  value
41
45
  end
42
46
  end
43
- end
47
+ end
@@ -3,6 +3,11 @@ require 'irb/driver'
3
3
  module IRB
4
4
  module Driver
5
5
  class TTY
6
+ move_one_line_up = "\e[1A"
7
+ move_to_begin_of_line = "\r"
8
+ clear_to_end_of_line = "\e[0K"
9
+ CLEAR_LAST_LINE = move_one_line_up + move_to_begin_of_line + clear_to_end_of_line
10
+
6
11
  attr_reader :input, :output, :context_stack
7
12
 
8
13
  def initialize(input = $stdin, output = $stdout)
@@ -14,7 +19,7 @@ module IRB
14
19
  def context
15
20
  @context_stack.last
16
21
  end
17
-
22
+
18
23
  def readline
19
24
  @output.print(context.prompt)
20
25
  @input.gets
@@ -27,6 +32,17 @@ module IRB
27
32
  context.clear_buffer
28
33
  ""
29
34
  end
35
+
36
+ def update_last_line(prompt, reformatted_line)
37
+ @output.print CLEAR_LAST_LINE
38
+ @output.puts("#{prompt}#{reformatted_line}")
39
+ end
40
+
41
+ def process_input(line)
42
+ context.process_line(line) do |prompt, line|
43
+ update_last_line(prompt, line)
44
+ end
45
+ end
30
46
 
31
47
  # Feeds input into a given context.
32
48
  #
@@ -35,7 +51,8 @@ module IRB
35
51
  def run(context)
36
52
  @context_stack << context
37
53
  while line = consume
38
- break unless context.process_line(line)
54
+ continue = process_input(line)
55
+ break unless continue
39
56
  end
40
57
  ensure
41
58
  @context_stack.pop
@@ -169,7 +169,7 @@ module IRB
169
169
  Ripper.lex(str).map { |_, type, token| colorize_token(type, token) }.join
170
170
  end
171
171
 
172
- def prompt(context)
172
+ def prompt(context, ignore_auto_indent = false, level = nil)
173
173
  colorize_token(:prompt, super)
174
174
  end
175
175
 
@@ -183,4 +183,4 @@ module IRB
183
183
  end
184
184
  end
185
185
 
186
- IRB.formatter = IRB::ColoredFormatter.new
186
+ IRB.formatter = IRB::ColoredFormatter.new
@@ -113,15 +113,28 @@ module IRB
113
113
  end
114
114
 
115
115
  result = if call
116
- if m = (methods || methods_of_object(root))
117
- format_methods(receiver, m, filter)
118
- end
119
- else
120
- match_methods_vars_or_consts_in_scope(root)
121
- end
116
+ if m = (methods || methods_of_object(root))
117
+ format_methods(receiver, m, filter)
118
+ end
119
+ elsif root[TYPE] == :string_literal && root[VALUE][TYPE] == :string_content
120
+ # in the form of: "~/code/
121
+ expand_path(source)
122
+ else
123
+ match_methods_vars_or_consts_in_scope(root)
124
+ end
122
125
  result.sort.uniq if result
123
126
  end
124
127
  end
128
+
129
+ def expand_path(source)
130
+ tokens = Ripper.lex(source)
131
+ path = tokens[0][2]
132
+ string_open = tokens[1][2]
133
+ end_with_slash = path.length > 1 && path.end_with?('/')
134
+ path = File.expand_path(path)
135
+ path << '/' if end_with_slash
136
+ Dir.glob("#{path}*", File::FNM_CASEFOLD).map { |f| "#{string_open}#{f}" }
137
+ end
125
138
 
126
139
  def unwind_callstack(root, stack = [])
127
140
  if root[TYPE] == :call
data/lib/irb/formatter.rb CHANGED
@@ -14,26 +14,35 @@ module IRB
14
14
  SIMPLE_PROMPT = ">> "
15
15
  NO_PROMPT = ""
16
16
  RESULT_PREFIX = "=>"
17
+ INDENTATION = " "
17
18
  SYNTAX_ERROR = "SyntaxError: compile error\n(irb):%d: %s"
18
- SOURCE_ROOT = /^#{File.expand_path('../../../', __FILE__)}/
19
+ SOURCE_ROOT = Regexp.new("^#{File.expand_path('../../../', __FILE__)}")
19
20
 
20
21
  attr_writer :prompt
21
22
  attr_accessor :inspect
23
+ attr_accessor :auto_indent
22
24
  attr_reader :filter_from_backtrace
23
25
 
24
26
  def initialize
25
- @prompt = :default
26
- @inspect = true
27
+ @prompt = :default
28
+ @inspect = true
29
+ @auto_indent = true
27
30
  @filter_from_backtrace = [SOURCE_ROOT]
28
31
  end
32
+
33
+ def indentation(level)
34
+ INDENTATION * level
35
+ end
29
36
 
30
- def prompt(context)
31
- case @prompt
32
- when :default then DEFAULT_PROMPT % [context.object.inspect, context.line, context.source.level]
37
+ def prompt(context, ignore_auto_indent = false, level = nil)
38
+ level ||= context.level
39
+ prompt = case @prompt
40
+ when :default then DEFAULT_PROMPT % [context.object.inspect, context.line, level]
33
41
  when :simple then SIMPLE_PROMPT
34
42
  else
35
43
  NO_PROMPT
36
44
  end
45
+ @auto_indent && !ignore_auto_indent ? "#{prompt}#{indentation(level)}" : prompt
37
46
  end
38
47
 
39
48
  def inspect_object(object)
@@ -42,12 +51,37 @@ module IRB
42
51
  result.strip!
43
52
  result
44
53
  else
45
- address = object.__id__ * 2
46
- address += 0x100000000 if address < 0
47
- "#<#{object.class}:0x%x>" % address
54
+ minimal_inspect_object(object)
48
55
  end
49
56
  end
50
-
57
+
58
+ def minimal_inspect_object(object)
59
+ address = object.__id__ * 2
60
+ address += 0x100000000 if address < 0
61
+ "#<#{object.class}:0x%x>" % address
62
+ end
63
+
64
+ def reindent_last_line(context)
65
+ unless @auto_indent
66
+ yield
67
+ nil
68
+ else
69
+ source = context.source
70
+ old_level = source.level
71
+ yield
72
+ if line = source.buffer[-1]
73
+ # only if the level raises do we use the new value
74
+ level = source.level < old_level ? source.level : old_level
75
+ new_line = "#{indentation(level)}#{line.lstrip}"
76
+ # don't return anything if the new line and level are the same
77
+ unless line == new_line && level == old_level
78
+ source.buffer[-1] = new_line
79
+ [prompt(context, true, level), new_line]
80
+ end
81
+ end
82
+ end
83
+ end
84
+
51
85
  def result(object)
52
86
  "#{RESULT_PREFIX} #{inspect_object(object)}"
53
87
  end
@@ -69,4 +103,4 @@ module IRB
69
103
  end
70
104
  end
71
105
 
72
- IRB.formatter = IRB::Formatter.new
106
+ IRB.formatter = IRB::Formatter.new
data/lib/irb/source.rb CHANGED
@@ -16,7 +16,7 @@ module IRB
16
16
 
17
17
  # Adds a source line to the buffer and flushes the cached reflection.
18
18
  def <<(source)
19
- source = source.strip
19
+ source = source.rstrip
20
20
  unless source.empty?
21
21
  @reflection = nil
22
22
  @buffer << source
data/lib/irb/version.rb CHANGED
@@ -8,8 +8,8 @@ module IRB
8
8
  module VERSION #:nodoc:
9
9
  NAME = 'DietRB'
10
10
  MAJOR = 0
11
- MINOR = 5
12
- TINY = 2
11
+ MINOR = 6
12
+ TINY = 0
13
13
 
14
14
  STRING = [MAJOR, MINOR, TINY].join('.')
15
15
  DESCRIPTION = "#{NAME} (#{STRING})"
data/spec/context_spec.rb CHANGED
@@ -117,28 +117,43 @@ describe "IRB::Context, when receiving input" do
117
117
  @output = setup_current_driver.output
118
118
  @context = IRB::Context.new(main)
119
119
  @context.extend(InputStubMixin)
120
+ @context.formatter.auto_indent = true
121
+ end
122
+
123
+ after do
124
+ @context.formatter.auto_indent = false
120
125
  end
121
126
 
122
127
  it "adds the received code to the source buffer" do
123
128
  @context.process_line("def foo")
124
129
  @context.process_line("p :ok")
125
- @context.source.to_s.should == "def foo\np :ok"
130
+ @context.source.to_s.should == "def foo\n p :ok"
126
131
  end
127
132
 
128
- it "clears the source buffer" do
129
- @context.process_line("def foo")
130
- @context.clear_buffer
131
- @context.source.to_s.should == ""
133
+ it "yields the line if it changed, *after* reindenting" do
134
+ prompt_and_line = nil
135
+ @context.process_line("def foo") { |p, l| prompt_and_line = [p, l] }
136
+ prompt_and_line.should == nil
137
+ @context.process_line("p :ok") { |p, l| prompt_and_line = [p, l] }
138
+ prompt_and_line.should == ["irb(main):002:1> ", " p :ok"]
132
139
  end
133
-
134
- it "increases the current line number" do
140
+
141
+ it "increases the current line number, *after* yielding the new re-indented line" do
135
142
  @context.line.should == 1
136
143
  @context.process_line("def foo")
137
144
  @context.line.should == 2
138
- @context.process_line("p :ok")
145
+ @context.process_line("p :ok")
139
146
  @context.line.should == 3
140
147
  end
141
148
 
149
+ it "increases the current source level, *after* yielding the new re-indented line" do
150
+ @context.level.should == 0
151
+ @context.process_line("def foo")
152
+ @context.level.should == 1
153
+ @context.process_line("end") { |_| @context.level.should == 1 }
154
+ @context.level.should == 0
155
+ end
156
+
142
157
  it "evaluates the buffered source once it's a valid code block" do
143
158
  def @context.evaluate(source); @evaled = source; end
144
159
 
@@ -147,7 +162,7 @@ describe "IRB::Context, when receiving input" do
147
162
  @context.process_line("end; p foo")
148
163
 
149
164
  source = @context.instance_variable_get(:@evaled)
150
- source.to_s.should == "def foo\n:ok\nend; p foo"
165
+ source.to_s.should == "def foo\n :ok\nend; p foo"
151
166
  end
152
167
 
153
168
  it "prints that a syntax error occurred on the last line and reset the buffer to the previous line" do
@@ -41,6 +41,11 @@ describe "IRB::Driver::Readline" do
41
41
  @driver = IRB::Driver::Readline.new(InputStub.new, OutputStub.new)
42
42
  @context = IRB::Context.new(Object.new)
43
43
  @driver.context_stack << @context
44
+ Readline.clear_printed!
45
+ end
46
+
47
+ after do
48
+ @context.formatter.auto_indent = false
44
49
  end
45
50
 
46
51
  it "is a subclass of IRB::Driver::TTY" do
@@ -57,10 +62,25 @@ describe "IRB::Driver::Readline" do
57
62
  end
58
63
 
59
64
  it "reads a line through the Readline module" do
60
- Readline.stub_input "nom nom nom"
65
+ Readline.stub_input("nom nom nom")
61
66
  @driver.readline.should == "nom nom nom"
62
67
  end
63
68
 
69
+ it "prints a prompt" do
70
+ @context.process_line("def foo")
71
+ Readline.stub_input("nom nom nom")
72
+ @driver.readline
73
+ Readline.printed.should == @context.prompt
74
+ end
75
+
76
+ it "prints a prompt with indentation if it's configured" do
77
+ @context.formatter.auto_indent = true
78
+ @context.process_line("def foo")
79
+ Readline.stub_input("nom nom nom")
80
+ @driver.readline
81
+ Readline.printed[-2,2].should == " "
82
+ end
83
+
64
84
  it "tells the Readline module to use the history" do
65
85
  Readline.use_history = false
66
86
  Readline.stub_input "nom nom nom"
@@ -1,21 +1,36 @@
1
1
  require File.expand_path('../../spec_helper', __FILE__)
2
2
  require 'irb/driver/tty'
3
3
 
4
+ main = self
5
+
4
6
  describe "IRB::Driver::TTY" do
5
7
  before do
6
8
  @driver = IRB::Driver::TTY.new(InputStub.new, OutputStub.new)
7
- @context = IRB::Context.new(Object.new)
9
+ @context = IRB::Context.new(main)
8
10
  @driver.context_stack << @context
9
11
  end
10
-
12
+
13
+ after do
14
+ @context.formatter.auto_indent = false
15
+ end
16
+
11
17
  it "prints the prompt and reads a line of input" do
12
- @driver.input.stub_input "calzone"
18
+ @context.process_line("def foo")
19
+ @driver.input.stub_input("calzone")
13
20
  @driver.readline.should == "calzone"
14
21
  @driver.output.printed.should == @context.prompt
15
22
  end
23
+
24
+ it "prints a prompt with indentation if it's configured" do
25
+ @context.formatter.auto_indent = true
26
+ @context.process_line("def foo")
27
+ @driver.input.stub_input("calzone")
28
+ @driver.readline
29
+ @driver.output.printed[-2,2].should == " "
30
+ end
16
31
 
17
32
  it "consumes input" do
18
- @driver.input.stub_input "calzone"
33
+ @driver.input.stub_input("calzone")
19
34
  @driver.consume.should == "calzone"
20
35
  end
21
36
 
@@ -25,6 +40,22 @@ describe "IRB::Driver::TTY" do
25
40
  @driver.consume.should == ""
26
41
  @context.source.to_s.should == ""
27
42
  end
43
+
44
+ it "feeds the input into the context" do
45
+ @driver.process_input("def foo")
46
+ @context.source.to_s.should == "def foo"
47
+ end
48
+
49
+ it "updates the previously printed line on the console, if a change to the input occurs (such as re-indenting)" do
50
+ @context.formatter.auto_indent = true
51
+ @driver.process_input("def foo")
52
+ @driver.process_input("p :ok")
53
+ @driver.process_input(" end")
54
+ @driver.output.printed.strip.should == [
55
+ IRB::Driver::TTY::CLEAR_LAST_LINE + "irb(main):002:1> p :ok",
56
+ IRB::Driver::TTY::CLEAR_LAST_LINE + "irb(main):003:0> end"
57
+ ].join("\n")
58
+ end
28
59
  end
29
60
 
30
61
  describe "IRB::Driver::TTY, when starting the runloop" do
@@ -36,7 +67,7 @@ describe "IRB::Driver::TTY, when starting the runloop" do
36
67
 
37
68
  it "makes the given context the current one, for this driver, for the duration of the runloop" do
38
69
  $from_context = nil
39
- @driver.input.stub_input "$from_context = IRB::Driver.current.context"
70
+ @driver.input.stub_input("$from_context = IRB::Driver.current.context")
40
71
  @driver.run(@context)
41
72
  $from_context.should == @context
42
73
  IRB::Driver.current.context.should == nil
@@ -44,7 +75,7 @@ describe "IRB::Driver::TTY, when starting the runloop" do
44
75
 
45
76
  it "feeds input into a given context" do
46
77
  $from_context = false
47
- @driver.input.stub_input "$from_context = true", "exit"
78
+ @driver.input.stub_input("$from_context = true", "exit")
48
79
  @driver.run(@context)
49
80
  $from_context.should == true
50
81
  end
@@ -52,7 +83,7 @@ describe "IRB::Driver::TTY, when starting the runloop" do
52
83
  it "makes sure there's a global output redirector while running a context" do
53
84
  before = $stdout
54
85
  $from_context = nil
55
- @driver.input.stub_input "$from_context = $stdout", "exit"
86
+ @driver.input.stub_input("$from_context = $stdout", "exit")
56
87
  @driver.run(@context)
57
88
  $from_context.class == IRB::Driver::OutputRedirector
58
89
  $stdout.should == before
@@ -67,4 +98,4 @@ end
67
98
  # irb(o)
68
99
  # IRBRan.should == o
69
100
  # end
70
- # end
101
+ # end
@@ -249,6 +249,13 @@ describe "IRB::Completion" do
249
249
  end
250
250
  end
251
251
 
252
+ it "completes file paths" do
253
+ complete("'/").should == Dir.glob('/*').sort.map { |f| "'#{f}" }
254
+ complete("'#{ROOT}/lib/../Ra").should == ["'#{File.join(ROOT, "Rakefile")}"]
255
+ complete("%{#{ROOT}/li").should == ['LICENSE', 'lib'].map { |f| "%{#{File.join(ROOT, f)}" }
256
+ complete("\"#{ROOT}/lib/").should == ['irb', 'irb.rb'].map { |f| "\"#{File.join(ROOT, 'lib', f)}" }
257
+ end
258
+
252
259
  it "does not crash when trying to complete garbage" do
253
260
  complete("/").should == nil
254
261
  complete("./Rake").should == nil
@@ -6,14 +6,15 @@ describe "IRB::Formatter" do
6
6
  before do
7
7
  @formatter = IRB::Formatter.new
8
8
  @context = IRB::Context.new(main)
9
+ @formatter.auto_indent = false
9
10
  end
10
11
 
11
12
  it "returns a prompt string, displaying line number and code indentation level" do
12
13
  @formatter.prompt(@context).should == "irb(main):001:0> "
13
14
  @context.instance_variable_set(:@line, 23)
14
15
  @formatter.prompt(@context).should == "irb(main):023:0> "
15
- @context.source << "def foo"
16
- @formatter.prompt(@context).should == "irb(main):023:1> "
16
+ @context.process_line("def foo")
17
+ @formatter.prompt(@context).should == "irb(main):024:1> "
17
18
  end
18
19
 
19
20
  it "describes the context's object in the prompt" do
@@ -77,4 +78,61 @@ describe "IRB::Formatter" do
77
78
  @formatter.syntax_error(2, "syntax error, unexpected '}'").should ==
78
79
  "SyntaxError: compile error\n(irb):2: syntax error, unexpected '}'"
79
80
  end
80
- end
81
+
82
+ it "always skips re-indenting the last line in a Source#buffer if `auto_indent' is turned off" do
83
+ @context.source << "class A"
84
+ @formatter.reindent_last_line(@context) { @context.source << "def foo" }.should == nil
85
+ @context.source.buffer.last.should == "def foo"
86
+ end
87
+
88
+ describe "with auto-indentation" do
89
+ before do
90
+ @formatter.auto_indent = true
91
+ end
92
+
93
+ it "returns the whitespace to append to the prompt, based on the given level" do
94
+ @formatter.indentation(0).should == ""
95
+ @formatter.indentation(1).should == " "
96
+ @formatter.indentation(2).should == " "
97
+ end
98
+
99
+ it "pads the prompt, based on the source level" do
100
+ @formatter.prompt(@context).should == "irb(main):001:0> "
101
+ @context.process_line("class A")
102
+ @formatter.prompt(@context).should == "irb(main):002:1> "
103
+ @context.process_line("def foo")
104
+ @formatter.prompt(@context).should == "irb(main):003:2> "
105
+ end
106
+
107
+ it "does not pad the prompt if it's explicitely specified" do
108
+ @context.process_line("class A")
109
+ @formatter.prompt(@context, true).should == "irb(main):002:1> "
110
+ end
111
+
112
+ it "reindents the last line in a Source#buffer after execution of the block, and returns the new line" do
113
+ # the line number in the prompt is irrelevant for this test
114
+ lines = [
115
+ ["\tclass A", ["irb(main):001:0> ", "class A"]],
116
+ ["def foo", ["irb(main):001:1> ", " def foo"]],
117
+ [" end", ["irb(main):001:1> ", " end"]],
118
+ [" end", ["irb(main):001:0> ", "end"]]
119
+ ]
120
+ lines.each do |line, expected_prompt_and_line|
121
+ @formatter.reindent_last_line(@context) do
122
+ @context.source << line
123
+ end.should == expected_prompt_and_line
124
+ end
125
+ @context.source.to_s.should == lines.map { |x| x[1][1] }.join("\n")
126
+ end
127
+
128
+ it "returns nil if the last line was not reindented and the level didn't change" do
129
+ @context.source << "class A"
130
+ @formatter.reindent_last_line(@context) { @context.source << " def foo" }.should == nil
131
+ @formatter.reindent_last_line(@context) { @context.source << " end" }.should_not == nil
132
+ end
133
+
134
+ it "returns nil if the source buffer is empty" do
135
+ @formatter.reindent_last_line(@context) {}.should == nil
136
+ end
137
+ end
138
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dietrb
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 7
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 5
9
- - 2
10
- version: 0.5.2
8
+ - 6
9
+ - 0
10
+ version: 0.6.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Eloy Duran
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-28 00:00:00 +02:00
18
+ date: 2010-11-02 00:00:00 +01:00
19
19
  default_executable: dietrb
20
20
  dependencies: []
21
21