Skip to content

Commit

Permalink
feat: relative window (#172)
Browse files Browse the repository at this point in the history
* feat: preview on the left

* WIP

* WIP

* fix

* shorter

* doc: demo

* doc: demo

* doc

* doc

* doc
  • Loading branch information
linrongbin16 committed Sep 15, 2023
1 parent 7589a88 commit 01caa8b
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 44 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@
[![license](https://img.shields.io/github/license/linrongbin16/lin.nvim)](https://github.com/linrongbin16/lin.nvim/blob/main/LICENSE)
[![ci.yml](https://img.shields.io/github/actions/workflow/status/linrongbin16/fzfx.nvim/ci.yml)](https://github.com/linrongbin16/fzfx.nvim/actions/workflows/ci.yml)
-->

<!-- ![Linux](https://img.shields.io/badge/Linux-%23.svg?style=flat-square&logo=linux&color=FCC624&logoColor=black) -->
<!-- ![macOS](https://img.shields.io/badge/macOS-%23.svg?style=flat-square&logo=apple&color=000000&logoColor=white) -->
<!-- ![Windows](https://img.shields.io/badge/Windows-%23.svg?style=flat-square&logo=windows&color=0078D6&logoColor=white) -->

E(x)tended commands missing in [fzf.vim](https://github.com/junegunn/fzf.vim).
<!-- > Search `fzfx` with rg's `-g *ch.lua` option on specific filetypes. -->

https://github.com/linrongbin16/fzfx.nvim/assets/6496887/aa5ef18c-26b4-4a93-bd0c-bfeba6f6caf1
<p align="center"> E(x)tended commands missing in <a href="https://github.com/junegunn/fzf.vim">fzf.vim</a>. </p>

<!-- > Search `fzfx` with rg's `-g *ch.lua` option on specific filetypes. -->
https://github.com/linrongbin16/fzfx.nvim/assets/6496887/aa5ef18c-26b4-4a93-bd0c-bfeba6f6caf1

This is the next generation of [fzfx.vim](https://github.com/linrongbin16/fzfx.vim), a brand new fzf plugin for Neovim, build from scratch, focused on user friendly, customization and performance.
> Search `fzfx` with rg's `-g *ch.lua` option.
- [Feature](#-feature)
- [Requirement](#-requirement)
Expand Down Expand Up @@ -66,6 +65,8 @@ This is the next generation of [fzfx.vim](https://github.com/linrongbin16/fzfx.v
- ...

> Actually all above features are built on an engine that support fully dynamic runtime & pipeline control, it allows you to do almost anything you want, please see [Configuration](#-configuration) and [Wiki](https://github.com/linrongbin16/fzfx.nvim/wiki).
>
> Please see [Demo](https://github.com/linrongbin16/fzfx.nvim/wiki/Demo) for more features & use cases.
## ✅ Requirement

Expand Down
49 changes: 45 additions & 4 deletions lua/fzfx/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ local default_fzf_options = {
preview_half_page_down = "--bind=ctrl-f:preview-half-page-down",
preview_half_page_up = "--bind=ctrl-b:preview-half-page-up",
no_multi = "--no-multi",
lsp_preview_window = { "--preview-window", "left,65%,+{2}-/2" },
}

local default_git_log_pretty =
Expand Down Expand Up @@ -1657,13 +1658,23 @@ local Defaults = {
},
fzf_opts = {
default_fzf_options.multi,
default_fzf_options.lsp_preview_window,
"--border=none",
{ "--delimiter", ":" },
{ "--preview-window", "+{2}-/2" },
{
"--prompt",
"Definitions > ",
},
},
win_opts = {
relative = "cursor",
height = 0.45,
width = 1,
row = 0,
col = 0,
border = "none",
zindex = 51,
},
}),

-- the 'Lsp Type Definitions' command
Expand Down Expand Up @@ -1704,13 +1715,23 @@ local Defaults = {
},
fzf_opts = {
default_fzf_options.multi,
default_fzf_options.lsp_preview_window,
"--border=none",
{ "--delimiter", ":" },
{ "--preview-window", "+{2}-/2" },
{
"--prompt",
"TypeDefinitions > ",
},
},
win_opts = {
relative = "cursor",
height = 0.45,
width = 1,
row = 0,
col = 0,
border = "none",
zindex = 51,
},
}),

-- the 'Lsp References' command
Expand Down Expand Up @@ -1751,13 +1772,23 @@ local Defaults = {
},
fzf_opts = {
default_fzf_options.multi,
default_fzf_options.lsp_preview_window,
"--border=none",
{ "--delimiter", ":" },
{ "--preview-window", "+{2}-/2" },
{
"--prompt",
"References > ",
},
},
win_opts = {
relative = "cursor",
height = 0.45,
width = 1,
row = 0,
col = 0,
border = "none",
zindex = 51,
},
}),

-- the 'Lsp Implementations' command
Expand Down Expand Up @@ -1798,13 +1829,23 @@ local Defaults = {
},
fzf_opts = {
default_fzf_options.multi,
default_fzf_options.lsp_preview_window,
"--border=none",
{ "--delimiter", ":" },
{ "--preview-window", "+{2}-/2" },
{
"--prompt",
"Implementations > ",
},
},
win_opts = {
relative = "cursor",
height = 0.45,
width = 1,
row = 0,
col = 0,
border = "none",
zindex = 51,
},
}),

-- the 'Yank History' commands
Expand Down
20 changes: 19 additions & 1 deletion lua/fzfx/general.lua
Original file line number Diff line number Diff line change
Expand Up @@ -755,8 +755,26 @@ local function general(name, query, bang, pipeline_configs, default_pipeline)
vim.list_extend(fzf_opts, vim.deepcopy(pipeline_configs.fzf_opts))
fzf_opts = helpers.preprocess_fzf_opts(fzf_opts)
local actions = pipeline_configs.actions
local win_opts = nil
if bang then
win_opts = vim.tbl_deep_extend(
"force",
vim.deepcopy(win_opts or {}),
{ height = 1, width = 1, row = 0, col = 0 }
)
end
if
type(pipeline_configs.win_opts) == "table"
and not vim.tbl_isempty(pipeline_configs.win_opts)
then
win_opts = vim.tbl_deep_extend(
"force",
vim.deepcopy(win_opts or {}),
pipeline_configs.win_opts
)
end
local p = Popup:new(
bang and { height = 1, width = 1, row = 0, col = 0 } or nil,
win_opts or {},
query_command,
fzf_opts,
actions,
Expand Down
93 changes: 59 additions & 34 deletions lua/fzfx/popup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ local PopupWindow = {
}

--- @class PopupWindowOpts
--- @field relative "editor"|"win"|nil
--- @field relative "editor"|"win"|"cursor"|nil
--- @field width integer|nil
--- @field height integer|nil
--- @field row integer|nil
Expand All @@ -40,23 +40,33 @@ end
--- @param win_opts Configs
--- @return PopupWindowOpts
local function make_popup_window_opts(win_opts)
--- @type integer
local columns = vim.o.columns
--- @type integer
local lines = vim.o.lines
--- @type "editor"|"win"|"cursor"
local relative = win_opts.relative or "editor"

local total_width = vim.o.columns
local total_height = vim.o.lines

if relative == "win" then
total_width = vim.api.nvim_win_get_width(0)
total_height = vim.api.nvim_win_get_height(0)
elseif relative == "cursor" then
total_width = vim.api.nvim_win_get_width(0)
total_height = vim.api.nvim_win_get_height(0)
end

--- @type integer
local width = safe_range(
3,
win_opts.width > 1 and win_opts.width
or math.floor(columns * win_opts.width),
columns
or math.floor(total_width * win_opts.width),
total_width
)
--- @type integer
local height = safe_range(
3,
win_opts.height > 1 and win_opts.height
or math.floor(lines * win_opts.height),
lines
or math.floor(total_height * win_opts.height),
total_height
)
--- @type integer
local row = nil
Expand All @@ -68,20 +78,28 @@ local function make_popup_window_opts(win_opts)
"error! invalid option win_opts.row '%s'!",
vim.inspect(win_opts.row)
)
else
local base_row = math.floor((lines - height) * 0.5)
if win_opts.row >= 0 then
local shift_row = win_opts.row < 1
and math.floor((lines - height) * win_opts.row)
or win_opts.row
row = safe_range(0, base_row + shift_row, lines - height)
else
local shift_row = win_opts.row > -1
and math.ceil((lines - height) * win_opts.row)
or win_opts.row
row = safe_range(0, base_row + shift_row, lines - height)
end

local base_row = math.floor((total_height - height) * 0.5)
if relative == "cursor" then
--- @type {[1]:integer,[2]:integer}
local cursor_pos = vim.api.nvim_win_get_cursor(0)
if type(cursor_pos) == "table" and not vim.tbl_isempty(cursor_pos) then
base_row = cursor_pos[1]
end
end
if win_opts.row >= 0 then
local shift_row = win_opts.row < 1
and math.floor((total_height - height) * win_opts.row)
or win_opts.row
row = safe_range(0, base_row + shift_row, total_height - height)
else
local shift_row = win_opts.row > -1
and math.ceil((total_height - height) * win_opts.row)
or win_opts.row
row = safe_range(0, base_row + shift_row, total_height - height)
end

--- @type integer
local col = nil
if
Expand All @@ -92,26 +110,33 @@ local function make_popup_window_opts(win_opts)
"error! invalid option win_opts.col '%s'!",
vim.inspect(win_opts.col)
)
else
local base_col = math.floor((columns - width) * 0.5)
if win_opts.col >= 0 then
local shift_col = win_opts.col < 1
and math.floor((columns - width) * win_opts.col)
or win_opts.col
col = safe_range(0, base_col + shift_col, columns - width)
else
local shift_col = win_opts.col > -1
and math.ceil((columns - width) * win_opts.col)
or win_opts.col
col = safe_range(0, base_col + shift_col, columns - width)
end

local base_col = math.floor((total_width - width) * 0.5)
if relative == "cursor" then
--- @type {row:integer,col:integer}
local cursor_pos = vim.api.nvim_win_get_cursor(0)
if type(cursor_pos) == "table" and not vim.tbl_isempty(cursor_pos) then
base_col = cursor_pos[2]
end
end
if win_opts.col >= 0 then
local shift_col = win_opts.col < 1
and math.floor((total_width - width) * win_opts.col)
or win_opts.col
col = safe_range(0, base_col + shift_col, total_width - width)
else
local shift_col = win_opts.col > -1
and math.ceil((total_width - width) * win_opts.col)
or win_opts.col
col = safe_range(0, base_col + shift_col, total_width - width)
end

--- @type PopupWindowOpts
local popup_window_opts =
vim.tbl_deep_extend("force", vim.deepcopy(PopupWindowOpts), {
anchor = "NW",
relative = "editor",
relative = relative,
width = width,
height = height,
-- start point on NW
Expand Down

0 comments on commit 01caa8b

Please sign in to comment.