Haroon Almadani
Haroon Almadani

Reputation: 11

NVIM Cmp LSP autocomplete not working. Using mason.nvim alongside nvim-cmp

I've been trying to get my autocomplete working. It works when I hit ctr space but everything I get says text.

Here is my lsp config:

local on_attach = function(_,_)

  vim.keymap.set('n', 'gd', vim.lsp.buf.definition, {})
  vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, {})
  vim.keymap.set('n', 'K', vim.lsp.buf.hover, {})
  vim.keymap.set('n', 'gr', vim.lsp.buf.references, {})
end

return {
  {
    "williamboman/mason.nvim",
    config = function()
      require("mason").setup()
    end,
  },
  {
    "williamboman/mason-lspconfig.nvim",
    dependencies = { "williamboman/mason.nvim" }, -- Ensure Mason is loaded first
    config = function()
      require("mason-lspconfig").setup({
        ensure_installed = {
          "clangd",
          "ts_ls",
          "omnisharp",
          "lua_ls", -- Correct LSP name for Lua
        },
      })
    end,
  },
  {
    "neovim/nvim-lspconfig",
    dependencies = { "williamboman/mason-lspconfig.nvim" }, -- Ensure LSP configurations are loaded after Mason
    config = function()
      require("lspconfig").lua_ls.setup({on_attach = on_attach, settings = {Lua = {completion = {callSnippet ="Replace", enable=true,}}}})
      
      require("lspconfig").ts_ls.setup({on_attack = on_attach})
      require("lspconfig").clangd.setup({on_attach = on_attach})
      require("lspconfig").omnisharp.setup({on_attach = on_attach})
    end,
  },
}

Here is my cmp config:

return {
  "hrsh7th/nvim-cmp",
  event = "InsertEnter",
  dependencies = {
    "hrsh7th/cmp-buffer",
    "hrsh7th/cmp-path",
    "L3MON4D3/LuaSnip",
    "saadparwaiz1/cmp_luasnip",
    "rafamadriz/friendly-snippets",
  },
  config = function()
    local cmp = require("cmp")
    local luasnip = require("luasnip")

    require("luasnip.loaders.from_vscode").lazy_load()

    cmp.setup({
      completion = {
        completeopt = "menu,menuone,preview,noselect"
      },
      snippet = {
        expand = function(args)
          luasnip.lsp_expand(args.body)
        end,
      },
      mapping = cmp.mapping.preset.insert({ -- Fix here
        ["<C-k>"] = cmp.mapping.select_prev_item(),
        ["<C-j>"] = cmp.mapping.select_next_item(),
        ["<C-Space>"] = cmp.mapping.complete(), -- Fix here
        ["<C-e>"] = cmp.mapping.abort(),
        ["<CR>"] = cmp.mapping.confirm({ select = false }),
      }),

      sources = cmp.config.sources({
        { name = "nvim_lsp" },
        { name = "luasnip" },
        { name = "buffer" },
        { name = "path" },
      })
    })
  end
}

I'm not really sure what I'm doing wrong. I straight up don't get any autocompletes at all, only if I hit ctrl space. I get snippets sometimes, but I'm not really sure why I'm not getting any lsp recommendations. I've already confirmed that my lsp is working properly. I get all the errors and stuff however I'm not sure why this specifically isn't working.

Upvotes: 1

Views: 30

Answers (1)

Angju
Angju

Reputation: 71

First, I have checked several times, but I don't see you have cmp-lsp in your config. After you have installed cmp-lsp you have to pass capabilities to the server setup table like in this example:

{
    "neovim/nvim-lspconfig",
    dependencies = { "williamboman/mason-lspconfig.nvim", "hrsh7th/cmp-nvim-lsp" },
    config = function()
        local lspconfig = require("lspconfig")

        local on_attach = function(_, _)
            vim.keymap.set("n", "gd", vim.lsp.buf.definition, {})
            vim.keymap.set("n", "gi", vim.lsp.buf.implementation, {})
            vim.keymap.set("n", "K", vim.lsp.buf.hover, {})
            vim.keymap.set("n", "gr", vim.lsp.buf.references, {})
        end

        local capabilities = vim.lsp.protocol.make_client_capabilities()
        capabilities = vim.tbl_deep_extend("force", capabilities, require("cmp_nvim_lsp").default_capabilities())

        lspconfig.lua_ls.setup({
            on_attach = on_attach,
            capabilities = capabilities,
            settings = { Lua = { completion = { callSnippet = "Replace", enable = true } } },
        })
    end,
}

You have to pass the capabilities to every server you set. You can automatically do that with a loop to avoid repetition.

I'd recommend you to check kickstart.nvim if you haven't. It is an amazing starting point to learn about Neovim config.

Upvotes: 1

Related Questions