1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-03-23 17:49:57 +08:00

fix(projectmanager): remove debug info

This commit is contained in:
wsdjeg 2022-10-26 17:17:56 +08:00
parent 7347ec4bff
commit d3a2916dc8

View File

@ -20,209 +20,219 @@ local project_rooter_patterns = {}
local project_rooter_ignores = {} local project_rooter_ignores = {}
local project_callback = {} local project_callback = {}
local cd = 'cd' local cd = 'cd'
if fn.exists(':tcd') then if fn.exists(':tcd') then
cd = 'tcd' cd = 'tcd'
elseif fn.exists(':lcd') then elseif fn.exists(':lcd') then
cd = 'lcd' cd = 'lcd'
end end
local function update_rooter_patterns() local function update_rooter_patterns()
project_rooter_patterns = {} project_rooter_patterns = {}
project_rooter_ignores = {} project_rooter_ignores = {}
for _,v in pairs(sp_opt.project_rooter_patterns) do for _, v in pairs(sp_opt.project_rooter_patterns) do
if string.match(v, '^!') == nil then if string.match(v, '^!') == nil then
table.insert(project_rooter_patterns, v) table.insert(project_rooter_patterns, v)
else else
table.insert(project_rooter_ignores, string.sub(v, 2, -1)) table.insert(project_rooter_ignores, string.sub(v, 2, -1))
end
end end
end
end end
local function is_ignored_dir(dir) local function is_ignored_dir(dir)
for _,v in pairs(project_rooter_ignores) do for _, v in pairs(project_rooter_ignores) do
if string.match(dir, v) ~= nil then if string.match(dir, v) ~= nil then
logger.debug('this is an ignored dir:' .. dir) logger.debug('this is an ignored dir:' .. dir)
return true return true
end
end end
return false end
return false
end end
local function cache() local function cache()
local path = sp_file.unify_path(project_cache_path, ':p') local path = sp_file.unify_path(project_cache_path, ':p')
local file = io.open(path, 'w') local file = io.open(path, 'w')
if file then if file then
if file:write(sp_json.json_encode(project_paths)) == nil then if file:write(sp_json.json_encode(project_paths)) == nil then
logger.debug('failed to write to file:' .. path) logger.debug('failed to write to file:' .. path)
end
io.close(file)
else
logger.debug('failed to open file:' .. path)
end end
io.close(file)
else
logger.debug('failed to open file:' .. path)
end
end end
local function readfile(path) local function readfile(path)
local file = io.open(path, "r") local file = io.open(path, 'r')
if file then if file then
local content = file:read("*a") local content = file:read('*a')
io.close(file) io.close(file)
return content return content
end end
return nil return nil
end end
local function filereadable(fpath) local function filereadable(fpath)
local f = io.open(fpath, 'r') local f = io.open(fpath, 'r')
if f ~= nil then io.close(f) return true else return false end if f ~= nil then
io.close(f)
return true
else
return false
end
end end
local function isdirectory(fpath) local function isdirectory(fpath)
local f, err, code = io.open(fpath, "r") local f, err, code = io.open(fpath, 'r')
if f ~= nil then if f ~= nil then
f:close() f:close()
return false return false
end end
return code == 13 return code == 13
end end
local function filter_invalid(projects) local function filter_invalid(projects)
for key, value in pairs(projects) do for key, value in pairs(projects) do
if fn.isdirectory(value.path) == 0 then if fn.isdirectory(value.path) == 0 then
projects[key] = nil projects[key] = nil
end
end end
return projects end
return projects
end end
local function load_cache() local function load_cache()
if filereadable(project_cache_path) then if filereadable(project_cache_path) then
logger.info('Load projects cache from: ' .. project_cache_path) logger.info('Load projects cache from: ' .. project_cache_path)
local cache_context = readfile(project_cache_path) local cache_context = readfile(project_cache_path)
if cache_context ~= nil then if cache_context ~= nil then
local cache_object = sp_json.json_decode(cache_context) local cache_object = sp_json.json_decode(cache_context)
if type(cache_object) == 'table' then if type(cache_object) == 'table' then
project_paths = filter_invalid(cache_object) project_paths = filter_invalid(cache_object)
end end
end
else
logger.info('projects cache file does not exists!')
end end
else
logger.info('projects cache file does not exists!')
end
end end
local function sort_by_opened_time() local function sort_by_opened_time()
local paths = {} local paths = {}
for k,v in pairs(project_paths) do table.insert(paths, k) end for k, v in pairs(project_paths) do
table.sort(paths, compare_time) table.insert(paths, k)
if sp_opt.projects_cache_num > 0 and #paths >= sp_opt.projects_cache_num then end
for i = sp_opt.projects_cache_num, #paths, 1 do table.sort(paths, compare_time)
project_paths[paths[sp_opt.projects_cache_num]] = nil if sp_opt.projects_cache_num > 0 and #paths >= sp_opt.projects_cache_num then
table.remove(paths, sp_opt.projects_cache_num) for i = sp_opt.projects_cache_num, #paths, 1 do
end project_paths[paths[sp_opt.projects_cache_num]] = nil
table.remove(paths, sp_opt.projects_cache_num)
end end
return paths end
return paths
end end
local function compare_time(d1, d2) local function compare_time(d1, d2)
local proj1 = project_paths[d1] or {} local proj1 = project_paths[d1] or {}
local proj1time = proj1['opened_time'] or 0 local proj1time = proj1['opened_time'] or 0
local proj2 = project_paths[d2] or {} local proj2 = project_paths[d2] or {}
local proj2time = proj2['opened_time'] or 0 local proj2time = proj2['opened_time'] or 0
return proj2time - proj1time return proj2time - proj1time
end end
local function change_dir(dir) local function change_dir(dir)
if dir == sp_file.unify_path(fn.getcwd()) then if dir == sp_file.unify_path(fn.getcwd()) then
logger.debug('same as current directory, no need to change.') logger.debug('same as current directory, no need to change.')
else else
if dir ~= nil then if dir ~= nil then
logger.info('change to root: ' .. dir) logger.info('change to root: ' .. dir)
sp.cmd(cd .. ' ' .. sp.fn.fnameescape(sp.fn.fnamemodify(dir, ':p'))) sp.cmd(cd .. ' ' .. sp.fn.fnameescape(sp.fn.fnamemodify(dir, ':p')))
end
end end
end
end end
local function sort_dirs(dirs) local function sort_dirs(dirs)
table.sort(dirs, compare) table.sort(dirs, compare)
local dir = dirs[1] local dir = dirs[1]
local bufdir = fn.getbufvar('%', 'rootDir', '') local bufdir = fn.getbufvar('%', 'rootDir', '')
if bufdir == dir then if bufdir == dir then
return '' return ''
else else
return dir return dir
end end
end end
local function compare(d1, d2) local function compare(d1, d2)
local _, al = string.gsub(d1, "/", "") local _, al = string.gsub(d1, '/', '')
local _, bl = string.gsub(d2, "/", "") local _, bl = string.gsub(d2, '/', '')
if sp_opt.project_rooter_outermost == 0 then if sp_opt.project_rooter_outermost == 0 then
return bl - al return bl - al
else else
return al - bl return al - bl
end end
end end
local function find_root_directory() local function find_root_directory()
local fd = fn.bufname('%') local fd = fn.bufname('%')
if fn == '' then if fn == '' then
logger.debug('bufname is empty') logger.debug('bufname is empty')
fd = fn.getcwd() fd = fn.getcwd()
end
logger.debug('start to find root for: ' .. fd)
local dirs = {}
for _, pattern in pairs(project_rooter_patterns) do
logger.debug('searching rooter_patterns:' .. pattern)
local find_path = ''
if string.sub(pattern, -1) == '/' then
if sp_opt.project_rooter_outermost == 1 then
find_path = sp_file.finddir(pattern, fd, -1)
else
find_path = sp_file.finddir(pattern, fd)
end
else
if sp_opt.project_rooter_outermost == 1 then
find_path = sp_file.findfile(pattern, fd, -1)
else
find_path = sp_file.findfile(pattern, fd)
end
end end
logger.debug('start to find root for: ' .. fd) local path_type = fn.getftype(find_path)
local dirs = {} if (path_type == 'dir' or path_type == 'file') and not (is_ignored_dir(find_path)) then
for _,pattern in pairs(project_rooter_patterns) do find_path = sp_file.unify_path(find_path, ':p')
logger.debug('searching rooter_patterns:' .. pattern) if path_type == 'dir' then
local find_path = '' find_path = sp_file.unify_path(find_path, ':h:h')
if string.sub(pattern, -1) == '/' then else
if sp_opt.project_rooter_outermost == 1 then find_path = sp_file.unify_path(find_path, ':h')
find_path = sp_file.finddir(pattern, fd, -1) end
else if find_path ~= sp_file.unify_path(fn.expand('$HOME')) then
find_path = sp_file.finddir(pattern, fd) logger.info(' (' .. pattern .. '):' .. find_path)
end table.insert(dirs, find_path)
else else
if sp_opt.project_rooter_outermost == 1 then logger.info('ignore $HOME directory:' .. find_path)
find_path = sp_file.findfile(pattern, fd, -1) end
else
find_path = sp_file.findfile(pattern, fd)
end
end
logger.debug('find_path is:' .. find_path)
local path_type = fn.getftype(find_path)
logger.debug('path_type is:' .. path_type)
if ( path_type == 'dir' or path_type == 'file' )
and not(is_ignored_dir(find_path)) then
find_path = sp_file.unify_path(find_path, ':p')
if path_type == 'dir' then
find_path = sp_file.unify_path(find_path, ':h:h')
else
find_path = sp_file.unify_path(find_path, ':h')
end
logger.debug('find_path is:' .. find_path)
if find_path ~= sp_file.unify_path(fn.expand('$HOME')) then
logger.info(' (' .. pattern .. '):' .. find_path)
table.insert(dirs, find_path)
else
logger.info('ignore $HOME directory:' .. find_path)
end
end
end end
return sort_dirs(dirs) end
return sort_dirs(dirs)
end end
local function cache_project(prj) local function cache_project(prj)
project_paths[prj.path] = prj project_paths[prj.path] = prj
sp.cmd('let g:unite_source_menu_menus.Projects.command_candidates = []') sp.cmd('let g:unite_source_menu_menus.Projects.command_candidates = []')
for _, key in pairs(sort_by_opened_time()) do for _, key in pairs(sort_by_opened_time()) do
local desc = '[' .. project_paths[key].name .. '] ' .. project_paths[key].path .. ' <' .. fn.strftime('%Y-%m-%d %T', project_paths[key].opened_time) .. '>' local desc = '['
local cmd = "call SpaceVim#plugins#projectmanager#open('" .. project_paths[key].path .. "')" .. project_paths[key].name
sp.cmd('call add(g:unite_source_menu_menus.Projects.command_candidates, ["' .. '] '
.. desc .. project_paths[key].path
.. '", "' .. ' <'
.. cmd .. fn.strftime('%Y-%m-%d %T', project_paths[key].opened_time)
.. '"])') .. '>'
end local cmd = "call SpaceVim#plugins#projectmanager#open('" .. project_paths[key].path .. "')"
if sp_opt.enable_projects_cache then sp.cmd(
cache() 'call add(g:unite_source_menu_menus.Projects.command_candidates, ["'
end .. desc
.. '", "'
.. cmd
.. '"])'
)
end
if sp_opt.enable_projects_cache then
cache()
end
end end
-- call add(g:spacevim_project_rooter_patterns, '.SpaceVim.d/') -- call add(g:spacevim_project_rooter_patterns, '.SpaceVim.d/')
@ -231,7 +241,7 @@ end
update_rooter_patterns() update_rooter_patterns()
if sp_opt.enable_projects_cache == 1 then if sp_opt.enable_projects_cache == 1 then
load_cache() load_cache()
end end
sp.cmd([[ sp.cmd([[
@ -241,143 +251,140 @@ sp.cmd([[
]]) ]])
if sp_opt.project_auto_root == 1 then if sp_opt.project_auto_root == 1 then
sp.cmd("augroup spacevim_project_rooter") sp.cmd('augroup spacevim_project_rooter')
sp.cmd("autocmd!") sp.cmd('autocmd!')
sp.cmd("autocmd VimEnter,BufEnter * call SpaceVim#plugins#projectmanager#current_root()") sp.cmd('autocmd VimEnter,BufEnter * call SpaceVim#plugins#projectmanager#current_root()')
sp.cmd("autocmd BufWritePost * :call setbufvar('%', 'rootDir', '') | call SpaceVim#plugins#projectmanager#current_root()") sp.cmd(
sp.cmd("augroup END") "autocmd BufWritePost * :call setbufvar('%', 'rootDir', '') | call SpaceVim#plugins#projectmanager#current_root()"
)
sp.cmd('augroup END')
end end
local M = {} local M = {}
function M.list() function M.list()
if layer.isLoaded('unite') then if layer.isLoaded('unite') then
sp.cmd('Unite menu:Projects') sp.cmd('Unite menu:Projects')
elseif layer.isLoaded('denite') then elseif layer.isLoaded('denite') then
sp.cmd('Denite menu:Projects') sp.cmd('Denite menu:Projects')
elseif layer.isLoaded('fzf') then elseif layer.isLoaded('fzf') then
sp.cmd('FzfMenu Projects') sp.cmd('FzfMenu Projects')
elseif layer.isLoaded('leaderf') then elseif layer.isLoaded('leaderf') then
sp.cmd("call SpaceVim#layers#leaderf#run_menu('Projects')") sp.cmd("call SpaceVim#layers#leaderf#run_menu('Projects')")
else else
logger.warn('fuzzy find layer is needed to find project!') logger.warn('fuzzy find layer is needed to find project!')
end end
end end
function M.open(project) function M.open(project)
local path = project_paths[project]['path'] local path = project_paths[project]['path']
local name = project_paths[project]['name'] local name = project_paths[project]['name']
sp.cmd('tabnew') sp.cmd('tabnew')
-- I am not sure we should set the project name here. -- I am not sure we should set the project name here.
-- sp.cmd('let t:_spacevim_tab_name = "[' .. name .. ']"') -- sp.cmd('let t:_spacevim_tab_name = "[' .. name .. ']"')
sp.cmd(cd .. ' ' .. path) sp.cmd(cd .. ' ' .. path)
if sp_opt.filemanager == 'vimfiler' then if sp_opt.filemanager == 'vimfiler' then
sp.cmd('Startify | VimFiler') sp.cmd('Startify | VimFiler')
elseif sp_opt.filemanager == 'nerdtree' then elseif sp_opt.filemanager == 'nerdtree' then
sp.cmd('Startify | NERDTree') sp.cmd('Startify | NERDTree')
elseif sp_opt.filemanager == 'defx' then elseif sp_opt.filemanager == 'defx' then
sp.cmd('Startify | Defx -new') sp.cmd('Startify | Defx -new')
end end
end end
function M.current_name() function M.current_name()
return sp.eval('b:_spacevim_project_name') return sp.eval('b:_spacevim_project_name')
end end
function M.RootchandgeCallback() function M.RootchandgeCallback()
local path = sp_file.unify_path(fn.getcwd(), ':p') local path = sp_file.unify_path(fn.getcwd(), ':p')
local name = fn.fnamemodify(path, ':h:t') local name = fn.fnamemodify(path, ':h:t')
logger.debug('project name is:' .. name) logger.debug('project name is:' .. name)
logger.debug('project path is:' .. path) logger.debug('project path is:' .. path)
local project = { local project = {
['path'] = path, ['path'] = path,
['name'] = name, ['name'] = name,
['opened_time'] = os.time() ['opened_time'] = os.time(),
} }
if project.path == '' then if project.path == '' then
return return
end end
cache_project(project) cache_project(project)
-- let g:_spacevim_project_name = project.name -- let g:_spacevim_project_name = project.name
-- let b:_spacevim_project_name = g:_spacevim_project_name -- let b:_spacevim_project_name = g:_spacevim_project_name
fn.setbufvar('%', '_spacevim_project_name', project.name) fn.setbufvar('%', '_spacevim_project_name', project.name)
for _, Callback in pairs(project_callback) do for _, Callback in pairs(project_callback) do
logger.debug('run callback:' .. Callback) logger.debug('run callback:' .. Callback)
fn.call(Callback, {}) fn.call(Callback, {})
end end
end end
function M.reg_callback(func) function M.reg_callback(func)
if type(func) == 'string' then if type(func) == 'string' then
if string.match(func, '^function%(') ~= nil then if string.match(func, '^function%(') ~= nil then
table.insert(project_callback, string.sub(func, 11, -3)) table.insert(project_callback, string.sub(func, 11, -3))
else
table.insert(project_callback, func)
end
else else
logger.warn('type of func is:' .. type(func)) table.insert(project_callback, func)
logger.warn('can not register the project callback: ' .. fn.string(func))
end end
else
logger.warn('type of func is:' .. type(func))
logger.warn('can not register the project callback: ' .. fn.string(func))
end
end end
function M.kill_project() function M.kill_project()
local name = sp.eval('b:_spacevim_project_name') local name = sp.eval('b:_spacevim_project_name')
if name ~= '' then if name ~= '' then
sp_buffer.filter_do( sp_buffer.filter_do({
{ ['expr'] = {
['expr'] = { 'buflisted(v:val)',
'buflisted(v:val)', 'getbufvar(v:val, "_spacevim_project_name") == "' .. name .. '"',
'getbufvar(v:val, "_spacevim_project_name") == "' .. name .. '"', },
}, ['do'] = 'bd %d',
['do'] = 'bd %d' })
} end
)
end
end end
function M.complete_project(arglead, cmdline, cursorpos) function M.complete_project(arglead, cmdline, cursorpos)
local dir = '~' local dir = '~'
local result = fn.split(fn.globpath(dir, '*'), "\n") local result = fn.split(fn.globpath(dir, '*'), '\n')
local ps = {} local ps = {}
for p in result do for p in result do
if fn.isdirectory(p) == 1 and fn.isdirectory(p .. sp_file.separator .. '.git') == 1 then if fn.isdirectory(p) == 1 and fn.isdirectory(p .. sp_file.separator .. '.git') == 1 then
table.insert(ps, fn.fnamemodify(p, ':t')) table.insert(ps, fn.fnamemodify(p, ':t'))
end
end end
return fn.join(ps, "\n") end
return fn.join(ps, '\n')
end end
function M.OpenProject(p) function M.OpenProject(p)
sp.cmd('CtrlP '.. dir) sp.cmd('CtrlP ' .. dir)
end end
function M.current_root() function M.current_root()
local bufname = fn.bufname('%') local bufname = fn.bufname('%')
if bufname == '[denite]' if bufname:match('%[denite%]') or bufname:match('denite-filter') or bufname:match('%[defx%]') then
or bufname == 'denite-filter' return
or bufname == '[defx]' then end
return if
end table.concat(sp_opt.project_rooter_patterns, ':')
if table.concat(sp_opt.project_rooter_patterns, ':') ~= table.concat(spacevim_project_rooter_patterns, ':') then ~= table.concat(spacevim_project_rooter_patterns, ':')
logger.info('project_rooter_patterns option has been change, clear b:rootDir') then
fn.setbufvar('%', 'rootDir', '') logger.info('project_rooter_patterns option has been change, clear b:rootDir')
spacevim_project_rooter_patterns = sp_opt.project_rooter_patterns fn.setbufvar('%', 'rootDir', '')
update_rooter_patterns() spacevim_project_rooter_patterns = sp_opt.project_rooter_patterns
end update_rooter_patterns()
local rootdir = fn.getbufvar('%', 'rootDir', '') end
-- @bug fn.getbufvar('%', 'rootDir', '') return nil local rootdir = fn.getbufvar('%', 'rootDir', '')
if rootdir == '' then
rootdir = find_root_directory()
if rootdir == nil or rootdir == '' then if rootdir == nil or rootdir == '' then
rootdir = find_root_directory() rootdir = sp_file.unify_path(fn.getcwd())
if rootdir == nil or rootdir == '' then
rootdir = sp_file.unify_path(fn.getcwd())
end
fn.setbufvar('%', 'rootDir', rootdir)
end end
change_dir(rootdir) fn.setbufvar('%', 'rootDir', rootdir)
M.RootchandgeCallback() end
return rootdir change_dir(rootdir)
M.RootchandgeCallback()
return rootdir
end end
return M return M