diff --git a/lua/project_nvim/config.lua b/lua/project_nvim/config.lua index 4eff745..af797a9 100644 --- a/lua/project_nvim/config.lua +++ b/lua/project_nvim/config.lua @@ -46,7 +46,7 @@ M.defaults = { } ---@type ProjectOptions -M.options = {} +M.options = nil or {} M.setup = function(options) M.options = vim.tbl_deep_extend("force", M.defaults, options or {}) diff --git a/lua/project_nvim/project.lua b/lua/project_nvim/project.lua index 76972ee..e084e6e 100644 --- a/lua/project_nvim/project.lua +++ b/lua/project_nvim/project.lua @@ -12,8 +12,8 @@ M.last_project = nil function M.find_lsp_root() -- Get lsp client for current buffer -- Returns nil or string - local buf_ft = vim.api.nvim_buf_get_option(0, "filetype") - local clients = vim.lsp.buf_get_clients() + local buf_ft = vim.api.nvim_get_option_value("filetype", { buf = 0 }) + local clients = vim.lsp.get_clients({ bufnr = 0 }) if next(clients) == nil then return nil end @@ -39,12 +39,12 @@ function M.find_pattern_root() local last_dir_cache = "" local curr_dir_cache = {} - local function get_parent(path) - path = path:match("^(.*)/") - if path == "" then - path = "/" + local function get_parent(p) + p = p:match("^(.*)/") + if p == "" then + p = "/" end - return path + return p end local function get_files(file_dir) @@ -72,22 +72,22 @@ function M.find_pattern_root() end local function sub(dir, identifier) - local path = get_parent(dir) + local p = get_parent(dir) while true do - if is(path, identifier) then + if is(p, identifier) then return true end - local current = path - path = get_parent(path) - if current == path then + local current = p + p = get_parent(p) + if current == p then return false end end end local function child(dir, identifier) - local path = get_parent(dir) - return is(path, identifier) + local p = get_parent(dir) + return is(p, identifier) end local function has(dir, identifier) @@ -160,7 +160,9 @@ function M.attach_to_lsp() local _on_attach = lsp_config.on_attach lsp_config.on_attach = function(client, bufnr) on_attach_lsp(client, bufnr) - _on_attach(client, bufnr) + if _on_attach ~= nil then + _on_attach(client, bufnr) + end end end return _start_client(lsp_config) @@ -176,12 +178,12 @@ function M.set_pwd(dir, method) if vim.fn.getcwd() ~= dir then local scope_chdir = config.options.scope_chdir - if scope_chdir == 'global' then + if scope_chdir == "global" then vim.api.nvim_set_current_dir(dir) - elseif scope_chdir == 'tab' then - vim.cmd('tcd ' .. dir) - elseif scope_chdir == 'win' then - vim.cmd('lcd ' .. dir) + elseif scope_chdir == "tab" then + vim.cmd("tcd " .. dir) + elseif scope_chdir == "win" then + vim.cmd("lcd " .. dir) else return end @@ -214,7 +216,7 @@ function M.get_project_root() end function M.is_file() - local buf_type = vim.api.nvim_buf_get_option(0, "buftype") + local buf_type = vim.api.nvim_get_option_value("filetype", { buf = 0 }) local whitelisted_buf_type = { "", "acwrite" } local is_in_whitelist = false @@ -251,7 +253,7 @@ end function M.add_project_manually() local current_dir = vim.fn.expand("%:p:h", true) - M.set_pwd(current_dir, 'manual') + M.set_pwd(current_dir, "manual") end function M.init() diff --git a/lua/project_nvim/utils/globtopattern.lua b/lua/project_nvim/utils/globtopattern.lua index 1d784ca..12c81f4 100644 --- a/lua/project_nvim/utils/globtopattern.lua +++ b/lua/project_nvim/utils/globtopattern.lua @@ -27,8 +27,8 @@ function M.globtopattern(g) end -- escape pattern char - local function escape(c) - return c:match("^%w$") and c or "%" .. c + local function escape(char) + return char:match("^%w$") and char or "%" .. char end -- Convert tokens at end of charset. diff --git a/lua/telescope/_extensions/projects.lua b/lua/telescope/_extensions/projects.lua index 7e35f3a..3c46474 100644 --- a/lua/telescope/_extensions/projects.lua +++ b/lua/telescope/_extensions/projects.lua @@ -67,18 +67,14 @@ local function delete_buffers() end end -local function change_working_directory(prompt_bufnr, prompt) - local selected_entry = state.get_selected_entry(prompt_bufnr) +local function change_working_directory(prompt_bufnr) + local selected_entry = state.get_selected_entry() if selected_entry == nil then actions.close(prompt_bufnr) return end local project_path = selected_entry.value - if prompt == true then - actions._close(prompt_bufnr, true) - else - actions.close(prompt_bufnr) - end + actions.close(prompt_bufnr) local cd_successful = project.set_pwd(project_path, "telescope") if cd_successful then delete_buffers() @@ -87,7 +83,7 @@ local function change_working_directory(prompt_bufnr, prompt) end local function find_project_files(prompt_bufnr) - local project_path, cd_successful = change_working_directory(prompt_bufnr, true) + local project_path, cd_successful = change_working_directory(prompt_bufnr) local opt = { cwd = project_path, hidden = config.options.show_hidden, @@ -99,7 +95,7 @@ local function find_project_files(prompt_bufnr) end local function browse_project_files(prompt_bufnr) - local project_path, cd_successful = change_working_directory(prompt_bufnr, true) + local project_path, cd_successful = change_working_directory(prompt_bufnr) local opt = { cwd = project_path, hidden = config.options.show_hidden, @@ -110,7 +106,7 @@ local function browse_project_files(prompt_bufnr) end local function search_in_project_files(prompt_bufnr) - local project_path, cd_successful = change_working_directory(prompt_bufnr, true) + local project_path, cd_successful = change_working_directory(prompt_bufnr) local opt = { cwd = project_path, hidden = config.options.show_hidden, @@ -122,7 +118,7 @@ local function search_in_project_files(prompt_bufnr) end local function recent_project_files(prompt_bufnr) - local _, cd_successful = change_working_directory(prompt_bufnr, true) + local _, cd_successful = change_working_directory(prompt_bufnr) local opt = { cwd_only = true, hidden = config.options.show_hidden, @@ -133,7 +129,7 @@ local function recent_project_files(prompt_bufnr) end local function delete_project(prompt_bufnr) - local selectedEntry = state.get_selected_entry(prompt_bufnr) + local selectedEntry = state.get_selected_entry() if selectedEntry == nil then actions.close(prompt_bufnr) return @@ -155,33 +151,35 @@ end local function projects(opts) opts = opts or {} - pickers.new(opts, { - prompt_title = "Recent Projects", - finder = create_finder(), - previewer = false, - sorter = telescope_config.generic_sorter(opts), - attach_mappings = function(prompt_bufnr, map) - map("n", "f", find_project_files) - map("n", "b", browse_project_files) - map("n", "d", delete_project) - map("n", "s", search_in_project_files) - map("n", "r", recent_project_files) - map("n", "w", change_working_directory) - - map("i", "", find_project_files) - map("i", "", browse_project_files) - map("i", "", delete_project) - map("i", "", search_in_project_files) - map("i", "", recent_project_files) - map("i", "", change_working_directory) - - local on_project_selected = function() - find_project_files(prompt_bufnr) - end - actions.select_default:replace(on_project_selected) - return true - end, - }):find() + pickers + .new(opts, { + prompt_title = "Recent Projects", + finder = create_finder(), + previewer = false, + sorter = telescope_config.generic_sorter(opts), + attach_mappings = function(prompt_bufnr, map) + map("n", "f", find_project_files) + map("n", "b", browse_project_files) + map("n", "d", delete_project) + map("n", "s", search_in_project_files) + map("n", "r", recent_project_files) + map("n", "w", change_working_directory) + + map("i", "", find_project_files) + map("i", "", browse_project_files) + map("i", "", delete_project) + map("i", "", search_in_project_files) + map("i", "", recent_project_files) + map("i", "", change_working_directory) + + local on_project_selected = function() + find_project_files(prompt_bufnr) + end + actions.select_default:replace(on_project_selected) + return true + end, + }) + :find() end return telescope.register_extension({