Fix ranking when multiple regions overlap
Before adoption: this commit came in with the history the project adopted, so no landing records it.
e.g. Match region #1: [-----------] Match region #2: [---] Match region #3: [------]
authored by Junegunn Choi · committed by Junegunn Choi · parent a9056ce · Browse files · 2 files·+51 added−33 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+51 added−33 removed
- tests:+23 added−9 removed
- other:+28 added−24 removed
Provenance
- Commit
fa212ef- Tree
d991fc5- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
a9056ce
2 files changed+51 added−33 removed
modified:test/test_fzf.rb+23 added-9 removed
398 unmodified lines399 ["____0_1", [[4, 7]]],400 ["0____1", [[0, 6]]],401 ["0_____1", [[0, 7]]],402 ["0______1", [[0, 8]]]],403- FZF.new([]).sort_by_rank(matcher.match(list, '01', '', '')))403+ FZF.sort(matcher.match(list, '01', '', '')))404 405 assert_equal(406 [["01", [[0, 1], [1, 2]]],407 ["01_", [[0, 1], [1, 2]]],2 unmodified lines410 ["0_____1", [[0, 1], [6, 7]]],411 ["____0_1", [[4, 5], [6, 7]]],412 ["0______1", [[0, 1], [7, 8]]],413 ["___01___", [[3, 4], [4, 5]]]],414- FZF.new([]).sort_by_rank(xmatcher.match(list, '0 1', '', '')))414+ FZF.sort(xmatcher.match(list, '0 1', '', '')))415 416 assert_equal(417- [["_01_", [[1, 3], [0, 4]]],418- ["0____1", [[0, 6], [1, 3]]],419- ["0_____1", [[0, 7], [1, 3]]],420- ["0______1", [[0, 8], [1, 3]]],421- ["___01___", [[3, 5], [0, 2]]],422- ["____0_1", [[4, 7], [0, 2]]]],423- FZF.new([]).sort_by_rank(xmatcher.match(list, '01 __', '', '')))417+ [["_01_", [[1, 3], [0, 4]], [4, 4, "_01_"]],418+ ["___01___", [[3, 5], [0, 2]], [4, 8, "___01___"]],419+ ["____0_1", [[4, 7], [0, 2]], [5, 7, "____0_1"]],420+ ["0____1", [[0, 6], [1, 3]], [6, 6, "0____1"]],421+ ["0_____1", [[0, 7], [1, 3]], [7, 7, "0_____1"]],422+ ["0______1", [[0, 8], [1, 3]], [8, 8, "0______1"]]],423+ FZF.sort(xmatcher.match(list, '01 __', '', '')).map { |tuple|424+ tuple << FZF.rank(tuple)425+ }426+ )427 end428 429 def test_extended_exact_mode430 exact = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE, :exact218 unmodified lines649 ensure650 $stdout = STDOUT651 end652 end653+654+ def test_ranking_overlap_match_regions655+ list = [656+ '1 3 4 2',657+ '1 2 3 4'658+ ]659+ assert_equal [660+ ['1 2 3 4', [[0, 13], [16, 22]]],661+ ['1 3 4 2', [[0, 24], [12, 17]]],662+ ], FZF.sort(FZF::ExtendedFuzzyMatcher.new(nil).match(list, '12 34', '', ''))663+ end664 end665 modified:fzf+28 added-24 removed
96 unmodified lines97 argv.dup98 end99 while o = argv.shift100 case o101- when '--version' then version101+ when '--version' then FZF.version102 when '-h', '--help' then usage 0103 when '-m', '--multi' then @multi = true104 when '+m', '--no-multi' then @multi = false105 when '-x', '--extended' then @extended = :fuzzy118 unmodified lines224 225 def filter_list list226 matches = matcher.match(list, @filter, '', '')227 if @sort && matches.length <= @sort228- matches = sort_by_rank(matches)228+ matches = FZF.sort(matches)229 end230 matches.each { |m| puts m.first }231 end232 5 unmodified lines238 FuzzyMatcher.new @rxflag, @nth, @delim239 end240 end241 242+ class << self242- def version243- File.open(__FILE__, 'r') do |f|244- f.each_line do |line|245- if line =~ /Version: (.*)/246- $stdout.puts 'fzf ' << $1247- exit243+ def version244+ File.open(__FILE__, 'r') do |f|245+ f.each_line do |line|246+ if line =~ /Version: (.*)/247+ $stdout.puts 'fzf ' << $1248+ exit249+ end250 end251 end252 end253+254+ def sort list255+ list.sort_by { |tuple| rank tuple }256+ end257+258+ def rank tuple259+ line, offsets = tuple260+ matchlen = 0261+ pe = 0262+ offsets.sort.each do |pair|263+ b, e = pair264+ b = pe if pe > b265+ pe = e if e > pe266+ matchlen += e - b if e > b267+ end268+ [matchlen, line.length, line]269+ end270 end271 272 def usage x, message = nil273 $stderr.puts message if message263 unmodified lines537 end538 C.attroff color(:chosen, true) if chosen539 end540 522- def sort_by_rank list523- list.sort_by { |tuple|524- line, offsets = tuple525- matchlen = 0526- pe = nil527- offsets.sort.each do |pair|528- b, e = pair529- b = pe if pe && pe > b530- pe = e531- matchlen += e - b532- end533- [matchlen, line.length, line]534- }535- end536-541 AFTER_1_9 = RUBY_VERSION.split('.').map { |e| e.rjust(3, '0') }.join >= '001009'542 543 if AFTER_1_9544 @@wrx = Regexp.new '\p{Han}|\p{Katakana}|\p{Hiragana}|\p{Hangul}'183 unmodified lines728 end729 next if skip730 matches = @sort ? found : found.reverse731 if !empty && @sort && matches.length <= @sort728- matches = sort_by_rank(matches)732+ matches = FZF.sort(matches)733 end734 fcache[q] = matches735 end736 545 unmodified lines