Skip to content
Myr

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

2 files+51 added−33 removed

modified:test/test_fzf.rb+23 added-9 removed
398 unmodified lines
399 ["____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', '', '')))404405 assert_equal(406 [["01", [[0, 1], [1, 2]]],407 ["01_", [[0, 1], [1, 2]]],
2 unmodified lines
410 ["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', '', '')))415416 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 end428429 def test_extended_exact_mode430 exact = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE, :exact
218 unmodified lines
649 ensure650 $stdout = STDOUT651 end652 end653654 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 end664end665
modified:fzf+28 added-24 removed
96 unmodified lines
97 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 = :fuzzy
118 unmodified lines
224225 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 lines
238 FuzzyMatcher.new @rxflag, @nth, @delim239 end240 end241242 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 end253254 def sort list255 list.sort_by { |tuple| rank tuple }256 end257258 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 end271272 def usage x, message = nil273 $stderr.puts message if message
263 unmodified lines
537 end538 C.attroff color(:chosen, true) if chosen539 end540522 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 end536541 AFTER_1_9 = RUBY_VERSION.split('.').map { |e| e.rjust(3, '0') }.join >= '001009'542543 if AFTER_1_9544 @@wrx = Regexp.new '\p{Han}|\p{Katakana}|\p{Hiragana}|\p{Hangul}'
183 unmodified lines
728 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