Skip to content

Commit

Permalink
Breaking Change: split ext_opts into node_ext_opts and child_ext_opts.
Browse files Browse the repository at this point in the history
The former is applied to this specific node, the latter to children of
that snippetNode.
Also add merge_{node,child}_ext_opts for control merge-behaviour of eg.
a nodes ext_opts with that of the surrounding snippet (both default to
true).
This is a breaking change because function/dynamicNode no longer have
the variable number of user_args as their last arg. That is now a table,
`opts`, which may contain the `user_args`.

```lua
d(pos, func, argnodes, user_arg1, user_arg2)

-- becomes

d(pos, func, argnodes, {user_args = {user_arg1, user_arg2}})

-- and likewise for functionNode.
```
  • Loading branch information
L3MON4D3 committed Mar 8, 2022
1 parent 36211f2 commit 48a79b7
Show file tree
Hide file tree
Showing 12 changed files with 321 additions and 184 deletions.
11 changes: 6 additions & 5 deletions lua/luasnip/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ local defaults = {
},
},
ext_base_prio = 200,
ext_prio_increase = 7,
ext_prio_increase = 9,
enable_autosnippets = false,
-- default applied in util.parser, requires iNode, cNode
-- (Dependency cycle if here).
Expand All @@ -90,10 +90,11 @@ c = {

-- remove unused highlights from default-ext_opts.
ext_util.clear_invalid(conf.ext_opts)
ext_util.complete(conf.ext_opts)
user_config.ext_opts = user_config.ext_opts or {}
ext_util.complete(user_config.ext_opts)
ext_util.extend(user_config.ext_opts, conf.ext_opts)
conf.ext_opts = ext_util.child_complete(conf.ext_opts)
user_config.ext_opts = ext_util.child_complete(
user_config.ext_opts or {}
)
ext_util.child_extend(user_config.ext_opts, conf.ext_opts)

for k, v in pairs(user_config) do
conf[k] = v
Expand Down
12 changes: 6 additions & 6 deletions lua/luasnip/nodes/choiceNode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ local function C(pos, choices, opts)
dependents = {},
-- default to true.
restore_cursor = opts.restore_cursor,
})
}, opts)
c:init_nodes()
return c
end
Expand Down Expand Up @@ -112,7 +112,7 @@ function ChoiceNode:put_initial(pos)
local mark_opts = vim.tbl_extend("keep", {
right_gravity = false,
end_right_gravity = false,
}, self.parent.ext_opts[self.active_choice.type].passive)
}, self.active_choice.ext_opts.passive)

self.active_choice.mark = mark(old_pos, pos, mark_opts)
self.visible = true
Expand All @@ -131,7 +131,7 @@ function ChoiceNode:expand_tabs(tabwidth)
end

function ChoiceNode:input_enter()
self.mark:update_opts(self.parent.ext_opts[self.type].active)
self.mark:update_opts(self.ext_opts.active)
self.parent:enter_node(self.indx)

self.prev_choice_node = session.active_choice_node
Expand All @@ -144,7 +144,7 @@ end
function ChoiceNode:input_leave()
self:event(events.leave)

self.mark:update_opts(self.parent.ext_opts[self.type].passive)
self.mark:update_opts(self.ext_opts.passive)
self:update_dependents()
session.active_choice_node = self.prev_choice_node
self.active = false
Expand Down Expand Up @@ -240,7 +240,7 @@ function ChoiceNode:set_choice(choice, current_node)
self.active_choice = choice

self.active_choice.mark = self.mark:copy_pos_gravs(
vim.deepcopy(self.parent.ext_opts[self.active_choice.type].passive)
vim.deepcopy(self.active_choice.ext_opts.passive)
)
self.active_choice:put_initial(self.mark:pos_begin_raw())

Expand Down Expand Up @@ -322,7 +322,7 @@ function ChoiceNode:set_mark_rgrav(rgrav_beg, rgrav_end)
end

function ChoiceNode:set_ext_opts(name)
self.mark:update_opts(self.parent.ext_opts[self.type][name])
self.mark:update_opts(self.ext_opts[name])
self.active_choice:set_ext_opts(name)
end

Expand Down
36 changes: 17 additions & 19 deletions lua/luasnip/nodes/dynamicNode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@ local conf = require("luasnip.config")
local FunctionNode = require("luasnip.nodes.functionNode").FunctionNode
local SnippetNode = require("luasnip.nodes.snippet").SN

local function D(pos, fn, args, ...)
local function D(pos, fn, args, opts)
opts = opts or {}

return DynamicNode:new({
pos = pos,
fn = fn,
args = node_util.wrap_args(args),
type = types.dynamicNode,
mark = nil,
user_args = { ... },
user_args = opts.user_args or {},
dependents = {},
active = false,
})
}, opts)
end

function DynamicNode:input_enter()
self.active = true
self.mark:update_opts(self.parent.ext_opts[self.type].active)
self.mark:update_opts(self.ext_opts.active)

self:event(events.enter)
end
Expand All @@ -34,7 +36,7 @@ function DynamicNode:input_leave()

self:update_dependents()
self.active = false
self.mark:update_opts(self.parent.ext_opts[self.type].passive)
self.mark:update_opts(self.ext_opts.passive)
end

local function snip_init(self, snip)
Expand Down Expand Up @@ -159,23 +161,19 @@ function DynamicNode:update()
tmp.next = self
tmp.prev = self

tmp.ext_opts = tmp.ext_opts
or ext_util.set_abs_prio(
vim.deepcopy(self.parent.ext_opts),
conf.config.ext_prio_increase
)
tmp.snippet = self.parent.snippet
tmp.mark = self.mark:copy_pos_gravs(
vim.deepcopy(self.parent.ext_opts[types.snippetNode].passive)
)

tmp:resolve_child_ext_opts()
tmp:resolve_node_ext_opts()
tmp:subsnip_init()

tmp.mark = self.mark:copy_pos_gravs(vim.deepcopy(tmp.ext_opts.passive))
tmp.dynamicNode = self
tmp.update_dependents = function(node)
node:_update_dependents()
node.dynamicNode:update_dependents()
end

tmp:subsnip_init()

tmp:init_positions(self.snip_absolute_position)
tmp:init_insert_positions(self.snip_absolute_insert_position)

Expand Down Expand Up @@ -275,6 +273,8 @@ function DynamicNode:update_static()
node.dynamicNode:update_dependents_static()
end

tmp:resolve_child_ext_opts()
tmp:resolve_node_ext_opts()
tmp:subsnip_init()

tmp:init_positions(self.snip_absolute_position)
Expand Down Expand Up @@ -317,7 +317,7 @@ function DynamicNode:exit()
end

function DynamicNode:set_ext_opts(name)
self.mark:update_opts(self.parent.ext_opts[self.type][name])
self.mark:update_opts(self.ext_opts[name])
-- might not have been generated (missing nodes).
if self.snip then
self.snip:set_ext_opts(name)
Expand All @@ -336,9 +336,7 @@ function DynamicNode:update_restore()
-- prevent entering the uninitialized snip in enter_node in a few lines.
local tmp = self.stored_snip

tmp.mark = self.mark:copy_pos_gravs(
vim.deepcopy(self.parent.ext_opts[types.snippetNode].passive)
)
tmp.mark = self.mark:copy_pos_gravs(vim.deepcopy(tmp.ext_opts.passive))
self.parent:enter_node(self.indx)
tmp:put_initial(self.mark:pos_begin_raw())
tmp:update_restore()
Expand Down
8 changes: 5 additions & 3 deletions lua/luasnip/nodes/functionNode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ local types = require("luasnip.util.types")
local events = require("luasnip.util.events")
local tNode = require("luasnip.nodes.textNode").textNode

local function F(fn, args, ...)
local function F(fn, args, opts)
opts = opts or {}

return FunctionNode:new({
fn = fn,
args = node_util.wrap_args(args),
type = types.functionNode,
mark = nil,
user_args = { ... },
})
user_args = opts.user_args or {},
}, opts)
end

FunctionNode.input_enter = tNode.input_enter
Expand Down
10 changes: 5 additions & 5 deletions lua/luasnip/nodes/insertNode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local config = require("luasnip.config")
local types = require("luasnip.util.types")
local events = require("luasnip.util.events")

local function I(pos, static_text)
local function I(pos, static_text, opts)
static_text = util.wrap_value(static_text)
if pos == 0 then
return ExitNode:new({
Expand All @@ -16,7 +16,7 @@ local function I(pos, static_text)
type = types.exitNode,
-- will only be needed for 0-node, -1-node isn't set with this.
ext_gravities_active = { false, false },
})
}, opts)
else
return InsertNode:new({
pos = pos,
Expand All @@ -25,7 +25,7 @@ local function I(pos, static_text)
dependents = {},
type = types.insertNode,
inner_active = false,
})
}, opts)
end
end

Expand Down Expand Up @@ -90,7 +90,7 @@ function ExitNode:update_dependents_static() end
function ExitNode:update_all_dependents_static() end

function InsertNode:input_enter(no_move)
self.mark:update_opts(self.parent.ext_opts[self.type].active)
self.mark:update_opts(self.ext_opts.active)
if not no_move then
self.parent:enter_node(self.indx)

Expand Down Expand Up @@ -186,7 +186,7 @@ function InsertNode:input_leave()
self:event(events.leave)

self:update_dependents()
self.mark:update_opts(self.parent.ext_opts[self.type].passive)
self.mark:update_opts(self.ext_opts.passive)
end

function InsertNode:exit()
Expand Down
53 changes: 47 additions & 6 deletions lua/luasnip/nodes/node.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
local session = require("luasnip.session")
local util = require("luasnip.util.util")
local types = require("luasnip.util.types")
local node_util = require("luasnip.nodes.util")
local ext_util = require("luasnip.util.ext_opts")
local events = require("luasnip.util.events")
local conf = require("luasnip.config")

local Node = {}

function Node:new(o)
function Node:new(o, opts)
o = o or {}
setmetatable(o, self)
self.__index = self

-- visible is true if the node is visible on-screen, during normal
-- expansion, static_visible is needed for eg. get_static_text, where
-- argnodes in inactive choices will happily provide their static text,
-- which leads to inaccurate docstrings.
o.visible = false
o.static_visible = false
o.old_text = {}
-- override existing keys, might be necessary due to double-init from
-- snippetProxy, but shouldn't hurt.
o = vim.tbl_extend("force", o, node_util.init_node_opts(opts or {}))

setmetatable(o, self)
self.__index = self

return o
end

Expand Down Expand Up @@ -57,7 +66,7 @@ function Node:put_initial(pos)
end

function Node:input_enter(_)
self.mark:update_opts(self.parent.ext_opts[self.type].active)
self.mark:update_opts(self.ext_opts.active)

self:event(events.enter)
end
Expand Down Expand Up @@ -137,7 +146,7 @@ end
function Node:input_leave()
self:event(events.leave)

self.mark:update_opts(self.parent.ext_opts[self.type].passive)
self.mark:update_opts(self.ext_opts.passive)
end

local function find_dependents(position_self, dict)
Expand Down Expand Up @@ -257,7 +266,7 @@ function Node:get_static_args()
end

function Node:set_ext_opts(name)
self.mark:update_opts(self.parent.ext_opts[self.type][name])
self.mark:update_opts(self.ext_opts[name])
end

-- for insert,functionNode.
Expand Down Expand Up @@ -307,6 +316,38 @@ function Node:static_init()
self.static_visible = true
end

-- resolve_*node*_ext_opts because snippet(Node)s have child_ext_opts, which
-- also have to be resolved.
-- This function generates a nodes ext_opts (those actually used in highlighting).
function Node:resolve_node_ext_opts(base_prio, parent_ext_opts)
-- if self.parent then
-- local ok, res = pcall(function()
-- local e = self.parent.effective_child_ext_opts[self.type]
-- end)
-- if not ok then
-- print(self.parent.type == types.snippetNode)
-- Insp(self.parent.child_ext_opts)
-- print(self.parent.snippet)
-- print(self.parent.absolute_position)
-- end
-- end

if self.merge_node_ext_opts then
self.ext_opts = ext_util.extend(
vim.deepcopy(self.node_ext_opts),
parent_ext_opts or self.parent.effective_child_ext_opts[self.type]
)
else
self.ext_opts = self.node_ext_opts
end

ext_util.set_abs_prio(
self.ext_opts,
(base_prio or self.parent.ext_opts.base_prio)
+ conf.config.ext_prio_increase
)
end

return {
Node = Node,
}
Loading

0 comments on commit 48a79b7

Please sign in to comment.