Autocompletion for first word (not using method names) #12

Closed
ibc opened this Issue Mar 2, 2011 · 4 comments

Comments

Projects
None yet
2 participants
@ibc

ibc commented Mar 2, 2011

Hi, I'm trying to figure out how to make autocompletion for the first word based on a list of stings, and also autocompletion of the first parameter for each word. Let me explain the issue I get.

First, this simple code:

#!/usr/bin/ruby

require "readline"
require "bond"

Bond.start
Bond.complete(:method => "hello") { %w{world universe} }
Bond.complete(:method => "bye")   { %w{forever} }

while line = Readline.readline(">>> ")
  puts "*** introduced line: #{line}"
end

Manually writting the method name and pressing TAB to get the arguments work well:

>>> hello [TAB]
universe world

>>> bye [TAB]
forever 

But just writting half a method doesn't work:

>> he[TAB]
(nothing)

Ok, this makes sense as there is not a method "hello". But I don't want "hello" to be a real method but a custom string from a list/array. So I add Readline completion:

#!/usr/bin/ruby

require "readline"
require "bond"

Bond.start

commands = %w{hello bye}.sort

Readline.completion_append_character = " "
Readline.completion_proc = proc do |s|
  commands.grep( /^#{Regexp.escape(s)}/ )
end

Bond.complete(:method => "hello") { %w{world universe} }
Bond.complete(:method => "bye")   { %w{forever} }

while line = Readline.readline(">>> ")
  puts "*** introduced line: #{line}"
end

In this case using TAB to complete the first word (hello or bye) works:

>>> he[TAB]
hello

but autocompletion of the argument fails:

>>> hello [TAB]
bye hello

If I move "Bond.start" after "Readline.completion_xxxxx" then it works as in first example code (this is, no autocompletion for the first word as there are no methods named "hello" and "bye").
I expect that "Bond.start" changes Readline autocompletion settings.

So my question is, how to make Bond to do autocompletion on the first word searching such words in a custom array I provide (rather than searching through existing methods)?

Thanks a lot.

@cldwalker

This comment has been minimized.

Show comment Hide comment
@cldwalker

cldwalker Mar 4, 2011

Owner

Nice explanation of your problem. Fortunately, there's a quick solution. Autocompleting your first word falls on using bond's default mission. For your example replace Bond.start with:

Bond.start :default_mission => lambda { %w{hello bye} }
Owner

cldwalker commented Mar 4, 2011

Nice explanation of your problem. Fortunately, there's a quick solution. Autocompleting your first word falls on using bond's default mission. For your example replace Bond.start with:

Bond.start :default_mission => lambda { %w{hello bye} }
@ibc

This comment has been minimized.

Show comment Hide comment
@ibc

ibc Mar 4, 2011

Oh, great solution, thanks :)

ibc commented Mar 4, 2011

Oh, great solution, thanks :)

@ibc

This comment has been minimized.

Show comment Hide comment
@ibc

ibc Mar 4, 2011

Just a comment: your suggestino fails un der Ruby 1.9 when pressing TAB for autocompletion:

Bond Error: Failed during completion action with 'wrong number of arguments (1 for 0)'.  Completion Info: Matches completion with condition nil.

But it works if I use proc rather than lambda. AFAIR this is because in Ruby 1.9 proc behaves as lambda in Ruby 1.8, while lambda in 1.9 has a different usage.

ibc commented Mar 4, 2011

Just a comment: your suggestino fails un der Ruby 1.9 when pressing TAB for autocompletion:

Bond Error: Failed during completion action with 'wrong number of arguments (1 for 0)'.  Completion Info: Matches completion with condition nil.

But it works if I use proc rather than lambda. AFAIR this is because in Ruby 1.9 proc behaves as lambda in Ruby 1.8, while lambda in 1.9 has a different usage.

@cldwalker

This comment has been minimized.

Show comment Hide comment
@cldwalker

cldwalker Mar 4, 2011

Owner

Good call (no pun intended).

Owner

cldwalker commented Mar 4, 2011

Good call (no pun intended).

This issue was closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment