Procházet zdrojové kódy

When using fuzzy matching, retain the MRU file order. Fixes issue #59

Yegappan Lakshmanan před 3 roky
rodič
revize
1d96d77d3d
4 změnil soubory, kde provedl 49 přidání a 11 odebrání
  1. 2 2
      README.md
  2. 3 3
      doc/mru.txt
  3. 10 6
      plugin/mru.vim
  4. 34 0
      test/unit_tests.vim

+ 2 - 2
README.md

@@ -31,13 +31,13 @@ For Vim 7.4 and before, you can use the following steps (on Unix/Linux/MacOS sys
 You can also install this plugin directly from github using the following steps (for Vim 8.0 and above):
 
     $ mkdir -p $HOME/.vim/pack/downloads/start/mru
-    $ cd $HOME/.vim/pack/downloads/start/mru
+    $ cd $HOME/.vim/pack/downloads/start
     $ git clone https://github.com/yegappan/mru
 
 For NeoVim:
 
     $ mkdir -p $HOME/.config/nvim/pack/downloads/start/mru
-    $ cd $HOME/.config/nvim/pack/downloads/start/mru
+    $ cd $HOME/.config/nvim/pack/downloads/start
     $ git clone https://github.com/yegappan/mru
 
 or you can use any one of the Vim plugin managers ([vim-plug](https://github.com/junegunn/vim-plug), [dein.vim](https://github.com/Shougo/dein.vim), [pathogen](https://github.com/tpope/vim-pathogen), [minpac](https://github.com/k-takata/minpac), [vam](https://github.com/MarcWeber/vim-addon-manager), [volt](https://github.com/vim-volt/volt), [Vundle](https://github.com/VundleVim/Vundle.vim), etc.) to install and manage this plugin.

+ 3 - 3
doc/mru.txt

@@ -2,7 +2,7 @@
 
 Author: Yegappan Lakshmanan  (yegappan AT yahoo DOT com)
 For Vim version 7.0 and above
-Last change: August 19, 2022
+Last change: October 16, 2022
 
 ==============================================================================
 						*mru-license*
@@ -86,13 +86,13 @@ You can also install the latest version of the plugin directly from github
 using the following steps (in Vim 8.0 and above): >
 
     $ mkdir -p $HOME/.vim/pack/downloads/start/mru
-    $ cd $HOME/.vim/pack/downloads/start/mru
+    $ cd $HOME/.vim/pack/downloads/start
     $ git clone https://github.com/yegappan/mru.git
 <
 For Neovim: >
 
     $ mkdir -p $HOME/.config/nvim/pack/downloads/start/mru
-    $ cd $HOME/.config/nvim/pack/downloads/start/mru
+    $ cd $HOME/.config/nvim/pack/downloads/start
     $ git clone https://github.com/yegappan/mru.git
 <
 ==============================================================================

+ 10 - 6
plugin/mru.vim

@@ -1,7 +1,7 @@
 " File: mru.vim
 " Author: Yegappan Lakshmanan (yegappan AT yahoo DOT com)
 " Version: 3.11
-" Last Modified: August 20, 2022
+" Last Modified: October 16, 2022
 " Copyright: Copyright (C) 2003-2022 Yegappan Lakshmanan
 " License:   Permission is hereby granted to use and distribute this code,
 "            with or without modifications, provided that this copyright
@@ -18,6 +18,7 @@ endif
 let loaded_mru=1
 
 if v:version < 700
+  " Needs Vim version 7.0 and above
   finish
 endif
 
@@ -26,7 +27,7 @@ let s:cpo_save = &cpoptions
 set cpoptions&vim
 
 " MRU configuration variables {{{1
-" Maximum number of entries allowed in the MRU list
+" Maximum number of file names stored in the MRU list
 if !exists('MRU_Max_Entries')
   let MRU_Max_Entries = 100
 endif
@@ -755,7 +756,7 @@ func! s:MRU_Open_Window(pat, splitdir, winsz) abort
     " Display only the entries matching the specified pattern. First try
     " fuzzy matching or as a literal pattern.
     if g:MRU_FuzzyMatch
-      let m = matchfuzzy(s:MRU_files, a:pat)
+      let m = filter(copy(s:MRU_files), '!matchfuzzy([v:val], a:pat)->empty()')
     else
       " do case insensitive file name comparison
       let spat = tolower(a:pat)
@@ -790,13 +791,16 @@ endfunc
 " MRU_Complete                          {{{1
 " Command-line completion function used by :MRU command
 func! s:MRU_Complete(ArgLead, CmdLine, CursorPos) abort
+  " Load the latest MRU file
+  call s:MRU_LoadList()
+
   if empty(a:ArgLead)
     " Return the complete list of MRU files
     return s:MRU_files
   else
     if g:MRU_FuzzyMatch
       " Return only the files fuzzy matching the specified pattern
-      return matchfuzzy(s:MRU_files, a:ArgLead)
+      return filter(copy(s:MRU_files), '!matchfuzzy([v:val], a:ArgLead)->empty()')
     else
       " Return only the files matching the specified pattern
       return filter(copy(s:MRU_files), 'v:val =~? a:ArgLead')
@@ -829,7 +833,7 @@ func! s:MRU_Cmd(pat, splitdir, winsz) abort
   " filename is found, then edit it (unless the user wants to open the MRU
   " window always)
   if g:MRU_FuzzyMatch
-    let m = matchfuzzy(s:MRU_files, a:pat)
+    let m = filter(copy(s:MRU_files), '!matchfuzzy([v:val], a:pat)->empty()')
   else
     " do case insensitive file name comparison
     let spat = tolower(a:pat)
@@ -1028,7 +1032,7 @@ func MruGetFiles(...)
   if a:0 == 1
     if g:MRU_FuzzyMatch
       " Return only the files fuzzy matching the specified pattern
-      return matchfuzzy(s:MRU_files, a:1)
+      return filter(copy(s:MRU_files), '!matchfuzzy([v:val], a:1)->empty()')
     endif
     " Return only the files matching the specified pattern
     return filter(copy(s:MRU_files), 'v:val =~? a:1')

+ 34 - 0
test/unit_tests.vim

@@ -1472,6 +1472,40 @@ func Test_61()
   endtry
 endfunc
 
+" ==========================================================================
+" Test62
+" When using fuzzy match, the order of the file names in the MRU list should
+" be maintained in the returned list.
+" Works only in Unix-like systems.
+" ==========================================================================
+func Test_62()
+  if !has('unix') || !exists('*matchfuzzy')
+    return
+  endif
+
+  let l = readfile(g:MRU_File)
+  call remove(l, 1, -1)
+  call writefile(l, g:MRU_File)
+  call s:MRU_Test_Add_Files(['a111b222c', 'a11b22c', 'abc123', 'a1b2c'])
+
+  " Test for command-line expansion
+  exe 'normal! :MRU abc' . "\<C-A>\<Home>let m='\<End>'\<CR>"
+  call s:Assert_equal('MRU a111b222c a11b22c abc123 a1b2c', m)
+
+  " Test for MruGetFiles()
+  let l = MruGetFiles('abc')
+  call s:Assert_equal(['a111b222c', 'a11b22c', 'abc123', 'a1b2c'], l)
+
+  " Test for MRU window
+  MRU abc
+  let l = getline(1, '$')
+  call s:Assert_match('a111b222c', l[0])
+  call s:Assert_match('a11b22c', l[1])
+  call s:Assert_match('abc123', l[2])
+  call s:Assert_match('a1b2c', l[3])
+  close
+endfunc
+
 " ==========================================================================
 
 " Create the files used by the tests