Skip to content
Myr

Implement --no-sort options with some fixes

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 9efb663 · Browse files · 3 files+122 added−73 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+122 added−73 removed

  • other:+122 added−73 removed

3 files+122 added−73 removed

modified:plugin/fzf.vim+3 added-3 removed
21 unmodified lines
22" WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.2324let s:exec = expand('<sfile>:h:h').'/fzf'2526function! s:fzf(cmd)26function! s:fzf(args)27 try28 let tf = tempname()29 let prefix = empty(a:cmd) ? '' : a:cmd.' | '30 execute "silent !".prefix."/usr/bin/env bash ".s:exec." > ".tf29 let prefix = exists('g:fzf_command') ? g:fzf_command.'|' : ''30 execute "silent !".prefix."/usr/bin/env bash ".s:exec." ".a:args." > ".tf31 if !v:shell_error32 execute 'silent e '.join(readfile(tf), '')33 endif34 finally
7 unmodified lines
modified:fzf+85 added-61 removed
55 unmodified lines
56@count = 057@cursor_x = 058@vcursor = 059@matches = []60@fan = '-\|/-\|/'.split(//)60@loaded = false61@sort = ARGV.delete('--no-sort').nil?62@stat = OpenStruct.new(:hit => 0, :partial_hit => 0,63 :prefix_hit => 0, :search => 0)6465def max_items; C.lines - 2; end
10 unmodified lines
76 cprint '> ', 177 cprint @query, 278end7979def print_info msg = nil80def print_info progress = true, msg = nil81 @fan ||= '-\|/-\|/'.split(//)82 C.setpos cursor_y - 1, 083 C.clrtoeol84 prefix =85 if fan = @fan.shift
2 unmodified lines
88 ' '89 else90 ' '91 end90 C.addstr "#{prefix}#{@matches.length}/#{@count}"92 C.addstr "#{prefix}#{@matches.length}/#{@count}" if progress93 C.addstr msg if msg94end9596def refresh
55 unmodified lines
152 @new << line.chomp153 @cv.broadcast154 end155 end156 @mtx.synchronize do157 @loaded = true158 @cv.broadcast159 end160 @smtx.synchronize { @fan = [] }161}162163searcher = Thread.new {164 fcache = {}165 matches = []166 new_length = 0161 pquery = nil162 vcursor = 0163 pvcursor = nil167 pquery = ''168 pvcursor = 0169 ploaded = false170 plength = 0171 zz = [0, 0]172173 begin174 while true175 query_changed = nil176 new_items = nil177 vcursor_moved = nil178 wait_for_completion = nil179 @mtx.synchronize do180 while true173 new_items = !@new.empty?174 query_changed = pquery != @query175 vcursor_moved = pvcursor != @vcursor181 new_items = !@new.empty?182 query_changed = pquery != @query183 vcursor_moved = pvcursor != @vcursor184 loading_finished = ploaded != @loaded185 wait_for_completion = !@sort && !@loaded186177 if !new_items && !query_changed && !vcursor_moved187 if !new_items && !query_changed && !vcursor_moved && !loading_finished188 @cv.wait @mtx189 next190 end181191 break192 end193185 if new_items194 if !wait_for_completion && new_items195 @lists << [@new, {}]196 @count += @new.length188189 @new = []190 fcache = {}197 @new = []198 fcache = {}199 end200201 pquery = @query202 pvcursor = @vcursor203 ploaded = @loaded204 end#mtx205206 if wait_for_completion207 @smtx.synchronize do208 print_info false, " +#{@new.length}"209 print_input210 refresh211 sleep 0.1212 end213 next214 end215216 new_search = new_items || query_changed198 if new_search217 if new_search && !@lists.empty?218 regexp = pquery.empty? ? nil :219 Regexp.new(pquery.split(//).inject('') { |sum, e|220 e = Regexp.escape e221 sum << "#{e}[^#{e}]*?"222 }, Regexp::IGNORECASE)223205 if fcache.has_key?(pquery)206 @stat.hit += 1207 else208 @smtx.synchronize do209 print_info ' ..'210 refresh211 end unless pquery.empty?212 end213 matches = fcache[pquery] ||= @lists.map { |pair|214 list, cache = pair215216 if cache[pquery]217 @stat.partial_hit += 1218 cache[pquery]224 matches =225 if fcache.has_key?(pquery)226 @stat.hit += 1227 fcache[pquery]228 else220 prefix_cache = nil221 (pquery.length - 1).downto(1) do |len|222 prefix = pquery[0, len]223 if prefix_cache = cache[prefix]224 @stat.prefix_hit += 1225 break226 end227 end229 @smtx.synchronize do230 print_info true, ' ..'231 refresh232 end unless pquery.empty?233229 cache[pquery] ||= (prefix_cache ? prefix_cache.map { |e| e.first } : list).map { |line|230 if regexp231 md = line.match regexp232 md ? [line, md.offset(0)] : nil234 found = @lists.map { |pair|235 list, cache = pair236237 if cache[pquery]238 @stat.partial_hit += 1239 cache[pquery]240 else241 prefix_cache = nil242 (pquery.length - 1).downto(1) do |len|243 prefix = pquery[0, len]244 if prefix_cache = cache[prefix]245 @stat.prefix_hit += 1246 break247 end248 end249250 cache[pquery] ||= (prefix_cache ? prefix_cache.map { |e| e.first } : list).map { |line|251 if regexp252 # Ignore errors: e.g. invalid byte sequence in UTF-8253 md = line.match(regexp) rescue nil254 md ? [line, md.offset(0)] : nil255 else234 [line, zz]256 [line, zz]257 end258 }.compact259 end236 }.compact260 }.inject([]) { |all, e| all.concat e }261 fcache[pquery] = @sort ? found : found.reverse262 end238 }.inject([]) { |all, e| all.concat e }263 @stat.search += 1264265 new_length = matches.length242 if new_length <= MAX_SORT_LEN266 if @sort && new_length <= MAX_SORT_LEN267 matches.replace matches.sort_by { |pair|268 line, offset = pair269 [offset.last - offset.first, line.length, line]270 }271 end248 end#new matches272 end#new_search273250 # This small delay reduces the number of partial lists.274 # This small delay reduces the number of partial lists275 sleep 0.2 if !query_changed && !vcursor_moved276253 prev_length = @matches.length277 if vcursor_moved || new_search255 vcursor =256 @mtx.synchronize do257 @matches = matches258 @vcursor = [0, [@vcursor, new_length - 1, max_items - 1].min].max259 end278 @mtx.synchronize do279 plength = [@matches.length, max_items].min280 @matches = matches281 pvcursor = @vcursor = [0, [@vcursor, new_length - 1, max_items - 1].min].max282 end283 end284285 # Output286 @smtx.synchronize do264 if new_length < prev_length265 prev_length.downto(new_length) do |idx|287 item_length = [new_length, max_items].min288 if item_length < plength289 plength.downto(item_length) do |idx|290 C.setpos cursor_y - idx - 2, 0291 C.clrtoeol292 end293 end294295 maxc = C.cols - 3296 matches[0, max_items].each_with_index do |item, idx|273 next if !new_search && !((vcursor-1)..(vcursor+1)).include?(idx)297 next if !new_search && !((pvcursor-1)..(pvcursor+1)).include?(idx)298299 line, offset = item300 row = cursor_y - idx - 2277 chosen = idx == vcursor301 chosen = idx == pvcursor302303 if line.length > maxc304 line = line[0, maxc] + '..'305 end
14 unmodified lines
320 end321 C.attroff C.color_pair(3) | C::A_BOLD if chosen322 end323300 print_info324 print_info if !@lists.empty? || ploaded325 print_input326 refresh327 end328 end
21 unmodified lines
350 ctrl(:a) => proc { cursor = 0 },351 ctrl(:e) => proc { cursor = input.length },352 ctrl(:j) => proc {353 @mtx.synchronize do330 @vcursor = [0, @vcursor - 1].max354 @vcursor -= 1355 @cv.broadcast356 end357 },358 ctrl(:k) => proc {359 @mtx.synchronize do336 @vcursor = [0, [@matches.length - 1, @vcursor + 1].min].max360 @vcursor += 1361 @cv.broadcast362 end363 },364 ctrl(:w) => proc {
55 unmodified lines
modified:README.md+34 added-9 removed