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(popup): realtime resize popup window #322

Merged
merged 30 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
05a5a41
feat(cache): add cache
linrongbin16 Oct 25, 2023
424ae17
WIP
linrongbin16 Oct 25, 2023
786b7cb
WIP
linrongbin16 Oct 25, 2023
36170ac
WIP: rename 'fzfx.helpers' to 'fzfx.fzf_helpers'
linrongbin16 Oct 26, 2023
2127a5c
WIP
linrongbin16 Oct 26, 2023
4804267
Merge branch 'main' into feat-resizing
linrongbin16 Oct 26, 2023
cb3e2ef
feat(popup): cache default fzf opts
linrongbin16 Oct 26, 2023
606a03d
perf(popup): cache default fzf opts
linrongbin16 Oct 26, 2023
408478a
feat(popup): realtime resize
linrongbin16 Oct 26, 2023
704e28b
perf(fzfopts): only listen to ColorScheme event
linrongbin16 Oct 26, 2023
d2a4d6e
refactor(popup): refactor
linrongbin16 Oct 26, 2023
4a8aae1
perf(fzf): improve test coverage
linrongbin16 Oct 26, 2023
ad1d4b7
test(visual): fix tests
linrongbin16 Oct 26, 2023
00d0eba
test(cache): add cases
linrongbin16 Oct 26, 2023
57120d6
test(fzf): add cases
linrongbin16 Oct 26, 2023
91f4805
test(popup): improve tests
linrongbin16 Oct 26, 2023
61b5a23
perf(popup): shorter internal API name
linrongbin16 Oct 26, 2023
07ebd10
perf(popup): improve test coverage
linrongbin16 Oct 26, 2023
1714271
perf(popup): improve test coverage
linrongbin16 Oct 26, 2023
4833785
docs(install): fix fzf build
linrongbin16 Oct 26, 2023
b3c5b2e
perf(fzfopts): add augroup
linrongbin16 Oct 26, 2023
7298157
feat(event): add augroup for all autocmd
linrongbin16 Oct 26, 2023
2fc20d3
fix(event): ignore resizing/recalculating
linrongbin16 Oct 26, 2023
72181fc
WIP
linrongbin16 Oct 26, 2023
8224de8
Revert "docs(install): fix fzf build"
linrongbin16 Oct 26, 2023
162979c
docs
linrongbin16 Oct 26, 2023
4ded918
revert
linrongbin16 Oct 26, 2023
150cb29
fix
linrongbin16 Oct 26, 2023
1d0be33
perf: remove useless code
linrongbin16 Oct 26, 2023
172d90f
perf: remove deadcode
linrongbin16 Oct 26, 2023
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
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,12 @@ require("lazy").setup({
{ "nvim-tree/nvim-web-devicons" },

-- mandatory
{ "junegunn/fzf", build = ":call fzf#install()" },
{
"junegunn/fzf",
build = function()
vim.fn["fzf#install"]()
end,
},
{
"linrongbin16/fzfx.nvim",
dependencies = { "junegunn/fzf", "nvim-tree/nvim-web-devicons" },
Expand Down
8 changes: 7 additions & 1 deletion lua/fzfx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,20 @@ local function setup(options)
end

-- lua module environment
require("fzfx.module").setup(configs)
require("fzfx.module").setup()

-- rpc server
require("fzfx.server").setup()

-- yank history
require("fzfx.yank_history").setup()

-- fzf helpers
require("fzfx.fzf_helpers").setup()

-- popup
require("fzfx.popup").setup()

-- files & buffers
general.setup("files", configs.files)
general.setup("buffers", configs.buffers)
Expand Down
70 changes: 55 additions & 15 deletions lua/fzfx/helpers.lua → lua/fzfx/fzf_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ local utils = require("fzfx.utils")

-- visual select {

--- @package
--- @param mode string
--- @return string
local function get_visual_lines(mode)
local function _get_visual_lines(mode)
local start_pos = vim.fn.getpos("'<")
local end_pos = vim.fn.getpos("'>")
local line_start = start_pos[2]
Expand All @@ -21,13 +22,13 @@ local function get_visual_lines(mode)
column_start = math.min(column_start, column_end)
column_end = math.max(column_start, column_end)
log.debug(
"|fzfx.helpers - get_visual_lines| mode:%s, start_pos:%s, end_pos:%s",
"|fzfx.fzf_helpers - _get_visual_lines| mode:%s, start_pos:%s, end_pos:%s",
vim.inspect(mode),
vim.inspect(start_pos),
vim.inspect(end_pos)
)
log.debug(
"|fzfx.helper - get_visual_lines| line_start:%s, line_end:%s, column_start:%s, column_end:%s",
"|fzfx.fzf_helpers - _get_visual_lines| line_start:%s, line_end:%s, column_start:%s, column_end:%s",
vim.inspect(line_start),
vim.inspect(line_end),
vim.inspect(column_start),
Expand All @@ -36,15 +37,15 @@ local function get_visual_lines(mode)

local lines = vim.api.nvim_buf_get_lines(0, line_start - 1, line_end, false)
if #lines == 0 then
log.debug("|fzfx.helpers - get_visual_lines| empty lines")
log.debug("|fzfx.fzf_helpers - _get_visual_lines| empty lines")
return ""
end

local cursor_pos = vim.fn.getpos(".")
local cursor_line = cursor_pos[2]
local cursor_column = cursor_pos[3]
log.debug(
"|fzfx.helpers - get_visual_lines| cursor_pos:%s, cursor_line:%s, cursor_column:%s",
"|fzfx.fzf_helpers - _get_visual_lines| cursor_pos:%s, cursor_line:%s, cursor_column:%s",
vim.inspect(cursor_pos),
vim.inspect(cursor_line),
vim.inspect(cursor_column)
Expand All @@ -54,27 +55,28 @@ local function get_visual_lines(mode)
lines[#lines] = string.sub(lines[#lines], 1, column_end - offset + 1)
lines[1] = string.sub(lines[1], column_start)
log.debug(
"|fzfx.helpers - get_visual_lines| v or \\22, lines:%s",
"|fzfx.fzf_helpers - _get_visual_lines| v or \\22, lines:%s",
vim.inspect(lines)
)
elseif mode == "V" then
if #lines == 1 then
lines[1] = vim.trim(lines[1])
end
log.debug(
"|fzfx.helpers - get_visual_lines| V, lines:%s",
"|fzfx.fzf_helpers - _get_visual_lines| V, lines:%s",
vim.inspect(lines)
)
end
return table.concat(lines, "\n")
end

--- @package
--- @return string
local function visual_select()
local function _visual_select()
vim.cmd([[ execute "normal! \<ESC>" ]])
local mode = vim.fn.visualmode()
if mode == "v" or mode == "V" or mode == "\22" then
return get_visual_lines(mode)
return _get_visual_lines(mode)
end
return ""
end
Expand All @@ -89,15 +91,15 @@ local function get_command_feed(opts, feed_type)
if feed_type == "args" then
return opts.args
elseif feed_type == "visual" then
return visual_select()
return _visual_select()
elseif feed_type == "cword" then
return vim.fn.expand("<cword>")
elseif feed_type == "put" then
local y = yank_history.get_yank()
return (y ~= nil and type(y.regtext) == "string") and y.regtext or ""
else
log.throw(
"|fzfx.helpers - get_command_feed| error! invalid command feed type! %s",
"|fzfx.fzf_helpers - get_command_feed| error! invalid command feed type! %s",
vim.inspect(feed_type)
)
return ""
Expand All @@ -123,7 +125,7 @@ local function generate_fzf_color_opts()
end
end
log.debug(
"|fzfx.helpers - make_fzf_color_opts| builder:%s",
"|fzfx.fzf_helpers - make_fzf_color_opts| builder:%s",
vim.inspect(builder)
)
return { { "--color", table.concat(builder, ",") } }
Expand Down Expand Up @@ -153,7 +155,7 @@ local function append_fzf_opt(opts, o)
table.insert(opts, string.format("%s %s", k, utils.shellescape(v)))
else
log.throw(
"|fzfx.helpers - append_fzf_opt| invalid fzf opt: %s",
"|fzfx.fzf_helpers - append_fzf_opt| invalid fzf opt: %s",
vim.inspect(o)
)
end
Expand Down Expand Up @@ -193,8 +195,11 @@ local function make_fzf_opts(opts)
return table.concat(result, " ")
end

--- @type string?
local CACHED_FZF_DEFAULT_OPTS = nil

--- @return string?
local function make_fzf_default_opts()
local function make_fzf_default_opts_impl()
local opts = conf.get_config().fzf_opts
local result = {}
if type(opts) == "table" and #opts > 0 then
Expand All @@ -214,9 +219,24 @@ local function make_fzf_default_opts()
append_fzf_opt(result, o)
end
end
log.debug(
"|fzfx.fzf_helpers - make_fzf_default_opts_impl| result:%s",
vim.inspect(result)
)
return table.concat(result, " ")
end

--- @param ignore_cache boolean?
--- @return string?
local function make_fzf_default_opts(ignore_cache)
if not ignore_cache and type(CACHED_FZF_DEFAULT_OPTS) == "string" then
return CACHED_FZF_DEFAULT_OPTS
end
local opts = make_fzf_default_opts_impl()
CACHED_FZF_DEFAULT_OPTS = opts
return opts
end

-- fzf opts }

--- @return string|nil
Expand Down Expand Up @@ -255,20 +275,40 @@ local function make_lua_command(...)
local nvim_path = nvim_exec()
local lua_path = path.join(path.base_dir(), "bin", ...)
-- log.debug(
-- "|fzfx.helpers - make_lua_command| lua_path:%s",
-- "|fzfx.fzf_helpers - make_lua_command| lua_path:%s",
-- vim.inspect(lua_path)
-- )
return string.format("%s -n --clean --headless -l %s", nvim_path, lua_path)
end

local function setup()
local recalculating = false
vim.api.nvim_create_autocmd("ColorScheme", {
pattern = { "*" },
callback = function()
if recalculating then
return
end
recalculating = true
make_fzf_default_opts(true)
vim.schedule(function()
recalculating = false
end)
end,
})
end

local M = {
_get_visual_lines = _get_visual_lines,
_visual_select = _visual_select,
get_command_feed = get_command_feed,
preprocess_fzf_opts = preprocess_fzf_opts,
make_fzf_opts = make_fzf_opts,
make_fzf_default_opts = make_fzf_default_opts,
nvim_exec = nvim_exec,
fzf_exec = fzf_exec,
make_lua_command = make_lua_command,
setup = setup,
}

return M
18 changes: 9 additions & 9 deletions lua/fzfx/general.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local log = require("fzfx.log")
local Popup = require("fzfx.popup").Popup
local helpers = require("fzfx.helpers")
local fzf_helpers = require("fzfx.fzf_helpers")
local server = require("fzfx.server")
local color = require("fzfx.color")
local utils = require("fzfx.utils")
Expand Down Expand Up @@ -674,22 +674,22 @@ local function general(name, query, bang, pipeline_configs, default_pipeline)

local query_command = string.format(
"%s %s %s %s %s",
helpers.make_lua_command("general", "provider.lua"),
fzf_helpers.make_lua_command("general", "provider.lua"),
provide_rpc_registry_id,
provider_switch.metafile,
provider_switch.resultfile,
utils.shellescape(query)
)
local reload_query_command = string.format(
"%s %s %s %s {q}",
helpers.make_lua_command("general", "provider.lua"),
fzf_helpers.make_lua_command("general", "provider.lua"),
provide_rpc_registry_id,
provider_switch.metafile,
provider_switch.resultfile
)
local preview_command = string.format(
"%s %s %s %s {}",
helpers.make_lua_command("general", "previewer.lua"),
fzf_helpers.make_lua_command("general", "previewer.lua"),
preview_rpc_registry_id,
previewer_switch.metafile,
previewer_switch.resultfile
Expand Down Expand Up @@ -745,7 +745,7 @@ local function general(name, query, bang, pipeline_configs, default_pipeline)

local action_command = string.format(
"%s %s {}",
helpers.make_lua_command("rpc", "client.lua"),
fzf_helpers.make_lua_command("rpc", "client.lua"),
interaction_rpc_registry_id
)
local bind_builder = string.format(
Expand Down Expand Up @@ -782,7 +782,7 @@ local function general(name, query, bang, pipeline_configs, default_pipeline)

local switch_command = string.format(
"%s %s",
helpers.make_lua_command("rpc", "client.lua"),
fzf_helpers.make_lua_command("rpc", "client.lua"),
switch_rpc_registry_id
)
local bind_builder = string.format(
Expand Down Expand Up @@ -822,7 +822,7 @@ local function general(name, query, bang, pipeline_configs, default_pipeline)

fzf_opts =
vim.list_extend(fzf_opts, vim.deepcopy(pipeline_configs.fzf_opts))
fzf_opts = helpers.preprocess_fzf_opts(fzf_opts)
fzf_opts = fzf_helpers.preprocess_fzf_opts(fzf_opts)
local actions = pipeline_configs.actions
local win_opts = nil
if
Expand Down Expand Up @@ -875,7 +875,7 @@ local function setup(name, pipeline_configs)
vim.api.nvim_create_user_command(
pipeline_configs.commands.name,
function(opts)
local query = helpers.get_command_feed(
local query = fzf_helpers.get_command_feed(
opts,
pipeline_configs.commands.feed
)
Expand All @@ -895,7 +895,7 @@ local function setup(name, pipeline_configs)
command_configs.name,
function(opts)
local query =
helpers.get_command_feed(opts, command_configs.feed)
fzf_helpers.get_command_feed(opts, command_configs.feed)
return general(
name,
query,
Expand Down
6 changes: 4 additions & 2 deletions lua/fzfx/module.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local conf = require("fzfx.config")
local log = require("fzfx.log")

--- @param plugin string
Expand Down Expand Up @@ -56,8 +57,9 @@ end
-- return nil
-- end

--- @param configs Options
local function setup(configs)
local function setup()
local configs = conf.get_config()

-- debug
vim.env._FZFX_NVIM_DEBUG_ENABLE = configs.debug.enable and 1 or 0

Expand Down
Loading