Skip to content

Commit

Permalink
fix issue #8 : Navigate tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Yggdroot committed Mar 11, 2017
1 parent b787149 commit 8287f34
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 7 deletions.
18 changes: 18 additions & 0 deletions autoload/leaderf.vim
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ exec g:Lf_py "sys.path.insert(0, cwd)"
exec g:Lf_py "from leaderf.bufExpl import *"
exec g:Lf_py "from leaderf.fileExpl import *"
exec g:Lf_py "from leaderf.mruExpl import *"
exec g:Lf_py "from leaderf.tagExpl import *"


function! leaderf#fileExplMaps()
Expand Down Expand Up @@ -64,6 +65,20 @@ function! leaderf#mruExplMaps()
nnoremap <buffer> <silent> c :exec g:Lf_py "mruExplManager.clearSelections()"<CR>
endfunction

function! leaderf#tagExplMaps()
nmapclear <buffer>
nnoremap <buffer> <silent> <CR> :exec g:Lf_py "tagExplManager.accept()"<CR>
nnoremap <buffer> <silent> o :exec g:Lf_py "tagExplManager.accept()"<CR>
nnoremap <buffer> <silent> <2-LeftMouse> :exec g:Lf_py "tagExplManager.accept()"<CR>
nnoremap <buffer> <silent> x :exec g:Lf_py "tagExplManager.accept('h')"<CR>
nnoremap <buffer> <silent> v :exec g:Lf_py "tagExplManager.accept('v')"<CR>
nnoremap <buffer> <silent> t :exec g:Lf_py "tagExplManager.accept('t')"<CR>
nnoremap <buffer> <silent> q :exec g:Lf_py "tagExplManager.quit()"<CR>
nnoremap <buffer> <silent> i :exec g:Lf_py "tagExplManager.input()"<CR>
nnoremap <buffer> <silent> <F1> :exec g:Lf_py "tagExplManager.toggleHelp()"<CR>
nnoremap <buffer> <silent> <F5> :exec g:Lf_py "tagExplManager.refresh()"<CR>
endfunction

function! leaderf#LfPy(cmd)
exec g:Lf_py . a:cmd
endfunction
Expand Down Expand Up @@ -94,3 +109,6 @@ function! leaderf#startMruExpl(win_pos, ...)
endif
endfunction

function! leaderf#startTagExpl(win_pos, ...)
call leaderf#LfPy("tagExplManager.startExplorer('".a:win_pos."')")
endfunction
3 changes: 3 additions & 0 deletions autoload/leaderf/explorer2.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ def supportsMulti(self):
def supportsNameOnly(self):
return False

def isFilePath(self):
return True

3 changes: 3 additions & 0 deletions autoload/leaderf/explorer3.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ def supportsMulti(self):
def supportsNameOnly(self):
return False

def isFilePath(self):
return True

6 changes: 3 additions & 3 deletions autoload/leaderf/fuzzyMatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ def evaluate(text, pattern, text_mask, j, pattern_mask, k, val):
special = 2 if not text[i-1].isupper() else 0
elif text[i-1] == '.':
special = 1.9
elif text[i-1] in '/\\':
special = 2
# elif not text[i-1].isalnum() : # ;,"'...
# elif text[i-1] in '/\\':
# special = 2
elif not text[i-1].isalnum() : # ;,"':...
special = 2
else:
special = 0
d = -2 # -0b10 or ~1
Expand Down
11 changes: 8 additions & 3 deletions autoload/leaderf/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,13 @@ def _createHelp(self):

def _setStlMode(self):
if self._cli.isFuzzy:
if self._cli.isFullPath:
mode = 'FullPath'
if self._getExplorer().isFilePath():
if self._cli.isFullPath:
mode = 'FullPath'
else:
mode = 'NameOnly'
else:
mode = 'NameOnly'
mode = 'Fuzzy'
else:
mode = 'Regex'
self._getInstance().setStlMode(mode)
Expand Down Expand Up @@ -488,6 +491,8 @@ def refresh(self, normal_mode=True):
self._resetHighlights()
self._getInstance().buffer.options['modifiable'] = False

self._getInstance().setStlTotal(len(self._content))

def addSelections(self):
nr = self._getInstance().window.number
if (int(lfEval("v:mouse_win")) != 0 and
Expand Down
133 changes: 133 additions & 0 deletions autoload/leaderf/tagExpl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import vim
import re
import os
import os.path
from functools import wraps
from leaderf.utils import *
from leaderf.explorer import *
from leaderf.manager import *
from leaderf.mru import *


#*****************************************************
# TagExplorer
#*****************************************************
class TagExplorer(Explorer):
def __init__(self):
self._tag_list = []

def getContent(self, *args, **kwargs):
if self._tag_list:
return self._tag_list
else:
return self.getFreshContent()

def getFreshContent(self, *args, **kwargs):
self._tag_list = []
for tagfile in vim.eval("tagfiles()"):
with lfOpen(os.path.abspath(tagfile), 'r', errors='ignore') as f:
taglist = f.readlines()
self._tag_list.extend(taglist[6:])
return self._tag_list

def getStlCategory(self):
return 'Tag'

def getStlCurDir(self):
return escQuote(lfEncode(os.getcwd()))

def isFilePath(self):
return False


#*****************************************************
# TagExplManager
#*****************************************************
class TagExplManager(Manager):
def __init__(self):
super(TagExplManager, self).__init__()
self._match_ids = []

def _getExplClass(self):
return TagExplorer

def _defineMaps(self):
lfCmd("call leaderf#tagExplMaps()")

def _acceptSelection(self, *args, **kwargs):
if len(args) == 0:
return
line = args[0]
# {tagname}<Tab>{tagfile}<Tab>{tagaddress}[;"<Tab>{tagfield}..]
tagfile, right = line.split('\t', 2)[1:]
tagaddress, tagfield = right.split(';"\t', 1)
lfCmd("hide edit %s" % escSpecial(tagfile))
if tagaddress[0] != '/':
lfCmd(tagaddress)
else:
lfCmd("norm! gg")
pattern = "\M" + tagaddress[1:-1]
lfCmd("call search('%s', 'w')" % escQuote(pattern))
lfCmd("norm! ^")

def _getDigest(self, line, mode):
"""
specify what part in the line to be processed and highlighted
Args:
mode: 0, return the full path
1, return the name only
2, return the directory name
"""
if not line:
return ''
return line.split('\t', 1)[0]

def _getDigestStartPos(self, line, mode):
"""
return the start position of the digest returned by _getDigest()
Args:
mode: 0, return the start postion of full path
1, return the start postion of name only
2, return the start postion of directory name
"""
return 0

def _createHelp(self):
help = []
help.append('" <CR>/<double-click>/o : open file under cursor')
help.append('" x : open file under cursor in a horizontally split window')
help.append('" v : open file under cursor in a vertically split window')
help.append('" t : open file under cursor in a new tabpage')
help.append('" i : switch to input mode')
help.append('" q : quit')
help.append('" <F5> : refresh the cache')
help.append('" <F1> : toggle this help')
help.append('" ---------------------------------------------------------')
return help

def _afterEnter(self):
super(TagExplManager, self)._afterEnter()
id = int(lfEval('''matchadd('Lf_hl_tagFile', '^.\{-}\t\zs.\{-}\ze\t')'''))
self._match_ids.append(id)
id = int(lfEval('''matchadd('Lf_hl_tagType', ';"\t\zs[cdefFgmpstuv]\ze\(\t\|$\)')'''))
self._match_ids.append(id)
keyword = "\(class\|enum\|file\|function\|kind\|struct\|union\)"
id = int(lfEval('''matchadd('Lf_hl_tagKeyword', ';"\t.\{-}\zs%s:\ze')''' % keyword))
self._match_ids.append(id)

def _beforeExit(self):
super(TagExplManager, self)._beforeExit()
for i in self._match_ids:
lfCmd("silent! call matchdelete(%d)" % i)
self._match_ids = []


#*****************************************************
# tagExplManager is a singleton
#*****************************************************
tagExplManager = TagExplManager()

__all__ = ['tagExplManager']
2 changes: 1 addition & 1 deletion doc/leaderf.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ g:Lf_ShowRelativePath *g:Lf_ShowRelativePath*

g:Lf_DefaultMode *g:Lf_DefaultMode*
Specify the default mode when LeaderF is launched.
There are 3 modes, and the corresponding values are:
There are 4 modes, and the corresponding values are:
'NameOnly' - fuzzy mode, match file name only when searching
'FullPath' - fuzzy mode, match full path when searching
'Fuzzy' - fuzzy mode, when lines in the result are not file path
Expand Down
1 change: 1 addition & 0 deletions plugin/leaderf.vim
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ command! -bar -nargs=0 LeaderfBuffer call leaderf#startBufExpl(g:Lf_WindowPositi
command! -bar -nargs=0 LeaderfBufferAll call leaderf#startBufExpl(g:Lf_WindowPosition, 1)
command! -bar -nargs=0 LeaderfMru call leaderf#startMruExpl(g:Lf_WindowPosition)
command! -bar -nargs=0 LeaderfMruCwd call leaderf#startMruExpl(g:Lf_WindowPosition, 1)
command! -bar -nargs=0 LeaderfTag call leaderf#startTagExpl(g:Lf_WindowPosition)


exec 'nnoremap <silent> ' g:Lf_ShortcutF ':<C-U>LeaderfFile<CR>'
Expand Down
3 changes: 3 additions & 0 deletions syntax/leaderf.vim
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ if has("syntax")
highlight def link Lf_hl_bufModified String
highlight def link Lf_hl_bufNomodifiable Comment
highlight def link Lf_hl_bufDirname Directory
highlight def link Lf_hl_tagFile Directory
highlight def link Lf_hl_tagType Type
highlight def link Lf_hl_tagKeyword Keyword
endif

let b:current_syntax = "leaderf"

0 comments on commit 8287f34

Please sign in to comment.