From 5a8f49e2ca5cc6ee4e901e66270df9d9d9d63abb Mon Sep 17 00:00:00 2001 From: Amanda Graven Date: Sun, 12 Nov 2023 09:52:29 +0100 Subject: [PATCH 1/2] mason_path, use js dap with mason --- init.lua | 132 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 68 insertions(+), 64 deletions(-) diff --git a/init.lua b/init.lua index b3285fd..b46fd3d 100644 --- a/init.lua +++ b/init.lua @@ -236,6 +236,10 @@ if fn.empty(fn.glob(packer_path)) > 0 then vim.notify('Downloading plugin manager with git') packer_bootstrap = fn.system({'git', 'clone', '--depth', '1', 'https://github.com/wbthomason/packer.nvim', packer_path}) end +--- @param package string +function mason_path(package) + return vim.fn.stdpath('data') .. '/mason/packages/' .. package +end require'packer'.startup(function(use) use 'wbthomason/packer.nvim' @@ -356,15 +360,30 @@ require'packer'.startup(function(use) vim.fn.sign_define('DapBreakpoint', {text='⯃', texthl='DiagnosticError'}) dap.adapters.codelldb = { type = 'server', + host = 'localhost', port = '${port}', executable = { - command = vim.fn.stdpath('data') .. '/mason/packages/codelldb/extension/adapter/codelldb', + --command = mason_path('codelldb') .. '/extension/adapter/codelldb', + --args = {'--liblldb', mason_path('codelldb') .. '/extension/lldb/lib/liblldb.so', '--port', '${port}'}, + command = 'lldb', args = {'--port', '${port}'}, }, } dap.adapters.sh = { type = 'executable', - command = vim.fn.stdpath('data') .. '/mason/packages/bash-debug-adapter/bash-debug-adapter' + command = mason_path('bash-debug-adapter') .. '/bash-debug-adapter' + } + dap.adapters['pwa-node'] = { + type = 'server', + host = 'localhost', + port = '${port}', + executable = { + command = 'node', + args = { + mason_path('js-debug-adapter') .. '/js-debug/src/dapDebugServer.js', + '${port}' + }, + }, } dap.configurations.cpp = { { @@ -387,8 +406,8 @@ require'packer'.startup(function(use) request = 'launch', program = '${file}', cwd = '${fileDirname}', - pathBashdb = vim.fn.stdpath('data') .. '/mason/packages/bash-debug-adapter/extension/bashdb_dir/bashdb', - pathBashdbLib = vim.fn.stdpath('data') .. '/mason/packages/bash-debug-adapter/extension/bashdb_dir', + pathBashdb = mason_path('bash-debug-adapter') .. '/extension/bashdb_dir/bashdb', + pathBashdbLib = mason_path('bash-debug-adapter') .. '/extension/bashdb_dir', pathBash = 'bash', pathCat = 'cat', pathMkfifo = 'mkfifo', @@ -399,6 +418,47 @@ require'packer'.startup(function(use) end, }, } + dap.configurations.javascript = { + { + type = 'pwa-node', + request = 'launch', + name = 'Launch current file', + program = '${file}', + cwd = '${workspaceFolder}', + }, + { + type = 'pwa-node', + request = 'launch', + name = 'Launch file', + program = function() + return vim.fn.input('Path to file: ', vim.fn.getcwd() .. '/', 'file') + end, + cwd = '${workspaceFolder}', + }, + { + type = 'pwa-node', + request = 'attach', + name = 'Attach', + processId = require'dap.utils'.pick_process, + cwd = '${workspaceFolder}', + }, + { + type = 'pwa-node', + request = 'launch', + name = 'Debug Mocha Tests', + -- trace = true, -- include debugger info + runtimeExecutable = 'node', + runtimeArgs = { + './node_modules/mocha/bin/mocha', + }, + rootPath = '${workspaceFolder}', + cwd = '${workspaceFolder}', + console = 'integratedTerminal', + internalConsoleOptions = 'neverOpen', + } + } + dap.configurations.typescript = dap.configurations.javascript + local function conditional_breakpoint() vim.ui.input({prompt = 'Breakpoint condition'}, function(condition) if not condition then return end @@ -444,57 +504,6 @@ require'packer'.startup(function(use) clear_on_continue = true, } end} - use {'mxsdev/nvim-dap-vscode-js', requires = {'mfussenegger/nvim-dap'}, config = function() - require'dap-vscode-js'.setup { - adapters = {'pwa-node', 'node-terminal'}, - } - for _, language in ipairs({'javascript', 'typescript'}) do - require'dap'.configurations[language] = { - { - type = 'pwa-node', - request = 'launch', - name = 'Launch current file', - program = '${file}', - cwd = '${workspaceFolder}', - }, - { - type = 'pwa-node', - request = 'launch', - name = 'Launch file', - program = function() - return vim.fn.input('Path to file: ', vim.fn.getcwd() .. '/', 'file') - end, - cwd = '${workspaceFolder}', - }, - { - type = 'pwa-node', - request = 'attach', - name = 'Attach', - processId = require'dap.utils'.pick_process, - cwd = '${workspaceFolder}', - }, - { - type = 'pwa-node', - request = 'launch', - name = 'Debug Mocha Tests', - -- trace = true, -- include debugger info - runtimeExecutable = 'node', - runtimeArgs = { - './node_modules/mocha/bin/mocha', - }, - rootPath = '${workspaceFolder}', - cwd = '${workspaceFolder}', - console = 'integratedTerminal', - internalConsoleOptions = 'neverOpen', - } - } - end - end} - use { - 'microsoft/vscode-js-debug', - opt = true, - run = 'npm install --legacy-peer-deps && npm run compile' - } use {'sindrets/diffview.nvim', config = function() require'diffview'.setup { -- Use nicer highlighting for diffs @@ -918,7 +927,6 @@ capabilities.textDocument.foldingRange = { --- @param bufnr number --- @diagnostic disable-next-line: unused-local local function on_attach(client, bufnr) - require'which-key'.register({ ['l'] = { D = {vim.lsp.buf.declaration, 'Declaration'}, @@ -1005,7 +1013,6 @@ au('FileType', { jdtls.dap = require'jdtls.dap' _G.jdt = jdtls - local path = vim.fn.stdpath('data') .. '/mason/packages/jdtls' local root_dir = jdtls.setup.find_root({'.git', 'mvnw', 'gradlew'}) local config = { @@ -1016,7 +1023,7 @@ au('FileType', { '--jvm-arg=-Dlog.level=ALL', '--jvm-arg=-Dlog.protocol=true', -- Enable lombok - '--jvm-arg=-javaagent:' .. path .. '/lombok.jar', + '--jvm-arg=-javaagent:' .. mason_path('jdtls') .. '/lombok.jar', -- store workpace data in ~/.local/share/eclipse/ '-data', vim.fn.expand('~/.local/share/eclipse/') .. vim.fn.fnamemodify(root_dir, ':p:h:t') }, @@ -1032,11 +1039,11 @@ au('FileType', { bundles = (function () -- add java-debug-adapter local bundles = { - vim.fn.glob(vim.fn.stdpath('data') .. '/mason/packages/java-debug-adapter/extension/server/com.microsoft.java.debug.plugin-*.jar', true) + vim.fn.glob(mason_path'java-debug-adapter' .. '/extension/server/com.microsoft.java.debug.plugin-*.jar', true) } -- add java-test vim.list_extend(bundles, vim.split( - vim.fn.glob(vim.fn.stdpath'data' .. '/mason/packages/java-test/extension/server/*.jar', true), + vim.fn.glob(mason_path'java-test' .. '/extension/server/*.jar', true), '\n' )) @@ -1151,10 +1158,7 @@ require'rust-tools'.setup { }, }, dap = { - adapter = require'rust-tools.dap'.get_codelldb_adapter( - vim.fn.stdpath('data') .. '/mason/packages/codelldb/extension/adapter/codelldb', - vim.fn.stdpath('data') .. '/mason/packages/codelldb/extension/lldb/lib/liblldb.so' - ) + adapter = require'dap'.adapters.codelldb, }, } From 542b72024f62513017a16e61334d02aa5888ff64 Mon Sep 17 00:00:00 2001 From: Amanda Graven Date: Sun, 12 Nov 2023 09:54:58 +0100 Subject: [PATCH 2/2] LspAttach autocmd instead of on_attach fn --- init.lua | 117 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 63 insertions(+), 54 deletions(-) diff --git a/init.lua b/init.lua index b46fd3d..b6b7377 100644 --- a/init.lua +++ b/init.lua @@ -351,6 +351,12 @@ require'packer'.startup(function(use) use 'vim-airline/vim-airline' use 'vim-airline/vim-airline-themes' use 'romgrk/barbar.nvim' + use {'luckasRanarison/clear-action.nvim', config = function() + require'clear-action'.setup { + signs = { enable = false }, + popup = { enable = true }, + } + end} use {'MattesGroeger/vim-bookmarks', config = function() vim.g.bookmark_no_default_key_mappings = 1 vim.g.bookmark_save_per_working_dir = 1 @@ -923,66 +929,73 @@ capabilities.textDocument.foldingRange = { } ---- lsp: language servers ---- ---- Performs keymaps and other setup specific to buffers with LSP enabled ---- @param bufnr number ---- @diagnostic disable-next-line: unused-local -local function on_attach(client, bufnr) - require'which-key'.register({ - ['l'] = { - D = {vim.lsp.buf.declaration, 'Declaration'}, - d = {'Telescope lsp_definitions', 'Definition'}, - i = {'Telescope lsp_implementations', 'Implementation'}, - a = {vim.lsp.buf.code_action, 'Code action'}, - r = {vim.lsp.buf.rename, 'Rename'}, - h = {function() vim.lsp.inlay_hint(0, nil) end, 'Inlay hints'}, - l = {vim.lsp.codelens.refresh, 'Show codelenses'}, - L = {vim.lsp.codelens.run, 'Run codelens'}, - t = {'Telescope lsp_type_definitions', 'Type definition'}, - u = {'Telescope lsp_references', 'Usages/references'} - }, - g = { - d = {vim.lsp.buf.definition, 'Goto definition'}, - D = {vim.lsp.buf.implementation, 'Goto implementation'}, - a = {vim.lsp.buf.code_action, 'Code action'}, - R = {vim.lsp.buf.rename, 'Rename'}, - y = {vim.lsp.buf.type_definition, 'Type definition'}, - }, - [']'] = { - g = {vim.diagnostic.goto_next, 'Next diagnostic'}, - e = {function() vim.diagnostic.goto_next { severity = vim.diagnostic.severity.ERROR } end, 'Next error'}, - w = {function() vim.diagnostic.goto_next { severity = vim.diagnostic.severity.WARN } end, 'Next warning'}, - q = {vim.diagnostic.setqflist, 'Quickfix diagnostics'}, - }, - ['['] = { - g = {vim.diagnostic.goto_prev, 'Previous diagnostic'}, - e = {function() vim.diagnostic.goto_prev { severity = vim.diagnostic.severity.ERROR } end, 'Previous error'}, - w = {function() vim.diagnostic.goto_prev { severity = vim.diagnostic.severity.WARN } end, 'Previous warning'}, - }, - [''] = {vim.lsp.buf.document_highlight, 'Highlight object under cursor'}, - }, {buffer = bufnr}) - -- Hover - require'which-key'.register({ - ['K'] = {vim.lsp.buf.hover, 'Documentation'}, - [''] = {vim.lsp.buf.signature_help, 'Function signature'}, - }, {buffer = bufnr}) - require'which-key'.register({ - [''] = {vim.lsp.buf.signature_help, 'Function signature'} - }, {buffer = bufnr, mode = 'i'}) +au('LspAttach', { + callback = function (event) + local bufnr = event.buf + local client = vim.lsp.get_client_by_id(event.data.client_id) + local function code_action() + local ok, clear_action = pcall(require, 'clear-action') + if ok then + clear_action.code_action() + else + vim.lsp.buf.code_action() + end + end + require'which-key'.register({ + ['l'] = { + D = {vim.lsp.buf.declaration, 'Declaration'}, + d = {'Telescope lsp_definitions', 'Definition'}, + i = {'Telescope lsp_implementations', 'Implementation'}, + a = {code_action, 'Code action'}, + r = {vim.lsp.buf.rename, 'Rename'}, + h = {function() vim.lsp.inlay_hint(0, nil) end, 'Inlay hints'}, + l = {vim.lsp.codelens.refresh, 'Show codelenses'}, + L = {vim.lsp.codelens.run, 'Run codelens'}, + t = {'Telescope lsp_type_definitions', 'Type definition'}, + u = {'Telescope lsp_references', 'Usages/references'} + }, + g = { + d = {vim.lsp.buf.definition, 'Goto definition'}, + D = {vim.lsp.buf.implementation, 'Goto implementation'}, + a = {code_action, 'Code action'}, + R = {vim.lsp.buf.rename, 'Rename'}, + y = {vim.lsp.buf.type_definition, 'Type definition'}, + }, + [']'] = { + g = {vim.diagnostic.goto_next, 'Next diagnostic'}, + e = {function() vim.diagnostic.goto_next { severity = vim.diagnostic.severity.ERROR } end, 'Next error'}, + w = {function() vim.diagnostic.goto_next { severity = vim.diagnostic.severity.WARN } end, 'Next warning'}, + q = {vim.diagnostic.setqflist, 'Quickfix diagnostics'}, + }, + ['['] = { + g = {vim.diagnostic.goto_prev, 'Previous diagnostic'}, + e = {function() vim.diagnostic.goto_prev { severity = vim.diagnostic.severity.ERROR } end, 'Previous error'}, + w = {function() vim.diagnostic.goto_prev { severity = vim.diagnostic.severity.WARN } end, 'Previous warning'}, + }, + [''] = {vim.lsp.buf.document_highlight, 'Highlight object under cursor'}, + }, {buffer = bufnr}) + -- Hover + require'which-key'.register({ + ['K'] = {vim.lsp.buf.hover, 'Documentation'}, + [''] = {vim.lsp.buf.signature_help, 'Function signature'}, + }, {buffer = bufnr}) + require'which-key'.register({ + [''] = {vim.lsp.buf.signature_help, 'Function signature'} + }, {buffer = bufnr, mode = 'i'}) - vim.bo.tagfunc = 'v:lua.vim.lsp.tagfunc' + vim.bo.tagfunc = 'v:lua.vim.lsp.tagfunc' - require'nvim-navic'.attach(client, bufnr) -end + require'nvim-navic'.attach(client, bufnr) + end +}) -- Enable language servers local default = { capabilities = capabilities, - on_attach = on_attach, } require'lspconfig'.lua_ls.setup { capabilities = capabilities, - on_attach = on_attach, settings = { Lua = { runtime = { @@ -1054,7 +1067,6 @@ au('FileType', { capabilities = capabilities, on_attach = function (client, bufnr) - on_attach(client, bufnr) jdtls.setup_dap() end, } @@ -1071,7 +1083,6 @@ au('FileType', { require'lspconfig'.sqls.setup { capabilities = capabilities, on_attach = function (client, bufnr) - on_attach(client, bufnr) local ok, sqls = pcall(require, 'sqls') if not ok then return end sqls.on_attach(client, bufnr) @@ -1123,7 +1134,6 @@ require'rust-tools'.setup { -- Enable standalone file (i.e. no cargo) support standalone = true, capabilities = capabilities, - on_attach = on_attach, settings = { ['rust-analyzer'] = { cargo = { @@ -1267,7 +1277,6 @@ vim.keymap.set('n', 't', 'NvimTreeFindFile', {desc = 'Nvim Tree ---- treesitter ---- ----@diagnostic disable-next-line: missing-fields require'nvim-treesitter.configs'.setup { -- Configure the nvim-ts-autotag plugin autotag = {