Configure DAP windows for python.

Also add vim bindings for lua_ls.
main
Joshua Potter 2023-11-17 15:02:07 -07:00
parent 0289522665
commit 0dcfc190ea
6 changed files with 151 additions and 10 deletions

View File

@ -1,7 +1,18 @@
{ config, pkgs, lib, ... }:
let
pluginGit = rev: repo: pkgs.vimUtils.buildVimPluginFrom2Nix {
pname = "${lib.strings.sanitizeDerivationName repo}";
version = builtins.substring 0 7 rev;
src = builtins.fetchGit {
url = "https://github.com/${repo}.git";
rev = rev;
};
};
nvim-dap = {
plugin = pkgs.vimPlugins.nvim-dap;
plugin = pluginGit
"e154fdb6d70b3765d71f296e718b29d8b7026a63"
"mfussenegger/nvim-dap";
config = config.programs.neovim.nvim-dap;
};
@ -66,10 +77,11 @@ in
config = {
programs.neovim = {
plugins = map (p: {
plugins = map (p:
if builtins.hasAttr "config" p then {
inherit (p) plugin;
config = "lua << EOF\n${p.config}\nEOF";
}) [
} else p) [
nvim-dap
nvim-lspconfig
nvim-treesitter
@ -95,6 +107,8 @@ in
'')
# Extra Lua configuration to be appended to `init.lua`.
(lib.mkAfter ''
vim.g.mapleader = ' '
vim.g.maplocalleader = '\\'
vim.o.colorcolumn = '80,100'
vim.o.expandtab = true -- Spaces instead of tabs.
vim.o.shiftwidth = 2 -- # of spaces to use for each (auto)indent.

View File

@ -2,7 +2,7 @@
{
programs.neovim = {
nvim-lspconfig = ''
require('lspconfig').lua_ls.setup { }
require('init.lua').nvim_lspconfig()
'';
extraPackages = [ pkgs.lua-language-server ];

View File

@ -23,4 +23,8 @@ in
extraPackages = [ venv ];
};
xdg.configFile."nvim/after/ftplugin/python.lua".text = ''
require('init.dap').buffer_map()
'';
}

View File

@ -0,0 +1,85 @@
local M = {}
local function sidebar_new(widget)
return require('dap.ui.widgets').sidebar(widget, { width = 40 }, '')
end
local function sidebar_is_open(sidebar)
return sidebar.win and vim.api.nvim_win_is_valid(sidebar.win)
end
function M.buffer_map()
local function set_nnoremap(key, func)
vim.keymap.set(
'n',
string.format('<localleader>%s', key),
func,
{ buffer = true }
)
end
local dap = require('dap')
local dap_ui_widgets = require('dap.ui.widgets')
local sidebars = {
expression = sidebar_new(dap_ui_widgets.expression),
frames = sidebar_new(dap_ui_widgets.frames),
scopes = sidebar_new(dap_ui_widgets.scopes),
sessions = sidebar_new(dap_ui_widgets.sessions),
threads = sidebar_new(dap_ui_widgets.threads),
}
local function any_sidebar_open()
for _, sb in pairs(sidebars) do
if sidebar_is_open(sb) then
return true
end
end
return false
end
local function toggle_sidebar(sidebar)
if sidebar_is_open(sidebar) then
sidebar.close({ mode = 'toggle' })
else
local win_id = vim.fn.win_getid()
vim.cmd.wincmd('t') -- Move to topleft-most window.
vim.cmd(any_sidebar_open() and 'leftabove split' or 'vertical topleft split')
sidebar.open()
vim.fn.win_gotoid(win_id)
end
end
set_nnoremap('<localleader>', dap.continue)
set_nnoremap('b', dap.toggle_breakpoint)
set_nnoremap('n', dap.step_over)
set_nnoremap('x', dap.clear_breakpoints)
set_nnoremap('we', function()
toggle_sidebar(sidebars.expression)
end)
set_nnoremap('wf', function()
toggle_sidebar(sidebars.frames)
end)
set_nnoremap('wc', function()
toggle_sidebar(sidebars.scopes)
end)
set_nnoremap('wr', function()
dap.repl.toggle({ height = 10 })
end)
set_nnoremap('ws', function()
toggle_sidebar(sidebars.sessions)
end)
set_nnoremap('wt', function()
toggle_sidebar(sidebars.threads)
end)
set_nnoremap('wx', function()
for _, sb in pairs(sidebars) do
if sidebar_is_open(sb) then
toggle_sidebar(sb)
end
dap.repl.close()
end
end)
end
return M

View File

@ -0,0 +1,38 @@
local M = {}
function M.nvim_lspconfig()
require('lspconfig').lua_ls.setup {
-- Provide completions, analysis, and location handling for plugins on the
-- vim runtime path.
-- https://github.com/neovim/nvim-lspconfig/blob/48347089666d5b77d054088aa72e4e0b58026e6e/doc/server_configurations.md#lua_ls
on_init = function(client)
local path = client.workspace_folders[1].name
if (
not vim.loop.fs_stat(path .. '/.luarc.json') and
not vim.loop.fs_stat(path .. '/.luarc.jsonc')
) then
client.config.settings = vim.tbl_deep_extend(
'force', client.config.settings, {
Lua = {
runtime = {
version = 'LuaJIT'
},
workspace = {
checkThirdParty = false,
library = {
vim.env.VIMRUNTIME
}
}
}
})
client.notify("workspace/didChangeConfiguration", {
settings = client.config.settings,
})
end
return true
end
}
end
return M

View File

@ -1,6 +1,6 @@
local module = {}
local M = {}
function module.nvim_dap(options)
function M.nvim_dap(options)
local dap = require('dap')
dap.adapters.python = function(callback, config)
@ -25,7 +25,7 @@ function module.nvim_dap(options)
})
end
function module.nvim_lspconfig()
function M.nvim_lspconfig()
require('lspconfig').pylsp.setup {
settings = {
pylsp = {
@ -46,4 +46,4 @@ function module.nvim_lspconfig()
}
end
return module
return M