Skip to content

Commit

Permalink
Format with stylua
Browse files Browse the repository at this point in the history
  • Loading branch information
L3MON4D3 authored and github-actions[bot] committed May 29, 2023
1 parent dfd195f commit e08e894
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 55 deletions.
9 changes: 6 additions & 3 deletions lua/luasnip/extras/snip_location.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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: "
Expand Down
47 changes: 30 additions & 17 deletions lua/luasnip/loaders/from_vscode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
16 changes: 12 additions & 4 deletions lua/luasnip/loaders/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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 = {}
Expand Down
17 changes: 10 additions & 7 deletions lua/luasnip/nodes/duplicate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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

Expand Down
14 changes: 9 additions & 5 deletions lua/luasnip/nodes/multiSnippet.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
10 changes: 8 additions & 2 deletions lua/luasnip/nodes/snippetProxy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
)
Expand Down
3 changes: 1 addition & 2 deletions lua/luasnip/session/snippet_collection/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'"
)

Expand Down
3 changes: 2 additions & 1 deletion tests/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 8 additions & 4 deletions tests/integration/add_snippets_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading

0 comments on commit e08e894

Please sign in to comment.