From 3994ae2237fb36a8146c58d23d09ce7c46e146bc Mon Sep 17 00:00:00 2001 From: mathijs-bakker Date: Thu, 21 Aug 2025 10:21:27 +0200 Subject: [PATCH] Dap support --- lua/godotdev/dap.lua | 37 +++++++++++++++++++++++++++++++++++++ lua/godotdev/init.lua | 1 - lua/godotdev/keymaps.lua | 24 +++++++++++++++++++++++- lua/godotdev/lsp.lua | 2 +- lua/godotdev/setup.lua | 21 +++++++++++++++++++-- lua/godotdev/utils.lua | 32 ++++++++++++++++---------------- 6 files changed, 96 insertions(+), 21 deletions(-) create mode 100644 lua/godotdev/dap.lua diff --git a/lua/godotdev/dap.lua b/lua/godotdev/dap.lua new file mode 100644 index 0000000..86e403d --- /dev/null +++ b/lua/godotdev/dap.lua @@ -0,0 +1,37 @@ +local M = {} + +function M.setup(config) + local dap = require("dap") + local dapui = require("dapui") + require("godotdev.keymaps").attach_dap() + + dap.adapters.godot = { + type = "server", + host = config.editor_host or "127.0.0.1", + port = config.debug_port or 6006, + } + + dap.configurations.gdscript = { + { + type = "godot", + request = "launch", + name = "Launch scene", + project = "${workspaceFolder}", + launch_scene = true, + }, + } + + dapui.setup() + + dap.listeners.after.event_initialized["dapui_config"] = function() + dapui.open() + end + dap.listeners.before.event_terminated["dapui_config"] = function() + dapui.close() + end + dap.listeners.before.event_exited["dapui_config"] = function() + dapui.close() + end +end + +return M diff --git a/lua/godotdev/init.lua b/lua/godotdev/init.lua index 33a37d7..399d96b 100644 --- a/lua/godotdev/init.lua +++ b/lua/godotdev/init.lua @@ -1,2 +1 @@ --- lua/godotdev/init.lua return require("godotdev.setup") diff --git a/lua/godotdev/keymaps.lua b/lua/godotdev/keymaps.lua index 04694b5..c090c9c 100644 --- a/lua/godotdev/keymaps.lua +++ b/lua/godotdev/keymaps.lua @@ -1,6 +1,6 @@ local M = {} -function M.attach(bufnr) +function M.attach_lsp(bufnr) local opts = { buffer = bufnr } local has_telescope, telescope_builtin = pcall(require, "telescope.builtin") @@ -57,4 +57,26 @@ function M.attach(bufnr) map("n", "]d", vim.diagnostic.goto_next, "LSP: Next diagnostic") end +function M.attach_dap() + local dap = require("dap") + local dapui = require("dapui") + + local function map(mode, lhs, rhs, desc) + vim.keymap.set(mode, lhs, rhs, { desc = desc }) + end + + -- Basic debugger controls + map("n", "", dap.continue, "DAP: Continue/Start") + map("n", "", dap.step_over, "DAP: Step over") + map("n", "", dap.step_into, "DAP: Step into") + map("n", "", dap.step_out, "DAP: Step out") + map("n", "db", dap.toggle_breakpoint, "DAP: Toggle breakpoint") + map("n", "dB", function() + dap.set_breakpoint(vim.fn.input("Breakpoint condition: ")) + end, "DAP: Conditional breakpoint") + + -- UI + map("n", "du", dapui.toggle, "DAP: Toggle UI") + map("n", "dr", dap.repl.open, "DAP: Open REPL") +end return M diff --git a/lua/godotdev/lsp.lua b/lua/godotdev/lsp.lua index a578a2e..6e29849 100644 --- a/lua/godotdev/lsp.lua +++ b/lua/godotdev/lsp.lua @@ -29,7 +29,7 @@ M.setup = function(config) capabilities = capabilities, on_attach = function(client, bufnr) utils.suppress_unsupported_lsp_messages(client, { "Method not found: godot/reloadScript" }) - keymaps.attach(bufnr) + keymaps.attach_lsp(bufnr) end, }) end diff --git a/lua/godotdev/setup.lua b/lua/godotdev/setup.lua index 83138c9..8932830 100644 --- a/lua/godotdev/setup.lua +++ b/lua/godotdev/setup.lua @@ -3,12 +3,29 @@ local M = {} M.opts = { editor_host = "127.0.0.1", editor_port = 6005, + debug_port = 6006, } function M.setup(opts) M.opts = vim.tbl_extend("force", M.opts, opts or {}) - require("godotdev.lsp").setup({ editor_host = M.opts.editor_host, editor_port = M.opts.editor_port }) - require("godotdev.health").setup({ port = M.opts.editor_port }) + + require("godotdev.lsp").setup({ + editor_host = M.opts.editor_host, + editor_port = M.opts.editor_port, + }) + + require("godotdev.health").setup({ + port = M.opts.editor_port, + }) + + local dap_ok, dap = pcall(require, "dap") + if dap_ok then + dap.adapters.godot = { + type = "server", + host = M.opts.editor_host, + port = M.opts.debug_port, + } + end end return M diff --git a/lua/godotdev/utils.lua b/lua/godotdev/utils.lua index 2fd1abe..55f4573 100644 --- a/lua/godotdev/utils.lua +++ b/lua/godotdev/utils.lua @@ -3,22 +3,22 @@ local M = {} --- Suppress specific LSP messages -- @param client vim.lsp.Client -- @param patterns table List of string patterns to ignore in client messages -function M.suppress_lsp_messages(client, patterns) - local orig_handler = vim.lsp.handlers["window/showMessage"] - vim.lsp.handlers["window/showMessage"] = function(err, method, params, client_id) - local lsp_client = vim.lsp.get_client_by_id(client_id) - if lsp_client and lsp_client.name == client.name then - if params and params.message then - for _, pat in ipairs(patterns) do - if params.message:match(pat) then - return -- silently ignore this message - end - end - end - end - -- fallback to original handler for all other messages - orig_handler(err, method, params, client_id) - end +function M.suppress_unsupported_lsp_messages(client, patterns) + local orig_handler = vim.lsp.handlers["window/showMessage"] + vim.lsp.handlers["window/showMessage"] = function(err, method, params, client_id) + local lsp_client = vim.lsp.get_client_by_id(client_id) + if lsp_client and lsp_client.name == client.name then + if params and params.message then + for _, pat in ipairs(patterns) do + if params.message:match(pat) then + return -- silently ignore this message + end + end + end + end + -- fallback to original handler for all other messages + orig_handler(err, method, params, client_id) + end end return M