Skip to content

Commit

Permalink
perf(config): migrate to 'cfg.lsp_diagnostics' (#459)
Browse files Browse the repository at this point in the history
perf(lib): add api `fzfx.lib.tables.tbl_get`
  • Loading branch information
linrongbin16 authored Dec 6, 2023
1 parent f3488fc commit 0d3d0b8
Show file tree
Hide file tree
Showing 15 changed files with 483 additions and 228 deletions.
33 changes: 15 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,17 @@ Windows actually already provide some commands (`find.exe`, `bash.exe`) in `C:\W

This plugin internally extends `nvim`, `fzf` and lua scripts to full path when launching command.

1. Example on macOS:

`/Users/rlin/.config/nvim/lazy/fzf/bin/fzf --print-query --listen --query '' --preview 'nvim -n -u NONE --clean --headless -l /Users/rlin/.local/share/nvim/site/pack/test/start/fzfx.nvim/bin/general/previewer.lua 2 /Users/rlin/.local/share/nvim/fzfx.nvim/previewer_metafile /Users/rlin/.local/share/nvim/fzfx.nvim/previewer_resultfile {}' --bind 'start:execute-silent(echo $FZF_PORT>/Users/rlin/.local/share/nvim/fzfx.nvim/fzf_port_file)' --multi --preview-window 'left,65%,+{2}-/2' --border=none --delimiter ':' --prompt 'Incoming Calls > ' --expect 'esc' --expect 'double-click' --expect 'enter' >/var/folders/5p/j4q6bz395fbbxdf_6b95_nz80000gp/T/nvim.rlin/fIj5xA/2`

2. Example on Windows 10:

`C:/Users/linrongbin/github/junegunn/fzf/bin/fzf --query "" --header ":: Press \27[38;2;255;121;198mCTRL-U\27[0m to unrestricted mode" --prompt "~/g/l/fzfx.nvim > " --bind "start:unbind(ctrl-r)" --bind "ctrl-u:unbind(ctrl-u)+execute-silent(C:\\Users\\linrongbin\\scoop\\apps\\neovim\\current\\bin\\nvim.exe -n --clean --headless -l C:\\Users\\linrongbin\\github\\linrongbin16\\fzfx.nvim\\bin\\rpc\\client.lua 1)+change-header(:: Press \27[38;2;255;121;198mCTRL-R\27[0m to restricted mode)+rebind(ctrl-r)+reload(C:\\Users\\linrongbin\\scoop\\apps\\neovim\\current\\bin\\nvim.exe -n --clean --headless -l C:\\Users\\linrongbin\\github\\linrongbin16\\fzfx.nvim\\bin\\files\\provider.lua C:\\Users\\linrongbin\\AppData\\Local\\nvim-data\\fzfx.nvim\\switch_files_provider)" --bind "ctrl-r:unbind(ctrl-r)+execute-silent(C:\\Users\\linrongbin\\scoop\\apps\\neovim\\current\\bin\\nvim.exe -n --clean --headless -l C:\\Users\\linrongbin\\github\\linrongbin16\\fzfx.nvim\\bin\\rpc\\client.lua 1)+change-header(:: Press \27[38;2;255;121;198mCTRL-U\27[0m to unrestricted mode)+rebind(ctrl-u)+reload(C:\\Users\\linrongbin\\scoop\\apps\\neovim\\current\\bin\\nvim.exe -n --clean --headless -l C:\\Users\\linrongbin\\github\\linrongbin16\\fzfx.nvim\\bin\\files\\provider.lua C:\\Users\\linrongbin\\AppData\\Local\\nvim-data\\fzfx.nvim\\switch_files_provider)" --preview "C:\\Users\\linrongbin\\scoop\\apps\\neovim\\current\\bin\\nvim.exe -n --clean --headless -l C:\\Users\\linrongbin\\github\\linrongbin16\\fzfx.nvim\\bin\\files\\previewer.lua {}" --bind "ctrl-l:toggle-preview" --expect "enter" --expect "double-click" >C:\\Users\\LINRON~1\\AppData\\Local\\Temp\\nvim.0\\JSmP06\\2`

But when there're whitespaces on the path, launching correct shell command becomes quite difficult, since it will seriously affected escaping characters. Here're two typical cases:

1. `C:\Program Files\Neovim\bin\nvim.exe` - `nvim.exe` installed in `C:\Program Files` directory.
1. `C:\Program Files\Neovim\bin\nvim.exe`: nvim installed in `C:\Program Files` directory.

Please add executables (`nvim.exe`, `fzf.exe`) to `%PATH%` (`$env:PATH` in PowerShell), and set the `env` configuration:

Expand All @@ -149,21 +157,13 @@ But when there're whitespaces on the path, launching correct shell command becom
})
```

This will help fzfx.nvim avoid the shell command issue.

2. `C:\Users\Lin Rongbin\opt\Neovim\bin\nvim.exe` or `/Users/linrongbin/Library/Application\ Support/Neovim/bin/nvim` - `Lin Rongbin` (user name) or `Application Support` (macOS application) contains whitespace.

We still cannot handle the 2nd case for now, please always try to avoid whitespaces in path.
This will help avoid the shell command issue.

Here's an example of searching files command (macOS):
2. `C:\Users\Lin Rongbin\opt\Neovim\bin\nvim.exe`: user name (`Lin Rongbin`) contains whitespace.

- `/opt/homebrew/bin/nvim -n --clean --headless -l /Users/linrongbin/.local/share/nvim/lazy/fzfx.nvim/bin/files/provider.lua /tmp/nvim.linrongbin/3NXwys/0`
We still cannot handle the 2nd case because all lua scripts in this plugin will thus always contain whitespaces in their path.

Here's an example of launching fzf command (Windows 10):

- `C:/Users/linrongbin/github/junegunn/fzf/bin/fzf --query "" --header ":: Press \27[38;2;255;121;198mCTRL-U\27[0m to unrestricted mode" --prompt "~/g/l/fzfx.nvim > " --bind "start:unbind(ctrl-r)" --bind "ctrl-u:unbind(ctrl-u)+execute-silent(C:\\Users\\linrongbin\\scoop\\apps\\neovim\\current\\bin\\nvim.exe -n --clean --headless -l C:\\Users\\linrongbin\\github\\linrongbin16\\fzfx.nvim\\bin\\rpc\\client.lua 1)+change-header(:: Press \27[38;2;255;121;198mCTRL-R\27[0m to restricted mode)+rebind(ctrl-r)+reload(C:\\Users\\linrongbin\\scoop\\apps\\neovim\\current\\bin\\nvim.exe -n --clean --headless -l C:\\Users\\linrongbin\\github\\linrongbin16\\fzfx.nvim\\bin\\files\\provider.lua C:\\Users\\linrongbin\\AppData\\Local\\nvim-data\\fzfx.nvim\\switch_files_provider)" --bind "ctrl-r:unbind(ctrl-r)+execute-silent(C:\\Users\\linrongbin\\scoop\\apps\\neovim\\current\\bin\\nvim.exe -n --clean --headless -l C:\\Users\\linrongbin\\github\\linrongbin16\\fzfx.nvim\\bin\\rpc\\client.lua 1)+change-header(:: Press \27[38;2;255;121;198mCTRL-U\27[0m to unrestricted mode)+rebind(ctrl-u)+reload(C:\\Users\\linrongbin\\scoop\\apps\\neovim\\current\\bin\\nvim.exe -n --clean --headless -l C:\\Users\\linrongbin\\github\\linrongbin16\\fzfx.nvim\\bin\\files\\provider.lua C:\\Users\\linrongbin\\AppData\\Local\\nvim-data\\fzfx.nvim\\switch_files_provider)" --preview "C:\\Users\\linrongbin\\scoop\\apps\\neovim\\current\\bin\\nvim.exe -n --clean --headless -l C:\\Users\\linrongbin\\github\\linrongbin16\\fzfx.nvim\\bin\\files\\previewer.lua {}" --bind "ctrl-l:toggle-preview" --expect "enter" --expect "double-click" >C:\\Users\\LINRON~1\\AppData\\Local\\Temp\\nvim.0\\JSmP06\\2`

If the path contains whitespace, that will make all lua scripts contains whitespace, thus the shell command cannot being correctly evaluated.
Please always avoid whitespaces in directories and file names.

</details>

Expand Down Expand Up @@ -213,7 +213,6 @@ end)

```lua
require("lazy").setup({

-- optional for icons
{ "nvim-tree/nvim-web-devicons" },

Expand All @@ -231,7 +230,6 @@ require("lazy").setup({
require("fzfx").setup()
end,
},

})
```

Expand Down Expand Up @@ -868,7 +866,7 @@ Lsp methods:
## 📌 Recommended Key Mappings

<details>
<summary><i>Click here to see how to configure vimscripts</i></summary>
<summary><i>Click here to see vim scripts</i></summary>
<br/>

```vim
Expand Down Expand Up @@ -983,10 +981,9 @@ nnoremap <space>xp :\<C-U>FzfxFileExplorer<CR>
```

</details>
<br/>

<details>
<summary><i>Click here to see how to configure lua scripts</i></summary>
<summary><i>Click here to see lua scripts</i></summary>
<br/>

```lua
Expand Down
1 change: 1 addition & 0 deletions REFERENCES.md
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ curl
#### Table

- `tbl_empty(t:any):boolean`/`tbl_not_empty(t:any):boolean`: detect whether a table is empty or not.
- `tbl_get(t:any, field:string):any`: retrieve value from table, with json-like field indexing via dot `.` delimiter, for example when parameter `t = {a = { b = 1 }}`, `field = 'a.b'`, this function will return `1`. When `field = ''` returns `t` itself, when field not exist returns `nil`.

#### List

Expand Down
2 changes: 1 addition & 1 deletion lua/fzfx/cfg/git_branches.lua
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ M._make_git_branches_provider = function(opts)
string.format("* %s", git_current_branch_cmd:output())
)
local git_branches_cmd = cmds.GitBranchesCommand:run(
(type(opts) == "table" and opts.remote_branch) and true or false
tbls.tbl_get(opts, "remote_branch") and true or false
)
if git_branches_cmd:failed() then
log.echo(
Expand Down
2 changes: 1 addition & 1 deletion lua/fzfx/cfg/git_commits.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ M._make_git_commits_provider = function(opts)
log.echo(LogLevels.INFO, "not in git repo.")
return nil
end
if type(opts) == "table" and opts.buffer then
if tbls.tbl_get(opts, "buffer") then
if not nvims.buf_is_valid(context.bufnr) then
log.echo(
LogLevels.INFO,
Expand Down
3 changes: 1 addition & 2 deletions lua/fzfx/cfg/git_files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ M._make_git_files_provider = function(opts)
log.echo(LogLevels.INFO, "not in git repo.")
return nil
end
return (type(opts) == "table" and opts.current_folder)
and { "git", "ls-files" }
return tbls.tbl_get(opts, "current_folder") and { "git", "ls-files" }
or { "git", "ls-files", ":/" }
end
return impl
Expand Down
2 changes: 1 addition & 1 deletion lua/fzfx/cfg/git_live_grep.lua
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ M.fzf_opts = {
consts.FZF_OPTS.MULTI,
consts.FZF_OPTS.DISABLED,
consts.FZF_OPTS.DELIMITER,
consts.FZF_OPTS.GREP_PREVIEW_WINDOW,
{ "--prompt", "Git Live Grep > " },
{ "--preview-window", "+{2}-/2" },
}

M.other_opts = {
Expand Down
28 changes: 5 additions & 23 deletions lua/fzfx/cfg/live_grep.lua
Original file line number Diff line number Diff line change
Expand Up @@ -186,20 +186,6 @@ M._get_buf_path = function(bufnr)
return bufpath
end

--- @param opts {unrestricted:boolean?,buffer:boolean?}?
--- @return boolean
M._is_unrestricted_mode = function(opts)
---@diagnostic disable-next-line: need-check-nil
return tbls.tbl_not_empty(opts) and opts.unrestricted --[[@as boolean]]
end

--- @param opts {unrestricted:boolean?,buffer:boolean?}?
--- @return boolean
M._is_buffer_mode = function(opts)
---@diagnostic disable-next-line: need-check-nil
return tbls.tbl_not_empty(opts) and opts.buffer --[[@as boolean]]
end

--- @param args_list string[]
--- @param option string?
--- @return string[]
Expand Down Expand Up @@ -229,16 +215,14 @@ M._make_provider_rg = function(opts)
local option = parsed.option

local args = nil
if M._is_unrestricted_mode(opts) then
args = vim.deepcopy(providers_helper.UNRESTRICTED_RG)
elseif M._is_buffer_mode(opts) then
if tbls.tbl_get(opts, "unrestricted") or tbls.tbl_get(opts, "buffer") then
args = vim.deepcopy(providers_helper.UNRESTRICTED_RG)
else
args = vim.deepcopy(providers_helper.RESTRICTED_RG)
end
args = M._append_options(args, option)

if M._is_buffer_mode(opts) then
if tbls.tbl_get(opts, "buffer") then
local bufpath = M._get_buf_path(context.bufnr)
if not bufpath then
return nil
Expand All @@ -265,16 +249,14 @@ M._make_provider_grep = function(opts)
local option = parsed.option

local args = nil
if M._is_unrestricted_mode(opts) then
args = vim.deepcopy(providers_helper.UNRESTRICTED_GREP)
elseif M._is_buffer_mode(opts) then
if tbls.tbl_get(opts, "unrestricted") or tbls.tbl_get(opts, "buffer") then
args = vim.deepcopy(providers_helper.UNRESTRICTED_GREP)
else
args = vim.deepcopy(providers_helper.RESTRICTED_GREP)
end
args = M._append_options(args, option)

if M._is_buffer_mode(opts) then
if tbls.tbl_get(opts, "buffer") then
local bufpath = M._get_buf_path(context.bufnr)
if not bufpath then
return nil
Expand Down Expand Up @@ -380,8 +362,8 @@ M.fzf_opts = {
consts.FZF_OPTS.MULTI,
consts.FZF_OPTS.DISABLED,
consts.FZF_OPTS.DELIMITER,
consts.FZF_OPTS.GREP_PREVIEW_WINDOW,
{ "--prompt", "Live Grep > " },
{ "--preview-window", "+{2}-/2" },
}

M.other_opts = {
Expand Down
Loading

0 comments on commit 0d3d0b8

Please sign in to comment.