davidgranstrom / scnvim

Neovim frontend for SuperCollider.
GNU General Public License v3.0
197 stars 26 forks source link

[FR]: Lazy_loading luasnippets #190

Closed kflak closed 2 years ago

kflak commented 2 years ago

Is your feature request related to a problem? Please describe.

The main problem is that loading the generated snippets causes nvim startup-time to increase dramatically.

Describe the solution you'd like

Some snippets have a lazy_load function, for example

require("luasnip.loaders.from_vscode").lazy_load()

I don't know too much about how these have been implemented, but it would be great if something like this could be implemented for scnvim-generated snippets as well. Or, at the very least, find a way to ensure the sc-snippets only load for supercollider filetype.

Not sure if this is within the scope of scnvim, though, or if it's something more specific to luasnip/nvim-cmp...

davidgranstrom commented 2 years ago

It has to be up to the snippet framework since scnvim does not load the snippets.

There is an API function to get the path of a generated asset without loading anything, maybe that can be used for lazy loading depending on how the snippet engine works?

local asset_path = require('scnvim.path').get_asset 'snippets'

I have started sketching on a notification system that might be useful for these types of tasks, not sure if it will make it into #153, but I imagine it to work something like this:

--- in init.vim

local notification = require 'scnvim.notification'
notification.add_subscriber('sclang_started', function(object)
  -- executed once sclang has been started
  vim.cmd [[ SCNvimGenerateAssets ]]
end)
kflak commented 2 years ago

Good, good, will start looking into how the other frameworks do the lazy loading and see if this can be used fon sclang.

davidgranstrom commented 2 years ago

Thinking about this some more, you could probably set up an autocmd for this (e.g. FileType) and load the snippets like you do now but instead the first time entering a supercollider buffer

kflak commented 2 years ago

That works indeed! However, I find nvim very sluggish in general when using the snippets, so I would like to find some other way of loading them. Homework for some other time...

ranjithshegde commented 2 years ago

because I lazy-load both scnvim and luasnip, to prevent erroring only when one of them is loaded, I use something like this

    vim.api.nvim_create_autocmd("InsertEnter", {
        pattern = "*.scd, *.sc, *.sc_help, *.quark",
        group = some_group_id,
        callback = function()
            require("luasnip").add_snippets("supercollider", require("scnvim/utils").get_snippets())
        end,
        once = true,
    })

Note the once flag! This does add a lag between 1-2 seconds the very first time I InsertEnter in scnvim but it does the job

Perhaps luasnip has some lazier method that replicates add_snippets method?

kflak commented 2 years ago

Thanks, @ranjithshegde! This works really well now on my system. There may be some small lag when typing, but not too much. I might have to look into how many items nvim-cmp populates the menu with, seems to be connected somehow...

davidgranstrom commented 2 years ago

Since the auto generated snippet file is quite large I would imagine that limiting the entries for nvim-cmp would help. (I have it configured to only auto complete after typing 3 characters and that seem to work pretty well also for language servers where I've experienced similar lag, not sure how it works with snippets since I'm not using that for the moment)

kflak commented 2 years ago

Thanks, @davidgranstrom! Didn't think about that approach. It helps a lot, and it also made me take a critical look at what exactly I stuffed into my nvim-cmp sources :-)

kflak commented 2 years ago

because I lazy-load both scnvim and luasnip, to prevent erroring only when one of them is loaded, I use something like this

    vim.api.nvim_create_autocmd("InsertEnter", {
        pattern = "*.scd, *.sc, *.sc_help, *.quark",
        group = some_group_id,
        callback = function()
            require("luasnip").add_snippets("supercollider", require("scnvim/utils").get_snippets())
        end,
        once = true,
    })

Note the once flag! This does add a lag between 1-2 seconds the very first time I InsertEnter in scnvim but it does the job

Perhaps luasnip has some lazier method that replicates add_snippets method?

Suddenly this approach stopped working for me, but was easily fixable by replacing

 pattern = "*.scd, *.sc, *.sc_help, *.quark",

with

    pattern = { "*.scd", "*.sc", "*.sc_help", "*.quark"},