dcampos / nvim-snippy

Snippet plugin for Neovim written in Lua
MIT License
316 stars 18 forks source link

Omni complete results get overwritten when triggering omni complete a second time #90

Closed heygarrett closed 1 year ago

heygarrett commented 1 year ago

Description

I'm using sourcekit-lsp for Swift. When using omni complete (eg, <c-x><c-o>), selecting the result from the pop-up menu and then continuing to type seems to create a weird state in which triggering omni complete a second time enters the first result again.

Steps to Reproduce

  1. Create a new executable Swift package (eg, mkdir swift_test && cd swift_test && swift package init --type executable)
  2. nvim Sources/swift_test/swift_test.swift
  3. Create a new line at the bottom of the file
  4. Type swift_ and then use <c-x><c-o> to open the pop-up menu for omni complete
  5. Use <c-n> or <c-p> to highlight the swift_test module in the pop-up menu
  6. Type a period
  7. Use <c-x><c-o> again

Expected Result

At step 5 swift_test would be inserted as text.
At step 6 the pop-up menu would be dismissed.
At step 7 the inserted text would remain as is and the pop-up menu would open again.

Actual Result

At step 7 the text swift_test. is replaced with sswift_test. If you type two periods at step 6 instead of only one, swift_test. is replaced with swswift_test.

Additional Context

https://user-images.githubusercontent.com/1154912/206866641-80674c1e-118d-46bf-acab-2854e29d25d5.mp4

Here's my snippy config:

return {
    "dcampos/nvim-snippy",
    config = function()
        local loaded, snippy = pcall(require, "snippy")
        if not loaded then return end

        vim.api.nvim_create_autocmd("CompleteDone", {
            group = vim.api.nvim_create_augroup("snippy", { clear = true }),
            callback = function() snippy.complete_done() end,
        })

        snippy.setup({
            mappings = {
                is = {
                    ["<tab>"] = "next",
                    ["<s-tab>"] = "previous",
                },
            },
        })
    end,
}
heygarrett commented 1 year ago

I just ran into this with lua-language-server, so it doesn't seem to be an issue unique to sourcekit-lsp.

CleanShot 2022-12-13 at 16 15 13@2x

dcampos commented 1 year ago

It's a weird issue, because snippy shouldn't be interfering with omni completion. I don't have a lot of time right know, but will try to look into it as soon as possible.

dcampos commented 1 year ago

I tried to reproduce the bug but wasn't successful. Could you try to reproduce it using the minimal config below?

minimal.lua ```lua local root = vim.fn.fnamemodify("./.repro", ":p") -- set stdpaths to use .repro for _, name in ipairs({ "config", "data", "state", "cache" }) do vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name end -- bootstrap lazy local lazypath = root .. "/plugins/lazy.nvim" if not vim.loop.fs_stat(lazypath) then vim.fn.system({ "git", "clone", "--filter=blob:none", "--branch=stable", "https://github.com/folke/lazy.nvim.git", lazypath, }) end vim.opt.runtimepath:prepend(lazypath) -- install plugins local plugins = { { 'dcampos/nvim-snippy', branch = 'local-snippets', config = function() local snippy = require('snippy') snippy.setup({ hl_group = 'Search', mappings = { i = { [""] = "expand_or_advance", [""] = "previous", }, s = { [""] = "next", [""] = "previous", }, }, }) vim.api.nvim_create_autocmd("CompleteDone", { group = vim.api.nvim_create_augroup("snippy", { clear = true }), callback = function() snippy.complete_done() end, }) end }, 'honza/vim-snippets', { 'neovim/nvim-lspconfig', config = function() require('lspconfig').sumneko_lua.setup { settings = { Lua = { runtime = { -- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim) version = 'LuaJIT', }, diagnostics = { -- Get the language server to recognize the `vim` global globals = {'vim'}, }, workspace = { -- Make the server aware of Neovim runtime files library = vim.api.nvim_get_runtime_file("", true), }, -- Do not send telemetry data containing a randomized but unique identifier telemetry = { enable = false, }, }, }, } end } } require("lazy").setup(plugins, { root = root .. "/plugins", }) ```

You can save it as minimal.lua and run as nvim --clean -u minimal-lazy.lua <file to edit>. I used the file below:

fooooooo = {
    bar = 1,
    baz = 2,
}

I tried the following:

  1. Open a new line at the end.
  2. Type foo, press <c-x><c-o> then <c-n> to select fooooooo and press '.'.
  3. Press <c-x><c-o> again. It shows bar and baz. Seems to work.
heygarrett commented 1 year ago

I found a way to reproduce it with your minimal.lua (though I had to change the snippy branch from local-snippets to master). First, add plugin neodev (which I can reproduce the issue without, but for the simplicity of this example) and set vim.bo.omnifunc in the config function:

minimal.lua ```lua local root = vim.fn.fnamemodify("./.repro", ":p") -- set stdpaths to use .repro for _, name in ipairs({ "config", "data", "state", "cache" }) do vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name end -- bootstrap lazy local lazypath = root .. "/plugins/lazy.nvim" if not vim.loop.fs_stat(lazypath) then vim.fn.system({ "git", "clone", "--filter=blob:none", "--branch=stable", "https://github.com/folke/lazy.nvim.git", lazypath, }) end vim.opt.runtimepath:prepend(lazypath) -- install plugins local plugins = { { "folke/neodev.nvim", config = function() vim.bo.omnifunc = "v:lua.vim.lsp.omnifunc" end, }, { "dcampos/nvim-snippy", branch = "master", config = function() local snippy = require("snippy") snippy.setup({ hl_group = "Search", mappings = { i = { [""] = "expand_or_advance", [""] = "previous", }, s = { [""] = "next", [""] = "previous", }, }, }) vim.api.nvim_create_autocmd("CompleteDone", { group = vim.api.nvim_create_augroup("snippy", { clear = true }), callback = function() snippy.complete_done() end, }) end, }, "honza/vim-snippets", { "neovim/nvim-lspconfig", config = function() require("lspconfig").sumneko_lua.setup({ settings = { Lua = { runtime = { -- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim) version = "LuaJIT", }, diagnostics = { -- Get the language server to recognize the `vim` global globals = { "vim" }, }, workspace = { -- Make the server aware of Neovim runtime files library = vim.api.nvim_get_runtime_file("", true), }, -- Do not send telemetry data containing a randomized but unique identifier telemetry = { enable = false, }, }, }, }) end, }, } require("lazy").setup(plugins, { root = root .. "/plugins", }) ```

Then follow these steps to reproduce inside your ~/.config/nvim directory:

  1. nvim -u minimal.lua <file-to-edit>.lua
  2. Enter insert mode and type: vim.fn.api
  3. <c-x><c-o> (it should insert completion vim.fn.api_info)
  4. Type ().
  5. <c-x><c-o>

CleanShot 2022-12-30 at 22 26 57

dcampos commented 1 year ago

It may be an upstream bug in Neovim. I just pushed a commit to try to work around that.