refactor a little bit

master
Bryan 2024-09-17 20:07:49 -06:00
parent 0992c22b6e
commit 5f74e2079f
2 changed files with 9 additions and 12 deletions

View File

@ -4,19 +4,15 @@ local utils = require("cd-project.utils")
---@param auto boolean are we auto creating a project?
---@return string|nil
local function find_project_dir(auto)
local function find_project_dir()
local found = vim.fs.find(
vim.g.cd_project_config.project_dir_pattern,
{ upward = true, stop = vim.loop.os_homedir(), path = vim.fs.dirname(vim.fn.expand("%:p")) }
)
if #found == 0 then
if auto then
return nil
end
-- if there are no parent directories matching the project pattern then use current directory as project directory
return vim.fn.getcwd()
end
local project_dir = vim.fs.dirname(found[1])
@ -158,14 +154,17 @@ local function delete_project(project)
vim.notify("Project deleted: \n" .. project.name, vim.log.levels.INFO, { title = "cd-project.nvim" })
end
---@param opts? {show_duplicate_hints: boolean}
---@param opts? {auto: boolean, show_duplicate_hints: boolean}
local function add_current_project(opts)
local project_dir = find_project_dir()
opts = opts or { show_duplicate_hints = true }
opts = vim.tbl_deep_extend("force", { { auto = false, show_duplicate_hints = true }, opts })
if not project_dir then
if opts.auto then
return utils.log_err("Can't find project path of current file")
end
project_dir = vim.fn.getcwd()
end
local project = build_project_obj(project_dir)

View File

@ -6,9 +6,7 @@ local function setup()
vim.api.nvim_create_autocmd({ "VimEnter" }, {
group = auto_group_name,
callback = function(_)
if api.find_project_dir(true) ~= nil then
api.add_current_project({ show_duplicate_hints = false })
end
api.add_current_project({ auto = true, show_duplicate_hints = false })
end,
})
end