Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicated snippets with LuaSnip #458

Closed
Davenchy opened this issue May 28, 2024 · 3 comments
Closed

Duplicated snippets with LuaSnip #458

Davenchy opened this issue May 28, 2024 · 3 comments

Comments

@Davenchy
Copy link

Davenchy commented May 28, 2024

I am using LuaSnip and nvim-cmp for my NeoVim configuration.

After I added friendly-snippets, I get duplicated snippets and whenever I hover on any of the duplicated snippets I get the following error.

I only tried with python, typescript, javascript, rust and c/cpp and I have the same problem.

Also I am using lazy.nvim

image

My LuaSnip config:

return {
  "L3MON4D3/LuaSnip",
  build = "make install_jsregexp",
  dependencies = {
    "rafamadriz/friendly-snippets",
  },
  opts = {
    history = true,
    delete_check_events = "TextChanged",
  },
  config = function (_, opts)
    local ls = require 'luasnip'
    local extends = {
      ["typescript"] = { "tsdoc" },
      ["javascript"] = { "jsdoc" },
      ["lua"] = { "luadoc" },
      ["python"] = { "pydoc" },
      ["rust"] = { "rustdoc" },
      ["cs"] = { "csharpdoc" },
      ["java"] = { "javadoc" },
      ["c"] = { "cdoc" },
      ["cpp"] = { "cppdoc" },
      ["php"] = { "phpdoc" },
      ["kotlin"] = { "kdoc" },
      ["ruby"] = { "rdoc" },
      ["sh"] = { "shelldoc" },
    };

    ls.setup(opts)
    require 'luasnip.loaders.from_vscode'.lazy_load()

    for k, v in ipairs(extends) do
      ls.filetype_extend(k, v)
    end
  end,
}

My nvim-cmp config:

return {
  "hrsh7th/nvim-cmp",
  dependencies = {
    "onsails/lspkind.nvim",
    "hrsh7th/cmp-cmdline",
    "hrsh7th/cmp-buffer",
    "hrsh7th/cmp-calc",
    "hrsh7th/cmp-emoji",
    "ray-x/cmp-treesitter",
    "saadparwaiz1/cmp_luasnip",
    "https://codeberg.org/FelipeLema/cmp-async-path",
  },
  config = function()
    local cmp = require "cmp"

    cmp.setup {
      snippet = {
        expand = function(args)
          require 'luasnip'.lsp_expand(args.body)
        end,
      },
      window = {
        completion = cmp.config.window.bordered(),
        documentation = cmp.config.window.bordered(),
      },
      mapping = cmp.mapping.preset.insert({
        ['<C-b>'] = cmp.mapping.scroll_docs(-4),
        ['<C-f>'] = cmp.mapping.scroll_docs(4),
        ['<C-Space>'] = cmp.mapping.complete(),
        ['<C-e>'] = cmp.mapping.abort(),
        ['<CR>'] = cmp.mapping.confirm({ select = true }),
        ['<C-l>'] = cmp.mapping(function()
          local ls = require 'luasnip'
          if ls.expand_or_locally_jumpable() then
            ls.expand_or_jump()
          end
        end, { 'i', 's' }),
        ['<C-h>'] = cmp.mapping(function()
          local ls = require 'luasnip'
          if ls.locally_jumpable(-1) then
            ls.jump(-1)
          end
        end, { 'i', 's' }),
      }),
      sources = cmp.config.sources({
        { name = 'calc' },
        -- { name = 'codeium' },
        { name = 'nvim_lsp' },
        { name = 'treesitter' },
        { name = 'luasnip', option = { show_autosnippets = true } },
        { name = 'async_path' },
        { name = 'buffer' },
        { name = 'emoji' },
      }),
      formatting = {
        format = require 'lspkind'.cmp_format {
          mode = 'symbol_text',
          symbol_map = {
            Codeium = "",
          },
        },
      },
    }

    -- Use buffer source for `/` and `?` (if you enabled `native_menu`, this won't work anymore).
    cmp.setup.cmdline({ '/', '?' }, {
      mapping = cmp.mapping.preset.cmdline(),
      sources = {
        { name = 'buffer' },
      }
    })

    -- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore).
    cmp.setup.cmdline(':', {
      mapping = cmp.mapping.preset.cmdline(),
      sources = cmp.config.sources({
        { name = 'cmdline' },
        { name = 'async_path' },
        { name = 'buffer' },
      }),
      matching = { disallow_symbol_nonprefix_matching = false }
    })
  end,
}
@OkelleyDevelopment
Copy link
Collaborator

Hi @Davenchy , regarding the Python duplication it's (largely) been resolved here. For the others, I wasn't able to reproduce the same issue you are seeing.

I remembered seeing a similar issue before (here) on the LuaSnip repo, so you might want to read through that thread too and potentially raise an issue there as they might have solutions more readily available.

@Davenchy
Copy link
Author

Davenchy commented May 28, 2024

Thank you @OkelleyDevelopment for pointing me in the right direction. After reviewing the issues you mentioned and adjusting my configuration, I discovered the root cause in [nvimtools/none-ls.nvim#130]. It turns out the problem was with none-ls, and the fix was to remove null_ls.builtins.completion.luasnip from my none-ls configuration. Everything is now working perfectly without any errors or any duplication.

I appreciate your help!

Here is my none-ls configuration after the fix:

-- replacment for null-ls
return {
  "nvimtools/none-ls.nvim",
  dependencies = {
    "nvim-lua/plenary.nvim",
    "davidmh/cspell.nvim",
  },
  config = function()
    local null_ls = require("null-ls")
    local cspell = require("cspell")

    null_ls.setup({
      sources = {
        null_ls.builtins.completion.spell,
        -- deleted to fix luasnip and nvim-cmp duplications and errors
        -- null_ls.builtins.completion.luasnip,

        null_ls.builtins.diagnostics.checkmake,
        null_ls.builtins.diagnostics.commitlint,
        null_ls.builtins.diagnostics.cppcheck,
        null_ls.builtins.diagnostics.pylint,
        null_ls.builtins.diagnostics.trivy,
        null_ls.builtins.diagnostics.eslint_d,
        null_ls.builtins.diagnostics.markdownlint,
        cspell.diagnostics,

        null_ls.builtins.formatting.stylua,
        null_ls.builtins.formatting.isortd,
        null_ls.builtins.formatting.blackd,
        null_ls.builtins.formatting.clang_format,
        null_ls.builtins.formatting.markdownlint,
        null_ls.builtins.formatting.prettierd,

        null_ls.builtins.hover.printenv,

        cspell.code_actions,
      },
    })

    vim.keymap.set("n", "<leader>cf", vim.lsp.buf.format, { desc = "LSP Code Format" })
  end,
}

@OkelleyDevelopment
Copy link
Collaborator

Oh very epic! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants