LuaSnip icon indicating copy to clipboard operation
LuaSnip copied to clipboard

When using luasnip buffer confusion

Open amistyrain opened this issue 4 years ago • 33 comments

nvim -v

NVIM v0.6.0-dev+181-g5d633546b
Build type: Release
LuaJIT 2.1.0-beta3
Compiled by   #@[email protected]

Features: +acl +iconv +tui
See ":help feature-compile"
system vimrc file: "$VIM/sysinit.vim"
fall-back for $VIM: "/usr/local/Cellar/neovim/HEAD-5d63354/share/nvim"
Run :checkhealth for more info

https://user-images.githubusercontent.com/16816655/130549127-165bbd2c-2d92-4729-ac42-48d9bc18deab.mov

luasnip config:

local present, luasnip = pcall(require, "luasnip")
if not present then
    return
end

local t = function(str)
    return vim.api.nvim_replace_termcodes(str, true, true, true)
end

local check_back_space = function()
    local col = vim.fn.col "." - 1
    if col == 0 or vim.fn.getline("."):sub(col, col):match "%s" then
        return true
    else
        return false
    end
end

_G.tab_complete = function()
    if vim.fn.pumvisible() == 1 then
        return t "<C-n>"
    elseif luasnip and luasnip.expand_or_jumpable() then
        return t "<Plug>luasnip-expand-or-jump"
    elseif check_back_space() then
        return t "<Tab>"
    else
        return vim.fn["compe#complete"]()
    end
end
_G.s_tab_complete = function()
    if vim.fn.pumvisible() == 1 then
        return t "<C-p>"
    elseif luasnip and luasnip.jumpable(-1) then
        return t "<Plug>luasnip-jump-prev"
    else
        return t "<S-Tab>"
    end
end

_G.completions = function()
    local npairs
    if
        not pcall(
            function()
                npairs = require "nvim-autopairs"
            end
        )
     then
        return
    end

    if vim.fn.pumvisible() == 1 then
        if vim.fn.complete_info()["selected"] ~= -1 then
            return vim.fn["compe#confirm"] "<CR>"
        end
    end
    return npairs.check_break_line_char()
end

vim.api.nvim_set_keymap("i", "<Tab>", "v:lua.tab_complete()", {expr = true})
vim.api.nvim_set_keymap("s", "<Tab>", "v:lua.tab_complete()", {expr = true})
vim.api.nvim_set_keymap("i", "<S-Tab>", "v:lua.s_tab_complete()", {expr = true})
vim.api.nvim_set_keymap("s", "<S-Tab>", "v:lua.s_tab_complete()", {expr = true})
vim.api.nvim_set_keymap("i", "<CR>", "v:lua.completions()", {expr = true})

luasnip.config.set_config {
    history = true,
    updateevents = "TextChanged,TextChangedI"
}
require("luasnip/loaders/from_vscode").load()

When using luasnip, save at this time, causing the interface to be confusing and cannot be rolled back or deleted

amistyrain avatar Aug 24 '21 03:08 amistyrain

What??? Does this happen with all snippets?

L3MON4D3 avatar Aug 24 '21 06:08 L3MON4D3

I tried something else, and so far it’s just the Golang for loop that’s the problem @L3MON4D3

amistyrain avatar Aug 24 '21 06:08 amistyrain

No other language has been tried

amistyrain avatar Aug 24 '21 06:08 amistyrain

Okay, could you post the snippet? Also, try running :LuaSnipUnlinkCurrent before deleting and see if that fixes your issue

L3MON4D3 avatar Aug 24 '21 07:08 L3MON4D3

  "for statement": {
    "prefix": "for",
    "body": "for ${1:i} := ${2:0}; $1 < ${3:count}; $1${4:++} {\n\t$0\n}",
    "description": "Snippet for a for loop"
  },

If it's this one, it works just fine for me. Maybe turn off all other plugins if you have any and see if the issue persists

L3MON4D3 avatar Aug 24 '21 07:08 L3MON4D3

When I delete this configuration, saving is normal,

luasnip.config.set_config {
    history = true,
    updateevents = "TextChanged,TextChangedI"
}

or Change the configuration as follows, save is also normal

luasnip.config.set_config {
    history = true,
    updateevents = "InsertLeave"
}

but When I use the snippet for loop, within the for, using tab, I copy three times

https://user-images.githubusercontent.com/16816655/130580644-840584db-a5d0-444b-87ef-6779845b7899.mov

amistyrain avatar Aug 24 '21 08:08 amistyrain

this is my nvim config https://github.com/amistyrain/nvim-lua.config

amistyrain avatar Aug 24 '21 08:08 amistyrain

Oh, I only tried with updateevents=TextChangedI, I'll check again with your exact settings.

L3MON4D3 avatar Aug 24 '21 08:08 L3MON4D3

Okay I tried with your settings, but I cannot reproduce it :/ Could you disable other plugins until it works with your original config?

L3MON4D3 avatar Aug 24 '21 08:08 L3MON4D3

also golang for loop?

amistyrain avatar Aug 24 '21 08:08 amistyrain

I used the one from friendly-snippets, im assuming you're talking about that one. If not, please post the snippet so I can try it.

L3MON4D3 avatar Aug 24 '21 08:08 L3MON4D3

I used the one from friendly-snippets, im assuming you're talking about that one. If not, please post the snippet so I can try it.

me too

amistyrain avatar Aug 24 '21 08:08 amistyrain

I deleted other plugins, the same problem

amistyrain avatar Aug 24 '21 09:08 amistyrain

Dammit, could you try the snippet in other filetypes eg. :lua require("luasnip").snippets = {all={require("luasnip").parser.parse_snippet("for", "for ${1:i} := ${2:0}; $1 < ${3:count}; $1${4:++} {\n\t$0\n}")}} and see if it still misbehaves?

L3MON4D3 avatar Aug 24 '21 09:08 L3MON4D3

Dammit, could you try the snippet in other filetypes eg. :lua require("luasnip").snippets = {all={require("luasnip").parser.parse_snippet("for", "for ${1:i} := ${2:0}; $1 < ${3:count}; $1${4:++} {\n\t$0\n}")}} and see if it still misbehaves?

use :lua require("luasnip").snippets = {all={require("luasnip").parser.parse_snippet("for", "for ${1:i} := ${2:0}; $1 < ${3:count}; $1${4:++} {\n\t$0\n}")}} Same as before

amistyrain avatar Aug 24 '21 11:08 amistyrain

https://github.com/amistyrain/nvim-lua.config
branch:main use luasnip branch:dev use vim-vsnip

All other plugins have the same configuration, Dev branch, and there’s nothing wrong with using vim-vsnip

amistyrain avatar Aug 24 '21 11:08 amistyrain

I'm on NVIM v0.6.0-dev+181-g5d633546b (current master), maybe try updating and see if that fixes anything

L3MON4D3 avatar Aug 24 '21 12:08 L3MON4D3

Do you still experience the issue?

L3MON4D3 avatar Aug 28 '21 08:08 L3MON4D3

Do you still experience the issue?

yes

amistyrain avatar Aug 28 '21 08:08 amistyrain

Could you post the output of :lua Luasnip_current_nodes[vim.api.nvim_get_current_buf()].parent:dump() directly after expanding the snippet and after jumping?

L3MON4D3 avatar Aug 28 '21 08:08 L3MON4D3

Could you post the output of :lua Luasnip_current_nodes[vim.api.nvim_get_current_buf()].parent:dump() directly after expanding the snippet and after jumping?

image

amistyrain avatar Aug 28 '21 09:08 amistyrain

image

amistyrain avatar Aug 28 '21 09:08 amistyrain

Okay, the extmarks are getting messed up pretty bad, does the issue also occur if you expand manually?

L3MON4D3 avatar Aug 28 '21 10:08 L3MON4D3

Okay, the extmarks are getting messed up pretty bad, does the issue also occur if you expand manually?

Occurs after expansion to save

amistyrain avatar Aug 28 '21 10:08 amistyrain

To save

Oh, does it not occur if you just jump to the next node?

L3MON4D3 avatar Aug 28 '21 10:08 L3MON4D3

the same as video

amistyrain avatar Aug 28 '21 10:08 amistyrain

Am I the only one in this situation? It’s very strange

amistyrain avatar Aug 28 '21 11:08 amistyrain

Seems like it :/ My only explaination would be another plugin interfering.

L3MON4D3 avatar Aug 28 '21 11:08 L3MON4D3

It’s almost certainly a problem with gofmt, which I format before saving, but I use vim-vsnip, which gofmt is normal

amistyrain avatar Aug 29 '21 12:08 amistyrain

Before formatting, I executed luasnipunlinkcurrent and everything was OK

amistyrain avatar Aug 29 '21 12:08 amistyrain