我是Neovim和lua编程语言的新手。我正在尝试将Neovim设置为一个完整的IDE。我的问题涉及梅森和LSP能力。我的LSP服务器似乎正在执行,但我的配置设置不正确我的观点是,我的附加表中的密钥映射没有应用我文件的设置如下:
~/.config/nvim/init.lua
~/.config/nvim/loa/jpv/{init.loa,…,mason.lua,lsp}
~/.config/nvim/loa/jpv/lsp/{init.loa,handlers.loa}
我有一个文件mason.lua
require("mason").setup({
ui = {
icons = {
package_installed = "✓",
package_pending = "➜",
package_uninstalled = "✗"
}
}
})
require("mason-lspconfig").setup_handlers({
function(server)
local opts = {
on_attach = require("jpv.lsp.handlers").on_attach,
capabilities = require("jpv.lsp.handlers").capabilities,
}
if server == "sumneko_lua" then
print("Inside" .. server)
local sumneko_opts = require("jpv.lsp.settings.sumneko_lua")
opts = vim.tbl_deep_extend("force", sumneko_opts, opts)
end
if server == "kotlin_language_server" then
print("Inside" .. server)
local kotlin_ls = require("jpv.lsp.settings.kotlin_ls")
opts = vim.tbl_deep_extend("force", kotlin_ls, opts)
end
-- print("For server " .. server)
-- for key, value in pairs(opts) do
-- print("Has key ".. key)
-- end
require("lspconfig")[server].setup{opts}
end
})
在另一边,我有一个lsp文件夹,里面有一个init.loa
local status_ok, nvm_lsp = pcall(require, "lspconfig")
if not status_ok then
return
end
require("jpv.lsp.handlers").setup()
require("jpv.mason")
最后,我的处理程序.loa是
local M = {}
-- TODO: backfill this to template
M.setup = function()
local signs = {
{ name = "DiagnosticSignError", text = "" },
{ name = "DiagnosticSignWarn", text = "" },
{ name = "DiagnosticSignHint", text = "" },
{ name = "DiagnosticSignInfo", text = "" },
}
for _, sign in ipairs(signs) do
vim.fn.sign_define(sign.name, { texthl = sign.name, text = sign.text, numhl = "" })
end
local config = {
-- disable virtual text
virtual_text = false,
-- show signs
signs = {
active = signs,
},
update_in_insert = true,
underline = true,
severity_sort = true,
float = {
focusable = false,
style = "minimal",
border = "rounded",
source = "always",
header = "",
prefix = "",
},
}
vim.diagnostic.config(config)
vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, {
border = "rounded",
width = 60,
})
vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, {
border = "rounded",
width = 60,
})
end
local function lsp_highlight_document(client)
-- Set autocommands conditional on server_capabilities
local status_ok, illuminate = pcall(require, "illuminate")
if not status_ok then
print("RETURNING")
return
end
print("NOT returning")
illuminate.on_attach(client)
-- end
end
local function lsp_keymaps(bufnr)
local opts = { noremap = true, silent = true }
vim.api.nvim_buf_set_keymap(bufnr, "n", "gD", "<cmd>lua vim.lsp.buf.declaration()<CR>", opts)
vim.api.nvim_buf_set_keymap(bufnr, "n", "gd", "<cmd>lua vim.lsp.buf.definition()<CR>", opts)
vim.api.nvim_buf_set_keymap(bufnr, "n", "K", "<cmd>lua vim.lsp.buf.hover()<CR>", opts)
vim.api.nvim_buf_set_keymap(bufnr, "n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts)
vim.api.nvim_buf_set_keymap(bufnr, "n", "<C-k>", "<cmd>lua vim.lsp.buf.signature_help()<CR>", opts)
-- vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>rn", "<cmd>lua vim.lsp.buf.rename()<CR>", opts)
vim.api.nvim_buf_set_keymap(bufnr, "n", "gr", "<cmd>lua vim.lsp.buf.references()<CR>", opts)
-- vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>ca", "<cmd>lua vim.lsp.buf.code_action()<CR>", opts)
-- vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>f", "<cmd>lua vim.diagnostic.open_float()<CR>", opts)
vim.api.nvim_buf_set_keymap(bufnr, "n", "[d", '<cmd>lua vim.diagnostic.goto_prev({ border = "rounded" })<CR>', opts)
vim.api.nvim_buf_set_keymap(
bufnr,
"n",
"gl",
'<cmd>lua vim.diagnostic.open_float({ border = "rounded" })<CR>',
opts
)
vim.api.nvim_buf_set_keymap(bufnr, "n", "]d", '<cmd>lua vim.diagnostic.goto_next({ border = "rounded" })<CR>', opts)
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>q", "<cmd>lua vim.diagnostic.setloclist()<CR>", opts)
vim.cmd([[ command! Format execute 'lua vim.lsp.buf.format{async=true}' ]])
print("Inside keymaps iiiiiiiiiiiiiiiiiiiiiii")
end
M.on_attach = function(client, bufnr)
-- vim.notify(client.name .. " starting...")
-- TODO: refactor this into a method that checks if string in list
print("Client is " .. client)
if client.name == "tsserver" then
client.resolved_capabilities.document_formatting = false
end
print("Assigning keymaps")
lsp_keymaps(bufnr)
print("Illuminating document")
lsp_highlight_document(client)
end
local capabilities = vim.lsp.protocol.make_client_capabilities()
local status_ok, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp")
if not status_ok then
return
end
print("LOOOAADIN")
M.capabilities = cmp_nvim_lsp.update_capabilities(capabilities)
return M
将mason.lua文件中的setup_handlers替换为以下内容:
require("mason-lspconfig").setup_handlers({
function(server)
local opts = {
on_attach = require("jpv.lsp.handlers").on_attach,
capabilities = require("jpv.lsp.handlers").capabilities,
}
require("lspconfig")[server].setup(opts)
if server == "sumneko_lua" then
local sumneko_opts = require("jpv.lsp.settings.sumneko_lua")
-- assuming sumneko_opts return a table
sumneko_opts["on_attach"] = opts.on_attach
sumneko_opts["capabilities"] = opts.capabilities
require("lspconfig")[server].setup(sumneko_opts)
end
end})