Skip to content

Commit

Permalink
Add comamnd LeaderfFiletype (Yggdroot#454)
Browse files Browse the repository at this point in the history
* Add feature LeaderF filetype

* Update doc, README

Add filetype description

* Add LeaderfFiletype to selfExpl

* Fix help (F1) of LeaderfFiletype

* Use getFreshContent()
  • Loading branch information
tamago324 authored and Yggdroot committed Dec 30, 2019
1 parent 3514b21 commit cdbfd9b
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ optional arguments:
subcommands:
{file,tag,function,mru,searchHistory,cmdHistory,help,line,colorscheme,gtags,self,bufTag,buffer,rg}
{file,tag,function,mru,searchHistory,cmdHistory,help,line,colorscheme,gtags,self,bufTag,buffer,rg,filetype}
file search files
tag navigate tags using the tags file
function navigate functions or methods in the buffer
Expand All @@ -140,6 +140,7 @@ subcommands:
bufTag navigate tags in the buffer
buffer search buffers
rg grep using rg
filetype navigate the filetype
If [!] is given, enter normal mode directly.
```
Expand Down
2 changes: 2 additions & 0 deletions autoload/leaderf/Any.vim
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ let g:Lf_Helps = {
\ "self": "execute the commands of itself",
\ "rg": "grep using rg",
\ "gtags": "navigate tags using the gtags",
\ "filetype": "navigate the filetype",
\ }

let g:Lf_Arguments = {
Expand Down Expand Up @@ -171,6 +172,7 @@ let g:Lf_Arguments = {
\ {"name": ["--result"], "nargs": 1, "choices": ["ctags", "ctags-x", "ctags-mod"], "metavar": "<FORMAT>", "help": "Show result using format, which may be one of: `ctags`(default), `ctags-x`, `ctags-mod`."},
\ {"name": ["--auto-jump"], "nargs": "?", "metavar": "<TYPE>", "help": "Jump to the tag directly when there is only one match. <TYPE> can be 'h', 'v' or 't', which mean jump to a horizontally, vertically split window, or a new tabpage respectively. If <TYPE> is omitted, jump to a position in current window."},
\ ],
\ "filetype": [],
\}

let g:Lf_CommonArguments = [
Expand Down
29 changes: 29 additions & 0 deletions autoload/leaderf/Filetype.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
" ============================================================================
" File: Filetype.vim
" Description:
" Author: tamago324 <[email protected]>
" Website: https://github.com/tamago324
" Note:
" License: Apache License, Version 2.0
" ============================================================================

if leaderf#versionCheck() == 0
finish
endif

exec g:Lf_py "from leaderf.filetypeExpl import *"

function! leaderf#Filetype#Maps()
nmapclear <buffer>
nnoremap <buffer> <silent> <CR> :exec g:Lf_py "filetypeExplManager.accept()"<CR>
nnoremap <buffer> <silent> o :exec g:Lf_py "filetypeExplManager.accept()"<CR>
nnoremap <buffer> <silent> <2-LeftMouse> :exec g:Lf_py "filetypeExplManager.accept()"<CR>
nnoremap <buffer> <silent> q :exec g:Lf_py "filetypeExplManager.quit()"<CR>
nnoremap <buffer> <silent> i :exec g:Lf_py "filetypeExplManager.input()"<CR>
nnoremap <buffer> <silent> <F1> :exec g:Lf_py "filetypeExplManager.toggleHelp()"<CR>
if has_key(g:Lf_NormalMap, "Filetype")
for i in g:Lf_NormalMap["Filetype"]
exec 'nnoremap <buffer> <silent> '.i[0].' '.i[1]
endfor
endif
endfunction
3 changes: 3 additions & 0 deletions autoload/leaderf/python/leaderf/anyExpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,9 @@ def _default_action(self, category, positional_args, arguments, *args, **kwargs)
elif category == "gtags":
from .gtagsExpl import gtagsExplManager
manager = gtagsExplManager
elif category == "filetype":
from .filetypeExpl import filetypeExplManager
manager = filetypeExplManager
else:
import ctypes
manager_id = lfFunction(lfEval("g:Lf_PythonExtensions['%s'].manager_id" % category))()
Expand Down
81 changes: 81 additions & 0 deletions autoload/leaderf/python/leaderf/filetypeExpl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import os.path
from leaderf.utils import *
from leaderf.explorer import *
from leaderf.manager import *


# *****************************************************
# FiletypeExplorer
# *****************************************************
class FiletypeExplorer(Explorer):
def __init__(self):
self._content = []

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

def getFreshContent(self, *args, **kwargs):
result = [
os.path.basename(f).replace(".vim", "")
for f in lfEval("globpath(&rtp, 'syntax/*.vim')").split("\n")
]

# to unique
self._content = sorted(set(x for x in result))

return self._content

def getStlCategory(self):
return "Filetype"

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


# *****************************************************
# FiletypeExplManager
# *****************************************************
class FiletypeExplManager(Manager):
def __init__(self):
super(FiletypeExplManager, self).__init__()

def _getExplClass(self):
return FiletypeExplorer

def _defineMaps(self):
lfCmd("call leaderf#Filetype#Maps()")

def _acceptSelection(self, *args, **kwargs):
if len(args) == 0:
return
lfCmd("set filetype=" + args[0])

def _getDigest(self, line, mode):
return line

def _getDegestStartPos(self, line, mode):
return 0

def _createHelp(self):
help = []
help.append('" <CR>/o : set filetype under cursor')
help.append('" q : quit')
help.append('" i : switch to input mode')
help.append('" <F1> : toggle this help')
help.append('" ---------------------------------------------------------')
return help


# *****************************************************
# filetypeExplManager is a singleton
# *****************************************************
filetypeExplManager = FiletypeExplManager()

__all__ = ["filetypeExplManager"]
8 changes: 4 additions & 4 deletions autoload/leaderf/python/leaderf/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ def _andModeFilter(self, iterable):
filter_method = partial(fuzzyEngine.fuzzyMatchEx, engine=self._fuzzy_engine, pattern=pattern,
is_name_only=False, sort_results=False)
elif self._getExplorer().getStlCategory() in ["Self", "Buffer", "Mru", "BufTag",
"Function", "History", "Cmd_History", "Search_History", "Tag", "Rg"]:
"Function", "History", "Cmd_History", "Search_History", "Tag", "Rg", "Filetype"]:
filter_method = partial(fuzzyEngine.fuzzyMatchEx, engine=self._fuzzy_engine, pattern=pattern,
is_name_only=True, sort_results=False)
else:
Expand Down Expand Up @@ -1087,7 +1087,7 @@ def _andModeFilter(self, iterable):
self._cli.isFullPath,
fuzzy_match.getWeight2)
elif self._getExplorer().getStlCategory() in ["Self", "Buffer", "Mru", "BufTag",
"Function", "History", "Cmd_History", "Search_History", "Tag", "Rg"]:
"Function", "History", "Cmd_History", "Search_History", "Tag", "Rg", "Filetype"]:
filter_method = partial(self._fuzzyFilterEx,
self._cli.isFullPath,
fuzzy_match.getWeight3)
Expand Down Expand Up @@ -1224,7 +1224,7 @@ def _fuzzySearch(self, content, is_continue, step):
filter_method = partial(fuzzyEngine.fuzzyMatchEx, engine=self._fuzzy_engine, pattern=pattern,
is_name_only=True, sort_results=not is_continue)
elif self._getExplorer().getStlCategory() in ["Self", "Buffer", "Mru", "BufTag",
"Function", "History", "Cmd_History", "Search_History", "Tag"]:
"Function", "History", "Cmd_History", "Search_History", "Tag", "Filetype"]:
return_index = True
filter_method = partial(fuzzyEngine.fuzzyMatchEx, engine=self._fuzzy_engine, pattern=pattern,
is_name_only=True, sort_results=not is_continue)
Expand Down Expand Up @@ -1255,7 +1255,7 @@ def _fuzzySearch(self, content, is_continue, step):
self._cli.isFullPath,
fuzzy_match.getWeight2)
elif self._getExplorer().getStlCategory() in ["Self", "Buffer", "Mru", "BufTag",
"Function", "History", "Cmd_History", "Search_History", "Rg"]:
"Function", "History", "Cmd_History", "Search_History", "Rg", "Filetype"]:
filter_method = partial(self._fuzzyFilter,
self._cli.isFullPath,
fuzzy_match.getWeight3)
Expand Down
3 changes: 2 additions & 1 deletion autoload/leaderf/python/leaderf/selfExpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def __init__(self):
'15 LeaderfHistoryCmd "execute the command in the history"',
'16 LeaderfHistorySearch "execute the search command in the history"',
'17 LeaderfHelp "navigate the help tags"',
'18 LeaderfColorscheme "switch between colorschemes"'
'18 LeaderfColorscheme "switch between colorschemes"',
'19 LeaderfFiletype "navigate the filetype"',
]

def getContent(self, *args, **kwargs):
Expand Down
3 changes: 3 additions & 0 deletions doc/leaderf.txt
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,9 @@ USAGE *leaderf-usage*
:LeaderfColorscheme *LeaderfColorscheme*
Launch LeaderF to switch between colorschemes.

:LeaderfFiletype *LeaderfFiletype*
Launch LeaderF to navigate the filetype.

:LeaderfRgInteractive *LeaderfRgInteractive*
Launch LeaderF to use rg interactively.

Expand Down
2 changes: 2 additions & 0 deletions plugin/leaderf.vim
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ command! -bar -nargs=0 LeaderfColorscheme Leaderf colorscheme
command! -bar -nargs=0 LeaderfRgInteractive call leaderf#Rg#Interactive()
command! -bar -nargs=0 LeaderfRgRecall exec "Leaderf! rg --recall"

command! -bar -nargs=0 LeaderfFiletype Leaderf filetype

try
if g:Lf_ShortcutF != ""
exec 'nnoremap <silent><unique> ' g:Lf_ShortcutF ':<C-U>LeaderfFile<CR>'
Expand Down

0 comments on commit cdbfd9b

Please sign in to comment.