فهرست منبع

[Refactor]: Remove vim-rooter and smart-cwd; then use project.nvim (#1315)

* Replace vim-rooter with project.nvim

* Implement stylua format

* Remove smart_cwd

* Implicitly update nvim-tree dir when project active

* Link datapath to cache

* Fix stylua

* Fix lint

* Fix telescope bug

* Fix telescope dependency

* Fix telescope once and for all

* Fix telescope once again
Ahmed Khalf 3 سال پیش
والد
کامیت
b9b9c69615
8فایلهای تغییر یافته به همراه69 افزوده شده و 42 حذف شده
  1. 4 7
      lua/core/dashboard.lua
  2. 6 0
      lua/core/nvimtree.lua
  3. 48 0
      lua/core/project.lua
  4. 0 15
      lua/core/rooter.lua
  5. 3 0
      lua/core/telescope.lua
  6. 2 4
      lua/default-config.lua
  7. 0 9
      lua/lsp/init.lua
  8. 6 7
      lua/plugins.lua

+ 4 - 7
lua/core/dashboard.lua

@@ -31,19 +31,16 @@ M.config = function()
         description = { "  Find File          " },
         command = "Telescope find_files",
       },
-      b = {
+      -- b is reserved for the core.project module
+      c = {
         description = { "  Recently Used Files" },
         command = "Telescope oldfiles",
       },
-      -- c = {
-      --   description = { "  Load Last Session  " },
-      --   command = "SessionLoad",
-      -- },
-      c = {
+      d = {
         description = { "  Find Word          " },
         command = "Telescope live_grep",
       },
-      d = {
+      e = {
         description = { "  Settings           " },
         command = ":e " .. USER_CONFIG_PATH,
       },

+ 6 - 0
lua/core/nvimtree.lua

@@ -60,6 +60,12 @@ M.setup = function()
     g["nvim_tree_" .. opt] = val
   end
 
+  -- Implicitly update nvim-tree when project module is active
+  if lvim.builtin.project.active then
+    vim.g.nvim_tree_update_cwd = 1
+    vim.g.nvim_tree_respect_buf_cwd = 1
+  end
+
   local tree_cb = nvim_tree_config.nvim_tree_callback
 
   if not g.nvim_tree_bindings then

+ 48 - 0
lua/core/project.lua

@@ -0,0 +1,48 @@
+local M = {}
+--
+function M.config()
+  lvim.builtin.project = {
+    --- This is on by default since it's currently the expected behavior.
+    ---@usage set to false to disable project.nvim.
+    active = true,
+
+    -- Manual mode doesn't automatically change your root directory, so you have
+    -- the option to manually do so using `:ProjectRoot` command.
+    manual_mode = false,
+
+    -- Methods of detecting the root directory. **"lsp"** uses the native neovim
+    -- lsp, while **"pattern"** uses vim-rooter like glob pattern matching. Here
+    -- order matters: if one is not detected, the other is used as fallback. You
+    -- can also delete or rearangne the detection methods.
+    detection_methods = { "lsp", "pattern" },
+
+    -- All the patterns used to detect root dir, when **"pattern"** is in
+    -- detection_methods
+    patterns = { ".git", "_darcs", ".hg", ".bzr", ".svn", "Makefile", "package.json" },
+
+    -- When set to false, you will get a message when project.nvim changes your
+    -- directory.
+    silent_chdir = true,
+  }
+end
+--
+function M.setup()
+  local settings = lvim.builtin.project
+
+  -- Table of lsp clients to ignore by name
+  -- eg: { "efm", ... }
+  settings["ignore_lsp"] = {}
+
+  -- Path where project.nvim will store the project history for use in
+  -- telescope
+  settings["datapath"] = CACHE_PATH
+
+  require("project_nvim").setup(settings)
+
+  lvim.builtin.dashboard.custom_section["b"] = {
+    description = { "  Recent Projects    " },
+    command = "Telescope projects",
+  }
+end
+--
+return M

+ 0 - 15
lua/core/rooter.lua

@@ -1,15 +0,0 @@
-local M = {}
-function M.config()
-  lvim.builtin.rooter = {
-    --- This is on by default since it's currently the expected behavior.
-    ---@usage set to false to disable vim-rooter.
-    active = true,
-    silent_chdir = 1,
-    manual_only = 0,
-  }
-end
-function M.setup()
-  vim.g.rooter_silent_chdir = lvim.builtin.rooter.silent_chdir
-  vim.g.rooter_manual_only = lvim.builtin.rooter.manual_only
-end
-return M

+ 3 - 0
lua/core/telescope.lua

@@ -119,6 +119,9 @@ function M.setup()
     return
   end
   telescope.setup(lvim.builtin.telescope)
+  if lvim.builtin.project.active then
+    pcall(require("telescope").load_extension, "projects")
+  end
 end
 
 return M

+ 2 - 4
lua/default-config.lua

@@ -26,7 +26,7 @@ lvim = {
     gitsigns = {},
     which_key = {},
     comment = {},
-    rooter = {},
+    project = {},
     galaxyline = {},
     bufferline = {},
     dap = {},
@@ -102,8 +102,6 @@ lvim = {
     popup_border = "single",
     on_attach_callback = nil,
     on_init_callback = nil,
-    ---@usage query the project directory from the language server and use it to set the CWD
-    smart_cwd = true,
     null_ls = {
       setup = {},
     },
@@ -1301,7 +1299,7 @@ require("core.terminal").config()
 require("core.telescope").config()
 require("core.treesitter").config()
 require("core.nvimtree").config()
-require("core.rooter").config()
+require("core.project").config()
 require("core.bufferline").config()
 require("core.autopairs").config()
 require("core.comment").config()

+ 0 - 9
lua/lsp/init.lua

@@ -55,14 +55,6 @@ local function add_lsp_buffer_keybindings(bufnr)
   wk.register(keys, { mode = "n", buffer = bufnr })
 end
 
-local function set_smart_cwd(client)
-  local proj_dir = client.config.root_dir
-  if lvim.lsp.smart_cwd and proj_dir ~= "/" then
-    vim.api.nvim_set_current_dir(proj_dir)
-    require("core.nvimtree").change_tree_dir(proj_dir)
-  end
-end
-
 function M.common_capabilities()
   local capabilities = vim.lsp.protocol.make_client_capabilities()
   capabilities.textDocument.completion.completionItem.snippetSupport = true
@@ -127,7 +119,6 @@ function M.common_on_attach(client, bufnr)
   end
   lsp_highlight_document(client)
   add_lsp_buffer_keybindings(bufnr)
-  set_smart_cwd(client)
   require("lsp.null-ls").setup(vim.bo.filetype)
 end
 

+ 6 - 7
lua/plugins.lua

@@ -147,17 +147,16 @@ return {
     disable = not lvim.builtin.comment.active,
   },
 
-  -- vim-rooter
+  -- project.nvim
   {
-    "airblade/vim-rooter",
-    -- event = "BufReadPre",
+    "ahmedkhalf/project.nvim",
     config = function()
-      require("core.rooter").setup()
-      if lvim.builtin.rooter.on_config_done then
-        lvim.builtin.rooter.on_config_done()
+      require("core.project").setup()
+      if lvim.builtin.project.on_config_done then
+        lvim.builtin.project.on_config_done()
       end
     end,
-    disable = not lvim.builtin.rooter.active,
+    disable = not lvim.builtin.project.active,
   },
 
   -- Icons