avataralpha2phi

Summary

This context provides a guide on enhancing the debugging experience using DAP in Neovim for Python, Golang, Rust, Node.js, Javascript/Typescript, and Lua.

Abstract

The context begins with an overview of the guide and references to previous articles on configuring LSP and DAP for Neovim using both nvim-dap and vimspector. The guide focuses on enhancing the debugging experience using nvim-dap for Python, Node.js/Javascript/Typescript, Golang, Rust, and Lua. The guide provides step-by-step instructions on installing and configuring plugins, setting up key bindings, and using virtual text and UI for debugging. It also introduces the use of telescope-dap for viewing nvim-dap commands and variables, and vim-ultest for debugging test cases. The guide concludes with instructions on installing DAPInstall.nvim for managing various debuggers and configuring other languages such as Node.js, Golang, Rust, and Lua.

Bullet points

  • The guide provides instructions on enhancing the debugging experience using nvim-dap for Python, Node.js/Javascript/Typescript, Golang, Rust, and Lua.
  • The guide provides step-by-step instructions on installing and configuring plugins, setting up key bindings, and using virtual text and UI for debugging.
  • The guide introduces the use of telescope-dap for viewing nvim-dap commands and variables, and vim-ultest for debugging test cases.
  • The guide concludes with instructions on installing DAPInstall.nvim for managing various debuggers and configuring other languages such as Node.js, Golang, Rust, and Lua.

Neovim DAP Enhanced

Enhance your debugging experience using DAP in Neovim for Python, Golang, Rust, Node.js, Javascript/Typescript, and Lua.

Neovim DAP Enhanced

Overview

In my previous articles, I walked through with you how to configure LSP and DAP for Neovim using both nvim-dap and vimspector. In this article, let’s continue to enhance the debugging experience using nvim-dap. I am going to focus on Python, Node.js/Javascript/Typescript, Golang, Rust, and Lua.

Getting Started

Let’s get started with Python before we delve into other languages.

Install Plugins

Using your favorite plugin managers, install the following plugins

I am going to install them using packer.nvim.

use {'mfussenegger/nvim-dap'}
use {'nvim-telescope/telescope-dap.nvim'}
use {'mfussenegger/nvim-dap-python'}

Run :luafile % and :PackerInstall to install them.

Configuration

Create the following files

  • lua/dbg/init.lua
  • lua/dbg/python.lua

In init.lua, add the following lines to configure the plugins and the key bindings. Change the key mappings based on your preferences.

In python.lua, add the following lines. Change the Python path accordingly.

require('dap-python').setup('~/miniconda3/bin/python')

In the main init.lua file, add the following line to load the debug module.

require('dbg')

Debugging

  • Open a project and press <Leader>dtb to toggle a breakpoint.
  • Press <Leader>dct to start debugging.
  • The application should stop at the breakpoint. Now press <Leader>dro to open the REPL.
nvim-dap: Debugging

By default nvim-dap provides widgets and UIs to display the variables. E.g. press <Leader>duf to display a floating window with details of the variables.

nvim-dap: Widget

Enhance DAP Experience

Let’s continue to enhance the debugging experience.

telescope-dap

I already installed telescope-dap. Using this plugin I can look at the debugging commands, list the variables, configurations, and breakpoints.

E.g., type :Telescope dap commands to view nvim-dap commands.

telescope-dap: List Commands

nvim-dap-virtual-text

Let’s install nvim-dap-virtual-text to add virtual text support.

use {'theHamsta/nvim-dap-virtual-text'}

Run :luafile % and :PackerInstall to install it.

In lua/dbg/init.lua, add the following lines.

-- nvim-dap-virtual-text. Show virtual text for current frame
vim.g.dap_virtual_text = true

Start the debugging session again and now you should see the values of the variables in the virtual text.

nvim-dap-virtual-text

nvim-dap-ui

nvim-dap-ui provides a user interface for nvim-dap.

use {'rcarriga/nvim-dap-ui'}

Run :luafile % and :PackerInstallto install it.

In lua/dbg/init.lua, add the following lines.

-- nvim-dap-ui
require("dapui").setup()

Start the debugging session again and now you should see the debugging UIs for watches, breakpoints, and stacks.

You can also add a key mapping to toggle the UI.

-- nvim-dap-ui
utils.map('n', '<leader>dui', '<cmd>lua require"dapui".toggle()<CR>')
nvim-dap-ui

vim-ultest

vim-ultestbuilds upon vim-test to make it even better while maintaining the ability to use your existing configuration. It also supports debugging using nvim-dap.

use {
        "rcarriga/vim-ultest",
        config = "require('config.ultest').post()",
        run = ":UpdateRemotePlugins",
        requires = {"vim-test/vim-test"}
}

Create a file call ultest.lua underlua/config with the following content.

Run :luafile % and :PackerInstall to install the plugin.

Now open a file with test cases, and run :UltestDebug.

vim-ultest: Debugging
vim-ultest: Test Summary

DAPInstall

Before continuing to explore other languages, let’s install DAPInstall which is a NeoVim plugin written in Lua that extends nvim-dap’s functionality for managing various debuggers.

use {'Pocco81/DAPInstall.nvim'}

Run :luafile % and :PackerInstall to install this plugin.

Refer to the documentation for a list of supported debuggers.

Configure Other Languages

Node.js

  • Run :DIInstall jsnode_dbg to install the debugger.
  • Create a new file lua/dbg/node.lua with the following content.
  • In lua/dbg/init.lua, import the module.
require('dbg.node')
  • Restart Neovim and now you can debug Node.js application.
nvim-dap: Debugging Node.js

Golang

  • Run :DIInstall go_delve_dbg to install the debugger.
  • Create a new file lua/dbg/go.lua with the following content.
  • In lua/dbg/init.lua, import the module.
require('dbg.go')
  • Restart Neovim and now you can debug Golang application.
nvim-dap: Debugging Golang

Rust

  • Run :DIInstall ccppr_lldb_dbg to install the debugger.
  • Create a new file lua/dbg/rust.lua with the following content.
  • In lua/dbg/init.lua, import the module.
require('dbg.rust')
  • Restart Neovim and now you can debug Rust application (Note: You will be asked to input the Rust executable path).
nvim-dap: Debugging Rust

Lua

  • In lua/dbg/init.lua, import the module.
require('dbg.lua')
  • Restart Neovim.
  • Open a Lua file, set a breakpoint, and run :lua require”osv”.run_this() (type :h osv for more details).
nvim-dap: Debugging Lua

Summary

There are many more features that you can explore but this article should get you started.

You can find the code I showed in this repository.

If you are not a Medium member yet and want to become one, click here. (A part of your subscription fee will be used to support alpha2phi.)

Vim
Neovim
Programming
Hacking
Coding
Recommended from ReadMedium