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 Mar 19, 2023
1 parent 24d84c6 commit de4c039
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 31 deletions.
62 changes: 41 additions & 21 deletions lua/luasnip/extras/snip_location.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,45 @@ local M = {}
local function lua_find_function_call_node_at(bufnr, line)
local has_parser, parser = pcall(vim.treesitter.get_parser, bufnr)
if not has_parser then
log.warn("Cannot determine range of function-call: treesitter-parser for lua not installed.")
log.warn(
"Cannot determine range of function-call: treesitter-parser for lua not installed."
)
return nil
end

local root = parser:parse()[1]:root()
local query = vim.treesitter.parse_query("lua", [[(function_call) @f_call]])
for _, node, _ in query:iter_captures(root, bufnr, line, line+300) do
for _, node, _ in query:iter_captures(root, bufnr, line, line + 300) do
if node:range() == line then
return {node:range()}
return { node:range() }
end
end
log.warn("Cannot determine range of function-call: Query for `(function_call)` starting at line %s did not yield a result.")
log.warn(
"Cannot determine range of function-call: Query for `(function_call)` starting at line %s did not yield a result."
)
return nil
end

local function range_highlight(line_start, line_end, hl_duration_ms)
-- make sure line_end is also visible.
vim.api.nvim_win_set_cursor(0, {line_end, 0})
vim.api.nvim_win_set_cursor(0, {line_start, 0})
vim.api.nvim_win_set_cursor(0, { line_end, 0 })
vim.api.nvim_win_set_cursor(0, { line_start, 0 })

if hl_duration_ms > 0 then
local hl_buf = vim.api.nvim_get_current_buf()

-- highlight snippet for 1000ms
local id = vim.api.nvim_buf_set_extmark(hl_buf, ls.session.ns_id, line_start-1, 0, {
-- one line below, at col 0 => entire last line is highlighted.
end_row = line_end-1+1,
hl_group = "Visual"
})
local id = vim.api.nvim_buf_set_extmark(
hl_buf,
ls.session.ns_id,
line_start - 1,
0,
{
-- one line below, at col 0 => entire last line is highlighted.
end_row = line_end - 1 + 1,
hl_group = "Visual",
}
)
vim.defer_fn(function()
vim.api.nvim_buf_del_extmark(hl_buf, ls.session.ns_id, id)
end, hl_duration_ms)
Expand All @@ -46,7 +56,9 @@ end
local function json_find_snippet_definition(bufnr, snippet_name)
local has_parser, parser = pcall(vim.treesitter.get_parser, bufnr)
if not has_parser then
log.warn("Cannot determine definition of snippet: treesitter-parser for json(c) not installed.")
log.warn(
"Cannot determine definition of snippet: treesitter-parser for json(c) not installed."
)
return nil
end

Expand All @@ -56,14 +68,20 @@ local function json_find_snippet_definition(bufnr, snippet_name)
-- end
-- don't want to pass through if this file is json or jsonc, just use
-- parser-language.
local query = vim.treesitter.parse_query(parser:lang(), ([[
local query = vim.treesitter.parse_query(
parser:lang(),
([[
(pair
key: (string (string_content) @key (#eq? @key "%s"))
) @snippet
]]):format(snippet_name))
]]):format(snippet_name)
)
for id, node, _ in query:iter_captures(root, bufnr) do
if query.captures[id] == "snippet" and node:parent():parent() == root then
return {node:range()}
if
query.captures[id] == "snippet"
and node:parent():parent() == root
then
return { node:range() }
end
end

Expand Down Expand Up @@ -99,9 +117,9 @@ function M.jump_to_snippet(snip, opts)
if source.line then
-- in lua-file, can get region of definition via treesitter.
-- 0: current buffer.
fcall_range = lua_find_function_call_node_at(0, source.line-1)
fcall_range = lua_find_function_call_node_at(0, source.line - 1)
if not fcall_range then
vim.api.nvim_win_set_cursor(0, {source.line, 0})
vim.api.nvim_win_set_cursor(0, { source.line, 0 })
return
end
else
Expand All @@ -123,16 +141,18 @@ function M.jump_to_snippet(snip, opts)

-- 1 is line_from, 3 is line_end.
-- +1 since range is row-0-indexed.
range_highlight(fcall_range[1]+1, fcall_range[3]+1, hl_duration_ms)
range_highlight(fcall_range[1] + 1, fcall_range[3] + 1, hl_duration_ms)

local new_source = Source.from_location(
source.file,
{ line = fcall_range[1]+1, line_end = fcall_range[3]+1 })
{ line = fcall_range[1] + 1, line_end = fcall_range[3] + 1 }
)
Source.set(snip, new_source)
end

function M.jump_to_active_snippet(opts)
local active_node = require("luasnip.session").current_nodes[vim.api.nvim_get_current_buf()]
local active_node =
require("luasnip.session").current_nodes[vim.api.nvim_get_current_buf()]
if not active_node then
print("No active snippet.")
return
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 @@ -99,10 +99,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 @@ -156,7 +160,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

0 comments on commit de4c039

Please sign in to comment.