From 0dcfc190ea3be570e30a595c1b60a837f926d90f Mon Sep 17 00:00:00 2001 From: Joshua Potter Date: Fri, 17 Nov 2023 15:02:07 -0700 Subject: [PATCH] Configure DAP windows for python. Also add vim bindings for lua_ls. --- jrpotter/neovim/default.nix | 24 ++++++-- jrpotter/neovim/lang/lua.nix | 2 +- jrpotter/neovim/lang/python.nix | 4 ++ jrpotter/neovim/lua/init/dap.lua | 85 +++++++++++++++++++++++++++++ jrpotter/neovim/lua/init/lua.lua | 38 +++++++++++++ jrpotter/neovim/lua/init/python.lua | 8 +-- 6 files changed, 151 insertions(+), 10 deletions(-) create mode 100644 jrpotter/neovim/lua/init/dap.lua create mode 100644 jrpotter/neovim/lua/init/lua.lua diff --git a/jrpotter/neovim/default.nix b/jrpotter/neovim/default.nix index a630e9d..55a43b8 100644 --- a/jrpotter/neovim/default.nix +++ b/jrpotter/neovim/default.nix @@ -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: { - inherit (p) plugin; - config = "lua << EOF\n${p.config}\nEOF"; - }) [ + 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. diff --git a/jrpotter/neovim/lang/lua.nix b/jrpotter/neovim/lang/lua.nix index 1b541da..3dcbff5 100644 --- a/jrpotter/neovim/lang/lua.nix +++ b/jrpotter/neovim/lang/lua.nix @@ -2,7 +2,7 @@ { programs.neovim = { nvim-lspconfig = '' - require('lspconfig').lua_ls.setup { } + require('init.lua').nvim_lspconfig() ''; extraPackages = [ pkgs.lua-language-server ]; diff --git a/jrpotter/neovim/lang/python.nix b/jrpotter/neovim/lang/python.nix index 23279fe..dd5aa24 100644 --- a/jrpotter/neovim/lang/python.nix +++ b/jrpotter/neovim/lang/python.nix @@ -23,4 +23,8 @@ in extraPackages = [ venv ]; }; + + xdg.configFile."nvim/after/ftplugin/python.lua".text = '' + require('init.dap').buffer_map() + ''; } diff --git a/jrpotter/neovim/lua/init/dap.lua b/jrpotter/neovim/lua/init/dap.lua new file mode 100644 index 0000000..8178b4b --- /dev/null +++ b/jrpotter/neovim/lua/init/dap.lua @@ -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('%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('', 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 diff --git a/jrpotter/neovim/lua/init/lua.lua b/jrpotter/neovim/lua/init/lua.lua new file mode 100644 index 0000000..d69fe34 --- /dev/null +++ b/jrpotter/neovim/lua/init/lua.lua @@ -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 diff --git a/jrpotter/neovim/lua/init/python.lua b/jrpotter/neovim/lua/init/python.lua index 0b327b8..15b7339 100644 --- a/jrpotter/neovim/lua/init/python.lua +++ b/jrpotter/neovim/lua/init/python.lua @@ -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