Update/Refactor

This commit is contained in:
mathijs-bakker
2025-08-20 17:19:02 +02:00
parent 70fec979b3
commit 64a2d99773
6 changed files with 45 additions and 212 deletions

View File

@@ -1,33 +1,53 @@
local M = {} local M = {}
local uv = vim.loop
function M.check() M.setup = function(config)
vim.health.start("Godotdev.nvim") local lspconfig = require("lspconfig")
local utils = require("godotdev.utils")
local keymaps = require("godotdev.keymaps")
-- ncat local capabilities = vim.lsp.protocol.make_client_capabilities()
if vim.fn.executable("ncat") == 1 then capabilities.textDocument.typeDefinition = nil
vim.health.ok("'ncat' is installed")
else
vim.health.error("'ncat' is missing! Required to attach to Godot editor LSP")
end
-- godot-lsp lspconfig.godot_lsp = {
if vim.fn.executable("godot-lsp") == 1 then default_config = {
vim.health.ok("'godot-lsp' executable found") name = "godot_editor",
else root_dir = lspconfig.util.root_pattern("project.godot"),
vim.health.warn("'godot-lsp' not found, standalone LSP mode will not work") filetypes = { "gd", "gdscript" },
end cmd = nil,
on_attach = function(client, bufnr)
utils.suppress_lsp_messages(client, { "Method not found: godot/reloadScript" })
keymaps.attach(bufnr)
end,
new_client = function(cfg)
local host = "127.0.0.1"
local port = config.editor_port or 6005
-- Godot editor port local sock = uv.new_tcp()
local handle = io.popen("nc -z 127.0.0.1 6005 >/dev/null 2>&1 && echo ok || echo fail") sock:connect(host, port, function(err)
if handle then if err then
local result = handle:read("*a") vim.schedule(function()
handle:close() vim.notify("Failed to connect to Godot editor LSP: " .. err, vim.log.levels.ERROR)
if result:match("ok") then end)
vim.health.ok("Godot editor LSP detected on port 6005") return
else end
vim.health.warn("Godot editor LSP not detected on port 6005")
end vim.lsp.start_client({
end name = "godot_editor",
cmd = nil,
root_dir = cfg.root_dir,
capabilities = cfg.capabilities,
handlers = cfg.handlers,
offset_encoding = "utf-8",
stdin = sock,
stdout = sock,
})
end)
end,
},
}
-- DO NOT call lspconfig.godot_lsp.setup()
end end
return M return M

View File

@@ -1,19 +0,0 @@
local M = {}
M.config = {
lint_mode = false,
}
M.setup = function(user_config)
if user_config then
M.config = vim.tbl_deep_extend("force", M.config, user_config)
end
-- load submodules
require("godotdev.lsp").setup(M.config)
package.loaded["godotdev.keymaps"] = nil
require("godotdev.keymaps").setup()
end
return M

View File

@@ -1,103 +0,0 @@
local M = {}
vim.api.nvim_create_autocmd("LspAttach", {
group = vim.api.nvim_create_augroup("MyLspKeymaps", { clear = true }),
callback = function(ev)
local client = vim.lsp.get_client_by_id(ev.data.client_id)
local opts = { buffer = ev.buf }
-- Lazy-load Telescope when needed
local has_telescope, telescope_builtin = pcall(require, "telescope.builtin")
local function map(mode, lhs, rhs, desc)
vim.keymap.set(mode, lhs, rhs, vim.tbl_extend("force", opts, { desc = desc }))
end
-- Definitions / Declarations
if client.server_capabilities.definitionProvider then
map(
"n",
"gd",
has_telescope and telescope_builtin.lsp_definitions or vim.lsp.buf.definition,
"LSP: Go to definition"
)
end
if client.server_capabilities.declarationProvider then
map("n", "gD", vim.lsp.buf.declaration, "LSP: Go to declaration")
end
if client.server_capabilities.typeDefinitionProvider then
map(
"n",
"gy",
has_telescope and telescope_builtin.lsp_type_definitions or vim.lsp.buf.type_definition,
"LSP: Type definition"
)
end
if client.server_capabilities.implementationProvider then
map(
"n",
"gi",
has_telescope and telescope_builtin.lsp_implementations or vim.lsp.buf.implementation,
"LSP: Go to implementation"
)
end
if client.server_capabilities.referencesProvider then
map(
"n",
"gr",
has_telescope and telescope_builtin.lsp_references or vim.lsp.buf.references,
"LSP: List references"
)
end
-- Info
if client.server_capabilities.hoverProvider then
map("n", "K", vim.lsp.buf.hover, "LSP: Hover documentation")
end
if client.server_capabilities.signatureHelpProvider then
map("n", "<C-k>", vim.lsp.buf.signature_help, "LSP: Signature help")
end
-- Symbols
if client.server_capabilities.documentSymbolProvider then
map(
"n",
"<leader>ds",
has_telescope and telescope_builtin.lsp_document_symbols or vim.lsp.buf.document_symbol,
"LSP: Document symbols"
)
end
if client.server_capabilities.workspaceSymbolProvider then
map(
"n",
"<leader>ws",
has_telescope and telescope_builtin.lsp_dynamic_workspace_symbols or vim.lsp.buf.workspace_symbol,
"LSP: Workspace symbols"
)
end
-- Actions
if client.server_capabilities.renameProvider then
map("n", "<leader>rn", vim.lsp.buf.rename, "LSP: Rename symbol")
end
if client.server_capabilities.codeActionProvider then
map({ "n", "v" }, "<leader>ca", vim.lsp.buf.code_action, "LSP: Code action")
end
if client.server_capabilities.documentFormattingProvider then
map("n", "<leader>f", function()
vim.lsp.buf.format({ async = true })
end, "LSP: Format buffer")
end
-- Diagnostics (always available)
map("n", "gl", vim.diagnostic.open_float, "LSP: Show diagnostics")
map("n", "[d", vim.diagnostic.goto_prev, "LSP: Previous diagnostic")
map("n", "]d", vim.diagnostic.goto_next, "LSP: Next diagnostic")
end,
})
M.setup = function()
-- global setup if needed
end
return M

View File

@@ -1,36 +0,0 @@
local M = {}
M.setup = function(config)
local lspconfig = require("lspconfig")
local utils = require("godotdev.utils")
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities.textDocument.typeDefinition = nil
-- GDscript standalone LSP
lspconfig.gdscript.setup({
cmd = { "godot-lsp", "--stdio" },
filetypes = { "gd", "gdscript" },
root_dir = lspconfig.util.root_pattern("project.godot"),
capabilities = capabilities,
})
-- Godot editor LSP
lspconfig.godot_lsp = {
default_config = {
cmd = { "nc", "127.0.0.1", "6005" },
root_dir = lspconfig.util.root_pattern("project.godot"),
filetypes = { "gd", "gdscript" },
name = "godot",
},
}
lspconfig.godot_lsp.setup({
capabilities = config.capabilities or vim.lsp.protocol.make_client_capabilities(),
on_attach = function(client, _)
-- Suppress known unsupported Godot messages
utils.suppress_lsp_messages(client, { "Method not found: godot/reloadScript" })
end,
})
end
return M

View File

@@ -1,24 +0,0 @@
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
end
return M

View File

@@ -1,5 +0,0 @@
if vim.fn.has("nvim-0.9") == 1 then
vim.api.nvim_create_user_command("CheckGodotdev", function()
require("godotdev.health").check()
end, {})
end