Search code examples
regexunicodeluaneovim

Buffer word competion configuration with extended characters (neovim)


I'm trying to get buffer word completion using nvim-cmp and the cmp-buffer source. And I have words from an African language with ɔ ɛ ɲ etc. As an FYI this is working ok with the default ctrl-p. The problem I have encountered seems to be evident with nvim_cmp and when I have snippets installed for asciidoc. (I use both the snippets and want buffer completion)

As an example, if i have mɔgɔ in the file by the time i type the g then the snippet completion kicks in, but there is no buffer completion option. But really even the ultisnip snippit option should only occur on a word boundry:

snippet gui "GUI Element Name" !w  

So I think this is a case of the ɔ ɛ (and other letters not being recognized as part of a word. The cmp-buffer documentation provides the following information:

keyword_pattern (type: string)
Default: [[\%(-\?\d\+\%(\.\d\+\)\?\|\h\w*\%([\-.]\w*\)*\)]]

A regular expression for creating a word list from buffer content.

You can set this to \k\+ if you want to use the iskeyword option for recognizing words.

I tried this in my config but it didn't work and I'm not sure this is the correct syntax:

  sources = {                                                                                                                                    
    { name = 'nvim_lsp' },    
    { name = 'ultisnips' },    
    { name = 'buffer', keyword_pattern = [[\k\+]] }    
  }    

I'm also not sure how to get those (and a few more with accents as well) characters into that regular expression (as well as the Lua syntax). If there were an example of how to add characters to this expression it would be helpful.

I was also a bit confused with this Q/A: Vim syntax file: Adding certain characters to iskeyword isn't working

On the one hand it seems you can't add anything above 255 to iskeywords. On the other hand Neovim seems to be working with these characters anyway with ctrl-p.

So if I could get this working with either an iskeyword solution or the regular expression i'd be very appreciative.


Solution

  • I believe the syntax is incorrect, in the docs you can see that there's a section to customize a source.

    Using this, I've customized the buffer source like so:

      sources = {
        { name = 'nvim_lsp' },
        {
          name = 'buffer',
          opts = {
            get_bufnrs = function()
              return vim.api.nvim_list_bufs()
            end
          },
        },
        { name = 'path' },
      },
    

    Therefore if you put the keyword_pattern option inside the opts field it should work as expected, given that the pattern is correct.