Skip to content
Myr

Implement smart-case match (#12)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Junegunn Choi · committed by Junegunn Choi · parent b30f21e · Browse files · 3 files+61 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+61 added−10 removed

  • tests:+47 added−3 removed
  • other:+14 added−7 removed

3 files+61 added−10 removed

modified:test/test_fzf.rb+47 added-3 removed
7 unmodified lines
89class TestFZF < MiniTest::Unit::TestCase10 def test_default_options11 fzf = FZF.new []12 assert_equal 1000, fzf.sort12 assert_equal 1000, fzf.sort13 assert_equal false, fzf.multi14 assert_equal true, fzf.color15 assert_equal Regexp::IGNORECASE, fzf.rxflag14 assert_equal true, fzf.color15 assert_equal nil, fzf.rxflag1617 begin18 ENV['FZF_DEFAULT_SORT'] = '1500'19 fzf = FZF.new []
131 unmodified lines
151152 # TODO : partial_cache153 end154155 def test_fuzzy_matcher_rxflag156 assert_equal nil, FZF::FuzzyMatcher.new(nil).rxflag157 assert_equal 0, FZF::FuzzyMatcher.new(0).rxflag158 assert_equal 1, FZF::FuzzyMatcher.new(1).rxflag159160 assert_equal 1, FZF::FuzzyMatcher.new(nil).rxflag_for('abc')161 assert_equal 0, FZF::FuzzyMatcher.new(nil).rxflag_for('Abc')162 assert_equal 0, FZF::FuzzyMatcher.new(0).rxflag_for('abc')163 assert_equal 0, FZF::FuzzyMatcher.new(0).rxflag_for('Abc')164 assert_equal 1, FZF::FuzzyMatcher.new(1).rxflag_for('abc')165 assert_equal 1, FZF::FuzzyMatcher.new(1).rxflag_for('Abc')166 end167168 def test_fuzzy_matcher_case_sensitive169 # Smart-case match (Uppercase found)170 assert_equal [['Fruit', [[0, 5]]]],171 FZF::FuzzyMatcher.new(nil).match(%w[Fruit Grapefruit], 'Fruit', '', '').sort172173 # Smart-case match (Uppercase not-found)174 assert_equal [["Fruit", [[0, 5]]], ["Grapefruit", [[5, 10]]]],175 FZF::FuzzyMatcher.new(nil).match(%w[Fruit Grapefruit], 'fruit', '', '').sort176177 # Case-sensitive match (-i)178 assert_equal [['Fruit', [[0, 5]]]],179 FZF::FuzzyMatcher.new(0).match(%w[Fruit Grapefruit], 'Fruit', '', '').sort180181 # Case-insensitive match (+i)182 assert_equal [["Fruit", [[0, 5]]], ["Grapefruit", [[5, 10]]]],183 FZF::FuzzyMatcher.new(Regexp::IGNORECASE).184 match(%w[Fruit Grapefruit], 'Fruit', '', '').sort185 end186187 def test_extended_fuzzy_matcher_case_sensitive188 %w['Fruit Fruit$].each do |q|189 # Smart-case match (Uppercase found)190 assert_equal [['Fruit', [[0, 5]]]],191 FZF::ExtendedFuzzyMatcher.new(nil).match(%w[Fruit Grapefruit], q, '', '').sort192193 # Smart-case match (Uppercase not-found)194 assert_equal [["Fruit", [[0, 5]]], ["Grapefruit", [[5, 10]]]],195 FZF::ExtendedFuzzyMatcher.new(nil).match(%w[Fruit Grapefruit], q.downcase, '', '').sort196197 # Case-sensitive match (-i)198 assert_equal [['Fruit', [[0, 5]]]],199 FZF::ExtendedFuzzyMatcher.new(0).match(%w[Fruit Grapefruit], q, '', '').sort200201 # Case-insensitive match (+i)202 assert_equal [["Fruit", [[0, 5]]], ["Grapefruit", [[5, 10]]]],203 FZF::ExtendedFuzzyMatcher.new(Regexp::IGNORECASE).204 match(%w[Fruit Grapefruit], q, '', '').sort205 end206 end207208 def test_extended_fuzzy_matcher209 matcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE210 list = %w[211 juice
183 unmodified lines
modified:fzf+13 added-7 removed
65 unmodified lines
66 end67 end6869 def initialize argv, source = $stdin70 @rxflag = Regexp::IGNORECASE70 @rxflag = nil71 @sort = ENV.fetch('FZF_DEFAULT_SORT', 1000).to_i72 @color = true73 @multi = false74 @xmode = false
3 unmodified lines
78 case o79 when '-h', '--help' then usage 080 when '-m', '--multi' then @multi = true81 when '-x', '--extended' then @xmode = true82 when '-i' then @rxflag = Regexp::IGNORECASE83 when '+i' then @rxflag = 084 when '+s', '--no-sort' then @sort = nil85 when '+c', '--no-color' then @color = false86 when '-q', '--query'
49 unmodified lines
136 -x, --extended Extended-search mode137 -q, --query=STR Initial query138 -s, --sort=MAX Maximum number of matched items to sort. Default: 1000139 +s, --no-sort Do not sort the result. Keep the sequence unchanged.140 -i Case-insensitive match (default: smart-case match)141 +i Case-sensitive match142 +c, --no-color Disable colors]143 exit x144 end
626 unmodified lines
771 def empty? q772 q.empty?773 end774775 def rxflag_for q776 @rxflag || (q =~ /[A-Z]/ ? 0 : Regexp::IGNORECASE)777 end778779 def fuzzy_regex q780 @regexp[q] ||= begin775 q = q.downcase if @rxflag != 0781 q = q.downcase if @rxflag == Regexp::IGNORECASE782 Regexp.new(query_chars(q).inject('') { |sum, e|783 e = Regexp.escape e784 sum << (e.length > 1 ? "(?:#{e}).*?" : # FIXME: not equivalent785 "#{e}[^#{e}]*?")780 }, @rxflag)786 }, rxflag_for(q))787 end788 end789790 def match list, q, prefix, suffix
44 unmodified lines
835 case w836 when ''837 nil838 when /^\^(.*)\$$/833 Regexp.new('^' << sanitize(Regexp.escape($1)) << '$', rxflag)839 Regexp.new('^' << sanitize(Regexp.escape($1)) << '$', rxflag_for(w))840 when /^'/841 w.length > 1 ?836 Regexp.new(sanitize(Regexp.escape(w[1..-1])), rxflag) : nil842 Regexp.new(sanitize(Regexp.escape(w[1..-1])), rxflag_for(w)) : nil843 when /^\^/844 w.length > 1 ?839 Regexp.new('^' << sanitize(Regexp.escape(w[1..-1])), rxflag) : nil845 Regexp.new('^' << sanitize(Regexp.escape(w[1..-1])), rxflag_for(w)) : nil846 when /\$$/847 w.length > 1 ?842 Regexp.new(sanitize(Regexp.escape(w[0..-2])) << '$', rxflag) : nil848 Regexp.new(sanitize(Regexp.escape(w[0..-2])) << '$', rxflag_for(w)) : nil849 else850 fuzzy_regex w851 end, invert ]852 }.select { |pair| pair.first }
32 unmodified lines
modified:README.md+1 added
55 unmodified lines
56 -x, --extended Extended-search mode57 -q, --query=STR Initial query58 -s, --sort=MAX Maximum number of matched items to sort. Default: 100059 +s, --no-sort Do not sort the result. Keep the sequence unchanged.60 -i Case-insensitive match (default: smart-case match)61 +i Case-sensitive match62 +c, --no-color Disable colors63```64
295 unmodified lines