0

In Neovim I tried adding my own VSCode style snippets for Apex programming language to LuaSnip and I can't get it to work.

This is my packer.lua file:

vim.cmd [[packadd packer.nvim]]

return require('packer').startup(function(use)
    
    -- [...]

    use {
        'VonHeikemen/lsp-zero.nvim',
        branch = 'v3.x',
        requires = {
            {'williamboman/mason.nvim'},
            {'williamboman/mason-lspconfig.nvim'},
            {'neovim/nvim-lspconfig'},
            {'hrsh7th/nvim-cmp'},
            {'hrsh7th/cmp-nvim-lsp'},
            {'L3MON4D3/LuaSnip'},
        }
    }
end)

This is my after/plugin/lsp.lua where I configure the LSP:

local lsp_zero = require('lsp-zero')

lsp_zero.on_attach(function(client, bufnr)
    lsp_zero.default_keymaps({buffer = bufnr})
end)

local capabilities = require('cmp_nvim_lsp').default_capabilities()

local servers = {
    apex_ls = {
        filetypes = {
            'apexcode',
            'apex',
            'soql',
            'sosl',
        }
    },
    lwc_ls = {
        filetypes = {
            'javascript',
            'html',
            'js-meta.xml'
        },
    },
    lua_ls = {
        settings = {
            Lua = {
                completion = {
                    callSnippet = 'Replace',
                },
            },
        },
    },
}

    require('mason').setup({})
    require('mason-lspconfig').setup({
        handlers = {
            function(server_name)
                local server = servers[server_name] or {}
                server.capabilities = vim.tbl_deep_extend('force', {}, capabilities, server.capabilities or {})
                require('lspconfig')[server_name].setup(server)
            end,
        },
    })

    local cmp = require('cmp')
    local cmp_select = {behavior = cmp.SelectBehavior.Select}
    
    require("luasnip.loaders.from_vscode").lazy_load() -- LuaSnip
    cmp.setup({
        preselect = 'item',
        completion = {
            completeopt = 'menu,menuone,noinsert'
        },
        expand = function(args)
            require('luasnip').lsp_expand(args.body)
        end,
        mapping = cmp.mapping.preset.insert({
            ['<C-p>'] = cmp.mapping.select_prev_item(cmp_select),
            ['<C-n>'] = cmp.mapping.select_next_item(cmp_select),
            ['<C-y>'] = cmp.mapping.confirm({ select = true }),
            ['<C-Space>'] = cmp.mapping.complete(),
        }),
        sources = cmp.config.sources({
            { name = 'nvim_lsp' },
            { name = 'luasnip' },
        }, {
            { name = 'buffer' },
        }),
        window = {
            completion = cmp.config.window.bordered(),
            documentation = cmp.config.window.bordered(),
        },
    })

And for the snippets

Those are some of my snippets in ~/.config/nvim/snippets/apex.json:

{
    "for": {
        "prefix": "for",
        "body": [
            "for (${1:type} ${2:variable} : ${3:Iterable}) {",
            "\t$0",
            "}"
        ],
        "description": "standard for loop"
    },
    "fori": {
        "prefix": "fori",
        "body": [
            "for (Integer i${1:i} = 0; i${1:i} < ${2:end}; i${1:i} ++) {",
            "\t$0",
            "}"
        ],
        "description": "for i loop"
    },
}

and this is my ~/.config/nvim/package.json:

{
    "name": "my-nvim-config",
    "contributes": {
        "snippets": [
            {
                "language": [
                    "apex"
                ],
                "path": "snippets/apex.json"
            }
        ]
    }
}

The luasnip works in .lua files, I get the snippets enter image description here

but when I try in apex file I don't get any snippets, just LSP suggestions. enter image description here

I already tried

  • like in this example from LuaSnip doc
  • like this comment suggests (current version)
  • changing language in package.json from apex to all

but noting works, I can't get any Apex snippets. Also, there is nothing in luasnip.log file and when I execute command :LspInfo I get correctly detected file type (Detected filetype: apex) and when I execute :LuaSnipListAvailable I get my snippets list

{                                                                                                                                                                                            
  all = {},                                                                                                                                                                                   
  apex = { { 
    ...
  }
}

I've been looking through the documentation, github issues, reddit, blogposts etc. for a long time now and have been trying a lot of different configuration options but I'm pretty new to neovim so it's kind of blind copying and pasting someone else's code and seeing what happens. I feel like I'm going too deep into this and that the problem might be somewhere else, if anyone could at least point me in the right direction that would be a huge help.

1
  • You might try How to debug my vimrc and How to navigate :help. Blindly copy-pasting is generally just going to be a bad idea—it's much harder to debug, as you're experiencing. I would recommend taking a step back and making sure you understand the code you use for your editor.
    – D. Ben Knoble
    Commented Jun 8 at 22:51

0

Your Answer

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

Browse other questions tagged or ask your own question.