hrsh7th / nvim-cmp

A completion plugin for neovim coded in Lua.
MIT License
8.04k stars 398 forks source link

Unexpected LuaSnip autosnippet expansion behaviour with native menu #1400

Open gordonwinship opened 1 year ago

gordonwinship commented 1 year ago

FAQ

Announcement

Minimal reproducible full config

if has('vim_starting')
  set encoding=utf-8
endif
scriptencoding utf-8

if &compatible
  set nocompatible
endif

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 'hrsh7th/nvim-cmp'
Plug 'hrsh7th/cmp-omni'
Plug 'hrsh7th/cmp-buffer'
Plug 'hrsh7th/cmp-nvim-lsp'
Plug 'hrsh7th/vim-vsnip'
Plug 'neovim/nvim-lspconfig'
Plug 'saadparwaiz1/cmp_luasnip'
Plug 'L3MON4D3/LuaSnip'
call plug#end()
PlugInstall | quit

" Setup global configuration. More on configuration below.
lua << EOF
local cmp = require "cmp"
local luasnip = require "luasnip"

cmp.setup {
  snippet = {
    expand = function(args)
      luasnip.lsp_expand(args.body) 
    end,
  },
  sources = cmp.config.sources({
    { name = "luasnip" },
    { name = "buffer" },
  }),
    view = {
        entries = 'native', -- this appears to be the issue
    },
}
EOF

lua << EOF
local capabilities = require('cmp_nvim_lsp').default_capabilities()

require'lspconfig'.cssls.setup {
  capabilities = capabilities,
}
EOF

lua << EOF
ls = require'luasnip'
local s = ls.snippet
local t = ls.text_node
local i = ls.insert_node

ls.config.setup({
  enable_autosnippets = true,
})

ls.add_snippets("tex", {
        s({trig="bf", dscr="\textbf{}", snippetType="autosnippet"},
          {
            t("\\textbf{"), 
            i(1),
            t("}"),
          }
        ),
    })

EOF

Description

Given the luasnip snippet

s({trig="bf", dscr="\textbf{}", snippetType="autosnippet"},
          {
            t("\\textbf{"), 
            i(1),
            t("}"),
          }
        ),

and the above nvim configuration cmp + luasnip exhibits the following behaviour.

If \textbf appears nowhere in the tex file before, it expands correctly to \textbf{}. If \textbf{} appears somewhere in the file instead it expands to bf}{}. If the luasnip trigger is changed to trig = '//' the expansion is correct every time.

Steps to reproduce

Open a *.tex file

\documentclass[12pt]{article}

\begin{document}

\end{document}

and enter the trigger bf once and after completing the snippet write the trigger again.

Expected behavior

The snippet correctly expands to \textbf{} every time.

Actual behavior

The snippet correctly expands to \textbf{} every time.

Additional context

I am aware the view.entries = 'native' is considered experimental. I changed my config to view.entries = 'custom'. This solved the issue.

gordonwinship commented 1 year ago

I also posted an issue for the LuaSnip plugin https://github.com/L3MON4D3/LuaSnip/issues/721