NvChad / NvChad

Blazing fast Neovim config providing solid defaults and a beautiful UI, enhancing your neovim experience.
https://nvchad.com/
GNU General Public License v3.0
24.7k stars 2.12k forks source link

Extension packs for debugging/languages? #625

Closed hunger closed 2 years ago

hunger commented 2 years ago

In my NVChad setup I have a non-trivial amount of plugins for rust support, incl. debugging support via DAP (debug adapter protocol). It would be cool if that could be made available to other users somehow.

I have been wondering whether it might make sense to implement something like "extension packs" for certain sets of features like DAP support that do require quite a bit of setup to be "nice". Programming specific setups might be another such expansion pack.

zbirenbaum commented 2 years ago

I think the closest thing to "extension packs" is the default configs listed in the README of most plugins. I think supplying prebuilt ones is a bit out of the scope of NvChad and a little too much work for the devs. I also feel like it encourages some poor habits where people rely too much on devs to pre-configure everything which is kinda against the whole vim/neovim philosophy. I believe NvChad is just meant to supply a very efficient basis to work off of. I could definitely be wrong about that and feel free to step in and correct me if I am @ devs

I'm sure other users would appreciate it if you shared your config though! I'll drop mine here in case anyone wants it for python. I will say while the novelty was kinda cool I really find myself just opening a terminal buffer and debugging from there most of the time, but here's how to set up dap if anyone wants it and finds this post.

First put something like this in your custom/init.lua:

  use {
    'mfussenegger/nvim-dap',
    after = "coq_nvim",
    disable = not plugin_status.dap,
    config = function()
      require "custom.plugins.dap_configs.python"
    end,
    requires = {
      "Pocco81/DAPInstall.nvim",
      "mfussenegger/nvim-dap-python",
    },
  }

if you want dapui, just do this in the same file:

  use {
    "rcarriga/nvim-dap-ui",
    after = "nvim-dap",
    config = function()
      require("dapui").setup()
    end,
  }

If you do the disable thing, make sure to put a corresponding flag in your chadrc. Just look at other plugins for examples.

My config referenced in the config = function() looks like this:


require('dap-python').setup("/home/zach/.virtualenvs/py3nvim/bin/python")

   vim.api.nvim_set_keymap("n", "<Leader>d", '<Cmd>lua require"dapui".toggle()<CR>', {
      silent = true,
      noremap = true,
   })
   vim.api.nvim_set_keymap("n", "<C-b>", '<Cmd>lua require"dap".toggle_breakpoint()<CR>', {
      silent = true,
      noremap = true,
   })
   vim.api.nvim_set_keymap("n", "<C-c>", '<Cmd>:lua require"dap".continue()<CR>',{
      silent = true,
      noremap = true,
   })
   vim.api.nvim_set_keymap("n", "<C-s>", '<Cmd>lua require"dap".step_into()<CR>', {
      silent = true,
      noremap = true,
   })
end

dap_mappings()

I don't think I've even used DAPInstall, I was trying out DAP with python but tbh didn't need it and didn't bother setting it up for tools I'm even more comfortable with like gdb, but its like LspInstall for DAP if you are into that kinda thing.

If you want to install and configure them manually, go here: https://github.com/mfussenegger/nvim-dap/wiki/Debug-Adapter-installation

siduck commented 2 years ago

@zbirenbaum Ive never tried DAP, can I add your dap sample config in our docs as an example?

zbirenbaum commented 2 years ago

@siduck Certainly! I will say that dap has a slightly different configuration for each adapter, and python is one of the few with something to preconfigure it, so I changed my config to be a bit more of a general template which should be a bit more helpful. I also included a comment with instructions on creating custom functions to open widgets like dapui does, in case they don't want to use dapui (as I find it somewhat intrusive) and a few more helpful hotkeys. I tested it all on a python file and it was working fine so feel free to post this.

Here's the link to configuration guides: https://github.com/mfussenegger/nvim-dap/wiki/Debug-Adapter-installation Here's my improved configuration:

custom/init.lua

  use {
    'mfussenegger/nvim-dap',
    after = "coq_nvim",
    disable = not plugin_status.dap,
    config = function()
      require "custom.plugins.dap"
    end,
    requires = {
      "Pocco81/DAPInstall.nvim",
      --"mfussenegger/nvim-dap-python", can be used instead of a config file for the Python adapter by requiring it where your config would be.
    },
  }
  use {
    "rcarriga/nvim-dap-ui",
    after = "nvim-dap",
    config = function()
      require("dapui").setup()
    end,
  }

custom/plugins/dap.lua

local adapters = {'python'}  --list your adapters here

for _, adapter in ipairs(adapters) do
  require("custom.plugins.dap_configs." .. adapter)
end

local function dap_mappings()
   vim.api.nvim_set_keymap("n", "<Leader>r", '<Cmd>lua require"dap".repl.toggle()<CR>', {
      silent = true,
      noremap = true,
   })
   vim.api.nvim_set_keymap("n", "<Leader>d", '<Cmd>lua require"dapui".toggle()<CR>', {
      silent = true,
      noremap = true,
   })
   vim.api.nvim_set_keymap("n", "<C-b>", '<Cmd>lua require"dap".toggle_breakpoint()<CR>', {
      silent = true,
      noremap = true,
   })
   vim.api.nvim_set_keymap("n", "<C-c>", '<Cmd>:lua require"dap".continue()<CR>',{
      silent = true,
      noremap = true,
   })
   vim.api.nvim_set_keymap("n", "<C-s>", '<Cmd>lua require"dap".step_over()<CR>', {
      silent = true,
      noremap = true,
   })
   vim.api.nvim_set_keymap("n", "<C-S>", '<Cmd>lua require"dap".step_into()<CR>', {
      silent = true,
      noremap = true,
   })
end

dap_mappings()

--if you do not want to use dapui, specific widget windows can be loaded via lua instead like so
-- vim.api.nvim_set_keymap("n", "<Leader>s", '<Cmd>lua require"custom.plugins.dap_configs.widget_config".load_scope_in_sidebar()<CR>', {
--    silent = true,
--    noremap = true,
-- })
-- where custom.plugins.dap_configs.widget_config contains:
-- M = {}
-- local widgets = require('dap.ui.widgets')
--
-- M.load_scope_in_sidebar = function ()
--   local my_sidebar = widgets.sidebar(widgets.scopes)
--   my_sidebar.toggle()
-- end
--
-- return M

custom/plugins/dap_configs/python.lua

local dap = require('dap')
dap.adapters.python = {
 type = 'executable';
 command = "/home/zach/.virtualenvs/py3nvim/bin/python";
 args = { '-m', 'debugpy.adapter' };
}

dap.configurations.python = {
 {
   -- The first three options are required by nvim-dap
   type = 'python'; -- the type here established the link to the adapter definition: `dap.adapters.python`
   request = 'launch';
   name = "Launch file";
   -- Options below are for debugpy, see https://github.com/microsoft/debugpy/wiki/Debug-configuration-settings for supported options
   program = "${file}"; -- This configuration will launch the current file if used.
   pythonPath = function()
     --The below line will work for virtualenvwrapper, as vim.env.VIRTUAL_ENV points to the active env directory if you use it
     --Test the variable by running :lua print(vim.env.VIRTUAL_ENV) and find your path from there if it is defined
     if vim.env.VIRTUAL_ENV then return vim.env.VIRTUAL_ENV ..'/bin/python' end
     -- debugpy supports launching an application with a different interpreter then the one used to launch debugpy itself.
     -- The code below looks for a `venv` or `.venv` folder in the current directly and uses the python within.
     -- You could adapt this - to for example use the `VIRTUAL_ENV` environment variable (done above).
     local cwd = vim.fn.getcwd()
     if vim.fn.executable(cwd .. '/venv/bin/python') == 1 then
       return cwd .. '/venv/bin/python'
     elseif vim.fn.executable(cwd .. '/.venv/bin/python') == 1 then
       return cwd .. '/.venv/bin/python'
     else
       return '/usr/bin/python'
     end
   end;
 },
}
LYK-love commented 1 year ago

@siduck Certainly! I will say that dap has a slightly different configuration for each adapter, and python is one of the few with something to preconfigure it, so I changed my config to be a bit more of a general template which should be a bit more helpful. I also included a comment with instructions on creating custom functions to open widgets like dapui does, in case they don't want to use dapui (as I find it somewhat intrusive) and a few more helpful hotkeys. I tested it all on a python file and it was working fine so feel free to post this.

Here's the link to configuration guides: https://github.com/mfussenegger/nvim-dap/wiki/Debug-Adapter-installation Here's my improved configuration:

custom/init.lua

  use {
    'mfussenegger/nvim-dap',
    after = "coq_nvim",
    disable = not plugin_status.dap,
    config = function()
      require "custom.plugins.dap"
    end,
    requires = {
      "Pocco81/DAPInstall.nvim",
      --"mfussenegger/nvim-dap-python", can be used instead of a config file for the Python adapter by requiring it where your config would be.
    },
  }
  use {
    "rcarriga/nvim-dap-ui",
    after = "nvim-dap",
    config = function()
      require("dapui").setup()
    end,
  }

custom/plugins/dap.lua

local adapters = {'python'}  --list your adapters here

for _, adapter in ipairs(adapters) do
  require("custom.plugins.dap_configs." .. adapter)
end

local function dap_mappings()
   vim.api.nvim_set_keymap("n", "<Leader>r", '<Cmd>lua require"dap".repl.toggle()<CR>', {
      silent = true,
      noremap = true,
   })
   vim.api.nvim_set_keymap("n", "<Leader>d", '<Cmd>lua require"dapui".toggle()<CR>', {
      silent = true,
      noremap = true,
   })
   vim.api.nvim_set_keymap("n", "<C-b>", '<Cmd>lua require"dap".toggle_breakpoint()<CR>', {
      silent = true,
      noremap = true,
   })
   vim.api.nvim_set_keymap("n", "<C-c>", '<Cmd>:lua require"dap".continue()<CR>',{
      silent = true,
      noremap = true,
   })
   vim.api.nvim_set_keymap("n", "<C-s>", '<Cmd>lua require"dap".step_over()<CR>', {
      silent = true,
      noremap = true,
   })
   vim.api.nvim_set_keymap("n", "<C-S>", '<Cmd>lua require"dap".step_into()<CR>', {
      silent = true,
      noremap = true,
   })
end

dap_mappings()

--if you do not want to use dapui, specific widget windows can be loaded via lua instead like so
-- vim.api.nvim_set_keymap("n", "<Leader>s", '<Cmd>lua require"custom.plugins.dap_configs.widget_config".load_scope_in_sidebar()<CR>', {
--    silent = true,
--    noremap = true,
-- })
-- where custom.plugins.dap_configs.widget_config contains:
-- M = {}
-- local widgets = require('dap.ui.widgets')
--
-- M.load_scope_in_sidebar = function ()
--   local my_sidebar = widgets.sidebar(widgets.scopes)
--   my_sidebar.toggle()
-- end
--
-- return M

custom/plugins/dap_configs/python.lua

local dap = require('dap')
dap.adapters.python = {
 type = 'executable';
 command = "/home/zach/.virtualenvs/py3nvim/bin/python";
 args = { '-m', 'debugpy.adapter' };
}

dap.configurations.python = {
 {
   -- The first three options are required by nvim-dap
   type = 'python'; -- the type here established the link to the adapter definition: `dap.adapters.python`
   request = 'launch';
   name = "Launch file";
   -- Options below are for debugpy, see https://github.com/microsoft/debugpy/wiki/Debug-configuration-settings for supported options
   program = "${file}"; -- This configuration will launch the current file if used.
   pythonPath = function()
     --The below line will work for virtualenvwrapper, as vim.env.VIRTUAL_ENV points to the active env directory if you use it
     --Test the variable by running :lua print(vim.env.VIRTUAL_ENV) and find your path from there if it is defined
     if vim.env.VIRTUAL_ENV then return vim.env.VIRTUAL_ENV ..'/bin/python' end
     -- debugpy supports launching an application with a different interpreter then the one used to launch debugpy itself.
     -- The code below looks for a `venv` or `.venv` folder in the current directly and uses the python within.
     -- You could adapt this - to for example use the `VIRTUAL_ENV` environment variable (done above).
     local cwd = vim.fn.getcwd()
     if vim.fn.executable(cwd .. '/venv/bin/python') == 1 then
       return cwd .. '/venv/bin/python'
     elseif vim.fn.executable(cwd .. '/.venv/bin/python') == 1 then
       return cwd .. '/.venv/bin/python'
     else
       return '/usr/bin/python'
     end
   end;
 },
}

Thanks. But this answer is too old. As latest Nvchad uses Lazy.nvim, instead of Packer.nvim, which is used in your answer. Can you post a new config?