0

I am new to Neovim and lua as programming language. I am trying to setup a Neovim as a full complete IDE. My issue deals with Mason and LSP capabilities. It seems that my LSP server is executing but my configuration is not correctly setup properly. My point is that the keymapping from my attach table are not applied My file setup is the following:

~/.config/nvim/init.lua

~/.config/nvim/lua/jpv/ {init.lua, ...., mason.lua, lsp}

~/.config/nvim/lua/jpv/lsp/ {init.lua, handlers.lua}

I have a file 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
  })

On the other side I have a lsp folder with an init.lua

local status_ok, nvm_lsp = pcall(require, "lspconfig")
if not status_ok then
  return
end

require("jpv.lsp.handlers").setup()
require("jpv.mason")

Last, my handlers.lua is

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
9
  • mason is not needed and it makes thing more complicated. You can configure nvim-lsp with only nvim-lspconfig. Commented Oct 9, 2022 at 3:53
  • Official documentation states that mason and mason-config should be replaces lsp installer . Why should I avoid mason then ? Commented Oct 10, 2022 at 13:13
  • On the other side I am hihgly interested in having an editor adjusted for kotlin, spring and java development. I see that kotlin-language-server does not work as good as the one from coc-kotlin in coc pluhgin. Any idea ? Commented Oct 10, 2022 at 13:18
  • what do you mean by official doc? See nvim-lspconfig doc here. Commented Oct 10, 2022 at 13:36
  • I have no experience for java and kotlin, so can not comment. But If you prefer less setup, you may try coc.nvim, which works more like out of the box (you do need some config). Commented Oct 10, 2022 at 13:38

1 Answer 1

0

Replace setup_handlers with this on your mason.lua file:

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})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.