Skip to content

Commit

Permalink
perf(buffer previewer): improve performance (#584)
Browse files Browse the repository at this point in the history
deprecate(config): deprecate the 'env' config (#584)
  • Loading branch information
linrongbin16 authored Feb 5, 2024
1 parent 194d338 commit b7c580f
Show file tree
Hide file tree
Showing 82 changed files with 1,361 additions and 2,300 deletions.
13 changes: 8 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: JohnnyMorganz/stylua-action@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
version: latest
args: --config-path .stylua.toml ./lua ./spec ./bin
- name: Install commons.nvim
if: ${{ github.ref != 'refs/heads/main' }}
shell: bash
Expand All @@ -33,11 +38,6 @@ jobs:
cp -rf ~/.commons.nvim/lua/commons ./lua/fzfx/
cp ~/.commons.nvim/version.txt ./lua/fzfx/commons/version.txt
find ./lua/fzfx/commons -type f -name '*.lua' -exec sed -i 's/"commons./"fzfx.commons./g' {} \;
- uses: JohnnyMorganz/stylua-action@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
version: latest
args: --config-path .stylua.toml ./lua ./spec ./bin
- uses: lunarmodules/luacheck@v1
with:
args: lua --config .luacheckrc
Expand Down Expand Up @@ -73,6 +73,8 @@ jobs:
- uses: leafo/gh-actions-luarocks@v4
- name: Run Tests
run: |
sudo apt-get -y -qq install bat
luarocks install luacheck
luarocks install vusted
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install
Expand All @@ -96,6 +98,7 @@ jobs:
- uses: leafo/gh-actions-luarocks@v4
- name: Run Tests
run: |
sudo apt-get -y -qq install bat
luarocks install luacov
luarocks install luacov-reporter-lcov
luarocks install vusted
Expand Down
6 changes: 5 additions & 1 deletion .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ globals = { "vim", "describe", "before_each", "it", "assert" }
max_line_length = 500
unused = false
unused_args = false
exclude_files = { "lua/fzfx/commons/*.lua" }
exclude_files = {
"lua/fzfx/commons/*.lua",
"lua/fzfx/commons/*/*.lua",
"lua/fzfx/commons/*/*/*.lua",
}
4 changes: 3 additions & 1 deletion .luarc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"inject-field",
"deprecated",
"luadoc-miss-module-name",
"undefined-doc-name"
"undefined-doc-name",
"lowercase-global"
],
"runtime.version": "LuaJIT",
"workspace.checkThirdParty": "Disable"
}
2 changes: 1 addition & 1 deletion .stylua.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
column_width = 80
column_width = 100
line_endings = "Unix"
indent_type = "Spaces"
indent_width = 2
2 changes: 2 additions & 0 deletions .styluaignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
lua/fzfx/commons/*.lua
lua/fzfx/commons/*/*.lua
lua/fzfx/commons/*/*/*.lua
56 changes: 8 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<p>
<a href="https://github.com/neovim/neovim/releases/v0.7.0"><img alt="require" src="https://img.shields.io/badge/require-0.7+-blue" /></a>
<a href="https://github.com/linrongbin16/commons.nvim"><img alt="commons.nvim" src="https://img.shields.io/badge/power_by-commons.nvim-pink" /></a>
<a href="https://github.com/linrongbin16/commons.nvim"><img alt="commons.nvim" src="https://img.shields.io/badge/power_by-commons.nvim-EA4AAA" /></a>
<a href="https://luarocks.org/modules/linrongbin16/fzfx.nvim"><img alt="luarocks" src="https://img.shields.io/luarocks/v/linrongbin16/fzfx.nvim" /></a>
<a href="https://github.com/linrongbin16/fzfx.nvim/actions/workflows/ci.yml"><img alt="ci.yml" src="https://img.shields.io/github/actions/workflow/status/linrongbin16/fzfx.nvim/ci.yml?label=ci" /></a>
<a href="https://app.codecov.io/github/linrongbin16/fzfx.nvim"><img alt="codecov" src="https://img.shields.io/codecov/c/github/linrongbin16/fzfx.nvim/main?label=codecov" /></a>
Expand All @@ -22,8 +22,7 @@ https://github.com/linrongbin16/fzfx.nvim/assets/6496887/47b03150-14e3-479a-b1af

- [Features](#-features)
- [Requirements](#-requirements)
- [Windows](#windows)
- [Whitespace Escaping Issue](#whitespace-escaping-issue)
- [For Windows](#for-windows)
- [Install](#-install)
- [Commands](#-commands)
- [Files & Buffers](#files--buffers)
Expand All @@ -43,7 +42,7 @@ https://github.com/linrongbin16/fzfx.nvim/assets/6496887/47b03150-14e3-479a-b1af

## ✨ Features

- Icons, colors, lua and windows support.
- Colors/icons, Windows and modern Neovim features support.
- Updates on every keystroke of user query and selection.
- Multiple variants to avoid manual input:
- Search by visual select.
Expand Down Expand Up @@ -71,15 +70,15 @@ https://github.com/linrongbin16/fzfx.nvim/assets/6496887/47b03150-14e3-479a-b1af
- [delta](https://github.com/dandavison/delta) (optional for preview git **diff, show, blame**).
- [lsd](https://github.com/lsd-rs/lsd)/[eza](https://github.com/eza-community/eza) (optional for **file explorer**, by default use [ls](https://man7.org/linux/man-pages/man1/ls.1.html)).

### Windows
### For Windows

Windows users will have to install the linux shell environment, since basic linux commands such as `echo`, `curl`, `mkdir` are internally required.

<details>
<summary><i>Click here to see how to install linux commands</i></summary>
<br/>

There're many ways to install portable linux shell and builtin commands on Windows, but personally I would recommend below two methods.
There're many ways to install portable linux shell and builtin commands on Windows, but personally I would recommend below two methods:

#### [Git for Windows](https://git-scm.com/download/win)

Expand Down Expand Up @@ -115,7 +114,9 @@ scoop install curl # curl
scoop install grep # grep
```

> Note: install the rust-reimplementation [uutils-coreutils](https://github.com/uutils/coreutils) instead of GNU [coreutils](https://www.gnu.org/software/coreutils/), since some (for example `ls`) GNU commands is actually broken on Windows 10 x86_64 PC.
> [!NOTE]
>
> Install the rust rewritten [uutils-coreutils](https://github.com/uutils/coreutils) instead of GNU [coreutils](https://www.gnu.org/software/coreutils/), some GNU commands (for example `ls`) is actually broken on Windows 10 x86_64 PC.
#### Fix conflicts between embedded commands in `C:\Windows\System32` and portable linux commands

Expand All @@ -125,47 +126,6 @@ Windows actually already provide some commands (`find.exe`, `bash.exe`) in `C:\W

</details>

### Whitespace Escaping Issue

<details>
<summary><i>Click here to see how whitespace affect characters escaping</i></summary>
<br/>

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 installed in `C:\Program Files` directory.

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

```lua
require("fzfx").setup({
env = {
nvim = 'nvim',
fzf = 'fzf',
}
})
```

This will help avoid the shell command issue.

2. `C:\Users\Lin Rongbin\opt\Neovim\bin\nvim.exe`: user name (`Lin Rongbin`) contains whitespace.

We still cannot handle the 2nd case because all lua scripts in this plugin will thus always contain whitespaces in their path.

Please always avoid whitespaces in directories and file names.

</details>

## 📦 Install

> [!IMPORTANT]
Expand Down
18 changes: 4 additions & 14 deletions bin/general/previewer.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
local SELF_PATH = vim.env._FZFX_NVIM_SELF_PATH
if type(SELF_PATH) ~= "string" or string.len(SELF_PATH) == 0 then
io.write(
string.format("|fzfx.bin.general.previewer| error! SELF_PATH is empty!")
)
io.write(string.format("|fzfx.bin.general.previewer| error! SELF_PATH is empty!"))
end
vim.opt.runtimepath:append(SELF_PATH)

Expand Down Expand Up @@ -94,13 +92,8 @@ elseif metaopts.previewer_type == "command_list" then
return
end

local sp =
spawn.run(cmd_splits, { on_stdout = println, on_stderr = function() end })
shell_helpers.log_ensure(
sp ~= nil,
"failed to open async command: %s",
vim.inspect(cmd_splits)
)
local sp = spawn.run(cmd_splits, { on_stdout = println, on_stderr = function() end })
shell_helpers.log_ensure(sp ~= nil, "failed to open async command: %s", vim.inspect(cmd_splits))
sp:wait()
elseif metaopts.previewer_type == "list" then
local f = io.open(resultfile, "r")
Expand All @@ -117,8 +110,5 @@ elseif metaopts.previewer_type == "list" then
--- @diagnostic disable-next-line: need-check-nil
f:close()
else
shell_helpers.log_throw(
"unknown previewer type:%s",
vim.inspect(metajsonstring)
)
shell_helpers.log_throw("unknown previewer type:%s", vim.inspect(metajsonstring))
end
38 changes: 8 additions & 30 deletions bin/general/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ vim.opt.runtimepath:append(vim.fn.stdpath("config"))
local decorator_module = nil
if metaopts.provider_decorator ~= nil then
if strings.not_empty(tables.tbl_get(metaopts.provider_decorator, "rtp")) then
vim.opt.runtimepath:append(
tables.tbl_get(metaopts.provider_decorator, "rtp")
)
vim.opt.runtimepath:append(tables.tbl_get(metaopts.provider_decorator, "rtp"))
end
shell_helpers.log_ensure(
strings.not_empty(tables.tbl_get(metaopts.provider_decorator, "module")),
Expand All @@ -98,8 +96,7 @@ local function println(line)
if tables.tbl_not_empty(decorator_module) then
-- shell_helpers.log_debug("decorate line:%s", vim.inspect(line))
vim.schedule(function()
local rendered_ok, rendered_line_or_err =
pcall(decorator_module.decorate, line)
local rendered_ok, rendered_line_or_err = pcall(decorator_module.decorate, line)
if rendered_ok then
io.write(string.format("%s\n", rendered_line_or_err))
else
Expand All @@ -125,21 +122,14 @@ if metaopts.provider_type == "plain" or metaopts.provider_type == "command" then
end

local p = io.popen(cmd)
shell_helpers.log_ensure(
p ~= nil,
"failed to open pipe on provider cmd! %s",
vim.inspect(cmd)
)
shell_helpers.log_ensure(p ~= nil, "failed to open pipe on provider cmd! %s", vim.inspect(cmd))
---@diagnostic disable-next-line: need-check-nil
for line in p:lines("*line") do
println(line)
end
---@diagnostic disable-next-line: need-check-nil
p:close()
elseif
metaopts.provider_type == "plain_list"
or metaopts.provider_type == "command_list"
then
elseif metaopts.provider_type == "plain_list" or metaopts.provider_type == "command_list" then
local cmd = fileios.readfile(resultfile, { trim = true }) --[[@as string]]
shell_helpers.log_debug("plain_list/command_list cmd:%s", vim.inspect(cmd))
if strings.empty(cmd) then
Expand All @@ -153,30 +143,18 @@ then
return
end

local sp =
spawn.run(cmd_splits, { on_stdout = println, on_stderr = function() end })
shell_helpers.log_ensure(
sp ~= nil,
"failed to open async command: %s",
vim.inspect(cmd_splits)
)
local sp = spawn.run(cmd_splits, { on_stdout = println, on_stderr = function() end })
shell_helpers.log_ensure(sp ~= nil, "failed to open async command: %s", vim.inspect(cmd_splits))
sp:wait()
elseif metaopts.provider_type == "list" then
local reader = fileios.FileLineReader:open(resultfile) --[[@as commons.FileLineReader ]]
shell_helpers.log_ensure(
reader ~= nil,
"failed to open resultfile: %s",
vim.inspect(resultfile)
)
shell_helpers.log_ensure(reader ~= nil, "failed to open resultfile: %s", vim.inspect(resultfile))

while reader:has_next() do
local line = reader:next()
println(line)
end
reader:close()
else
shell_helpers.log_throw(
"unknown provider type:%s",
vim.inspect(metajsonstring)
)
shell_helpers.log_throw("unknown provider type:%s", vim.inspect(metajsonstring))
end
24 changes: 24 additions & 0 deletions docs/KnownIssues.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
# Known Issues

## Whitespace Escaping Issue

This plugin internally extends `nvim`, `fzf` and lua scripts to full path when launching the underground fzf 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, escaping fzf command becomes quite difficult, since it will seriously affected single/double quotes. Here're two typical cases:

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

?> Please add executables (`nvim.exe`, `fzf.exe`) to `%PATH%` (`$env:PATH` in PowerShell), so let this plugin find them.

2. `C:\Users\Lin Rongbin\opt\Neovim\bin\nvim.exe` - User name (`Lin Rongbin`) contains whitespace.

!> We still cannot handle the 2nd case because all lua scripts in this plugin will thus always contain whitespaces in their path.

Please always avoid whitespaces in directories and file names.

## Previewer

### 1. Cannot use both nvim buffer previewer and fzf's builtin-previewer in the same command?
Expand Down
5 changes: 1 addition & 4 deletions lua/fzfx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ local function setup(opts)

-- cache
if vim.fn.filereadable(configs.cache.dir) > 0 then
log.throw(
"the 'cache.dir' (%s) already exist but not a directory!",
configs.cache.dir
)
log.throw("the 'cache.dir' (%s) already exist but not a directory!", configs.cache.dir)
else
vim.fn.mkdir(configs.cache.dir, "p")
end
Expand Down
Loading

0 comments on commit b7c580f

Please sign in to comment.