diff --git a/README.md b/README.md index 98b9509..11877d9 100644 --- a/README.md +++ b/README.md @@ -98,11 +98,15 @@ run any fzf-lua command like this: ```lua :lua require('fzf-lua').files() +-- or using the `FzfLua` vim command: +:FzfLua files ``` or with arguments: ```lua :lua require('fzf-lua').files({ cwd = '~/.config' }) +-- or using the `FzfLua` vim command: +:FzfLua files cwd=~/.config ``` which can be easily mapped to: diff --git a/lua/fzf-lua/cmd.lua b/lua/fzf-lua/cmd.lua new file mode 100644 index 0000000..c12d34c --- /dev/null +++ b/lua/fzf-lua/cmd.lua @@ -0,0 +1,107 @@ +-- Modified from Telescope 'command.lua' +if not pcall(require, "fzf") then + return +end + +local builtin = require "fzf-lua" +local utils = require "fzf-lua.utils" +local command = {} + +local arg_value = { + ["nil"] = nil, + ['""'] = "", + ['"'] = "", +} + +local bool_type = { + ["false"] = false, + ["true"] = true, +} + +-- convert command line string arguments to +-- lua number boolean type and nil value +local function convert_user_opts(user_opts) + + local _switch = { + ["boolean"] = function(key, val) + if val == "false" then + user_opts[key] = false + return + end + user_opts[key] = true + end, + ["number"] = function(key, val) + user_opts[key] = tonumber(val) + end, + ["string"] = function(key, val) + if arg_value[val] ~= nil then + user_opts[key] = arg_value[val] + return + end + + if bool_type[val] ~= nil then + user_opts[key] = bool_type[val] + end + end, + } + + local _switch_metatable = { + __index = function(_, k) + utils.info(string.format("Type of %s does not match", k)) + end, + } + + setmetatable(_switch, _switch_metatable) + + for key, val in pairs(user_opts) do + _switch["string"](key, val) + end +end + +-- receive the viml command args +-- it should output a table value like +-- { +-- cmd = 'files', +-- opts = { +-- cwd = '***', +-- } +local function run_command(args) + local user_opts = args or {} + if next(user_opts) == nil and not user_opts.cmd then + utils.info("[Fzf-lua] missing command args") + return + end + + local cmd = user_opts.cmd + local opts = user_opts.opts or {} + + if next(opts) ~= nil then + convert_user_opts(opts) + end + + if builtin[cmd] then + builtin[cmd](opts) + return + end +end + +function command.load_command(cmd, ...) + local args = { ... } + if cmd == nil then + run_command { cmd = "builtin" } + return + end + + local user_opts = {} + user_opts["cmd"] = cmd + user_opts.opts = {} + + for _, arg in ipairs(args) do + local param = vim.split(arg, "=") + user_opts.opts[param[1]] = param[2] + end + + run_command(user_opts) +end + +return command diff --git a/plugin/fzf-lua.vim b/plugin/fzf-lua.vim index c78ed4a..48a5d61 100644 --- a/plugin/fzf-lua.vim +++ b/plugin/fzf-lua.vim @@ -1,8 +1,23 @@ -if exists('g:loaded_fzf_lua') | finish | endif - if !has('nvim-0.5') echohl Error echomsg "Fzf-lua is only available for Neovim versions 0.5 and above" echohl clear finish endif + +if exists('g:loaded_fzf_lua') | finish | endif +let g:loaded_fzf_lua = 1 + +" FzfLua builtin lists +function! s:fzflua_complete(arg,line,pos) + let l:builtin_list = luaeval('vim.tbl_keys(require("fzf-lua"))') + + let list = [l:builtin_list] + let l = split(a:line[:a:pos-1], '\%(\%(\%(^\|[^\\]\)\\\)\@)