bond 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,9 @@
1
+ == 0.1.4
2
+ * Added Bond.recomplete to make redefinition of completion missions easy.
3
+ * Added flag to readline extension building so that installing bond doesn't fail for
4
+ users without readline.
5
+ * bond/completion allows require to autocomplete gems and within gems.
6
+
1
7
  == 0.1.3
2
8
  * Fixing deployment mistake
3
9
 
@@ -9,6 +15,7 @@
9
15
  * Fixed irb's completion inconsistencies
10
16
  * Added ability to specify :default_search for Bond.debrief
11
17
  * Added placement of completions with :place for Bond.complete
18
+
12
19
  == 0.1.1
13
20
  * Added Bond.spy to debug completions
14
21
  * Fixed object completion failing in irbrc
data/README.rdoc CHANGED
@@ -190,6 +190,8 @@ this Readline enhancement to ruby.
190
190
 
191
191
  == Links
192
192
  * http://tagaholic.me/2009/07/16/bond-from-irb-with-completion-love.html
193
+ * http://tagaholic.me/2009/07/22/better-irb-completion-with-bond.html
194
+ * http://tagaholic.me/2009/07/23/mini-irb-and-mini-script-console.html
193
195
 
194
196
  == Todo
195
197
  * Allow usage of prefined Bond::Actions in action procs.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
- :major: 0
3
2
  :minor: 1
4
- :patch: 3
3
+ :patch: 4
4
+ :major: 0
@@ -1,3 +1,4 @@
1
1
  require "mkmf"
2
+ have_header 'readline/readline.h'
2
3
  dir_config 'readline_line_buffer'
3
- create_makefile 'readline_line_buffer'
4
+ create_makefile 'readline_line_buffer'
@@ -2,6 +2,7 @@
2
2
  Copyright (C) 1997-2001 Shugo Maeda */
3
3
  /* body of line_buffer() from irb enhancements at http://www.creo.hu/~csaba/ruby/ */
4
4
 
5
+ #ifdef HAVE_READLINE_READLINE_H
5
6
  #include "ruby.h"
6
7
  #include <errno.h>
7
8
  #include <stdio.h>
@@ -20,3 +21,4 @@ void Init_readline_line_buffer() {
20
21
  c = rb_const_get(c, rb_intern("Readline"));
21
22
  rb_define_singleton_method(c, "line_buffer", (VALUE(*)(ANYARGS))line_buffer, -1);
22
23
  }
24
+ #endif
data/lib/bond.rb CHANGED
@@ -54,17 +54,23 @@ module Bond
54
54
  # Bond.complete(:object=>ActiveRecord::Base) {|input| input.object.class.instance_methods(false) }
55
55
  # Bond.complete(:method=>'you', :search=>proc {|input, list| list.grep(/#{input}/i)} ) {|input| %w{Only Live Twice} }
56
56
  def complete(options={}, &block)
57
- agent.complete(options, &block)
58
- true
59
- rescue InvalidMissionError
60
- $stderr.puts "Invalid mission given. Mission needs an action and a condition."
61
- false
62
- rescue InvalidMissionActionError
63
- $stderr.puts "Invalid mission action given. Make sure action responds to :call or refers to a predefined action that does."
64
- false
65
- rescue
66
- $stderr.puts "Mission setup failed with:", $!
67
- false
57
+ if (result = agent.complete(options, &block)).is_a?(String)
58
+ $stderr.puts result
59
+ false
60
+ else
61
+ true
62
+ end
63
+ end
64
+
65
+ # Redefines an existing completion mission. Takes same options as Bond.complete. This is useful when wanting to override existing
66
+ # completions or when wanting to toggle between multiple definitions or modes of a completion.
67
+ def recomplete(options={}, &block)
68
+ if (result = agent.recomplete(options, &block)).is_a?(String)
69
+ $stderr.puts result
70
+ false
71
+ else
72
+ true
73
+ end
68
74
  end
69
75
 
70
76
  # Resets Bond so that next time Bond.complete is called, a new set of completion missions are created. This does not
data/lib/bond/actions.rb CHANGED
@@ -11,7 +11,7 @@ module Bond
11
11
  def current_eval(string)
12
12
  Missions::ObjectMission.current_eval(string)
13
13
  rescue Exception
14
- nil
14
+ []
15
15
  end
16
16
 
17
17
  # Completes backtick and Kernel#system with shell commands available in ENV['PATH']
@@ -50,7 +50,7 @@ module Bond
50
50
  File.directory?(File.join(dir,f)) ? f+fs : f } }
51
51
  input_regex = /^#{Regexp.escape(input)}/
52
52
 
53
- $:.select {|e| File.directory?(e)}.inject([]) do |t,dir|
53
+ require_paths.select {|e| File.directory?(e)}.inject([]) do |t,dir|
54
54
  if input[/.$/] == fs && File.directory?(File.join(dir,input))
55
55
  matches = dir_entries.call(File.join(dir,input)).select {|e| e =~ extensions_regex }.map {|e| input + e }
56
56
  else
@@ -61,5 +61,9 @@ module Bond
61
61
  t += matches
62
62
  end
63
63
  end
64
+
65
+ def require_paths
66
+ $: + Gem.path.map {|e| Dir["#{e}/gems/*/lib"] }.flatten.uniq rescue $:
67
+ end
64
68
  end
65
69
  end
data/lib/bond/agent.rb CHANGED
@@ -14,10 +14,38 @@ module Bond
14
14
  @missions = []
15
15
  end
16
16
 
17
- def complete(options={}, &block) #:nodoc:
17
+ def complete(options={}, &block)
18
+ if (mission = create_mission(options, &block)).is_a?(Mission)
19
+ mission.place.is_a?(Integer) ? @missions.insert(mission.place - 1, mission).compact! : @missions << mission
20
+ sort_last_missions
21
+ end
22
+ mission
23
+ end
24
+
25
+ def create_mission(options, &block) #:nodoc:
18
26
  options[:action] ||= block
19
- mission = Mission.create(options.merge(:eval_binding=>@eval_binding))
20
- mission.place.is_a?(Integer) ? @missions.insert(mission.place - 1, mission).compact! : @missions << mission
27
+ Mission.create(options.merge(:eval_binding=>@eval_binding))
28
+ rescue InvalidMissionError
29
+ "Invalid mission given. Mission needs an action and a condition."
30
+ rescue InvalidMissionActionError
31
+ "Invalid mission action given. Make sure action responds to :call or refers to a predefined action that does."
32
+ rescue
33
+ "Mission setup failed with:\n#{$!}"
34
+ end
35
+
36
+ def recomplete(options={}, &block)
37
+ if (mission = create_mission(options, &block)).is_a?(Mission)
38
+ if (existing_mission = @missions.find {|e| e.unique_id == mission.unique_id })
39
+ @missions[@missions.index(existing_mission)] = mission
40
+ sort_last_missions
41
+ else
42
+ return "No existing mission found to recomplete."
43
+ end
44
+ end
45
+ mission
46
+ end
47
+
48
+ def sort_last_missions #:nodoc:
21
49
  @missions.replace @missions.partition {|e| e.place != :last }.flatten
22
50
  end
23
51
 
@@ -21,7 +21,7 @@ Bond.complete(:on=>/(\$[^\s.]*)$/, :search=>false) {|e|
21
21
  global_variables.grep(/^#{Regexp.escape(e.matched[1])}/)
22
22
  }
23
23
  # Completes files
24
- Bond.complete(:on=>/\s+["']([^'"]*)$/, :search=>false, :action=>:quoted_files, :place=>:last)
24
+ Bond.complete(:on=>/[\s(]["']([^'"]*)$/, :search=>false, :action=>:quoted_files, :place=>:last)
25
25
  # Completes any object's methods
26
26
  Bond.complete(:object=>"Object", :place=>:last)
27
27
  # Completes method completion anywhere in the line
data/lib/bond/mission.rb CHANGED
@@ -90,6 +90,10 @@ module Bond
90
90
  end
91
91
 
92
92
  #:stopdoc:
93
+ def unique_id
94
+ @condition
95
+ end
96
+
93
97
  def set_input(input, match)
94
98
  @input = input[/\S+$/]
95
99
  end
@@ -8,6 +8,10 @@ class Bond::Missions::MethodMission < Bond::Mission
8
8
  super
9
9
  end
10
10
 
11
+ def unique_id #:nodoc:
12
+ @method_condition.is_a?(Regexp) ? @method_condition : @method_condition.to_s
13
+ end
14
+
11
15
  def set_input(input, match) #:nodoc:
12
16
  @input = match[-1]
13
17
  end
@@ -13,6 +13,10 @@ class Bond::Missions::ObjectMission < Bond::Mission
13
13
  super
14
14
  end
15
15
 
16
+ def unique_id
17
+ "#{@object_condition.inspect}+#{@condition.inspect}"
18
+ end
19
+
16
20
  def handle_valid_match(input)
17
21
  if (match = super)
18
22
  begin
data/test/agent_test.rb CHANGED
@@ -42,6 +42,61 @@ class Bond::AgentTest < Test::Unit::TestCase
42
42
  end
43
43
  end
44
44
 
45
+ context "complete" do
46
+ test "prints error if no action given" do
47
+ capture_stderr { complete :on=>/blah/ }.should =~ /Invalid mission/
48
+ end
49
+
50
+ test "prints error if no condition given" do
51
+ capture_stderr { complete {|e| []} }.should =~ /Invalid mission/
52
+ end
53
+
54
+ test "prints error if invalid condition given" do
55
+ capture_stderr { complete(:on=>'blah') {|e| []} }.should =~ /Invalid mission/
56
+ end
57
+
58
+ test "prints error if invalid symbol action given" do
59
+ capture_stderr { complete(:on=>/blah/, :action=>:bling) }.should =~ /Invalid mission action/
60
+ end
61
+
62
+ test "prints error if setting mission fails unpredictably" do
63
+ Bond::Mission.expects(:create).raises(RuntimeError)
64
+ capture_stderr { complete(:on=>/blah/) {|e| [] } }.should =~ /Mission setup failed/
65
+ end
66
+ end
67
+
68
+ context "recomplete" do
69
+ before(:each) {|e| Bond.agent.reset }
70
+
71
+ test "recompletes a mission" do
72
+ Bond.complete(:on=>/man/) { %w{1 2 3}}
73
+ Bond.recomplete(:on=>/man/) { %w{4 5 6}}
74
+ tabtab('man ').should == %w{4 5 6}
75
+ end
76
+
77
+ test "recompletes a method mission" do
78
+ Bond.complete(:method=>'blah') { %w{1 2 3}}
79
+ Bond.recomplete(:method=>'blah') { %w{4 5 6}}
80
+ tabtab('blah ').should == %w{4 5 6}
81
+ end
82
+
83
+ test "recompletes an object mission" do
84
+ Bond.complete(:object=>'String') { %w{1 2 3}}
85
+ Bond.recomplete(:object=>'String') { %w{4 5 6}}
86
+ tabtab('"blah".').should == %w{.4 .5 .6}
87
+ end
88
+
89
+ test "prints error if no existing mission" do
90
+ Bond.complete(:object=>'String') { %w{1 2 3}}
91
+ capture_stderr { Bond.recomplete(:object=>'Array') { %w{4 5 6}}}.should =~ /No existing mission/
92
+ tabtab('[].').should == []
93
+ end
94
+
95
+ test "prints error if invalid condition given" do
96
+ capture_stderr { Bond.recomplete}.should =~ /Invalid mission/
97
+ end
98
+ end
99
+
45
100
  context "spy" do
46
101
  before(:all) {
47
102
  Bond.reset; complete(:on=>/end$/) { [] }; complete(:method=>'the') { %w{spy who loved me} }
data/test/bond_test.rb CHANGED
@@ -31,29 +31,6 @@ class BondTest < Test::Unit::TestCase
31
31
  end
32
32
  end
33
33
 
34
- context "complete" do
35
- test "prints error if no action given" do
36
- capture_stderr { complete :on=>/blah/ }.should =~ /Invalid mission/
37
- end
38
-
39
- test "prints error if no condition given" do
40
- capture_stderr { complete {|e| []} }.should =~ /Invalid mission/
41
- end
42
-
43
- test "prints error if invalid condition given" do
44
- capture_stderr { complete(:on=>'blah') {|e| []} }.should =~ /Invalid mission/
45
- end
46
-
47
- test "prints error if invalid symbol action given" do
48
- capture_stderr { complete(:on=>/blah/, :action=>:bling) }.should =~ /Invalid mission action/
49
- end
50
-
51
- test "prints error if setting mission fails unpredictably" do
52
- Bond.agent.expects(:complete).raises(ArgumentError)
53
- capture_stderr { complete(:on=>/blah/) {|e| [] } }.should =~ /Mission setup failed/
54
- end
55
- end
56
-
57
34
  test "reset clears existing missions" do
58
35
  complete(:on=>/blah/) {[]}
59
36
  Bond.agent.missions.size.should_not == 0
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bond
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Horner
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-07-22 00:00:00 -04:00
12
+ date: 2009-07-30 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -72,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
72
  requirements: []
73
73
 
74
74
  rubyforge_project: tagaholic
75
- rubygems_version: 1.3.2
75
+ rubygems_version: 1.3.5
76
76
  signing_key:
77
77
  specification_version: 3
78
78
  summary: "Mission: Easy custom autocompletion for arguments, methods and beyond. Accomplished for irb and any other readline-like console environments."