refactor telescope command (#398)
* refactor telescope command * addd telescope default options support * fix variable name wrong * convert command line string to lua type * add comment. * update readme for use theme in commandline * enhance complete in commandline * enhance complete in commandline * enhance covert commandline options
This commit is contained in:
@@ -510,6 +510,10 @@ nnoremap <Leader>f :lua require'telescope.builtin'.find_files(require('telescope
|
|||||||
" Change an option
|
" Change an option
|
||||||
nnoremap <Leader>f :lua require'telescope.builtin'.find_files(require('telescope.themes').get_dropdown({ winblend = 10 }))<cr>
|
nnoremap <Leader>f :lua require'telescope.builtin'.find_files(require('telescope.themes').get_dropdown({ winblend = 10 }))<cr>
|
||||||
```
|
```
|
||||||
|
or use with command:
|
||||||
|
```vim
|
||||||
|
Telescope find_files theme=get_dropdown
|
||||||
|
```
|
||||||
|
|
||||||
Themes should work with every `telescope.builtin` function. If you wish to
|
Themes should work with every `telescope.builtin` function. If you wish to
|
||||||
make theme, check out `lua/telescope/themes.lua`.
|
make theme, check out `lua/telescope/themes.lua`.
|
||||||
|
|||||||
96
lua/telescope/command.lua
Normal file
96
lua/telescope/command.lua
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
local themes = require('telescope.themes')
|
||||||
|
local builtin = require('telescope.builtin')
|
||||||
|
local extensions = require('telescope._extensions').manager
|
||||||
|
local config = require('telescope.config')
|
||||||
|
local command = {}
|
||||||
|
|
||||||
|
-- convert command line string arguments to
|
||||||
|
-- lua number boolean type and nil value
|
||||||
|
local function convert_user_opts(user_opts)
|
||||||
|
local default_opts = config.values
|
||||||
|
|
||||||
|
local _switch = {
|
||||||
|
['boolean'] = function(key,val)
|
||||||
|
if val == 'false' then
|
||||||
|
user_opts[key] = false
|
||||||
|
end
|
||||||
|
user_opts[key] = true
|
||||||
|
end,
|
||||||
|
['number'] = function(key,val)
|
||||||
|
user_opts[key] = tonumber(val)
|
||||||
|
end,
|
||||||
|
['string'] = function(key,val)
|
||||||
|
if val == 'nil' then
|
||||||
|
user_opts[key] = nil
|
||||||
|
end
|
||||||
|
if val == '""' then
|
||||||
|
user_opts[key] = ''
|
||||||
|
end
|
||||||
|
if val == '"' then
|
||||||
|
user_opts[key] = ''
|
||||||
|
end
|
||||||
|
end
|
||||||
|
}
|
||||||
|
|
||||||
|
local _switch_metatable = {
|
||||||
|
__index = function(_,k)
|
||||||
|
print(string.format('Type of %s does not match',k))
|
||||||
|
end
|
||||||
|
}
|
||||||
|
|
||||||
|
setmetatable(_switch,_switch_metatable)
|
||||||
|
|
||||||
|
for key,val in pairs(user_opts) do
|
||||||
|
if default_opts[key] == nil then
|
||||||
|
print('[Telescope] argument not exists in default options')
|
||||||
|
return
|
||||||
|
end
|
||||||
|
_switch[type(default_opts[key])](key,val)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- receive the viml command args
|
||||||
|
-- it should be a table value like
|
||||||
|
-- {
|
||||||
|
-- cmd = 'find_files',
|
||||||
|
-- theme = 'dropdown',
|
||||||
|
-- extension_type = 'command'
|
||||||
|
-- opts = {
|
||||||
|
-- cwd = '***',
|
||||||
|
-- }
|
||||||
|
function command.run_command(args)
|
||||||
|
local user_opts = args or {}
|
||||||
|
if next(user_opts) == nil and not user_opts.cmd then
|
||||||
|
print('[Telescope] your command miss args')
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local cmd = user_opts.cmd
|
||||||
|
local opts = user_opts.opts or {}
|
||||||
|
local extension_type = user_opts.extension_type or ''
|
||||||
|
local theme = user_opts.theme or ''
|
||||||
|
|
||||||
|
if next(opts) ~= nil then
|
||||||
|
convert_user_opts(opts)
|
||||||
|
end
|
||||||
|
|
||||||
|
if string.len(theme) > 0 then
|
||||||
|
opts = themes[theme](opts)
|
||||||
|
end
|
||||||
|
|
||||||
|
if string.len(extension_type) > 0 and extension_type ~= '"' then
|
||||||
|
extensions[cmd][extension_type](opts)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if builtin[cmd] then
|
||||||
|
builtin[cmd](opts)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if rawget(extensions,cmd) then
|
||||||
|
extensions[cmd][cmd](opts)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return command
|
||||||
@@ -53,53 +53,64 @@ cnoremap <silent> <Plug>(TelescopeFuzzyCommandSearch) <C-\>e
|
|||||||
\ }"<CR><CR>
|
\ }"<CR><CR>
|
||||||
|
|
||||||
" Telescope builtin lists
|
" Telescope builtin lists
|
||||||
function! s:telescope_complete(...)
|
function! s:telescope_complete(arg,line,pos)
|
||||||
let l:builtin_list = luaeval('vim.tbl_keys(require("telescope.builtin"))')
|
let l:builtin_list = luaeval('vim.tbl_keys(require("telescope.builtin"))')
|
||||||
let l:extensions_list = luaeval('vim.tbl_keys(require("telescope._extensions").manager)')
|
let l:extensions_list = luaeval('vim.tbl_keys(require("telescope._extensions").manager)')
|
||||||
return join(extend(l:builtin_list,l:extensions_list),"\n")
|
let l:options_list = luaeval('vim.tbl_keys(require("telescope.config").values)')
|
||||||
|
let ext_type = v:lua.require('telescope._extensions').manager
|
||||||
|
let l:ext_type_list = []
|
||||||
|
|
||||||
|
for val in values(ext_type)
|
||||||
|
if len(val) > 1
|
||||||
|
call extend(l:ext_type_list,keys(val))
|
||||||
|
endif
|
||||||
|
endfor
|
||||||
|
|
||||||
|
let list = [extend(l:builtin_list,l:extensions_list),l:options_list]
|
||||||
|
let l = split(a:line[:a:pos-1], '\%(\%(\%(^\|[^\\]\)\\\)\@<!\s\)\+', 1)
|
||||||
|
let n = len(l) - index(l, 'Telescope') - 2
|
||||||
|
|
||||||
|
if n == 0
|
||||||
|
return join(list[0],"\n")
|
||||||
|
endif
|
||||||
|
|
||||||
|
if n == 1
|
||||||
|
if index(l:extensions_list,l[1]) >= 0
|
||||||
|
return join(l:ext_type_list,"\n")
|
||||||
|
endif
|
||||||
|
return join(list[1],"\n")
|
||||||
|
endif
|
||||||
|
|
||||||
|
if n > 1
|
||||||
|
return join(list[1],"\n")
|
||||||
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
" TODO: If the lua datatype contains complex type,It will cause convert to
|
|
||||||
" viml datatype failed. So current doesn't support config telescope.themes
|
|
||||||
function! s:load_command(builtin,...) abort
|
function! s:load_command(builtin,...) abort
|
||||||
let opts = {}
|
let user_opts = {}
|
||||||
let type = ''
|
let user_opts.cmd = a:builtin
|
||||||
|
let user_opts.opts = {}
|
||||||
|
|
||||||
" range command args
|
" range command args
|
||||||
" if arg in lua code is table type,we split command string by `,` to vimscript
|
" if arg in lua code is table type,we split command string by `,` to vimscript
|
||||||
" list type.
|
" list type.
|
||||||
for arg in a:000
|
for arg in a:000
|
||||||
if stridx(arg,'=') < 0
|
if stridx(arg,'=') < 0
|
||||||
let type = arg
|
let user_opts.extension_type = arg
|
||||||
continue
|
continue
|
||||||
endif
|
endif
|
||||||
|
" split args by =
|
||||||
let opt = split(arg,'=')
|
let arg_list = split(arg,'=')
|
||||||
if opt[0] == 'find_command' || opt[0] == 'vimgrep_arguments'
|
if arg_list[0] == 'find_command' || arg_list[0] == 'vimgrep_arguments'
|
||||||
let opts[opt[0]] = split(opt[1],',')
|
let user_opts.opts[arg_list[0]] = split(arg_list[1],',')
|
||||||
|
elseif arg_list[0] == 'theme'
|
||||||
|
let user_opts.theme = arg_list[1]
|
||||||
else
|
else
|
||||||
let opts[opt[0]] = opt[1]
|
let user_opts.opts[arg_list[0]] = arg_list[1]
|
||||||
endif
|
endif
|
||||||
endfor
|
endfor
|
||||||
|
|
||||||
let telescope = v:lua.require('telescope.builtin')
|
call v:lua.require('telescope.command').run_command(user_opts)
|
||||||
let extensions = v:lua.require('telescope._extensions').manager
|
|
||||||
if has_key(telescope,a:builtin)
|
|
||||||
call telescope[a:builtin](opts)
|
|
||||||
return
|
|
||||||
endif
|
|
||||||
|
|
||||||
if has_key(extensions,a:builtin)
|
|
||||||
if has_key(extensions[a:builtin],a:builtin)
|
|
||||||
call extensions[a:builtin][a:builtin](opts)
|
|
||||||
return
|
|
||||||
endif
|
|
||||||
|
|
||||||
if has_key(extensions[a:builtin],type)
|
|
||||||
call extensions[a:builtin][type](opts)
|
|
||||||
endif
|
|
||||||
endif
|
|
||||||
|
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
" Telescope Commands with complete
|
" Telescope Commands with complete
|
||||||
|
|||||||
Reference in New Issue
Block a user