Skip to content

Commit

Permalink
fix: remove nvim_exec use api to create autocmd
Browse files Browse the repository at this point in the history
  • Loading branch information
zztrieuzz committed Oct 1, 2022
1 parent 0d1b472 commit 9396040
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 49 deletions.
29 changes: 11 additions & 18 deletions lua/windline/cache_utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
--- it save data on buffer and only update when event happen

local M = {}
local api = vim.api

--- store an auto events if value equal false mean it need to update in a next call
--- it doesn't update on autocmd it update when statusline call a render so if
Expand Down Expand Up @@ -38,21 +39,13 @@ local function cache_func(auto_event, variable_name, action, loading_action, vim
if d_check[variable_name] == nil then
d_check[variable_name] = false
local target = auto_event:match('User') and '' or '*'
vim.api.nvim_exec(
string.format(
[[
augroup WL%s
au!
au %s %s lua WindLine.cache_buffer_cb('%s')
augroup END
]],
variable_name,
auto_event,
target,
variable_name
),
false
)
api.nvim_create_autocmd(auto_event, {
group = api.nvim_create_augroup('WL' .. variable_name, { clear = true }),
pattern = target,
callback = function()
WindLine.cache_buffer_cb(variable_name)
end
})
end

local func = function(bufnr, winid, width)
Expand Down Expand Up @@ -96,12 +89,12 @@ end
---@param action function action to do on buffer
---@return function(bufnr, winr)
M.cache_on_global = function(auto_event, global_variable_name, action)
return cache_func(auto_event, global_variable_name, action, nil, vim.g)
return cache_func(auto_event, global_variable_name, action, nil, vim.g)
end

M.cache_buffer_cb = function(identifier)
d_check[identifier] = false
local bufnr = vim.api.nvim_get_current_buf()
local bufnr = api.nvim_get_current_buf()
if d_value[bufnr] then
d_value[bufnr] = {}
end
Expand Down Expand Up @@ -149,4 +142,4 @@ M.set_cache_buffer = function(bufnr, variable_name, value)
end

_G.WindLine.cache_buffer_cb = M.cache_buffer_cb
return M
return M
21 changes: 11 additions & 10 deletions lua/windline/components/vim.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ local setup_search_count = function()
end
is_sc_setup = true

vim.api.nvim_exec(
[[
aug WLSearchLens
au!
au BufEnter * lua require('windline.components.vim').cmdl_search_enter()
au CmdlineLeave [/\?] lua require('windline.components.vim').cmdl_search_leave()
aug END
]],
false
)
local group = vim.api.nvim_create_augroup("WLSearchLens", { clear = true })
vim.api.nvim_create_autocmd("BufEnter", {
group = group,
pattern = "*",
callback = M.cmdl_search_enter
})
vim.api.nvim_create_autocmd("CmdlineLeave", {
group = group,
pattern = "[/\\?]",
callback = M.cmdl_search_leave
})
end

M.search_count = function(opt)
Expand Down
44 changes: 30 additions & 14 deletions lua/windline/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -303,20 +303,36 @@ end


M.setup_event = function()
vim.cmd([[set statusline=%!v:lua.WindLine.show()]])
api.nvim_exec(
[[augroup WindLine
au!
au BufWinEnter,WinEnter * lua WindLine.on_win_enter()
au FileType * lua WindLine.on_ft()
au VimEnter * lua WindLine.on_vimenter()
au ColorScheme * lua WindLine.on_colorscheme()
au OptionSet laststatus lua WindLine.on_set_laststatus()
augroup END]],
false
)
api.nvim_exec("command! -nargs=* WindLineBenchmark lua require('windline.benchmark').benchmark()", false)
api.nvim_exec("command! -nargs=* WindLineFloatToggle lua require('wlfloatline').toggle()", false)
vim.opt.statusline = "%!v:lua.WindLine.show()"
local group = api.nvim_create_augroup("WindLine", { clear = true })
api.nvim_create_autocmd("BufWinEnter,WinEnter", {
group = group,
pattern = "*",
callback = function() M.on_win_enter() end
})
api.nvim_create_autocmd("FileType", {
group = group,
pattern = "*",
callback = function() M.on_ft() end
})
api.nvim_create_autocmd("VimEnter", {
group = group,
pattern = "*",
callback = function() M.on_vimenter() end
})
api.nvim_create_autocmd("ColorScheme", {
group = group,
pattern = "*",
callback = function() M.on_colorscheme() end
})
api.nvim_create_autocmd("OptionSet", {
group = group,
pattern = "laststatus",
callback = function() M.on_set_laststatus() end
})
api.nvim_create_user_command("WindLineBenchmark", "lua require('windline.benchmark').benchmark()", {})
api.nvim_create_user_command("WindLineFloatToggle", "lua require('wlfloatline').toggle()", {})

end

M.remove_status_by_ft = function(filetypes)
Expand Down
12 changes: 5 additions & 7 deletions lua/wlanimation/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,10 @@ M.on_vimenter = function()
end
M.stop_all = Animation.stop_all

vim.api.nvim_exec(
[[augroup WLAnimation
au!
au VimEnter * lua require('wlanimation').on_vimenter()
augroup END]],
false
)
vim.api.nvim_create_autocmd("VimEnter", {
group = vim.api.nvim_create_augroup("WLAnimation", { clear = true }),
pattern = "*",
command = "lua require('wlanimation').on_vimenter()"
})

return M

0 comments on commit 9396040

Please sign in to comment.