Skip to content

Commit

Permalink
move config-defaults to prevent empty config and require-loop (close #…
Browse files Browse the repository at this point in the history
  • Loading branch information
L3MON4D3 committed Dec 2, 2023
1 parent 1e57afa commit c6e964b
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 181 deletions.
162 changes: 2 additions & 160 deletions lua/luasnip/config.lua
Original file line number Diff line number Diff line change
@@ -1,163 +1,6 @@
local types = require("luasnip.util.types")
local ext_util = require("luasnip.util.ext_opts")
local ft_functions = require("luasnip.extras.filetype_functions")
local session = require("luasnip.session")
local util = require("luasnip.util.util")

-- Inserts a insert(1) before all other nodes, decreases node.pos's as indexing is "wrong".
local function modify_nodes(snip)
for i = #snip.nodes, 1, -1 do
snip.nodes[i + 1] = snip.nodes[i]
local node = snip.nodes[i + 1]
if node.pos then
node.pos = node.pos + 1
end
end

local iNode = require("luasnip.nodes.insertNode")
snip.nodes[1] = iNode.I(1)
end

-- stylua: ignore
local lazy_snip_env = {
s = function() return require("luasnip.nodes.snippet").S end,
sn = function() return require("luasnip.nodes.snippet").SN end,
isn = function() return require("luasnip.nodes.snippet").ISN end,
t = function() return require("luasnip.nodes.textNode").T end,
i = function() return require("luasnip.nodes.insertNode").I end,
f = function() return require("luasnip.nodes.functionNode").F end,
c = function() return require("luasnip.nodes.choiceNode").C end,
d = function() return require("luasnip.nodes.dynamicNode").D end,
r = function() return require("luasnip.nodes.restoreNode").R end,
events = function() return require("luasnip.util.events") end,
ai = function() return require("luasnip.nodes.absolute_indexer") end,
extras = function() return require("luasnip.extras") end,
l = function() return require("luasnip.extras").lambda end,
rep = function() return require("luasnip.extras").rep end,
p = function() return require("luasnip.extras").partial end,
m = function() return require("luasnip.extras").match end,
n = function() return require("luasnip.extras").nonempty end,
dl = function() return require("luasnip.extras").dynamic_lambda end,
fmt = function() return require("luasnip.extras.fmt").fmt end,
fmta = function() return require("luasnip.extras.fmt").fmta end,
conds = function() return require("luasnip.extras.expand_conditions") end,
postfix = function() return require("luasnip.extras.postfix").postfix end,
types = function() return require("luasnip.util.types") end,
parse = function() return require("luasnip.util.parser").parse_snippet end,
ms = function() return require("luasnip.nodes.multiSnippet").new_multisnippet end,
}

local defaults = {
-- corresponds to legacy "history=false".
keep_roots = false,
link_roots = false,
link_children = false,

update_events = "InsertLeave",
-- see :h User, event should never be triggered(except if it is `doautocmd`'d)
region_check_events = nil,
delete_check_events = nil,
store_selection_keys = nil, -- Supossed to be the same as the expand shortcut
ext_opts = {
[types.textNode] = {
active = { hl_group = "LuasnipTextNodeActive" },
passive = { hl_group = "LuasnipTextNodePassive" },
visited = { hl_group = "LuasnipTextNodeVisited" },
unvisited = { hl_group = "LuasnipTextNodeUnvisited" },
snippet_passive = { hl_group = "LuasnipTextNodeSnippetPassive" },
},
[types.insertNode] = {
active = { hl_group = "LuasnipInsertNodeActive" },
passive = { hl_group = "LuasnipInsertNodePassive" },
visited = { hl_group = "LuasnipInsertNodeVisited" },
unvisited = { hl_group = "LuasnipInsertNodeUnvisited" },
snippet_passive = {
hl_group = "LuasnipInsertNodeSnippetPassive",
},
},
[types.exitNode] = {
active = { hl_group = "LuasnipExitNodeActive" },
passive = { hl_group = "LuasnipExitNodePassive" },
visited = { hl_group = "LuasnipExitNodeVisited" },
unvisited = { hl_group = "LuasnipExitNodeUnvisited" },
snippet_passive = { hl_group = "LuasnipExitNodeSnippetPassive" },
},
[types.functionNode] = {
active = { hl_group = "LuasnipFunctionNodeActive" },
passive = { hl_group = "LuasnipFunctionNodePassive" },
visited = { hl_group = "LuasnipFunctionNodeVisited" },
unvisited = { hl_group = "LuasnipFunctionNodeUnvisited" },
snippet_passive = {
hl_group = "LuasnipFunctionNodeSnippetPassive",
},
},
[types.snippetNode] = {
active = { hl_group = "LuasnipSnippetNodeActive" },
passive = { hl_group = "LuasnipSnippetNodePassive" },
visited = { hl_group = "LuasnipSnippetNodeVisited" },
unvisited = { hl_group = "LuasnipSnippetNodeUnvisited" },
snippet_passive = {
hl_group = "LuasnipSnippetNodeSnippetPassive",
},
},
[types.choiceNode] = {
active = { hl_group = "LuasnipChoiceNodeActive" },
passive = { hl_group = "LuasnipChoiceNodePassive" },
visited = { hl_group = "LuasnipChoiceNodeVisited" },
unvisited = { hl_group = "LuasnipChoiceNodeUnvisited" },
snippet_passive = {
hl_group = "LuasnipChoiceNodeSnippetPassive",
},
},
[types.dynamicNode] = {
active = { hl_group = "LuasnipDynamicNodeActive" },
passive = { hl_group = "LuasnipDynamicNodePassive" },
visited = { hl_group = "LuasnipDynamicNodeVisited" },
unvisited = { hl_group = "LuasnipDynamicNodeUnvisited" },
snippet_passive = {
hl_group = "LuasnipDynamicNodeSnippetPassive",
},
},
[types.snippet] = {
active = { hl_group = "LuasnipSnippetActive" },
passive = { hl_group = "LuasnipSnippetPassive" },
-- not used!
visited = { hl_group = "LuasnipSnippetVisited" },
unvisited = { hl_group = "LuasnipSnippetUnvisited" },
snippet_passive = { hl_group = "LuasnipSnippetSnippetPassive" },
},
[types.restoreNode] = {
active = { hl_group = "LuasnipRestoreNodeActive" },
passive = { hl_group = "LuasnipRestoreNodePassive" },
visited = { hl_group = "LuasnipRestoreNodeVisited" },
unvisited = { hl_group = "LuasnipRestoreNodeUnvisited" },
snippet_passive = {
hl_group = "LuasnipRestoreNodeSnippetPassive",
},
},
},
ext_base_prio = 200,
ext_prio_increase = 9,
enable_autosnippets = false,
parser_nested_assembler = function(pos, snip)
-- only require here, to prevent some upfront load-cost.
local iNode = require("luasnip.nodes.insertNode")
local cNode = require("luasnip.nodes.choiceNode")

modify_nodes(snip)
snip:init_nodes()
snip.pos = nil

return cNode.C(pos, { snip, iNode.I(nil, { "" }) })
end,
-- Function expected to return a list of filetypes (or empty list)
ft_func = ft_functions.from_filetype,
-- fn(bufnr) -> string[] (filetypes).
load_ft_func = ft_functions.from_filetype_load,
-- globals injected into luasnippet-files.
snip_env = util.lazy_table({}, lazy_snip_env),
loaders_store_source = false,
}
local conf_defaults = require("luasnip.default_config")

local function set_snip_env(target_conf_defaults, user_config)
if not user_config.snip_env then
Expand Down Expand Up @@ -190,12 +33,11 @@ end

-- declare here to use in set_config.
local c
session.config = vim.deepcopy(defaults)

c = {
set_config = function(user_config)
user_config = user_config or {}
local conf = vim.deepcopy(defaults)
local conf = vim.deepcopy(conf_defaults)

-- remove unused highlights from default-ext_opts.
ext_util.clear_invalid(conf.ext_opts)
Expand Down
159 changes: 159 additions & 0 deletions lua/luasnip/default_config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
local types = require("luasnip.util.types")
local lazy_table = require("luasnip.util.lazy_table")

local ft_functions = require("luasnip.extras.filetype_functions")

-- Inserts a insert(1) before all other nodes, decreases node.pos's as indexing is "wrong".
local function modify_nodes(snip)
for i = #snip.nodes, 1, -1 do
snip.nodes[i + 1] = snip.nodes[i]
local node = snip.nodes[i + 1]
if node.pos then
node.pos = node.pos + 1
end
end

local iNode = require("luasnip.nodes.insertNode")
snip.nodes[1] = iNode.I(1)
end

local lazy_snip_env = {
s = function() return require("luasnip.nodes.snippet").S end,
sn = function() return require("luasnip.nodes.snippet").SN end,
isn = function() return require("luasnip.nodes.snippet").ISN end,
t = function() return require("luasnip.nodes.textNode").T end,
i = function() return require("luasnip.nodes.insertNode").I end,
f = function() return require("luasnip.nodes.functionNode").F end,
c = function() return require("luasnip.nodes.choiceNode").C end,
d = function() return require("luasnip.nodes.dynamicNode").D end,
r = function() return require("luasnip.nodes.restoreNode").R end,
events = function() return require("luasnip.util.events") end,
ai = function() return require("luasnip.nodes.absolute_indexer") end,
extras = function() return require("luasnip.extras") end,
l = function() return require("luasnip.extras").lambda end,
rep = function() return require("luasnip.extras").rep end,
p = function() return require("luasnip.extras").partial end,
m = function() return require("luasnip.extras").match end,
n = function() return require("luasnip.extras").nonempty end,
dl = function() return require("luasnip.extras").dynamic_lambda end,
fmt = function() return require("luasnip.extras.fmt").fmt end,
fmta = function() return require("luasnip.extras.fmt").fmta end,
conds = function() return require("luasnip.extras.expand_conditions") end,
postfix = function() return require("luasnip.extras.postfix").postfix end,
types = function() return require("luasnip.util.types") end,
parse = function() return require("luasnip.util.parser").parse_snippet end,
ms = function() return require("luasnip.nodes.multiSnippet").new_multisnippet end,
}

-- stylua: ignore
return {
-- corresponds to legacy "history=false".
keep_roots = false,
link_roots = false,
link_children = false,

update_events = "InsertLeave",
-- see :h User, event should never be triggered(except if it is `doautocmd`'d)
region_check_events = nil,
delete_check_events = nil,
store_selection_keys = nil, -- Supossed to be the same as the expand shortcut
ext_opts = {
[types.textNode] = {
active = { hl_group = "LuasnipTextNodeActive" },
passive = { hl_group = "LuasnipTextNodePassive" },
visited = { hl_group = "LuasnipTextNodeVisited" },
unvisited = { hl_group = "LuasnipTextNodeUnvisited" },
snippet_passive = { hl_group = "LuasnipTextNodeSnippetPassive" },
},
[types.insertNode] = {
active = { hl_group = "LuasnipInsertNodeActive" },
passive = { hl_group = "LuasnipInsertNodePassive" },
visited = { hl_group = "LuasnipInsertNodeVisited" },
unvisited = { hl_group = "LuasnipInsertNodeUnvisited" },
snippet_passive = {
hl_group = "LuasnipInsertNodeSnippetPassive",
},
},
[types.exitNode] = {
active = { hl_group = "LuasnipExitNodeActive" },
passive = { hl_group = "LuasnipExitNodePassive" },
visited = { hl_group = "LuasnipExitNodeVisited" },
unvisited = { hl_group = "LuasnipExitNodeUnvisited" },
snippet_passive = { hl_group = "LuasnipExitNodeSnippetPassive" },
},
[types.functionNode] = {
active = { hl_group = "LuasnipFunctionNodeActive" },
passive = { hl_group = "LuasnipFunctionNodePassive" },
visited = { hl_group = "LuasnipFunctionNodeVisited" },
unvisited = { hl_group = "LuasnipFunctionNodeUnvisited" },
snippet_passive = {
hl_group = "LuasnipFunctionNodeSnippetPassive",
},
},
[types.snippetNode] = {
active = { hl_group = "LuasnipSnippetNodeActive" },
passive = { hl_group = "LuasnipSnippetNodePassive" },
visited = { hl_group = "LuasnipSnippetNodeVisited" },
unvisited = { hl_group = "LuasnipSnippetNodeUnvisited" },
snippet_passive = {
hl_group = "LuasnipSnippetNodeSnippetPassive",
},
},
[types.choiceNode] = {
active = { hl_group = "LuasnipChoiceNodeActive" },
passive = { hl_group = "LuasnipChoiceNodePassive" },
visited = { hl_group = "LuasnipChoiceNodeVisited" },
unvisited = { hl_group = "LuasnipChoiceNodeUnvisited" },
snippet_passive = {
hl_group = "LuasnipChoiceNodeSnippetPassive",
},
},
[types.dynamicNode] = {
active = { hl_group = "LuasnipDynamicNodeActive" },
passive = { hl_group = "LuasnipDynamicNodePassive" },
visited = { hl_group = "LuasnipDynamicNodeVisited" },
unvisited = { hl_group = "LuasnipDynamicNodeUnvisited" },
snippet_passive = {
hl_group = "LuasnipDynamicNodeSnippetPassive",
},
},
[types.snippet] = {
active = { hl_group = "LuasnipSnippetActive" },
passive = { hl_group = "LuasnipSnippetPassive" },
-- not used!
visited = { hl_group = "LuasnipSnippetVisited" },
unvisited = { hl_group = "LuasnipSnippetUnvisited" },
snippet_passive = { hl_group = "LuasnipSnippetSnippetPassive" },
},
[types.restoreNode] = {
active = { hl_group = "LuasnipRestoreNodeActive" },
passive = { hl_group = "LuasnipRestoreNodePassive" },
visited = { hl_group = "LuasnipRestoreNodeVisited" },
unvisited = { hl_group = "LuasnipRestoreNodeUnvisited" },
snippet_passive = {
hl_group = "LuasnipRestoreNodeSnippetPassive",
},
},
},
ext_base_prio = 200,
ext_prio_increase = 9,
enable_autosnippets = false,
parser_nested_assembler = function(pos, snip)
-- only require here, to prevent some upfront load-cost.
local iNode = require("luasnip.nodes.insertNode")
local cNode = require("luasnip.nodes.choiceNode")

modify_nodes(snip)
snip:init_nodes()
snip.pos = nil

return cNode.C(pos, { snip, iNode.I(nil, { "" }) })
end,
-- Function expected to return a list of filetypes (or empty list)
ft_func = ft_functions.from_filetype,
-- fn(bufnr) -> string[] (filetypes).
load_ft_func = ft_functions.from_filetype_load,
-- globals injected into luasnippet-files.
snip_env = lazy_table({}, lazy_snip_env),
loaders_store_source = false,
}
4 changes: 1 addition & 3 deletions lua/luasnip/extras/filetype_functions.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
local util = require("luasnip.util.util")

local function from_cursor_pos()
-- get_parser errors if parser not present (no grammar for language).
local has_parser, parser = pcall(vim.treesitter.get_parser)

if has_parser then
local cursor = util.get_cursor_0ind()
local cursor = require("luasnip.util.util").get_cursor_0ind()
-- assumption: languagetree uses 0-indexed byte-ranges.
return {
parser
Expand Down
3 changes: 2 additions & 1 deletion lua/luasnip/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local util = require("luasnip.util.util")
local lazy_table = require("luasnip.util.lazy_table")
local types = require("luasnip.util.types")
local node_util = require("luasnip.nodes.util")

Expand Down Expand Up @@ -834,7 +835,7 @@ local ls_lazy = {
select_keys = function() return require("luasnip.util.select").select_keys end
}

ls = util.lazy_table({
ls = lazy_table({
expand_or_jumpable = expand_or_jumpable,
expand_or_locally_jumpable = expand_or_locally_jumpable,
locally_jumpable = locally_jumpable,
Expand Down
7 changes: 5 additions & 2 deletions lua/luasnip/session/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ M.last_expand_opts = nil
-- init with false, it will be set by (eg.) ls.jump().
M.jump_active = false

M.config = nil
-- initial value, might be overwritten immediately.
-- No danger of overwriting user-config, since this has to be loaded to allow
-- overwriting.
M.config = require("luasnip.default_config")

M.loaded_fts = {}

function M.get_snip_env()
return M.config and M.config.snip_env
return M.config.snip_env
end

return M
14 changes: 14 additions & 0 deletions lua/luasnip/util/lazy_table.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
return function(lazy_t, lazy_defs)
return setmetatable(lazy_t, {
__index = function(t, k)
local v = lazy_defs[k]
if v then
local v_resolved = v()
rawset(t, k, v_resolved)
return v_resolved
end
return nil
end,
})
end

Loading

0 comments on commit c6e964b

Please sign in to comment.