Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use libuv-file-watcher to update loaded snippet-collections. #1033

Merged
merged 9 commits into from
Dec 2, 2023
19 changes: 19 additions & 0 deletions DOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -2544,12 +2544,28 @@ where `opts` can contain the following keys:
- `snipmate`: similar to lua, but the directory has to be `"snippets"`.
- `vscode`: any directory in `runtimepath` that contains a
`package.json` contributing snippets.
- `lazy_paths`: behaves essentially like `paths`, with two exceptions: if it is
`nil`, it does not default to `runtimepath`, and the paths listed here do not
need to exist, and will be loaded on creation.
LuaSnip will do its best to determine the path that this should resolve to,
but since the resolving we do is not very sophisticated it may produce
incorrect paths. Definitely check the log if snippets are not loaded as
expected.
- `exclude`: List of languages to exclude, empty by default.
- `include`: List of languages to include, includes everything by default.
- `{override,default}_priority`: These keys are passed straight to the
`add_snippets`-calls (documented in [API](#api)) and can therefore change the
priority of snippets loaded from some colletion (or, in combination with
`{in,ex}clude`, only some of its snippets).
- `fs_event_providers`: `table<string, boolean>?`, specifies which mechanisms
should be used to watch files for updates/creation.
If `autocmd` is set to `true`, a `BufWritePost`-hook watches files of this
collection, if `libuv` is set, the file-watcher-api exposed by libuv is used
to watch for updates.
Use `libuv` if you want snippets to update from other neovim-instances, and
`autocmd` if the collection resides on a filesystem where the libuv-watchers
may not work correctly. Or, of course, just enable both :D
By default, only `autocmd` is enabled.

While `load` will immediately load the snippets, `lazy_load` will defer loading until
the snippets are actually needed (whenever a new buffer is created, or the
Expand Down Expand Up @@ -2723,6 +2739,9 @@ If `scope` is not set, the snippet will be added to the global filetype (`all`).
- `{override,default}_priority`: These keys are passed straight to the
`add_snippets`-calls (documented in [API](#api)) and can be used to change
the priority of the loaded snippets.
- `lazy`: `boolean`, if it is set, the file does not have to exist when
`load_standalone` is called, and it will be loaded on creation.
`false` by default.

**Example**:
`a.code-snippets`:
Expand Down
21 changes: 20 additions & 1 deletion doc/luasnip.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*luasnip.txt* For NVIM v0.8.0 Last change: 2023 December 01
*luasnip.txt* For NVIM v0.8.0 Last change: 2023 December 02

==============================================================================
Table of Contents *luasnip-table-of-contents*
Expand Down Expand Up @@ -2367,12 +2367,28 @@ where `opts` can contain the following keys:
- `snipmate`: similar to lua, but the directory has to be `"snippets"`.
- `vscode`: any directory in `runtimepath` that contains a
`package.json` contributing snippets.
- `lazy_paths`: behaves essentially like `paths`, with two exceptions: if it is
`nil`, it does not default to `runtimepath`, and the paths listed here do not
need to exist, and will be loaded on creation.
LuaSnip will do its best to determine the path that this should resolve to,
but since the resolving we do is not very sophisticated it may produce
incorrect paths. Definitely check the log if snippets are not loaded as
expected.
- `exclude`: List of languages to exclude, empty by default.
- `include`: List of languages to include, includes everything by default.
- `{override,default}_priority`: These keys are passed straight to the
`add_snippets`-calls (documented in |luasnip-api|) and can therefore change the
priority of snippets loaded from some colletion (or, in combination with
`{in,ex}clude`, only some of its snippets).
- `fs_event_providers`: `table<string, boolean>?`, specifies which mechanisms
should be used to watch files for updates/creation.
If `autocmd` is set to `true`, a `BufWritePost`-hook watches files of this
collection, if `libuv` is set, the file-watcher-api exposed by libuv is used
to watch for updates.
Use `libuv` if you want snippets to update from other neovim-instances, and
`autocmd` if the collection resides on a filesystem where the libuv-watchers
may not work correctly. Or, of course, just enable both :D
By default, only `autocmd` is enabled.

While `load` will immediately load the snippets, `lazy_load` will defer loading
until the snippets are actually needed (whenever a new buffer is created, or
Expand Down Expand Up @@ -2560,6 +2576,9 @@ set. If `scope` is not set, the snippet will be added to the global filetype
- `{override,default}_priority`: These keys are passed straight to the
`add_snippets`-calls (documented in |luasnip-api|) and can be used to change
the priority of the loaded snippets.
- `lazy`: `boolean`, if it is set, the file does not have to exist when
`load_standalone` is called, and it will be loaded on creation.
`false` by default.

**Example**: `a.code-snippets`:

Expand Down
3 changes: 3 additions & 0 deletions lua/luasnip/_types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
---@class LuaSnip.MatchRegion 0-based region
---@field row integer 0-based row
---@field col_range { [1]: integer, [2]: integer } 0-based column range, from-in, to-exclusive

---@alias LuaSnip.Addable table
---Anything that can be passed to ls.add_snippets().
2 changes: 1 addition & 1 deletion lua/luasnip/extras/_treesitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ end
local function captures_iter(captures)
-- turn string/string[] into map: string -> bool, for querying whether some
-- string is present in captures.
local capture_map = tbl.normalize_search_table(captures)
local capture_map = tbl.list_to_set(captures)

-- receives the query and the iterator over all its matches.
return function(query, match_iter)
Expand Down
2 changes: 1 addition & 1 deletion lua/luasnip/extras/treesitter_postfix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ local function generate_simple_parent_lookup_function(lookup_fun)
---@param types string|string[]
---@return LuaSnip.extra.MatchTSNodeFunc
return function(types)
local type_checker = tbl.normalize_search_table(types)
local type_checker = tbl.list_to_set(types)
---@param parser LuaSnip.extra.TSParser
---@param pos { [1]: number, [2]: number }
return function(parser, pos)
Expand Down
19 changes: 2 additions & 17 deletions lua/luasnip/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -697,22 +697,7 @@ local function cleanup()
end

local function refresh_notify(ft)
-- vim.validate({
-- filetype = { ft, { "string", "nil" } },
-- })

if not ft then
-- call refresh_notify for all filetypes that have snippets.
for ft_, _ in pairs(ls.snippets) do
refresh_notify(ft_)
end
else
session.latest_load_ft = ft
vim.api.nvim_exec_autocmds(
"User",
{ pattern = "LuasnipSnippetsAdded", modeline = false }
)
end
snippet_collection.refresh_notify(ft)
end

local function setup_snip_env()
Expand All @@ -724,7 +709,7 @@ local function setup_snip_env()
setfenv(2, combined_table)
end
local function get_snip_env()
return session.config.snip_env
return session.get_snip_env()
end

local function get_id_snippet(id)
Expand Down
77 changes: 0 additions & 77 deletions lua/luasnip/loaders/_caches.lua

This file was deleted.

27 changes: 27 additions & 0 deletions lua/luasnip/loaders/data.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--- This module stores all files loaded by any of the loaders, ordered by their
--- filetype, and other data.
--- This is to facilitate luasnip.loaders.edit_snippets, and to handle
--- persistency of data, which is not given if it is stored in the module-file,
--- since the module-name we use (luasnip.loaders.*) is not necessarily the one
--- used by the user (luasnip/loader/*, for example), and the returned modules
--- are different tables.

local autotable = require("luasnip.util.auto_table").autotable

local M = {
lua_collections = {},
lua_ft_paths = autotable(2),

snipmate_collections = {},
snipmate_ft_paths = autotable(2),
-- set by loader.
snipmate_cache = nil,

vscode_package_collections = {},
vscode_standalone_watchers = {},
vscode_ft_paths = autotable(2),
-- set by loader.
vscode_cache = nil,
}

return M
Loading