Skip to content

Commit

Permalink
fix issue #195, #222: allow for bottom-up ordering of results
Browse files Browse the repository at this point in the history
  • Loading branch information
Yggdroot committed Oct 2, 2018
1 parent 3a8fd74 commit 08794e9
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 56 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,15 @@ Usage
-----

```
usage: Leaderf[!] [-h] [--stayOpen] [--input INPUT | --cword]
usage: Leaderf[!] [-h] [--reverse] [--stayOpen] [--input INPUT | --cword]
[--top | --bottom | --left | --right | --belowright | --aboveleft | --fullScreen]
[--nameOnly | --fullPath | --fuzzy | --regex]
{file,tag,function,mru,searchHistory,cmdHistory,help,line,colorscheme,self,bufTag,buffer}
...
optional arguments:
-h, --help show this help message and exit
--reverse show results in bottom-up order
--stayOpen don't quit LeaderF after accepting an entry
--input INPUT specifies INPUT as the pattern inputted in advance
--cword current word under cursor is inputted in advance
Expand Down Expand Up @@ -122,7 +123,8 @@ If [!] is given, enter normal mode directly.
use `:Leaderf <subcommand> -h` to get specific help of subcommand, e.g., `:Leaderf mru -h`
```
usage: Leaderf[!] mru [-h] [--cwd] [--stayOpen] [--input INPUT | --cword]
usage: Leaderf[!] mru [-h] [--cwd] [--reverse] [--stayOpen]
[--input INPUT | --cword]
[--top | --bottom | --left | --right | --belowright | --aboveleft | --fullScreen]
[--nameOnly | --fullPath | --fuzzy | --regex]
Expand All @@ -133,6 +135,7 @@ specific arguments:
--cwd search MRU in current working directory
common arguments:
--reverse show results in bottom-up order
--stayOpen don't quit LeaderF after accepting an entry
--input INPUT specifies INPUT as the pattern inputted in advance
--cword current word under cursor is inputted in advance
Expand Down
1 change: 1 addition & 0 deletions autoload/leaderf/Any.vim
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ let g:Lf_Arguments = {
\}

let g:Lf_CommonArguments = [
\ {"name": ["--reverse"], "nargs": 0, "help": "show results in bottom-up order"},
\ {"name": ["--stayOpen"], "nargs": 0, "help": "don't quit LeaderF after accepting an entry"},
\ [
\ {"name": ["--input"], "nargs": 1, "help": "specifies INPUT as the pattern inputted in advance"},
Expand Down
29 changes: 26 additions & 3 deletions autoload/leaderf/python/leaderf/bufTagExpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,10 @@ def _acceptSelection(self, *args, **kwargs):
if line[0].isspace(): # if g:Lf_PreviewCode == 1
buffer = args[1]
line_nr = args[2]
line = buffer[line_nr - 2]
if self._getInstance().isReverseOrder():
line = buffer[line_nr]
else:
line = buffer[line_nr - 2]
# {tag} {kind} {scope} {file}:{line} {buf_number}
items = re.split(" *\t *", line)
tagname = items[0]
Expand Down Expand Up @@ -388,16 +391,36 @@ def _getList(self, pairs):

def _toUp(self):
if self._supports_preview:
lfCmd("norm! 2k")
if self._getInstance().isReverseOrder() and self._getInstance().getCurrentPos()[0] <= 3:
self._setResultContent()
if self._cli.pattern and len(self._highlight_pos) < len(self._getInstance().buffer) // 2 \
and len(self._highlight_pos) < int(lfEval("g:Lf_NumberOfHighlight")):
self._highlight_method()

if self._getInstance().isReverseOrder():
lfCmd("norm! 3kj")
self._getInstance().setLineNumber()
else:
lfCmd("norm! 2k")
else:
super(BufTagExplManager, self)._toUp()

lfCmd("setlocal cursorline!") # these two help to redraw the statusline,
lfCmd("setlocal cursorline!") # also fix a weird bug of vim

def _toDown(self):
if self._supports_preview:
lfCmd("norm! 3jk")
if self._getInstance().isReverseOrder():
lfCmd("norm! 2j")
self._getInstance().setLineNumber()
else:
lfCmd("norm! 3jk")
else:
super(BufTagExplManager, self)._toDown()

lfCmd("setlocal cursorline!") # these two help to redraw the statusline,
lfCmd("setlocal cursorline!") # also fix a weird bug of vim

def removeCache(self, buf_number):
self._getExplorer().removeCache(buf_number)

Expand Down
92 changes: 76 additions & 16 deletions autoload/leaderf/python/leaderf/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ def __init__(self, category,
self._win_height = float(lfEval("g:Lf_WindowHeight"))
self._show_tabline = int(lfEval("&showtabline"))
self._is_autocmd_set = False
self._reverse_order = lfEval("get(g:, 'Lf_ReverseOrder', 0)") == '1'
self._orig_pos = () # (tabpage, window, buffer)
self._initStlVar()
self._highlightStl()

def _initStlVar(self):
lfCmd("let g:Lf_{}_StlCategory = '-'".format(self._category))
lfCmd("let g:Lf_{}_StlMode = '-'".format(self._category))
lfCmd("let g:Lf_{}_StlCwd= '-'".format(self._category))
lfCmd("let g:Lf_{}_StlTotal = '0'".format(self._category))
lfCmd("let g:Lf_{}_StlLineNumber = '1'".format(self._category))
lfCmd("let g:Lf_{}_StlResultsCount = '0'".format(self._category))

stl = "%#Lf_hl_{0}_stlName# LeaderF "
Expand All @@ -56,7 +57,10 @@ def _initStlVar(self):
stl += "%#Lf_hl_{0}_stlSeparator3#%{{g:Lf_StlSeparator.left}}"
stl += "%=%#Lf_hl_{0}_stlBlank#"
stl += "%#Lf_hl_{0}_stlSeparator4#%{{g:Lf_StlSeparator.right}}"
stl += "%#Lf_hl_{0}_stlLineInfo# %l/%{{g:Lf_{0}_StlResultsCount}} "
if self._reverse_order:
stl += "%#Lf_hl_{0}_stlLineInfo# %{{g:Lf_{0}_StlLineNumber}}/%{{g:Lf_{0}_StlResultsCount}} "
else:
stl += "%#Lf_hl_{0}_stlLineInfo# %l/%{{g:Lf_{0}_StlResultsCount}} "
stl += "%#Lf_hl_{0}_stlSeparator5#%{{g:Lf_StlSeparator.right}}"
stl += "%#Lf_hl_{0}_stlTotal# Total: %{{g:Lf_{0}_StlTotal}} "
self._stl = stl.format(self._category)
Expand All @@ -71,18 +75,25 @@ def _setAttributes(self):
lfCmd("setlocal undolevels=-1")
lfCmd("setlocal noswapfile")
lfCmd("setlocal nolist")
lfCmd("setlocal number")
lfCmd("setlocal norelativenumber")
lfCmd("setlocal nospell")
lfCmd("setlocal wrap")
lfCmd("setlocal nofoldenable")
lfCmd("setlocal foldcolumn=0")
lfCmd("setlocal foldmethod=manual")
lfCmd("setlocal shiftwidth=4")
lfCmd("setlocal cursorline")
lfCmd("setlocal filetype=leaderf")
if self._reverse_order:
lfCmd("setlocal nonumber")
lfCmd("setlocal foldcolumn=1")
lfCmd("setlocal winfixheight")
else:
lfCmd("setlocal number")
lfCmd("setlocal foldcolumn=0")
lfCmd("setlocal nowinfixheight")

def _setStatusline(self):
self._initStlVar()
self.window.options["statusline"] = self._stl
lfCmd("redrawstatus")
if not self._is_autocmd_set:
Expand Down Expand Up @@ -145,6 +156,10 @@ def _createBufWindow(self, win_pos):

self._tabpage_object = vim.current.tabpage
self._window_object = vim.current.window
self._initial_win_height = self._window_object.height
if self._reverse_order:
self._window_object.height = 1

if self._buffer_object is None or not self._buffer_object.valid:
self._buffer_object = vim.current.buffer
lfCmd("augroup Lf_{}_Colorscheme".format(self._category))
Expand All @@ -154,6 +169,8 @@ def _createBufWindow(self, win_pos):
lfCmd("autocmd ColorScheme * call leaderf#colorscheme#highlightMode('{0}', g:Lf_{0}_StlMode)"
.format(self._category))
lfCmd("autocmd ColorScheme <buffer> doautocmd syntax")
lfCmd("autocmd CursorMoved <buffer> let g:Lf_{}_StlLineNumber = 1 + line('$') - line('.')"
.format(self._category))
lfCmd("autocmd VimResized * let g:Lf_VimResized = 1")
lfCmd("augroup END")

Expand All @@ -167,6 +184,13 @@ def _enterOpeningBuffer(self):
return True
return False

def setArguments(self, arguments):
self._arguments = arguments
if "--reverse" in self._arguments or lfEval("get(g:, 'Lf_ReverseOrder', 0)") == '1':
self._reverse_order = True
else:
self._reverse_order = False

def setStlCategory(self, category):
lfCmd("let g:Lf_{}_StlCategory = '{}'".format(self._category, category) )

Expand All @@ -188,6 +212,7 @@ def enterBuffer(self, win_pos):
if self._enterOpeningBuffer():
return

lfCmd("let g:Lf_{}_StlLineNumber = '1'".format(self._category))
self._orig_pos = (vim.current.tabpage, vim.current.window, vim.current.buffer)
self._orig_cursor = vim.current.window.cursor

Expand Down Expand Up @@ -241,7 +266,25 @@ def setBuffer(self, content):
# if lfEval("has('nvim')") == '1':
# # NvimError: string cannot contain newlines
# content = [ line.rstrip("\r\n") for line in content ]
self._buffer_object[:] = content
if self._reverse_order:
orig_row = self._window_object.cursor[0]
orig_buf_len = len(self._buffer_object)

self._buffer_object[:] = content[::-1]
buffer_len = len(self._buffer_object)
if buffer_len < self._initial_win_height:
self._window_object.height = buffer_len
elif self._window_object.height < self._initial_win_height:
self._window_object.height = self._initial_win_height

try:
self._window_object.cursor = (orig_row + buffer_len - orig_buf_len, 0)
except vim.error:
self._window_object.cursor = (buffer_len, 0)

self.setLineNumber()
else:
self._buffer_object[:] = content

def appendLine(self, line):
self._buffer_object.append(line)
Expand All @@ -250,35 +293,37 @@ def initBuffer(self, content, unit, set_content):
if isinstance(content, list):
self.setBuffer(content)
self.setStlTotal(len(content)//unit)
self.setStlResultsCount(len(content))
return
self.setStlResultsCount(len(content)//unit)
return content

self.buffer.options['modifiable'] = True
self._buffer_object[:] = []

try:
start = time.time()
cur_content = []
for line in content:
if line is None:
continue
if self.empty():
self._buffer_object[0] = line
else:
self._buffer_object.append(line)
cur_content.append(line)
if time.time() - start > 0.1:
start = time.time()
self.setBuffer(cur_content)
if self._reverse_order:
lfCmd("normal! G")
self.setStlTotal(len(self._buffer_object)//unit)
self.setStlResultsCount(len(self._buffer_object))
self.setStlResultsCount(len(self._buffer_object)//unit)
lfCmd("redrawstatus")
self.setBuffer(cur_content)
self.setStlTotal(len(self._buffer_object)//unit)
self.setStlResultsCount(len(self._buffer_object))
self.setStlResultsCount(len(self._buffer_object)//unit)
lfCmd("redrawstatus")
set_content(self.buffer[:])
set_content(cur_content)
except vim.error: # neovim <C-C>
pass
except KeyboardInterrupt: # <C-C>
pass

return cur_content

@property
def tabpage(self):
return self._tabpage_object
Expand Down Expand Up @@ -307,4 +352,19 @@ def getOriginalPos(self):
def getOriginalCursor(self):
return self._orig_cursor

def getInitialWinHeight(self):
if self._reverse_order:
return self._initial_win_height
else:
return 200

def isReverseOrder(self):
return self._reverse_order

def setLineNumber(self):
if self._reverse_order:
line_nr = 1 + len(self._buffer_object) - self._window_object.cursor[0]
lfCmd("let g:Lf_{}_StlLineNumber = '{}'".format(self._category, line_nr))


# vim: set ts=4 sw=4 tw=0 et :
Loading

2 comments on commit 08794e9

@xltan
Copy link

@xltan xltan commented on 08794e9 Oct 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

autocmd filetype leaderf set nonumber is broken after this commit

@Yggdroot
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

autocmd filetype leaderf set nonumber is broken after this commit

Fixed.

Please sign in to comment.