Minimal NeoVim lua config for using nvim-cmp with lsp - lua

I'm trying to write my own NeoVim Lua based config, stripped down to the minimum I need and with the goal to understand at least most of the config. LSP setup is working fine and is the only source I configured for nvim-cmp:
local cmp = require("cmp")
cmp.setup {
sources = {
{ name = 'nvim_lsp' }
}
}
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = require('cmp_nvim_lsp').update_capabilities(capabilities)
After some startup delay the completion is working in the sense that I see popups with proposed completions, based on information from LSP.
But I cannot select any of the proposed completions. I can just continue to type which reduces the proposed completions, but I cannot use tab, arrow keys, ... to select an entry from the popup. I saw in the docs that one can define keyboard mappings but cannot make sense out of them. They are all rather sophisticated, require a snippet package to be installed, ...
I would prefer to select the next completion via tab and to navigate them via arrow key. "Enter" should select the current one.
Could somebody show me a minimal configuration for this setup or point me to more "basic" docs?

Nvim-cmp requires you to set the mapping for tab and other keys explicitly, here is my working example:
local cmp = require'cmp'
local lspkind = require'lspkind'
cmp.setup({
snippet = {
expand = function(args)
-- For `ultisnips` user.
vim.fn["UltiSnips#Anon"](args.body)
end,
},
mapping = cmp.mapping.preset.insert({
['<Tab>'] = function(fallback)
if cmp.visible() then
cmp.select_next_item()
else
fallback()
end
end,
['<S-Tab>'] = function(fallback)
if cmp.visible() then
cmp.select_prev_item()
else
fallback()
end
end,
['<CR>'] = cmp.mapping.confirm({ select = true }),
['<C-e>'] = cmp.mapping.abort(),
['<Esc>'] = cmp.mapping.close(),
['<C-d>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
}),
sources = {
{ name = 'nvim_lsp' }, -- For nvim-lsp
{ name = 'ultisnips' }, -- For ultisnips user.
{ name = 'nvim_lua' }, -- for nvim lua function
{ name = 'path' }, -- for path completion
{ name = 'buffer', keyword_length = 4 }, -- for buffer word completion
{ name = 'omni' },
{ name = 'emoji', insert = true, } -- emoji completion
},
completion = {
keyword_length = 1,
completeopt = "menu,noselect"
},
view = {
entries = 'custom',
},
formatting = {
format = lspkind.cmp_format({
mode = "symbol_text",
menu = ({
nvim_lsp = "[LSP]",
ultisnips = "[US]",
nvim_lua = "[Lua]",
path = "[Path]",
buffer = "[Buffer]",
emoji = "[Emoji]",
omni = "[Omni]",
}),
}),
},
})
This is working great for me. The mapping part corresponds to the config for key mapping in the table. You can tweak your conf based on my config to make it work for you.

Related

Trying to figure out how to Grab part of a table. (hard to explain fully)

So, what i am trying to do is... well it's best to show. oh and This is with Fivem and trying to modify an existing resource, but...
`BMProducts = {
{
["lostItems"] = {
[1] = { name = "weapon_shotgun", price = 1500, crypto = 2500, amount = 1 },
},
}
}
table.insert(BMProducts, {
["ballasItems"] = {
[1] = { name = "weapon_pistol", price = 1500, crypto = 2500, amount = 1 },
},
})
Config.Products = BMProducts`
And I have a another config, that i need to pull the Correct table, but now sure entirely how
`["products"] = Config.Products["ballasItems"],`
Is what I have but it won't read it, due to what I assume is what i saw when debugging, that when inserting to the table, it assigns a number, ie;
[1] = {lostitems = {... [2] = {ballasItems = {...
One that works, but what my ultimate goal is to make the code plug and play with the table inserts, is this
`BMProducts =
{
["lostItems"] = {
[1] = { name = "weapon_pistol", price = 1500, crypto = 2500, amount = 1 },
},
["ballasItems"] = {
[1] = { name = "weapon_pistol", price = 1500, crypto = 2500, amount = 1 },
},
}`
which works with the config above because the way just above does not assign numbers and not inserting into a table. Any ideas how i can go about setting that config for the correct Products table?
When i try it with the table insert, and with the
`["products"] = Config.Products["ballasItems"],`
It can't find the table, which is due to what i assume the table format being different than what it was, which was the code block at the bottom
so my main thing, is to get
`["products"] = Config.Products["ballasItems"],`
to = the correct table when there is a table insert.
If you don't want to table.insert, then don't table.insert:
BMProducts["ballasItems"] = {
{ name = "weapon_pistol", price = 1500, crypto = 2500, amount = 1 },
}
Now I think you told to not modify the config, so another approach is to just use that additional array index when indexing:
["products"] = Config.Products[2]["ballasItems"]
Which obviously assumes that the config never changes and the entry with ballasItems is on index 2.
If you do not know the index, you may want to iterate over Config.Products and look for the right product.
for i,v in ipairs(Config.Products) do
if v["ballasItems"] then
end
end
You may also consider the case where two or more products match.

Overriding the package.loaded table in a function in Lua

I'm trying to override the package.loaded table in the scope of a function using setfenv, but it's not quite working.
local env = {
require = require,
print = print,
package = {
cpath = package.cpath,
loadlib = package.loadlib,
path = package.path,
preload = package.preload,
seeall = package.seeall,
loaded = {
foobar = {
fn = function() print("hello world") end,
},
},
},
}
setfenv(assert(loadstring("require('foobar')")), env)()
Executing the above code, will result in a error because the module foobar isn't found.
It seems that it's for some reason not picking up the package table override that I've specified in the environment.
How do I make this work?

How to use values ​from one module script in another module script?

Faced such problem: it is necessary to use values ​​from other module script in one module script. Both module scripts contain tables with float, boolean and string values. How in theory can this be done and is it possible at all? And if not, what are the alternative solutions? I read on the forum that this can be done, but how exactly was not explained anywhere.
For example.
One module script:
local characters = {
["CharacterOne"] = {
["Health"] = 100,
["InGroup"] = false,
["Eating"] = {"Fruit", "Meat"}
};
["CharacterTwo"] = {
["Health"] = 260,
["InGroup"] = true,
["Eating"] = {"Meat"}
}
}
return characters
Two module script:
local food = {
["Fruit"] = {
["Saturation"] = 20,
["Price"] = 2
},
["Meat"] = {
["Saturation"] = 50,
["Price"] = 7
}
}
return food
The documentation for ModuleScripts tells you how to use them in other source containers: the require function.
So assuming that you've named your ModuleScripts Characters and Food respectively, and assuming that they are siblings in a folder, you could utilize the require function like this :
local Food = require(script.Parent.Food)
local characters = {
["CharacterOne"] = {
["Health"] = 100,
["InGroup"] = false,
["Eating"] = { Food.Fruit, Food.Meat }
};
["CharacterTwo"] = {
["Health"] = 260,
["InGroup"] = true,
["Eating"] = { Food.Meat }
}
}
return characters

Nvim-cmp is adding multiple times the same sources

I'm using nvim-cmpto have a contextual window to display my LSP suggestions and my snippets but when I open multiple buffers, I have an issue : the same source is added multiple times to nvim-cmp causing the same result to be repeated in the popup.
For example, here is the result of :CmpStatus: after a few minutes of work.
# ready source names
- vsnip
- buffer
- nvim_lsp:pylsp
- vsnip
- nvim_lsp:pylsp
- nvim_lsp:pylsp
Here is my nvim-cmpconfig :
cmp.setup({
snippet = {
expand = function(args)
vim.fn["vsnip#anonymous"](args.body)
end,
},
...
sources = {
{ name = 'vsnip' },
{ name = 'nvim_lua' },
{ name = 'nvim_lsp' },
{ name = 'buffer', keyword_length = 3 }
},
}
Does anyone know how to adress this issue ? Is it a problem with my configuration ?
In your cmp configuration, you can use the dup keyword for vim_item with the formatting option / format function. See help for complete-item for explanations (:help complete-item).
cmp.setup({
formatting = {
format = function(entry, vim_item)
vim_item.menu = ({
nvim_lsp = '[LSP]',
vsnip = '[Snippet]',
nvim_lua = '[Nvim Lua]',
buffer = '[Buffer]',
})[entry.source.name]
vim_item.dup = ({
vsnip = 0,
nvim_lsp = 0,
nvim_lua = 0,
buffer = 0,
})[entry.source.name] or 0
return vim_item
end
}
})
You can see details in this feature request for nvim-cmp plugin.
I had the same problem and managed to solve the issue by realizing that cmp is somehow installed twice.
Try removing or better first renaming the cmp-packages in the plugged directory, for example
cmp-nvim-lsp to cmp-nvim-lsp_not
cml-nvim-buffer to cmp-nvim-buffer_not
etc.
This did the job for me.

Setup lunarvim to use prettier-eslint

I'm migrating to Lunarvim from Atom and I need to configure Lunarvim to use prettier-eslint for Javascript files.
Read lunarvim docs but not sure how to do it.
Solved with this custom LUA Config
-- optional: set your prefered indent with size
vim.opt.shiftwidth = 4
vim.opt.tabstop = 4
-- load required null-ls references
local h = require("null-ls.helpers")
local cmd_resolver = require("null-ls.helpers.command_resolver")
local methods = require("null-ls.methods")
local u = require("null-ls.utils")
local FORMATTING = methods.internal.FORMATTING
-- Define the new javascript formatter
local pe = h.make_builtin({
name = "prettier_eslint",
meta = {
url = "https://github.com/prettier/prettier-eslint-cli",
description = "Eslint + Prettier",
},
method = FORMATTING,
filetypes = {
"javascript",
"javascriptreact",
"typescript",
"typescriptreact",
"vue",
"jsx"
},
factory = h.formatter_factory,
generator_opts = {
command = "prettier-eslint",
args = { "--stdin", "--parser", "babel", "--resolve-plugins-relative-to", "~/.nvm/versions/node/v16.16.0/lib" },
to_stdin = true,
},
})
-- optional: Define a second formatter for JSON
local pejson = h.make_builtin({
name = "prettier_eslint_json",
meta = {
url = "https://github.com/prettier/prettier-eslint-cli",
description = "Eslint + Prettier",
},
method = FORMATTING,
filetypes = {
"json",
"cjson",
},
factory = h.formatter_factory,
generator_opts = {
command = "prettier-eslint",
args = { "--stdin", "--parser", "json" },
to_stdin = true,
},
})
-- Enable the the defined formatters
-- if you are using vanilla NeoVim + null-ls please
-- read how to install/enable on
-- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/main/doc/CONFIG.md
local nls = require("null-ls")
nls.setup {
on_attach = require("lvim.lsp").common_on_attach,
sources = {
pe,
pejson
}
}
-- optional: LunarVim related step. Here we enable eslint as linter for Javascript.
local linters = require "lvim.lsp.null-ls.linters"
linters.setup {
{
command = "eslint",
filetypes = { "javascript" }
}
}
Full tutorial here

Resources