Extended mode
Before adoption: this commit came in with the history the project adopted, so no landing records it.
- Implement prefix caching of extended mode - Improved ranking algorithm for extended mode - Fix nfc conversion bug
authored by Junegunn Choi · committed by Junegunn Choi · parent 545e8bf · Browse files · 4 files·+230 added−39 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+230 added−39 removed
- tests:+155 added−15 removed
- other:+75 added−24 removed
Provenance
- Commit
43acf5c- Tree
8939f7d- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
545e8bf
4 files changed+230 added−39 removed
modified:test/test_fzf.rb+155 added-15 removed
54 unmodified lines55 end56 57 def test_trim58 fzf = FZF.new []59- assert_equal ['사.', 6], fzf.trim('가나다라마바사.', 4, true)60- assert_equal ['바사.', 5], fzf.trim('가나다라마바사.', 5, true)61- assert_equal ['가나', 6], fzf.trim('가나다라마바사.', 4, false)62- assert_equal ['가나', 6], fzf.trim('가나다라마바사.', 5, false)63- assert_equal ['가나a', 6], fzf.trim('가나ab라마바사.', 5, false)59+ assert_equal ['사.', 6], fzf.trim('가나다라마바사.', 4, true)60+ assert_equal ['바사.', 5], fzf.trim('가나다라마바사.', 5, true)61+ assert_equal ['바사.', 5], fzf.trim('가나다라마바사.', 6, true)62+ assert_equal ['마바사.', 4], fzf.trim('가나다라마바사.', 7, true)63+ assert_equal ['가나', 6], fzf.trim('가나다라마바사.', 4, false)64+ assert_equal ['가나', 6], fzf.trim('가나다라마바사.', 5, false)65+ assert_equal ['가나a', 6], fzf.trim('가나ab라마바사.', 5, false)66+ assert_equal ['가나ab', 5], fzf.trim('가나ab라마바사.', 6, false)67+ assert_equal ['가나ab', 5], fzf.trim('가나ab라마바사.', 7, false)68 end69 70 def test_format71 fzf = FZF.new []38 unmodified lines110 juiceless111 juicily112 juiciness113 juicy]110- assert matcher.cache.empty?114+ assert matcher.caches.empty?115 assert_equal(116 [["juice", [[0, 1]]],117 ["juiceful", [[0, 1]]],118 ["juiceless", [[0, 1]]],119 ["juicily", [[0, 1]]],120 ["juiciness", [[0, 1]]],121 ["juicy", [[0, 1]]]], matcher.match(list, 'j', '', '').sort)118- assert !matcher.cache.empty?119- assert_equal [list.object_id], matcher.cache.keys120- assert_equal 1, matcher.cache[list.object_id].length121- assert_equal 6, matcher.cache[list.object_id]['j'].length122+ assert !matcher.caches.empty?123+ assert_equal [list.object_id], matcher.caches.keys124+ assert_equal 1, matcher.caches[list.object_id].length125+ assert_equal 6, matcher.caches[list.object_id]['j'].length126 127 assert_equal(128 [["juicily", [[0, 5]]],129 ["juiciness", [[0, 5]]]], matcher.match(list, 'jii', '', '').sort)1 unmodified line131 assert_equal(132 [["juicily", [[2, 5]]],133 ["juiciness", [[2, 5]]]], matcher.match(list, 'ii', '', '').sort)134 131- assert_equal 3, matcher.cache[list.object_id].length132- assert_equal 2, matcher.cache[list.object_id]['ii'].length135+ assert_equal 3, matcher.caches[list.object_id].length136+ assert_equal 2, matcher.caches[list.object_id]['ii'].length137 138 # TODO : partial_cache139 end140 141+ def test_fuzzy_matcher_case_sensitive142+ assert_equal [['Fruit', [[0, 5]]]],143+ FZF::FuzzyMatcher.new(0).match(%w[Fruit Grapefruit], 'Fruit', '', '').sort144+145+ assert_equal [["Fruit", [[0, 5]]], ["Grapefruit", [[5, 10]]]],146+ FZF::FuzzyMatcher.new(Regexp::IGNORECASE).147+ match(%w[Fruit Grapefruit], 'Fruit', '', '').sort148+ end149+150+ def test_extended_fuzzy_matcher151+ matcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE152+ list = %w[153+ juice154+ juiceful155+ juiceless156+ juicily157+ juiciness158+ juicy159+ _juice]160+ match = proc { |q, prefix|161+ matcher.match(list, q, prefix, '').sort.map { |p| [p.first, p.last.sort] }162+ }163+164+ assert matcher.caches.empty?165+ 3.times do166+ ['y j', 'j y'].each do |pat|167+ (0..pat.length - 1).each do |prefix_length|168+ prefix = pat[0, prefix_length]169+ assert_equal(170+ [["juicily", [[0, 1], [6, 7]]],171+ ["juicy", [[0, 1], [4, 5]]]],172+ match.call(pat, prefix))173+ end174+ end175+176+ # $177+ assert_equal [["juiceful", [[7, 8]]]], match.call('l$', '')178+ assert_equal [["juiceful", [[7, 8]]],179+ ["juiceless", [[5, 6]]],180+ ["juicily", [[5, 6]]]], match.call('l', '')181+182+ # ^183+ assert_equal list.length, match.call('j', '').length184+ assert_equal list.length - 1, match.call('^j', '').length185+186+ # !187+ assert_equal 0, match.call('!j', '').length188+189+ # ! + ^190+ assert_equal [["_juice", []]], match.call('!^j', '')191+192+ # ! + $193+ assert_equal list.length - 1, match.call('!l$', '').length194+195+ # ! + f196+ assert_equal [["juicy", [[4, 5]]]], match.call('y !l', '')197+ end198+ assert !matcher.caches.empty?199+ end200+201+ def test_xfuzzy_matcher_prefix_cache202+ matcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE203+ list = %w[204+ a.java205+ b.java206+ java.jive207+ c.java$208+ d.java209+ ]210+ 2.times do211+ assert_equal 5, matcher.match(list, 'java', 'java', '').length212+ assert_equal 3, matcher.match(list, 'java$', 'java$', '').length213+ assert_equal 1, matcher.match(list, 'java$$', 'java$$', '').length214+215+ assert_equal 0, matcher.match(list, '!java', '!java', '').length216+ assert_equal 4, matcher.match(list, '!^jav', '!^jav', '').length217+ assert_equal 4, matcher.match(list, '!^java', '!^java', '').length218+ assert_equal 2, matcher.match(list, '!^java !b !c', '!^java', '').length219+ end220+ end221+222 def test_sort_by_rank138- matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE223+ matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE224+ xmatcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE225 list = %w[226 0____1227 0_____1228 012 unmodified lines231 _01_232 0______1233 ___01___234 ]149- assert_equal %w[01 01_ _01_ ___01___ ____0_1 0____1 0_____1 0______1],150- FZF.new([]).sort_by_rank(matcher.match(list, '01', '', '')).map(&:first)235+ assert_equal(236+ [["01", [[0, 2]]],237+ ["01_", [[0, 2]]],238+ ["_01_", [[1, 3]]],239+ ["___01___", [[3, 5]]],240+ ["____0_1", [[4, 7]]],241+ ["0____1", [[0, 6]]],242+ ["0_____1", [[0, 7]]],243+ ["0______1", [[0, 8]]]],244+ FZF.new([]).sort_by_rank(matcher.match(list, '01', '', '')))245+246+ assert_equal(247+ [["01", [[0, 1], [1, 2]]],248+ ["01_", [[0, 1], [1, 2]]],249+ ["_01_", [[1, 2], [2, 3]]],250+ ["0____1", [[0, 1], [5, 6]]],251+ ["0_____1", [[0, 1], [6, 7]]],252+ ["____0_1", [[4, 5], [6, 7]]],253+ ["0______1", [[0, 1], [7, 8]]],254+ ["___01___", [[3, 4], [4, 5]]]],255+ FZF.new([]).sort_by_rank(xmatcher.match(list, '0 1', '', '')))256+257+ assert_equal(258+ [["_01_", [[1, 3], [0, 4]]],259+ ["0____1", [[0, 6], [1, 3]]],260+ ["0_____1", [[0, 7], [1, 3]]],261+ ["0______1", [[0, 8], [1, 3]]],262+ ["___01___", [[3, 5], [0, 2]]],263+ ["____0_1", [[4, 7], [0, 2]]]],264+ FZF.new([]).sort_by_rank(xmatcher.match(list, '01 __', '', '')))265 end266+267+ if RUBY_PLATFORM =~ /darwin/268+ NFD = '한글'269+ def test_nfc270+ assert_equal 6, NFD.length271+ assert_equal ["한글", [[0, 1], [1, 2]]],272+ FZF::UConv.nfc(NFD, [[0, 3], [3, 6]])273+274+ nfd2 = 'before' + NFD + 'after'275+ assert_equal 6 + 6 + 5, nfd2.length276+277+ nfc, offsets = FZF::UConv.nfc(nfd2, [[4, 14], [9, 13]])278+ o1, o2 = offsets279+ assert_equal 'before한글after', nfc280+ assert_equal 're한글af', nfc[(o1.first...o1.last)]281+ assert_equal '글a', nfc[(o2.first...o2.last)]282+ end283+284+ def test_nfd285+ nfc = '한글'286+ nfd = FZF::UConv.nfd(nfc)287+ assert_equal 6, nfd.length288+ assert_equal NFD, nfd289+ end290+ end291 end292+modified:fzf+55 added-24 removed
66 unmodified lines67 68 def initialize argv, source = $stdin69 usage 0 unless (%w[--help -h] & argv).empty?70 @rxflag = argv.delete('+i') ? 0 : Regexp::IGNORECASE71- @sort = %w[+s --no-sort].map { |e| argv.delete e }.compact.empty? ?71+ @sort = %w[+s --no-sort].map { |e| argv.delete e }.compact.empty? ?72 ENV.fetch('FZF_DEFAULT_SORT', 500).to_i : nil73 @color = %w[+c --no-color].map { |e| argv.delete e }.compact.empty?74 @multi = !%w[-m --multi].map { |e| argv.delete e }.compact.empty?75 @xmode = !%w[-x --extended].map { |e| argv.delete e }.compact.empty?72 unmodified lines148 end149 ret150 end151 152+ def self.to_nfc arr153+ [NFC_BEGIN + arr[0] * JJCOUNT +154+ (arr[1] || 0) * JONGSUNGS +155+ (arr[2] || 0)].pack('U*')156+ end157+158 def self.nfc str, offsets = []159 ret = ''160 omap = []161 pend = []8 unmodified lines170 pend << cp - JONGSUNG171 next172 else173 omap[-1] = omap[-1] + 1168- ret << [NFC_BEGIN + pend[0] * JJCOUNT +169- (pend[1] || 0) * JONGSUNGS +170- (pend[2] || 0)].pack('U*')174+ ret << to_nfc(pend)175 pend.clear176 end177 end178 if cp >= CHOSUNG && cp < CHOSUNG + CHOSUNGS1 unmodified line180 else181 ret << c182 end183 end184+ ret << to_nfc(pend) unless pend.empty?185 return [ret,186 offsets.map { |pair|187 b, e = pair188 [omap[b] || 0, omap[e] || ((omap.last || 0) + 1)] }]139 unmodified lines328 329 def sort_by_rank list330 list.sort_by { |tuple|331 line, offsets = tuple327- matchlen = (offsets.map { |pair| pair.last }.max || 0) -328- (offsets.map { |pair| pair.first }.min || 0)332+ matchlen = 0333+ pe = nil334+ offsets.sort.each do |pair|335+ b, e = pair336+ b = pe if pe && pe > b337+ pe = e338+ matchlen += e - b339+ end340 [matchlen, line.length, line]341 }342 end343 119 unmodified lines463 end464 465 def start_search466 main = Thread.current456- matcher = (@xmode ? XFuzzyMatcher : FuzzyMatcher).new @rxflag467+ matcher = (@xmode ? ExtendedFuzzyMatcher : FuzzyMatcher).new @rxflag468 searcher = Thread.new {469 lists = []470 events = {}471 fcache = {}192 unmodified lines664 @stdout.puts sel665 end666 @stdout.puts got667 end657- exit 0668 end669 end670 671 class FuzzyMatcher < Matcher662- attr_reader :cache, :rxflag672+ attr_reader :caches, :rxflag673 674 def initialize rxflag665- @cache = Hash.new { |h, k| h[k] = {} }675+ @caches = Hash.new { |h, k| h[k] = {} }676 @regexp = {}677 @rxflag = rxflag678 end679 9 unmodified lines689 690 def match list, q, prefix, suffix691 regexp = fuzzy_regex q692 683- cache = @cache[list.object_id]693+ cache = @caches[list.object_id]694 prefix_cache = nil695 (prefix.length - 1).downto(1) do |len|696 break if prefix_cache = cache[prefix[0, len]]697 end13 unmodified lines711 }.compact712 end713 end714 705- class XFuzzyMatcher < FuzzyMatcher715+ class ExtendedFuzzyMatcher < FuzzyMatcher716+ def initialize rxflag717+ super718+ require 'set'719+ @regexps = {}720+ end721+722 def match list, q, prefix, suffix723+ q = q.strip724+707- regexps = q.strip.split(/\s+/).map { |w|725+ regexps = @regexps[q] ||= q.split(/\s+/).map { |w|726 invert =727 if w =~ /^!/728 w = w[1..-1]729 true730 end731 714- [ case w715- when ''716- nil717- when /^\^/718- w.length > 1 ? Regexp.new('^' << w[1..-1], rxflag) : nil719- when /\$$/720- w.length > 1 ? Regexp.new(w[0..-2] << '$', rxflag) : nil721- else722- fuzzy_regex w723- end, invert ]732+ [ @regexp[w] ||=733+ case w734+ when ''735+ nil736+ when /^\^/737+ w.length > 1 ?738+ Regexp.new('^' << Regexp.escape(w[1..-1]), rxflag) : nil739+ when /\$$/740+ w.length > 1 ?741+ Regexp.new(Regexp.escape(w[0..-2]) << '$', rxflag) : nil742+ else743+ fuzzy_regex w744+ end, invert ]745 }.select { |pair| pair.first }746 747+ # Look for prefix cache748+ cache = @caches[list.object_id]749+ prefix = prefix.strip.sub(/\$\S+$/, '').sub(/!\S+$/, '')750+ prefix_cache = nil751+ (prefix.length - 1).downto(1) do |len|752+ break if prefix_cache = cache[Set[@regexps[prefix[0, len]]]]753+ end754+755+ cache[Set[regexps]] ||= (prefix_cache ?756+ prefix_cache.map { |e| e.first } :726- list.map { |line|757+ list).map { |line|758 offsets = []759 regexps.all? { |pair|760 regexp, invert = pair761 md = line.match(regexp) rescue nil13 unmodified linesmodified:Rakefile+7 added
1 require "bundler/gem_tasks"2+require 'rake/testtask'3+4+Rake::TestTask.new(:test) do |test|5+ test.pattern = 'test/**/test_*.rb'6+ test.verbose = true7+end8+modified:README.md+13 added
112 unmodified lines113 114 If you enable multi-select mode with `-m` option, you can select multiple items115 with TAB or Shift-TAB key.116 117+### Extended mode118+119+With `-x` or `--extended` option, fzf will start in "extended mode".120+121+In extended mode, you can specify multiple patterns delimited by spaces, such as: `^music .mp3$ sbtrkt !rmx`122+123+| Token | Description | Match type |124+| -------- | ----------------------------- | -------------------- |125+| `^music` | Items that start with `music` | prefix-exact-match |126+| `.mp3$` | Items that end with `.mp3` | suffix-exact-match |127+| `sbtrkt` | Items that match `sbtrkt` | fuzzy-match |128+| `!rmx` | Items that do not match `rmx` | invert-fuzzy-match |129+130 Usage as Vim plugin131 -------------------132 133 If you install fzf as a Vim plugin, `:FZF` command will be added.115 unmodified lines