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 Aug 27, 2022
1 parent fa55a9a commit 5fbebf6
Show file tree
Hide file tree
Showing 19 changed files with 312 additions and 168 deletions.
20 changes: 12 additions & 8 deletions lua/luasnip/loaders/from_snipmate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,18 @@ local function parse_snipmate(buffer, filename)
end

body = table.concat(body, "\n")
local snip = sp({
trig = prefix,
dscr = description,
wordTrig = true,
priority = snipmate_opts.priority,
}, body, {
parse_fn = snipmate_parse_fn
})
local snip = sp(
{
trig = prefix,
dscr = description,
wordTrig = true,
priority = snipmate_opts.priority,
},
body,
{
parse_fn = snipmate_parse_fn,
}
)
table.insert(snippets[snippet_type], snip)
end

Expand Down
2 changes: 1 addition & 1 deletion lua/luasnip/nodes/dynamicNode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function DynamicNode:get_static_text()
if self.snip then
return self.snip:get_static_text()
else
return {""}
return { "" }
end
end
end
Expand Down
10 changes: 8 additions & 2 deletions lua/luasnip/nodes/functionNode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,16 @@ function FunctionNode:set_dependents()
-- appends the key.
-- Maybe this is stupid??
if rawget(arg, "type") ~= nil then
dict:set(vim.list_extend({arg}, append_list), self)
dict:set(vim.list_extend({ arg }, append_list), self)
elseif arg.absolute_insert_position then
-- copy, list_extend mutates.
dict:set(vim.list_extend(vim.deepcopy(arg.absolute_insert_position), append_list), self)
dict:set(
vim.list_extend(
vim.deepcopy(arg.absolute_insert_position),
append_list
),
self
)
end
end
end
Expand Down
9 changes: 7 additions & 2 deletions lua/luasnip/nodes/node.lua
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ local function find_dependents(self, position_self, dict)
vim.list_extend(nodes, dict:find_all(position_self, "dependent") or {})
position_self[#position_self] = nil

vim.list_extend(nodes, dict:find_all({self, "dependents"}, "dependent") or {})
vim.list_extend(
nodes,
dict:find_all({ self, "dependents" }, "dependent") or {}
)

return nodes
end
Expand Down Expand Up @@ -251,7 +254,9 @@ local function get_args(node, get_text_func_name)
-- the node is not (yet, maybe) visible.
return nil
end
local arg_table = node.parent.snippet.dependents_dict:get(arg.absolute_insert_position)
local arg_table = node.parent.snippet.dependents_dict:get(
arg.absolute_insert_position
)
if not arg_table then
return nil
end
Expand Down
2 changes: 1 addition & 1 deletion lua/luasnip/nodes/snippetProxy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ 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}
local license_to_nil = { priority = 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 Down
2 changes: 1 addition & 1 deletion lua/luasnip/nodes/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ local function make_args_absolute(args, parent_insert_position, target)
-- parent.
local t = vim.deepcopy(parent_insert_position)
table.insert(t, arg)
target[i] = {absolute_insert_position = t}
target[i] = { absolute_insert_position = t }
else
-- insert node or absolute_indexer itself, node's absolute_insert_position may be nil, check for that during
-- usage.
Expand Down
5 changes: 2 additions & 3 deletions lua/luasnip/util/directed_graph.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ Vertex.__index = Vertex
local function new_graph()
return setmetatable({
-- all vertices of this graph.
vertices = {}
vertices = {},
}, DirectedGraph)
end
local function new_vertex()
return setmetatable({
-- vertices this vertex has an edge from/to.
-- map[vert -> bool]
incoming_edge_verts = {},
outgoing_edge_verts = {}
outgoing_edge_verts = {},
}, Vertex)
end

Expand Down Expand Up @@ -125,7 +125,6 @@ function DirectedGraph:topological_sort()
-- function (alternative: maybe return indices in graph.vertices?).
table.insert(sorting, original_vert[v])


-- find vertices which, if v is removed from graph, have no more incoming edges.
-- Those are sources after v is removed.
for outgoing_edge_vert, _ in pairs(v.outgoing_edge_verts) do
Expand Down
6 changes: 3 additions & 3 deletions lua/luasnip/util/environ.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ _env_namespace("", builtin_namespace)
function Environ.env_namespace(name, opts)
assert(
name:match("^[a-zA-Z][a-zA-Z0-9]*$"),
(
"You can't create a namespace with name '%s' it has to contain only and at least a non alpha-numeric character"
):format(name)
("You can't create a namespace with name '%s' it has to contain only and at least a non alpha-numeric character"):format(
name
)
)
assert(
not builtin_namespace.builtin_ns[name],
Expand Down
15 changes: 11 additions & 4 deletions lua/luasnip/util/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,17 @@ return {
eval_vim_dynamic = function(vimstring)
return function()
-- 'echo'd string is returned to lua.
return sn(nil, {t(vim.split(
vim.api.nvim_exec("echo " .. vimstring, true),
"\n"
))})
return sn(
nil,
{
t(
vim.split(
vim.api.nvim_exec("echo " .. vimstring, true),
"\n"
)
),
}
)
end
end,
copy = function(args)
Expand Down
30 changes: 18 additions & 12 deletions lua/luasnip/util/parser/ast_parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ local function var_func(ast)
return variable_default
else
-- lines might still just be {} (#lines == 0).
lines = {""}
lines = { "" }
end
end

Expand Down Expand Up @@ -124,7 +124,10 @@ local function placeholder_func(_, parent, _, placeholder_snip)
return sNode.SN(nil, iNode.I(1, iText))
end

return sNode.SN(nil, session.config.parser_nested_assembler(1, placeholder_snip))
return sNode.SN(
nil,
session.config.parser_nested_assembler(1, placeholder_snip)
)
end

---If this tabstop-node (CHOICE, TABSTOP or PLACEHOLDER) is a copy of another,
Expand All @@ -135,7 +138,8 @@ local function tabstop_node_copy_inst(ast)
local existing_tabstop_ast_node = ast.copies
if existing_tabstop_ast_node then
-- this tabstop is a mirror of an already-parsed tabstop/placeholder.
ast.parsed = fNode.F(copy_func(ast), { existing_tabstop_ast_node.parsed })
ast.parsed =
fNode.F(copy_func(ast), { existing_tabstop_ast_node.parsed })
return true
end
return false
Expand Down Expand Up @@ -186,7 +190,7 @@ local to_node_funcs = {
local snip = sNode.SN(1, ast2luasnip_nodes(ast.children))
node = dNode.D(ast.tabstop, placeholder_func, {}, {
-- pass snip here, again to preserve references to other tables.
user_args = {snip}
user_args = { snip },
})
end

Expand Down Expand Up @@ -223,13 +227,13 @@ local to_node_funcs = {

-- just wrap it for more uniformity.
if type(var_value) == "string" then
var_value = {var_value}
var_value = { var_value }
end

if
(#var_value == 1 and #var_value[1] == 0)
or #var_value == 0 then

or #var_value == 0
then
-- var is empty, default is inserted.
-- if no default, it's not interactive (an empty string is inserted).
return default and default:is_interactive()
Expand Down Expand Up @@ -262,16 +266,15 @@ local to_node_funcs = {
-- just needs to be documented a bit.
--
-- `default` is potentially nil.
user_args = {default}
user_args = { default },
})
d.is_interactive = is_interactive_fn

-- if the variable is preceded by \n<indent>, the indent is applied to
-- all lines of the variable (important for eg. TM_SELECTED_TEXT).
if ast.previous_text ~= nil and #ast.previous_text > 1 then
local last_line_indent = ast.previous_text[#ast.previous_text]:match(
"^%s+$"
)
local last_line_indent =
ast.previous_text[#ast.previous_text]:match("^%s+$")
if last_line_indent then
-- TM_SELECTED_TEXT contains the indent of the selected
-- snippets, which leads to correct indentation if the
Expand Down Expand Up @@ -333,7 +336,10 @@ function M.to_luasnip_nodes(ast, state)
ast_utils.give_vars_previous_text(ast)

local ast_nodes_topsort = ast_utils.parse_order(ast)
assert(ast_nodes_topsort, "cannot represent snippet: contains circular dependencies")
assert(
ast_nodes_topsort,
"cannot represent snippet: contains circular dependencies"
)
for _, node in ipairs(ast_nodes_topsort) do
to_node(node, state)
end
Expand Down
28 changes: 11 additions & 17 deletions lua/luasnip/util/parser/ast_utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,9 @@ function M.fix_zero(ast)
or (
zn
and not is_copied
and (
zn.type == types.TABSTOP or
(zn.type == types.PLACEHOLDER and text_only_placeholder(zn))
)
and (zn.type == types.TABSTOP or (zn.type == types.PLACEHOLDER and text_only_placeholder(
zn
)))
and ast.children[ast_child_with_0_indx] == zn
)
then
Expand All @@ -241,10 +240,7 @@ function M.fix_zero(ast)

-- insert $0 as a direct child to snippet, just behind the original $0/the
-- node containing it.
table.insert(
ast.children,
ast_child_with_0_indx + 1,
Ast.tabstop(0))
table.insert(ast.children, ast_child_with_0_indx + 1, Ast.tabstop(0))
end

---This function identifies which tabstops/placeholder/choices are copies, and
Expand Down Expand Up @@ -352,10 +348,8 @@ end

function M.apply_transform(transform)
if jsregexp_ok then
local reg_compiled = jsregexp.compile(
transform.pattern,
transform.option
)
local reg_compiled =
jsregexp.compile(transform.pattern, transform.option)
-- can be passed to functionNode!
return function(lines)
-- luasnip expects+passes lines as list, but regex needs one string.
Expand All @@ -370,13 +364,13 @@ function M.apply_transform(transform)
for _, match in ipairs(matches) do
-- begin_ind and end_ind are inclusive.
transformed = transformed
.. lines:sub(prev_match_end+1, match.begin_ind - 1)
.. lines:sub(prev_match_end + 1, match.begin_ind - 1)
.. apply_transform_format(transform.format, match.groups)

-- end-inclusive
prev_match_end = match.end_ind
end
transformed = transformed .. lines:sub(prev_match_end+1, #lines)
transformed = transformed .. lines:sub(prev_match_end + 1, #lines)

return vim.split(transformed, "\n")
end
Expand All @@ -396,7 +390,7 @@ end
---The text is accessible as ast_node.previous_text, a string[].
---@param ast table: the AST.
function M.give_vars_previous_text(ast)
local last_text = {""}
local last_text = { "" }
-- important: predicate_ltr_nodes visits the node in the order they appear,
-- textually, in the snippet.
-- This is necessary to actually ensure the variables actually get the text just in front of them.
Expand All @@ -420,7 +414,7 @@ function M.give_vars_previous_text(ast)
node.previous_text = last_text
else
-- reset last_text when a different node is encountered.
last_text = {""}
last_text = { "" }
end
-- continue..
return false
Expand All @@ -432,7 +426,7 @@ end
---variable is just some text, inserts its default, or its variable-name has to
---be deferred to runtime.
---So, each variable is a dynamicNode, and needs a tabstop.
---In vscode the variables are visited
---In vscode the variables are visited
--- 1) after all other tabstops/placeholders/choices and
--- 2) in the order they appear in the snippet-body.
---We mimic this behaviour.
Expand Down
7 changes: 4 additions & 3 deletions lua/luasnip/util/parser/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function M.parse_snippet(context, body, opts)
end

local nodes = ast_parser.to_luasnip_nodes(ast, {
var_functions = opts.variables
var_functions = opts.variables,
})

if type(context) == "number" then
Expand Down Expand Up @@ -81,7 +81,8 @@ local function backticks_to_variable(body)
.. "}"

-- don't include backticks in vimscript.
var_map[varname] = functions.eval_vim_dynamic(body:sub(from + 1, to - 1))
var_map[varname] =
functions.eval_vim_dynamic(body:sub(from + 1, to - 1))
processed_to = to + 1
variable_indx = variable_indx + 1
end
Expand All @@ -100,7 +101,7 @@ function M.parse_snipmate(context, body, opts)
opts.variables = {}
for name, fn in pairs(new_vars) do
-- created dynamicNode is not interactive.
opts.variables[name] = {fn, util.no}
opts.variables[name] = { fn, util.no }
end
return M.parse_snippet(context, body, opts)
end
Expand Down
6 changes: 5 additions & 1 deletion lua/luasnip/util/parser/neovim_ast.lua
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,11 @@ function M.merge_adjacent_text(ast)
-- When we do this is not important, since it does not change the TEXT-nodes, here is just comfortable.
M.merge_adjacent_text(child)

if child.type == node_type.TEXT and last_child and last_child.type == node_type.TEXT then
if
child.type == node_type.TEXT
and last_child
and last_child.type == node_type.TEXT
then
last_child.raw = last_child.raw .. child.raw
last_child.esc = last_child.esc .. child.esc
else
Expand Down
Loading

0 comments on commit 5fbebf6

Please sign in to comment.