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 1, 2023
1 parent ecca1d3 commit c034587
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 40 deletions.
7 changes: 5 additions & 2 deletions lua/luasnip/extras/snip_location.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ local function json_find_snippet_definition(bufnr, extension, snippet_name)
local root = parser:parse()[1]:root()
-- don't want to pass through whether this file is json or jsonc, just use
-- parser-language.
local query = tsquery_parse(parser:lang(), ([[
local query = tsquery_parse(
parser:lang(),
([[
(pair
key: (string (string_content) @key (#eq? @key "%s"))
) @snippet
Expand Down Expand Up @@ -134,7 +136,8 @@ function M.jump_to_snippet(snip, opts)
elseif vim.api.nvim_buf_get_name(0):match("%.jsonc?$") then
local extension = vim.api.nvim_buf_get_name(0):match("jsonc?$")
local ok
ok, fcall_range = pcall(json_find_snippet_definition, 0, extension, snip.name)
ok, fcall_range =
pcall(json_find_snippet_definition, 0, extension, snip.name)
if not ok then
print(
"Could not determine range of snippet-definition: "
Expand Down
18 changes: 13 additions & 5 deletions lua/luasnip/loaders/from_lua.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ local M = {}
-- Instead, it is inserted into the global environment before a luasnippet-file
-- is loaded, and removed from it immediately when this is done
local function get_loaded_file_debuginfo()
-- we can skip looking at the first four stackframes, since
-- we can skip looking at the first four stackframes, since
-- 1 is this function
-- 2 is the snippet-constructor
-- ... (here anything is going on, could be 0 stackframes, could be many)
Expand All @@ -51,7 +51,7 @@ local function get_loaded_file_debuginfo()
repeat
current_call_depth = current_call_depth + 1
debuginfo = debug.getinfo(current_call_depth, "n")
until (debuginfo.name == "_luasnip_load_files")
until debuginfo.name == "_luasnip_load_files"

-- ret is stored into a local, and not returned immediately to prevent tail
-- call optimization, which seems to invalidate the stackframe-numbers
Expand Down Expand Up @@ -112,10 +112,14 @@ local function _luasnip_load_files(ft, files, add_opts)
-- Since this function has to reach the snippet-constructor, and fenvs
-- aren't inherited by called functions, we have to set it in the global
-- environment.
_G.__luasnip_get_loaded_file_frame_debuginfo = util.ternary(session.config.loaders_store_source, get_loaded_file_debuginfo, nil)
_G.__luasnip_get_loaded_file_frame_debuginfo = util.ternary(
session.config.loaders_store_source,
get_loaded_file_debuginfo,
nil
)
local run_ok, file_snippets, file_autosnippets = pcall(func)
-- immediately nil it.
_G.__luasnip_get_loaded_file_frame_debuginfo = nil
_G.__luasnip_get_loaded_file_frame_debuginfo = nil

if not run_ok then
log.error("Failed to execute\n: %s", file, file_snippets)
Expand Down Expand Up @@ -169,7 +173,11 @@ end

function M._load_lazy_loaded_ft(ft)
for _, load_call_paths in ipairs(cache.lazy_load_paths) do
_luasnip_load_files(ft, load_call_paths[ft] or {}, load_call_paths.add_opts)
_luasnip_load_files(
ft,
load_call_paths[ft] or {},
load_call_paths.add_opts
)
end
end

Expand Down
5 changes: 4 additions & 1 deletion lua/luasnip/loaders/from_snipmate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ local function parse_snipmate(buffer, filename)
}
)
if session.config.loaders_store_source then
snip._source = source.from_location(filename, {line = snip_begin_line, line_end = i-1})
snip._source = source.from_location(
filename,
{ line = snip_begin_line, line_end = i - 1 }
)
end
table.insert(snippets[snippet_type], snip)
end
Expand Down
3 changes: 2 additions & 1 deletion lua/luasnip/nodes/snippet.lua
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ local function S(context, nodes, opts)

if __luasnip_get_loaded_file_frame_debuginfo ~= nil then
-- this snippet is being lua-loaded, and the source should be recorded.
snip._source = source.from_debuginfo(__luasnip_get_loaded_file_frame_debuginfo())
snip._source =
source.from_debuginfo(__luasnip_get_loaded_file_frame_debuginfo())
end

return snip
Expand Down
9 changes: 6 additions & 3 deletions lua/luasnip/session/snippet_collection/source.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@ local M = {}

function M.from_debuginfo(debuginfo)
assert(debuginfo.source, "debuginfo contains source")
assert(debuginfo.source:match("^@"), "debuginfo-source is a file: " .. debuginfo.source)
assert(
debuginfo.source:match("^@"),
"debuginfo-source is a file: " .. debuginfo.source
)

return {
-- omit leading '@'.
file = debuginfo.source:sub(2),
line = debuginfo.currentline
line = debuginfo.currentline,
}
end

function M.from_location(file, opts)
assert(file, "source needs file")
opts = opts or {}

return {file = file, line = opts.line, line_end = opts.line_end}
return { file = file, line = opts.line, line_end = opts.line_end }
end

function M.set(snippet, source)
Expand Down
10 changes: 5 additions & 5 deletions tests/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ function M.session_setup_luasnip(opts)
opts = opts or {}
local no_snip_globals = opts.no_snip_globals ~= nil and opts.no_snip_globals
or false
local setup_extend = opts.setup_extend ~= nil and opts.setup_extend
or {}
local setup_extend = opts.setup_extend ~= nil and opts.setup_extend or {}
local setup_parsers
if opts.setup_parsers ~= nil then
setup_parsers = opts.setup_parsers
Expand All @@ -50,7 +49,7 @@ function M.session_setup_luasnip(opts)
if setup_parsers then
-- adding the lua-parser, is either a nop or adds the parser on
-- versions where it does not exist by default.
exec_lua[[
exec_lua([[
ts_lang_add =
(vim.treesitter.language and vim.treesitter.language.add)
and function(lang, path)
Expand All @@ -63,10 +62,11 @@ function M.session_setup_luasnip(opts)
ts_lang_add("json", os.getenv("LUASNIP_SOURCE") .. "/tests/parsers/json.so")
ts_lang_add("jsonc", os.getenv("LUASNIP_SOURCE") .. "/tests/parsers/jsonc.so")
]]
]])
end

helpers.exec_lua([[
helpers.exec_lua(
[[
-- MYVIMRC might not be set when nvim is loaded like this.
vim.env.MYVIMRC = "/.vimrc"
Expand Down
63 changes: 40 additions & 23 deletions tests/integration/source_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe("loaders:", function()
"expands? jumps? $1 $2 !" |
] |
}, |
Could not determine ran...help treesitter-parsers |]]
Could not determine ran...help treesitter-parsers |]],
})
end)

Expand Down Expand Up @@ -135,63 +135,80 @@ describe("loaders:", function()
it("vscode: correctly highlights definition if parser installed", function()
ls_helpers.session_setup_luasnip({
no_snip_globals = true,
setup_extend = {loaders_store_source = true},
setup_parsers = true })
setup_extend = { loaders_store_source = true },
setup_parsers = true,
})

ls_helpers.loaders["vscode(rtp)"]()

feed("iall1")
exec_lua("ls.expand()")
screen:expect{grid=[[
screen:expect({
grid = [[
expands? jumps? ^ ! |
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
{2:-- INSERT --} |]],
})
feed("<esc>")
exec_lua([[require("luasnip.extras.snip_location").jump_to_active_snippet()]])
screen:expect{grid=[[
exec_lua(
[[require("luasnip.extras.snip_location").jump_to_active_snippet()]]
)
screen:expect({
grid = [[
{ |
{3: ^ "snip1": {} |
{3: "prefix": "all1",} |
{3: "body": [} |
{3: "expands? jumps? $1 $2 !"} |
{3: ]} |
{3: },} |
|]]}
|]],
})
end)

it("lua: highlights definition (should always work, the lua-parser is installed by default).", function()
ls_helpers.session_setup_luasnip({
no_snip_globals = true,
setup_extend = {loaders_store_source = true},
setup_parsers = true })
ls_helpers.loaders["lua(rtp)"]()
it(
"lua: highlights definition (should always work, the lua-parser is installed by default).",
function()
ls_helpers.session_setup_luasnip({
no_snip_globals = true,
setup_extend = { loaders_store_source = true },
setup_parsers = true,
})
ls_helpers.loaders["lua(rtp)"]()

feed("iall1")
exec_lua("ls.expand()")
screen:expect{grid=[[
feed("iall1")
exec_lua("ls.expand()")
screen:expect({
grid = [[
expands? jumps? ^ ! |
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{2:-- INSERT --} |]]}
feed("<esc>")
exec_lua([[require("luasnip.extras.snip_location").jump_to_active_snippet()]])
screen:expect{grid=[[
{2:-- INSERT --} |]],
})
feed("<esc>")
exec_lua(
[[require("luasnip.extras.snip_location").jump_to_active_snippet()]]
)
screen:expect({
grid = [[
return { |
{3: ^ s("all1", fmt("expands? jumps? {} {} !", {}|
{3: i(1), i(2) })),} |
}, { |
parse("auto???", "autotriggered????????"),|
} |
{0:~ }|
|]]}
end)
|]],
})
end
)
end)

0 comments on commit c034587

Please sign in to comment.