-
Notifications
You must be signed in to change notification settings - Fork 391
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
Support for nvim-cmp. #2215
Comments
I would not mind it at all, but I don't use nvim-cmp. Thus it would be very nice to get input from someone who does that can verify that the configuration works as intended. |
Thank you! I can verify the configs for you, |
I've found some relevant info here: I'll be bold and ping some of the participants there who might have a working configuration: @DerWeh @ranebrown |
I'll also ping @hrsh7th, as he seems both friendly and very able to help assist in figuring out a proper configuration for using VimTeXs omnicompletion with nvim-cmp. When we have something that works, we'll add it to the docs. Also, a question: Should I write in the VimTeX docs that nvim-compe is regarded as deprecated and should not be used? |
I think you should. It might help stop others from opening bug reports on nvim-compe completion if it ever breaks. |
I am experimenting on this although I am a little short on time for a couple days. So far I have followed this issue, and I have come across some interesting behavior: starting the commands with a small letter does not trigger any completion whatsoever, but writing the same command but with a capital letter in the beginning shows completion for all the commands that match, including commands with small letters (e.g. writing if has('vim_starting')
set encoding=utf-8
endif
scriptencoding utf-8
if &compatible
set nocompatible
endif
set packpath=/tmp/plugged/
let s:plug_dir = expand('/tmp/plugged/vim-plug')
if !filereadable(s:plug_dir .. '/plug.vim')
execute printf('!curl -fLo %s/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim', s:plug_dir)
end
execute 'set runtimepath+=' . s:plug_dir
call plug#begin(s:plug_dir)
Plug 'lervag/vimtex'
Plug 'hrsh7th/nvim-cmp'
Plug 'hrsh7th/cmp-omni'
call plug#end()
PlugInstall | quit
let g:testpattern = ''
lua << EOF
local cmp = require "cmp"
cmp.setup {
sources = {
{ name = "omni" ,
keyword_pattern = vim.g["vimtex#re#neocomplete"]},
},
}
EOF I am open to suggestions concerning this minimal vim example. |
I think the keyword pattern needs rewritten to be compatible with lua. I tried copying the contents of |
Same. I get lots of errors in using this with |
What does "compatible with lua" mean here? That is, my understanding is that nvim-cmp uses vim regexes, and |
One suggestion would be to hard code everything in the first iteration. I.e., copy the regex from here: Lines 35 to 58 in 8c1a8fd
Possibly simplify and only use the part for the commands, so e.g. keyword_pattern = '\v\\\a*' Then add the branches one by one, e.g. keyword_pattern = '\v\\%(%(\a*cite|Cite)\a*\*?%(\s*\[[^]]*\]){0,2}\s*\{[^}]*|\a*)' and test if things works as expected. |
Patterns are indeed evaluated using vim regex in the lua/cmp/utils/pattern.lua file.
None of these patterns trigger nvim-cmp so far, normal omnicompletion via |
The problem is that the omnifunc is called with the wrong string: local result = self:_invoke(vim.bo.omnifunc, { 0, string.sub(params.context.cursor_before_line, offset) }) The last part is the string that will be passed to the omnifunc. The problem is that the backslash is also passed to the omnifunc, so when typing cmp = require'cmp'
cmp.setup {
...
sources = cmp.config.sources {
{ name = 'omni', keyword_length = 0 }
}
} I think this is a bug on cmp-omni's side, so I will give an update over there as well. As soon as this is fixed I will give an update here. |
Great, glad to get help on this one! |
Thanks for the update on this. I have another question: Pressing |
I think the best thing is to write a new source for nvim-cmp and pass everything that the vimtex omnifunc provides. I'll work on that if I have some free time |
Hmm ... I don't think the offset should be a I think it's a matter of keyword pattern regex. What's wrong? |
Let's move the discussion here and get back when the issue is solved. |
@lervag I am a little confused about the |
Isn't that a custom thing for VimTeX to indicate whether you're in math mode or not (so you can get context-dependent completion)? vimtex/autoload/vimtex/complete.vim Line 319 in d9e05f5
|
Precisely. VimTeX "abuses" the completion entry dicts in this case and adds some context to help filter candidates.
Great! For reference, could you be specific which configuration would be the corret to use? Did you refer to what you wrote here? I'd be happy to write out some descriptions in the docs to guide users to a functional configuration, but it helps to be sure what would be considered good.
Vim's internal completion automatically displays the completion in a nicely aligned table. VimTeX does not need to do anything except providing the candidates with the standard dictionaries with e.g. I don't know any of the |
@lervag It works fine with the config given by @jhossbach. cmp = require'cmp'
cmp.setup {
...
sources = cmp.config.sources {
{ name = 'omni', keyword_length = 0 }
}
} Omitting |
The menu key is now also displayed in the completion menu similar to the omnifunc, great thanks to @hrsh7th for helping on that!
I think you're right, the
|
Thanks! Silly question: do I understand correctly that
The default value is 1, right? Which means that omni completion would only be triggered after typing at least 1 character, e.g. Also, do I understand correctly that there is no keyword pattern? Does this mean that nvim-cmp will attempt omni completion after every single key press?
How would one do that? It could be useful to add that as an example in the docs.
Great! Good work!
Thanks! I'm pushing a first version now. |
Yes, that's a Lua specialty: you can omit parentheses around single arguments that are either string or table literals, so Which you prefer is a matter of taste (without parentheses, you can mimic "casts", like |
The trigger characters is for force invoking the completion with specific characters. |
Thanks, @jhossbach! I've pushed a minor update now. Is there a simple, built-in way to make this configuration only applicable to tex files? I.e., let's say one wants the One way, of course, is to use the autocmd approach. But it seems quite verbose from a user perspective. Say I want Or - let's say I activate the omni source at the global level, perhaps with a preferred global augroup VimTeX
autocmd!
autocmd FileType tex
\ lua require('cmp').setup.buffer { sources = { { name = 'omni', keyword_pattern = ... } } }
augroup END Do I understand correctly that this will override the global settings? |
The The sources[n].keyrod_pattern is source-specific keyword pattern. It is preferred over a global one. (but global keyword length isn't overwrite) |
@hrsh7th Ok. Let's say I wanted one |
Hello, I tried the suggested settings from 0f8b4f0, but it ended up in following error:
I then tried But as I'm here, I may also have a proposal for improvement of the documentation. In nvim-cmp it is possible to format the completion menu. But then the additional information from vimtex are lost, for example from which package the command is from. We discussed that in this Issue. I currently load following snippet in local cmp = require('cmp')
cmp.setup.buffer {
formatting = {
format = function(entry, vim_item)
vim_item.menu = ({
omni = (vim.inspect(vim_item.menu):gsub('%"', "")),
luasnip = "[LuaSnip]",
buffer = "[Buffer]",
})[entry.source.name]
return vim_item
end,
},
sources = {
{ name = 'omni' },
{ name = 'luasnip' },
{ name = 'buffer' },
},
} This results in a completion window like this: As you can see it uses the information provided by vimtex in the menu, and for the other sources it uses the custom defined descriptions. Maybe it would be interesting to mention that in the documentation too. |
Thanks! I'm updating the docs again. I'll remove the mention of I'll also take the liberty of closing this issue. Feel free to continue the discussion or opening related issues. |
@lervag It seems that now the config does not work anymore. I have the following conf under
But the autocompletion does not work. I run the Tested on latest master of nvim-cmp, cmp-omni and vimtex. |
For me it works with that config. I don't know why it does not for you. Have you double checked, that the Another thing you could test is, if the omni source works if you specify it in the main cmp config. Also note that the |
I have tried to specify cmp-omni source in main cmp config. It does not work either.
|
@jdhao I can also confirm it works for me. Can you check if removing buffer as source and in the function changes anything? |
This is my full cmp config for now:
I can trigger omni completion with My nvim version: 0.6.1, nvim-cmp, cmp-omni, and vimtex are in the latest master. What is your nvim version? Maybe my env is wrong? I have no ideas... |
@jdhao Thanks for sharing your problems. I'm sorry to say I can't be of much help, but I'm glad to see @Xystel and @jhossbach is pitching in and trying to assist. I'll follow the discussion, and if I notice anything where I can be of help, I'll let you know! Also, to all of you: If you notice something that should be fixed or update (e.g. in docs), please let me know! |
@lervag On a sidenote, the following conf can not start vimtex properly:
I open a test.tex file like this: It seems that vimtex is not properly initialized. I manually run this command: Still the autocompletion won't work if I type |
Don't put VimTeX in |
The autocompletion issue is what I referred to when I stated that I can't be of much more help. As we see from the earlier posts, things seem to work well for others who use nvim-cmp. |
@jhossbach Can you try my minimal conf here (change the plugin path to your actual path) and confirm if it works for you? |
I just tried to use your provided cmp config to make a minimal example in one file. This is my result: require('packer').startup(function(use)
use { 'wbthomason/packer.nvim' }
use { 'lervag/vimtex' }
use { 'hrsh7th/nvim-cmp' }
use { 'hrsh7th/cmp-omni' }
use { 'hrsh7th/cmp-buffer' }
use { 'SirVer/ultisnips' }
use { 'onsails/lspkind-nvim' }
use { 'hrsh7th/cmp-nvim-lsp' }
use { 'hrsh7th/cmp-nvim-lua' }
use { 'hrsh7th/cmp-path' }
use { 'hrsh7th/cmp-emoji' }
end)
-- Setup nvim-cmp.
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 = {
['<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,
['<Esc>'] = cmp.mapping.close(),
['<CR>'] = cmp.mapping.confirm({ select = true }),
['<C-d>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
['<C-Space>'] = cmp.mapping.complete(),
},
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 = 'omni' },
{ name = 'buffer' },
{ 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]",
}),
}),
},
}) For me it worked. I added a shortcut Can you make a backup of your |
@Xystel Thanks, your config works for me, autocompletion now starts immediately as I type |
@jdhao Glad to help. :) If you want to lazy load things, maybe NvChad provides a good reference. But I'm unsure, if you don't get the same error with it. For me there isn't a reason to lazy load plugins. I have a total of 42 Plugins and it still loads in 120ms. But most of my plugins are in lua, which is AFAIK much faster parsed then vimscript. As I'm talking about lua and vimscript plugins: Is there a special reason, why you are using UltiSnips over LuaSnip? |
I have been using Ultisnips for over three years, and I have written quite a few snippets for ultisnips. The transition cost for me would be huge, with no perceivable benefit. With its powerful Python interpolation feature, I am sure that no snippet plugin can compete with Ultisnips. So there is no reason for switching to LuaSnip or other similar plugins. |
Good to hear things are working now! Btw., I do agree with @jdhao reg. UltiSnips. Perhaps LuaSnip is good, even great, but I also use Ultisnips - it works very well, and I don't see any reason to change it. There is no significant performance cost either. So, if it ain't broke, don't fix it. |
That sounds like a good reason. I agree that switching for no explicit benefit is pointless. I was curious, I never used UltiSnips, thanks for the answers. From my point of view, I wouldn't change to UltiSnips either. That Python interpolation feature sounds great. But in LuaSnip you could use Lua functions, which is also powerful. So I assume they are both good choices. |
Yes, exactly: I also have the impression that both are good choices. I can see how one can be tempted to change to LuaSnip if one is going into a pure neovim Lua configuration, but essentially I don't think there is much to gain. And in some cases, it can be a big job to convert existing snippets to a new format. |
One benefit of LuaSnip (or similar) is that you remove the need for the external dependency (Python3), which some (but not all, clearly) will consider an advantage. And LuaSnip can (now) load snippets in SnipMate format (https://github.com/L3MON4D3/LuaSnip#add-snippets), so in many cases there will be no need to rewrite existing snippets. (Not that that will convince anybody; just wanted to add these data points.) |
Vimtex has documentations for how to make it work with nivm-compe. However nvim-compe is now deprecated and succeeded by nvim-cmp. Can you guys add documentations for nvim-cmp?
The text was updated successfully, but these errors were encountered: