From 031a6a2497c167752ac4ca77651ad2ae709f93b5 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 24 Feb 2024 15:38:20 +0800 Subject: [PATCH] feat(git): update remote manager context when switch project --- bundle/git.vim/lua/git/ui/remote.lua | 32 ++++++++++++++++++++++---- docs/cn/roadmap.md | 1 + docs/roadmap.md | 1 + lua/spacevim/plugin/projectmanager.lua | 15 ++++++++---- 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/bundle/git.vim/lua/git/ui/remote.lua b/bundle/git.vim/lua/git/ui/remote.lua index e170b2614..8fe6b0239 100644 --- a/bundle/git.vim/lua/git/ui/remote.lua +++ b/bundle/git.vim/lua/git/ui/remote.lua @@ -13,7 +13,7 @@ local log = require('git.log') -- script local valuables -local show_help_infoo = false +local show_help_info = false local update_branch_list_jobid = -1 local update_branch_list_name = '' local update_branch_list_branches = {} @@ -31,8 +31,14 @@ local help_info = { '" : view git log', '" f: fetch remote under cursor', '" o: toggle display of branchs', + '" q: close windows"' } + +-- project_manager support + +local project_manager_registered = false + local bufnr = -1 local bufname = '' @@ -74,7 +80,7 @@ local function update_buf_context() return end local context = {} - if show_help_infoo then + if show_help_info then for _, v in ipairs(help_info) do table.insert(context, v) end @@ -231,10 +237,10 @@ local function toggle_remote_branch() end local function toggle_help() - if show_help_infoo then - show_help_infoo = false + if show_help_info then + show_help_info = false else - show_help_infoo = true + show_help_info = true end update_buf_context() end @@ -269,6 +275,10 @@ local function fetch_remote() end function M.open() + if not project_manager_registered then + require('spacevim.plugin.projectmanager').reg_callback(M.on_cwd_changed) + project_manager_registered = true + end if bufnr ~= -1 and vim.api.nvim_buf_is_valid(bufnr) then vim.api.nvim_buf_delete(bufnr, { force = true, @@ -304,6 +314,18 @@ function M.open() vim.api.nvim_buf_set_keymap(bufnr, 'n', 'f', '', { callback = fetch_remote, }) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'q', '', { + callback = function () + vim.cmd('quit') + show_help_info = false + end, + }) +end + +function M.on_cwd_changed() + if vim.api.nvim_buf_is_valid(bufnr) then + update() + end end return M diff --git a/docs/cn/roadmap.md b/docs/cn/roadmap.md index 8596f595b..17dfbf053 100644 --- a/docs/cn/roadmap.md +++ b/docs/cn/roadmap.md @@ -38,6 +38,7 @@ lang: zh - [x] 使得 `:A` 命令支持 toml 配置文件 - [x] 增加 git 远程仓库管理插件 - [x] 使用 `` 快捷键展示 git log + - [x] 切换项目时,更新 remote 窗口信息 - [ ] 缓存远程仓库以及分支名称等信息 - [ ] 基于项目路径存储信息 - [x] 使用 lua 实现 `ctags#update` 函数 diff --git a/docs/roadmap.md b/docs/roadmap.md index a8a1f83c1..d562197ad 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -37,6 +37,7 @@ The roadmap defines the project direction and priorities. - [x] make `:A` command support toml configuration file - [x] add git remote manager - [x] make `` show git log + - [x] update remote context when switch project - [ ] cache remote and branch info - [ ] cache info based on project root - [x] implement `ctags#update` in lua diff --git a/lua/spacevim/plugin/projectmanager.lua b/lua/spacevim/plugin/projectmanager.lua index e8ef5315d..65a0ff56a 100644 --- a/lua/spacevim/plugin/projectmanager.lua +++ b/lua/spacevim/plugin/projectmanager.lua @@ -158,8 +158,7 @@ local function compare(d1, d2) -- logger.debug('al is ' .. al) -- logger.debug('bl is ' .. bl) -- the project_rooter_outermost is 0/false or 1 true - if sp_opt.project_rooter_outermost == 0 - or sp_opt.project_rooter_outermost == false then + if sp_opt.project_rooter_outermost == 0 or sp_opt.project_rooter_outermost == false then if bl >= al then return false else @@ -344,8 +343,13 @@ function M.RootchandgeCallback() -- let b:_spacevim_project_name = g:_spacevim_project_name fn.setbufvar('%', '_spacevim_project_name', project.name) for _, Callback in pairs(project_callback) do - logger.debug(' run callback:' .. Callback) - fn.call(Callback, {}) + if type(Callback) == 'string' then + logger.debug(' run callback:' .. Callback) + fn.call(Callback, {}) + elseif type(Callback) == 'function' then + logger.debug(' run callback:' .. tostring(Callback)) + pcall(Callback) + end end end @@ -356,6 +360,9 @@ function M.reg_callback(func) else table.insert(project_callback, func) end + elseif type(func) == 'function' then + -- support lua function + table.insert(project_callback, func) else logger.warn('type of func is:' .. type(func)) logger.warn('can not register the project callback: ' .. fn.string(func))