Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(previewer): enable buffer previewer for files #591

Merged
merged 36 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions .prettierrc

This file was deleted.

28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ https://github.com/linrongbin16/fzfx.nvim/assets/6496887/47b03150-14e3-479a-b1af

## ✅ Requirements

> [!IMPORTANT]
>
> Always keep fzf upgrade to the latest version, you could do that by installing as a nvim plugin (see [Install](#-install)).

- neovim ≥ 0.7.
- [fzf](https://github.com/junegunn/fzf).
- [nerd fonts](https://www.nerdfonts.com/) (optional for icons).
Expand Down Expand Up @@ -130,7 +134,7 @@ Windows actually already provide some commands (`find.exe`, `bash.exe`) in `C:\W

> [!IMPORTANT]
>
> Specify a version/tag (such as `v5.*`) to avoid break changes between major versions!
> Specify plugin version/tag (e.g. `v5.*`) to avoid break changes between major versions!

<details>
<summary><b>With <a href="https://github.com/folke/lazy.nvim">lazy.nvim</a></b></summary>
Expand Down Expand Up @@ -1245,6 +1249,28 @@ For complete default options, please see [config.lua](https://github.com/linrong

For advanced configurations, please check [Advanced Configuration](https://github.com/linrongbin16/fzfx.nvim/wiki/Advanced-Configuration).

To globally enable/disable some features:

- `fzfx_disable_buffer_previewer`: Disable nvim buffer for previewing file contents, use fzf builtin preview window with `bat`, which has the best performance.

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

```vim
" vim scripts

let g:fzfx_disable_buffer_previewer=1 " or v:true
```

```lua
-- lua scripts

vim.g.fzfx_disable_buffer_previewer=1 -- or true
```

</details>

### Create Your Own Command

Here's a minimal commands group example that implement the `ls -1` like command `FzfxLs`:
Expand Down
42 changes: 23 additions & 19 deletions lua/fzfx/cfg/files.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local consts = require("fzfx.lib.constants")
local paths = require("fzfx.commons.paths")
local constants = require("fzfx.lib.constants")
local switches = require("fzfx.lib.switches")

local actions_helper = require("fzfx.helper.actions")
local labels_helper = require("fzfx.helper.previewer_labels")
Expand Down Expand Up @@ -79,9 +80,9 @@ M.variants = {
},
}

local restricted_provider = consts.HAS_FD and providers_helper.RESTRICTED_FD
local restricted_provider = constants.HAS_FD and providers_helper.RESTRICTED_FD
or providers_helper.RESTRICTED_FIND
local unrestricted_provider = consts.HAS_FD and providers_helper.UNRESTRICTED_FD
local unrestricted_provider = constants.HAS_FD and providers_helper.UNRESTRICTED_FD
or providers_helper.UNRESTRICTED_FIND

M.providers = {
Expand All @@ -97,27 +98,30 @@ M.providers = {
},
}

M.previewers = {
restricted_mode = {
previewer = previewers_helper.preview_files_find,
previewer_type = PreviewerTypeEnum.COMMAND_LIST,
-- if you want to use fzf-builtin previewer with bat, please use below configs:
--
-- previewer = previewers_helper.preview_files_find
-- previewer_type = PreviewerTypeEnum.COMMAND_LIST

-- if you want to try experimental buffer previewer, please enable below configs:
--
-- previewer = previewers_helper.buffer_preview_files_find,
-- previewer_type = PreviewerTypeEnum.BUFFER_FILE,
-- if you want to use nvim buffer previewer, please use below configs:
--
-- previewer = previewers_helper.buffer_preview_files_find
-- previewer_type = PreviewerTypeEnum.BUFFER_FILE

local previewer = switches.buffer_previewer_disabled() and previewers_helper.preview_files_find
or previewers_helper.buffer_preview_files_find
local previewer_type = switches.buffer_previewer_disabled() and PreviewerTypeEnum.COMMAND_LIST
or PreviewerTypeEnum.BUFFER_FILE

M.previewers = {
restricted_mode = {
previewer = previewer,
previewer_type = previewer_type,
previewer_label = labels_helper.label_find,
},
unrestricted_mode = {
previewer = previewers_helper.preview_files_find,
previewer_type = PreviewerTypeEnum.COMMAND_LIST,

-- if you want to try experimental buffer previewer, please enable below configs:
--
-- previewer = previewers_helper.buffer_preview_files_find,
-- previewer_type = PreviewerTypeEnum.BUFFER_FILE,

previewer = previewer,
previewer_type = previewer_type,
previewer_label = labels_helper.label_find,
},
}
Expand Down
2 changes: 1 addition & 1 deletion lua/fzfx/detail/popup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@

-- log.debug("|Popup:new| $FZF_DEFAULT_OPTS:%s", vim.inspect(vim.env.FZF_DEFAULT_OPTS))
-- log.debug("|Popup:new| $FZF_DEFAULT_COMMAND:%s", vim.inspect(vim.env.FZF_DEFAULT_COMMAND))
-- log.debug("|Popup:new| fzf_command:%s", vim.inspect(fzf_command))
log.debug("|Popup:new| fzf_command:%s", vim.inspect(fzf_command))

Check warning on line 258 in lua/fzfx/detail/popup.lua

View check run for this annotation

Codecov / codecov/patch

lua/fzfx/detail/popup.lua#L258

Added line #L258 was not covered by tests

-- launch
local jobid = vim.fn.termopen(fzf_command, { on_exit = on_fzf_exit }) --[[@as integer ]]
Expand Down
14 changes: 10 additions & 4 deletions lua/fzfx/detail/popup/buffer_popup_window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,8 @@
--- @param file_content fzfx.BufferPopupWindowPreviewFileContentJob
--- @param top_line integer
--- @param on_complete (fun(done:boolean):any)|nil
function BufferPopupWindow:render_file_contents(file_content, top_line, on_complete)
--- @param line_step integer?
function BufferPopupWindow:render_file_contents(file_content, top_line, on_complete, line_step)
local function do_complete(done)
if type(on_complete) == "function" then
on_complete(done)
Expand All @@ -704,7 +705,9 @@
end

local function falsy_rendering()
self._rendering = false
vim.schedule(function()
self._rendering = false

Check warning on line 709 in lua/fzfx/detail/popup/buffer_popup_window.lua

View check run for this annotation

Codecov / codecov/patch

lua/fzfx/detail/popup/buffer_popup_window.lua#L708-L709

Added lines #L708 - L709 were not covered by tests
end)
end

self._rendering = true
Expand All @@ -727,7 +730,7 @@
local TOP_LINE = top_line
local BOTTOM_LINE = math.min(WIN_HEIGHT + TOP_LINE, LINES_COUNT)
local line_index = TOP_LINE
local line_step = 5
line_step = line_step or 10

Check warning on line 733 in lua/fzfx/detail/popup/buffer_popup_window.lua

View check run for this annotation

Codecov / codecov/patch

lua/fzfx/detail/popup/buffer_popup_window.lua#L733

Added line #L733 was not covered by tests

local function set_buf_lines()
vim.defer_fn(function()
Expand Down Expand Up @@ -933,6 +936,9 @@
end
local TARGET_FIRST_LINENO = math.max(TOP_LINE + SHIFT_LINES, 1)
local TARGET_LAST_LINENO = math.min(BOTTOM_LINE + SHIFT_LINES, LINES_COUNT)
if TARGET_LAST_LINENO >= LINES_COUNT then
TARGET_FIRST_LINENO = math.max(1, TARGET_LAST_LINENO - WIN_HEIGHT + 1)

Check warning on line 940 in lua/fzfx/detail/popup/buffer_popup_window.lua

View check run for this annotation

Codecov / codecov/patch

lua/fzfx/detail/popup/buffer_popup_window.lua#L939-L940

Added lines #L939 - L940 were not covered by tests
end

log.debug(
"|BufferPopupWindow:scroll_by| percent:%s, up:%s, LINES/HEIGHT/SHIFT:%s/%s/%s, top/bottom:%s/%s, target top/bottom:%s/%s",
Expand All @@ -958,7 +964,7 @@

self:render_file_contents(file_content, TARGET_FIRST_LINENO, function()
falsy_scrolling()
end)
end, math.max(WIN_HEIGHT, 30))

Check warning on line 967 in lua/fzfx/detail/popup/buffer_popup_window.lua

View check run for this annotation

Codecov / codecov/patch

lua/fzfx/detail/popup/buffer_popup_window.lua#L967

Added line #L967 was not covered by tests
end

function BufferPopupWindow:preview_page_down()
Expand Down
14 changes: 14 additions & 0 deletions lua/fzfx/lib/switches.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
local M = {}

--- @return boolean
M.buffer_previewer_disabled = function()
return (

Check warning on line 5 in lua/fzfx/lib/switches.lua

View check run for this annotation

Codecov / codecov/patch

lua/fzfx/lib/switches.lua#L5

Added line #L5 was not covered by tests
type(vim.g.fzfx_disable_buffer_previewer) == "number"
and vim.g.fzfx_disable_buffer_previewer > 0
)
or (

Check warning on line 9 in lua/fzfx/lib/switches.lua

View check run for this annotation

Codecov / codecov/patch

lua/fzfx/lib/switches.lua#L9

Added line #L9 was not covered by tests
type(vim.g.fzfx_disable_buffer_previewer) == "boolean" and vim.g.fzfx_disable_buffer_previewer
)
end

return M
1 change: 0 additions & 1 deletion spec/lib/env_spec.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---@diagnostic disable: undefined-field, unused-local
local cwd = vim.fn.getcwd()

describe("lib.env", function()
Expand Down
35 changes: 35 additions & 0 deletions spec/lib/switches_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
local cwd = vim.fn.getcwd()

describe("lib.switches", function()
local assert_eq = assert.is_equal
local assert_true = assert.is_true
local assert_false = assert.is_false

before_each(function()
vim.api.nvim_command("cd " .. cwd)
end)

local switches = require("fzfx.lib.switches")
describe("[fzfx_disable_buffer_previewer]", function()
it("disabled", function()
vim.g.fzfx_disable_buffer_previewer = 1
assert_true(switches.buffer_previewer_disabled())
vim.g.fzfx_disable_buffer_previewer = true
assert_true(switches.buffer_previewer_disabled())
vim.g.fzfx_disable_buffer_previewer = "true"
assert_false(switches.buffer_previewer_disabled())
vim.g.fzfx_disable_buffer_previewer = nil
assert_false(switches.buffer_previewer_disabled())
end)
it("enabled", function()
vim.g.fzfx_disable_buffer_previewer = 0
assert_false(switches.buffer_previewer_disabled())
vim.g.fzfx_disable_buffer_previewer = false
assert_false(switches.buffer_previewer_disabled())
vim.g.fzfx_disable_buffer_previewer = "false"
assert_false(switches.buffer_previewer_disabled())
vim.g.fzfx_disable_buffer_previewer = nil
assert_false(switches.buffer_previewer_disabled())
end)
end)
end)
Loading