Skip to content

Commit

Permalink
perf: imrpovements/fixes for edfaf03 (#1124)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibhagwan committed Apr 5, 2024
1 parent edfaf03 commit 97a88bb
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 27 deletions.
10 changes: 8 additions & 2 deletions autoload/fzf_lua.vim
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@
function! fzf_lua#getbufinfo(bufnr) abort
let info = getbufinfo(a:bufnr)
if empty(info)
return v:false " there is no way to return `nil` from vimscript
return [] " there is no way to return `nil` from vimscript
endif
let vars = info[0].variables
unlet! info[0].variables
" Make sure we copy 'current_syntax' as `utils.buf_is_qf`
" uses it to detect quickfix buffers
if !empty(vars) && has_key(vars, "current_syntax")
let info[0].variables = { "current_syntax": vars.current_syntax }
endif
return info[0]
endfunction

" Similar to fzf_lua#getbufinfo, but for getwininfo.
function! fzf_lua#getwininfo(winid) abort
let info = getwininfo(a:winid)
if empty(info)
return v:false
return []
endif
unlet! info[0].variables
return info[0]
Expand Down
29 changes: 17 additions & 12 deletions lua/fzf-lua/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,27 @@ local utils = require "fzf-lua.utils"
local config = require "fzf-lua.config"

do
-- using the latest nightly 'NVIM v0.6.0-dev+569-g2ecf0a4c6'
-- plugin '.vim' initialization sometimes doesn't get called
local function source_vimL(path_parts)
local vimL_file = path.join(path_parts)
if vim.loop.fs_stat(vimL_file) then
vim.cmd("source " .. vimL_file)
-- print(string.format("loaded '%s'", vimL_file))
end
end

local currFile = debug.getinfo(1, "S").source:gsub("^@", "")
vim.g.fzf_lua_directory = path.normalize(path.parent(currFile))
local fzf_lua_root = path.parent(path.parent(vim.g.fzf_lua_directory))

-- Manually source the vimL script containing ':FzfLua' cmd
if not vim.g.loaded_fzf_lua then
local fzf_lua_vim = path.join({
path.parent(path.parent(vim.g.fzf_lua_directory)),
"plugin", "fzf-lua.vim"
})
if vim.loop.fs_stat(fzf_lua_vim) then
vim.cmd(("source %s"):format(fzf_lua_vim))
-- utils.info(("manually loaded '%s'"):format(fzf_lua_vim))
end
end
-- does nothing if already loaded due to `vim.g.loaded_fzf_lua`
source_vimL({ fzf_lua_root, "plugin", "fzf-lua.vim" })
-- Autoload scipts dynamically loaded on `vim.fn[fzf_lua#...]` call
-- `vim.fn.exists("*fzf_lua#...")` will return 0 unless we manuall source
source_vimL({ fzf_lua_root, "autoload", "fzf_lua.vim" })
-- Set var post source as the top of the file `require` will return 0
-- due to it potentially being loaded before "autoload/fzf_lua.vim"
utils.__HAS_AUTOLOAD_FNS = vim.fn.exists("*fzf_lua#getbufinfo") == 1

-- Create a new RPC server (tmp socket) to listen to messages (actions/headless)
-- this is safer than using $NVIM_LISTEN_ADDRESS. If the user is using a custom
Expand Down
4 changes: 2 additions & 2 deletions lua/fzf-lua/previewer/builtin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ end

function Previewer.base:preview_is_terminal()
if not self.win or not self.win:validate_preview() then return end
return vim.fn["fzf_lua#getwininfo"](self.win.preview_winid).terminal == 1
return utils.getwininfo(self.win.preview_winid).terminal == 1
end

function Previewer.base:get_tmp_buffer()
Expand Down Expand Up @@ -368,7 +368,7 @@ function Previewer.base:scroll(direction)
-- user scrolls, the highlight is no longer relevant (#462).
-- Conditionally toggle 'cursorline' based on cursor position
if self.orig_pos and self.winopts.cursorline then
local wininfo = vim.fn["fzf_lua#getwininfo"](preview_winid)
local wininfo = utils.getwininfo(preview_winid)
if wininfo and
self.orig_pos[1] >= wininfo.topline and
self.orig_pos[1] <= wininfo.botline then
Expand Down
3 changes: 1 addition & 2 deletions lua/fzf-lua/providers/buffers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ local utils = require "fzf-lua.utils"
local shell = require "fzf-lua.shell"
local config = require "fzf-lua.config"
local base64 = require "fzf-lua.lib.base64"
local make_entry = require "fzf-lua.make_entry"
local devicons = require "fzf-lua.devicons"

local M = {}
Expand Down Expand Up @@ -67,7 +66,7 @@ local populate_buffer_entries = function(opts, bufnrs, tabh)
local element = {
bufnr = bufnr,
flag = flag,
info = vim.fn["fzf_lua#getbufinfo"](bufnr),
info = utils.getbufinfo(bufnr),
readonly = vim.bo[bufnr].readonly
}

Expand Down
30 changes: 23 additions & 7 deletions lua/fzf-lua/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -805,15 +805,15 @@ function M.is_term_buffer(bufnr)
bufnr = bufnr == 0 and vim.api.nvim_get_current_buf() or bufnr
local winid = vim.fn.bufwinid(bufnr)
if tonumber(winid) > 0 and vim.api.nvim_win_is_valid(winid) then
return vim.fn["fzf_lua#getwininfo"](winid).terminal == 1
return M.getwininfo(winid).terminal == 1
end
local bufname = vim.api.nvim_buf_is_valid(bufnr) and vim.api.nvim_buf_get_name(bufnr)
return M.is_term_bufname(bufname)
end

function M.buffer_is_dirty(bufnr, warn, only_if_last_buffer)
bufnr = tonumber(bufnr) or vim.api.nvim_get_current_buf()
local info = bufnr and vim.fn["fzf_lua#getbufinfo"](bufnr)
local info = bufnr and M.getbufinfo(bufnr)
if info and info.changed ~= 0 then
if only_if_last_buffer and 1 < #vim.fn.win_findbuf(bufnr) then
return false
Expand All @@ -829,7 +829,7 @@ end

function M.save_dialog(bufnr)
bufnr = tonumber(bufnr) or vim.api.nvim_get_current_buf()
local info = bufnr and vim.fn["fzf_lua#getbufinfo"](bufnr)
local info = bufnr and M.getbufinfo(bufnr)
if not info.name or #info.name == 0 then
-- unnamed buffers can't be saved
M.warn(string.format("buffer %d has unsaved changes", bufnr))
Expand All @@ -853,17 +853,15 @@ end
-- 1 for qf list
-- 2 for loc list
function M.win_is_qf(winid, wininfo)
wininfo = wininfo or
(vim.api.nvim_win_is_valid(winid) and vim.fn["fzf_lua#getwininfo"](winid))
wininfo = wininfo or (vim.api.nvim_win_is_valid(winid) and M.getwininfo(winid))
if wininfo and wininfo.quickfix == 1 then
return wininfo.loclist == 1 and 2 or 1
end
return false
end

function M.buf_is_qf(bufnr, bufinfo)
bufinfo = bufinfo or
(vim.api.nvim_buf_is_valid(bufnr) and vim.fn["fzf_lua#getbufinfo"](bufnr))
bufinfo = bufinfo or (vim.api.nvim_buf_is_valid(bufnr) and M.getbufinfo(bufnr))
if bufinfo and bufinfo.variables and
bufinfo.variables.current_syntax == "qf" and
not vim.tbl_isempty(bufinfo.windows) then
Expand Down Expand Up @@ -975,6 +973,24 @@ function M.nvim_buf_delete(bufnr, opts)
vim.o.eventignore = save_ei
end

function M.getbufinfo(bufnr)
if M.__HAS_AUTOLOAD_FNS then
return vim.fn["fzf_lua#getbufinfo"](bufnr)
else
local info = vim.fn.getbufinfo(bufnr)
return info[1] or info
end
end

function M.getwininfo(winid)
if M.__HAS_AUTOLOAD_FNS then
return vim.fn["fzf_lua#getwininfo"](winid)
else
local info = vim.fn.getwininfo(winid)
return info[1] or info
end
end

-- Backward compat 'vim.keymap.set', will probably be deprecated soon
function M.keymap_set(mode, lhs, rhs, opts)
if vim.keymap then
Expand Down
4 changes: 2 additions & 2 deletions lua/fzf-lua/win.lua
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ end

function FzfWin:preview_layout()
if self.winopts.split and self.previewer_is_builtin then
local wininfo = fn["fzf_lua#getwininfo"](self.fzf_winid)
local wininfo = utils.getwininfo(self.fzf_winid)
-- unlike floating win popups, split windows inherit the global
-- 'signcolumn' setting which affects the available width for fzf
-- 'generate_layout' will then use the sign column available width
Expand Down Expand Up @@ -1144,7 +1144,7 @@ function FzfWin:update_scrollbar(hide)
local buf = api.nvim_win_get_buf(self.preview_winid)

local o = {}
o.wininfo = fn["fzf_lua#getwininfo"](self.preview_winid)
o.wininfo = utils.getwininfo(self.preview_winid)
o.line_count = api.nvim_buf_line_count(buf)

local topline, height = o.wininfo.topline, o.wininfo.height
Expand Down

0 comments on commit 97a88bb

Please sign in to comment.