Format with stylua

This commit is contained in:
L3MON4D3 2023-05-31 09:37:13 +00:00 committed by github-actions[bot]
parent 3b7dbba9f7
commit eac1a14df3
10 changed files with 106 additions and 55 deletions

View file

@ -114,7 +114,11 @@ function M.jump_to_snippet(snip, opts)
end
local fcall_range
local ft = util.ternary(vim.bo[0].filetype ~= "", vim.bo[0].filetype, vim.api.nvim_buf_get_name(0):match("%.([^%.]+)$"))
local ft = util.ternary(
vim.bo[0].filetype ~= "",
vim.bo[0].filetype,
vim.api.nvim_buf_get_name(0):match("%.([^%.]+)$")
)
if ft == "lua" then
if source.line then
-- in lua-file, can get region of definition via treesitter.
@ -137,8 +141,7 @@ function M.jump_to_snippet(snip, opts)
-- matches *.json or *.jsonc.
elseif ft == "json" or ft == "jsonc" then
local ok
ok, fcall_range =
pcall(json_find_snippet_definition, 0, ft, snip.name)
ok, fcall_range = pcall(json_find_snippet_definition, 0, ft, snip.name)
if not ok then
print(
"Could not determine range of snippet-definition: "

View file

@ -14,7 +14,7 @@ local duplicate = require("luasnip.nodes.duplicate")
local json_decoders = {
json = util.json_decode,
jsonc = require("luasnip.util.jsonc").decode,
["code-snippets"] = require("luasnip.util.jsonc").decode
["code-snippets"] = require("luasnip.util.jsonc").decode,
}
local function read_json(fname)
@ -69,7 +69,7 @@ local function get_file_snippets(file)
dscr = parts.description or name,
wordTrig = ls_conf.wordTrig,
priority = ls_conf.priority,
snippetType = ls_conf.autotrigger and "autosnippet" or "snippet"
snippetType = ls_conf.autotrigger and "autosnippet" or "snippet",
}
-- Sometimes it's a list of prefixes instead of a single one
@ -78,12 +78,16 @@ local function get_file_snippets(file)
-- vscode documents `,`, but `.` also works.
-- an entry `false` in this list will cause a `ft=nil` for the snippet.
local filetypes = parts.scope and vim.split(parts.scope, "[.,]") or {false}
local filetypes = parts.scope and vim.split(parts.scope, "[.,]")
or { false }
local contexts = {}
for _, prefix in ipairs(prefixes) do
for _, filetype in ipairs(filetypes) do
table.insert(contexts, {filetype = filetype or nil, trig = prefix})
table.insert(
contexts,
{ filetype = filetype or nil, trig = prefix }
)
end
end
@ -117,12 +121,15 @@ end
-- `force_reload`: don't use cache when reloading, default false
local function load_snippet_file(file, filetype, add_opts, opts)
opts = opts or {}
local refresh_notify = util.ternary(opts.refresh_notify ~= nil, opts.refresh_notify, false)
local force_reload = util.ternary(opts.force_reload ~= nil, opts.force_reload, false)
local refresh_notify =
util.ternary(opts.refresh_notify ~= nil, opts.refresh_notify, false)
local force_reload =
util.ternary(opts.force_reload ~= nil, opts.force_reload, false)
if not Path.exists(file) then
log.error(
"Trying to read snippets from file %s, but it does not exist.", file
"Trying to read snippets from file %s, but it does not exist.",
file
)
return
end
@ -151,11 +158,7 @@ local function load_snippet_file(file, filetype, add_opts, opts)
refresh_notify = refresh_notify,
}, add_opts)
)
log.info(
"Adding %s snippets from %s",
#file_snippets,
file
)
log.info("Adding %s snippets from %s", #file_snippets, file)
end
--- Find all files+associated filetypes in a package.
@ -274,7 +277,7 @@ local function update_cache(cache, file, filetype, add_opts)
if not filecache then
filecache = {
filetype_add_opts = {},
filetypes = {}
filetypes = {},
}
cache.path_snippets[file] = filecache
end
@ -299,7 +302,7 @@ function M.load(opts)
update_cache(package_cache, file, ft, add_opts)
-- `false`: don't refresh while adding.
load_snippet_file(file, ft, add_opts, {refresh_notify = false})
load_snippet_file(file, ft, add_opts, { refresh_notify = false })
end
ls.refresh_notify(ft)
end
@ -311,7 +314,7 @@ function M._load_lazy_loaded_ft(ft)
file,
ft,
package_cache.path_snippets[file].filetype_add_opts[ft],
{refresh_notify = false}
{ refresh_notify = false }
)
end
ls.refresh_notify(ft)
@ -352,7 +355,12 @@ function M.lazy_load(opts)
if package_cache.lazy_loaded_ft[ft] then
for _, file in ipairs(files) do
-- instantly load snippets if they were already loaded...
load_snippet_file(file, ft, add_opts, {refresh_notify = false})
load_snippet_file(
file,
ft,
add_opts,
{ refresh_notify = false }
)
log.info(
"Immediately loading lazy-load-snippets for already-active filetype %s from files:\n%s",
ft,
@ -426,7 +434,12 @@ function M._reload_file(filename)
-- just use its snippets.
local force_reload = true
for ft, _ in pairs(package_cached_data.filetypes) do
load_snippet_file(filename, ft, package_cached_data.filetype_add_opts[ft], {force_reload = force_reload})
load_snippet_file(
filename,
ft,
package_cached_data.filetype_add_opts[ft],
{ force_reload = force_reload }
)
-- only force-reload once, then reuse updated snippets.
force_reload = false
end

View file

@ -10,7 +10,7 @@ local clean_name = {
vscode_packages = "vscode",
vscode_standalone = "vscode-standalone",
snipmate = "snipmate",
lua = "lua"
lua = "lua",
}
local function default_format(path, _)
path = path:gsub(
@ -46,7 +46,7 @@ function M.edit_snippet_files(opts)
opts = opts or {}
local format = opts.format or default_format
local edit = opts.edit or default_edit
local extend = opts.extend or function(_,_)
local extend = opts.extend or function(_, _)
return {}
end
@ -56,7 +56,12 @@ function M.edit_snippet_files(opts)
local items = {}
-- concat files from all loaders for the selected filetype ft.
for _, cache_name in ipairs({ "vscode_packages", "vscode_standalone", "snipmate", "lua" }) do
for _, cache_name in ipairs({
"vscode_packages",
"vscode_standalone",
"snipmate",
"lua",
}) do
for _, path in ipairs(Cache[cache_name].ft_paths[ft] or {}) do
local fmt_name = format(path, clean_name[cache_name])
if fmt_name then
@ -96,7 +101,10 @@ function M.edit_snippet_files(opts)
local all_fts = {}
vim.list_extend(all_fts, util.get_snippet_filetypes())
vim.list_extend(all_fts, loader_util.get_load_fts(vim.api.nvim_get_current_buf()))
vim.list_extend(
all_fts,
loader_util.get_load_fts(vim.api.nvim_get_current_buf())
)
all_fts = util.deduplicate(all_fts)
local filtered_fts = {}

View file

@ -5,7 +5,9 @@ local M = {}
local DupExpandable = {}
-- just pass these through to _expandable.
function DupExpandable:get_docstring() return self._expandable:get_docstring() end
function DupExpandable:get_docstring()
return self._expandable:get_docstring()
end
function DupExpandable:copy()
local copy = self._expandable:copy()
copy.id = self.id
@ -23,18 +25,20 @@ function DupExpandable:matches(...)
end
-- invalidate has to be called on this snippet itself.
function DupExpandable:invalidate() snip_mod.Snippet.invalidate(self) end
function DupExpandable:invalidate()
snip_mod.Snippet.invalidate(self)
end
local dup_mt = {
-- index DupExpandable for own functions, and then the expandable stored in
-- self/t.
__index = function(t,k)
__index = function(t, k)
if DupExpandable[k] then
return DupExpandable[k]
end
return t._expandable[k]
end
end,
}
function M.duplicate_expandable(expandable)
@ -48,19 +52,18 @@ function M.duplicate_expandable(expandable)
}, dup_mt)
end
local DupAddable = {}
function DupAddable:retrieve_all()
return vim.tbl_map(M.duplicate_expandable, self.addable:retrieve_all())
end
local DupAddable_mt = {
__index = DupAddable
__index = DupAddable,
}
function M.duplicate_addable(addable)
return setmetatable({
addable = addable
addable = addable,
}, DupAddable_mt)
end

View file

@ -77,14 +77,18 @@ local function multisnippet_from_nodes(contexts, nodes, opts)
-- create snippet without `context`-fields!
-- compare to `S` (aka `s`, the default snippet-constructor) in
-- `nodes/snippet.lua`.
return multisnippet_from_snippet_obj(contexts, snip_mod._S(
snip_mod.init_snippet_opts(common_snip_opts),
nodes,
return multisnippet_from_snippet_obj(
contexts,
snip_mod._S(
snip_mod.init_snippet_opts(common_snip_opts),
nodes,
common_snip_opts
),
common_snip_opts
), common_snip_opts)
)
end
return {
new_multisnippet = multisnippet_from_nodes,
_raw_ms = multisnippet_from_snippet_obj
_raw_ms = multisnippet_from_snippet_obj,
}

View file

@ -47,7 +47,8 @@ end
-- some values of the snippet are nil by default, list them here so snippets
-- aren't instantiated because of them.
local license_to_nil = { priority = true, snippetType = true, _source = true, filetype = true }
local license_to_nil =
{ priority = true, snippetType = true, _source = true, filetype = true }
-- context and opts are (almost) the same objects as in s(contex, nodes, opts), snippet is a string representing the snippet.
-- opts can aditionally contain the key `parse_fn`, which will be used to parse
@ -69,7 +70,12 @@ local function new(context, snippet, opts)
local sp = vim.tbl_extend(
"error",
{},
context and snip_mod.init_snippet_context(node_util.wrap_context(context), opts) or {},
context
and snip_mod.init_snippet_context(
node_util.wrap_context(context),
opts
)
or {},
snip_mod.init_snippet_opts(opts),
node_util.init_node_opts(opts)
)

View file

@ -256,8 +256,7 @@ function M.add_snippets(snippets, opts)
local snip_type = snip.snippetType ~= nil and snip.snippetType
or opts.type
assert(
snip_type == "autosnippets"
or snip_type == "snippets",
snip_type == "autosnippets" or snip_type == "snippets",
"snippetType must be either 'autosnippets' or 'snippets'"
)

View file

@ -158,7 +158,8 @@ M.loaders = {
exec_lua(
string.format(
[[require("luasnip.loaders.from_vscode").load_standalone({path="%s"})]],
os.getenv("LUASNIP_SOURCE") .. "/tests/data/vscode-standalone.code-snippets"
os.getenv("LUASNIP_SOURCE")
.. "/tests/data/vscode-standalone.code-snippets"
)
)
end,

View file

@ -319,16 +319,20 @@ describe("add_snippets", function()
exec("set ft=c")
feed("iin_lua")
exec_lua("ls.expand()")
screen:expect{grid=[[
screen:expect({
grid = [[
in_lua^ |
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
exec("set ft=lua")
feed("<Cr>in_lua")
exec_lua("ls.expand()")
screen:expect{grid=[[
screen:expect({
grid = [[
in_lua |
expanded in lua^ |
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
end)
end)

View file

@ -496,12 +496,14 @@ describe("loaders:", function()
feed("icodesnippets")
exec_lua("ls.expand()")
screen:expect{grid=[[
screen:expect({
grid = [[
code-snippets!!!^ |
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
end)
it("Respects `scope` (vscode)", function()
@ -509,41 +511,49 @@ describe("loaders:", function()
feed("icc")
exec_lua("ls.expand()")
screen:expect{grid=[[
screen:expect({
grid = [[
cc^ |
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
exec("set ft=c")
feed("<Cr>cc")
exec_lua("ls.expand()")
screen:expect{grid=[[
screen:expect({
grid = [[
cc |
3^ |
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
-- check if invalidation affects the duplicated snippet.
exec_lua([[ls.get_snippets("c")[1]:invalidate()]])
feed("<Cr>cc")
exec_lua("ls.expand()")
screen:expect{grid=[[
screen:expect({
grid = [[
cc |
3 |
cc^ |
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
exec("set ft=cpp")
feed("<Cr>cc")
exec_lua("ls.expand()")
screen:expect{grid=[[
screen:expect({
grid = [[
cc |
3 |
cc |
3^ |
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
end)
end)