From bc94ec8f4c1b903236171c8b3cd062e3684514b8 Mon Sep 17 00:00:00 2001 From: nobu Date: Mon, 17 Nov 2003 01:50:31 +0000 Subject: [PATCH] * lib/optparse.rb (OptionParser::Completion::complete): allow least common completion for three or more candidates. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4972 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ lib/optparse.rb | 15 +++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 63d3cb00e0..07eb85e5f2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Mon Nov 17 10:50:27 2003 Nobuyoshi Nakada + + * lib/optparse.rb (OptionParser::Completion::complete): allow least + common completion for three or more candidates. + Mon Nov 17 09:41:38 2003 Nobuyoshi Nakada * lib/test/unit/ui/tk/testrunner.rb, diff --git a/lib/optparse.rb b/lib/optparse.rb index c00b152a21..50da8b329d 100644 --- a/lib/optparse.rb +++ b/lib/optparse.rb @@ -83,6 +83,7 @@ Keyword completion module. pat ||= Regexp.new('\A' + Regexp.quote(key).gsub(/\w+(?=.)/, '\&\w*'), ignore_case?) canon, sw, k, v, cn = nil + candidates = [] each do |k, *v| (if Regexp === k kn = nil @@ -92,14 +93,20 @@ Keyword completion module. pat === kn end) or next v << k if v.empty? - if !canon - canon, sw, cn = k, v, kn - elsif sw != v + candidates << [k, v, kn] + end + candidates = candidates.sort_by {|k, v, kn| kn.size} + if candidates.size == 1 + canon, sw, * = candidates[0] + elsif candidates.size > 1 + canon, sw, cn = candidates.shift + candidates.each do |k, v, kn| + next if sw == v if String === cn and String === kn if cn.rindex(kn, 0) canon, sw, cn = k, v, kn next - elsif kn.rindex(canon, 0) + elsif kn.rindex(cn, 0) next end end