Skip to content

Commit

Permalink
BREAKING(feat): Add snippet_passive-key to ext_opts (closes #180).
Browse files Browse the repository at this point in the history
snippet_passive` gets the old behaviour of `passive`, whereas `passive` now
means that the node isn't active, but the snippet still is.
  • Loading branch information
L3MON4D3 committed Sep 23, 2021
1 parent 800e187 commit 257f65b
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 24 deletions.
30 changes: 19 additions & 11 deletions DOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,29 +480,37 @@ ls.config.setup({
virt_text = {{"choiceNode", "GruvboxOrange"}}
}
},
[types.textNode] = {
snippet_passive = {
hl_group = "GruvboxGreen"
}
},
},
ext_base_prio = 200,
ext_prio_increase = 7,
ext_prio_increase = 3,
})
```

This highlights `insertNodes` red (both when active and passive) and adds
virtualText and italics to `choiceNode` while it is active (unspecified
values in `active` are populated with values from `passive`). The `active`/
`passive`-tables are passed to `nvim_buf_set_extmark` as `opts` which means only
entries valid there can be used here. `priority`, while still affecting the
priority of highlighting, is interpreted as a relative value here, not absolute
(`0 <= priority < ext_prio_increase`).
virtualText and italics to `choiceNode` while it is active (both only if the
snippet is active). `textNodes` are highlighted green, even if the snippet is
no longer active. (unspecified values in `passive` are populated with values
from `snippet_passive`, those in `active` with those from the new `passive`).

The `active`/ `passive`-tables are passed to `nvim_buf_set_extmark` as `opts`
which means only entries valid there can be used here. `priority`, while still
affecting the priority of highlighting, is interpreted as a relative value here,
not absolute (`0 <= priority < ext_prio_increase`).
The absolute range of priorities can still be somewhat controlled using
`ext_base_prio` and `ext_prio_increase` (all highlights start out with
`ext_base_prio`+their own priority, for highlights belonging to a nested
snippet(Node), `ext_base_prio` is increased by `ext_prio_increase`)).

As a shortcut for setting `hl_group`, the highlight-groups
`Luasnip*Node{Active,Passive}` may be defined (to be actually used by LuaSnip,
`ls.config.setup` has to be called after defining). They are overridden by the
values defined in `ext_opts` directly, but otherwise behave the same (active is
extended by passive).
`Luasnip*Node{Active,Passive,SnippetPassive}` may be defined (to be actually
used by LuaSnip, `ls.config.setup` has to be called after defining). They are
overridden by the values defined in `ext_opts` directly, but otherwise behave
the same (active is extended by passive).



Expand Down
9 changes: 9 additions & 0 deletions lua/luasnip/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,43 @@ local defaults = {
[types.textNode] = {
active = { hl_group = "LuasnipTextNodeActive" },
passive = { hl_group = "LuasnipTextNodePassive" },
snippet_passive = { hl_group = "LuasnipTextNodeSnippetPassive" },
},
[types.insertNode] = {
active = { hl_group = "LuasnipInsertNodeActive" },
passive = { hl_group = "LuasnipInsertNodePassive" },
snippet_passive = { hl_group = "LuasnipInsertNodeSnippetPassive" },
},
[types.exitNode] = {
active = { hl_group = "LuasnipExitNodeActive" },
passive = { hl_group = "LuasnipExitNodePassive" },
snippet_passive = { hl_group = "LuasnipExitNodeSnippetPassive" },
},
[types.functionNode] = {
active = { hl_group = "LuasnipFunctionNodeActive" },
passive = { hl_group = "LuasnipFunctionNodePassive" },
snippet_passive = { hl_group = "LuasnipFunctionNodeSnippetPassive" },
},
[types.snippetNode] = {
active = { hl_group = "LuasnipSnippetNodeActive" },
passive = { hl_group = "LuasnipSnippetNodePassive" },
snippet_passive = { hl_group = "LuasnipSnippetNodeSnippetPassive" },
},
[types.choiceNode] = {
active = { hl_group = "LuasnipChoiceNodeActive" },
passive = { hl_group = "LuasnipChoiceNodePassive" },
snippet_passive = { hl_group = "LuasnipChoiceNodeSnippetPassive" },
},
[types.dynamicNode] = {
active = { hl_group = "LuasnipDynamicNodeActive" },
passive = { hl_group = "LuasnipDynamicNodePassive" },
snippet_passive = { hl_group = "LuasnipDynamicNodeSnippetPassive" },
},
[types.snippet] = {
active = { hl_group = "LuasnipSnippetActive" },
passive = { hl_group = "LuasnipSnippetPassive" },
-- not used!
snippet_passive = { hl_group = "LuasnipSnippetSnippetPassive" },
},
},
ext_base_prio = 200,
Expand Down
5 changes: 5 additions & 0 deletions lua/luasnip/nodes/choiceNode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ function ChoiceNode:set_mark_rgrav(rgrav_beg, rgrav_end)
self.active_choice: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.active_choice:set_ext_opts(name)
end

return {
C = C,
}
5 changes: 5 additions & 0 deletions lua/luasnip/nodes/dynamicNode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ function DynamicNode:exit()
self.active = false
end

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

return {
D = D,
}
4 changes: 4 additions & 0 deletions lua/luasnip/nodes/node.lua
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ function Node:get_static_args()
return get_args(self, "get_static_text")
end

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

Node.ext_gravities_active = { false, true }

return {
Expand Down
18 changes: 16 additions & 2 deletions lua/luasnip/nodes/snippet.lua
Original file line number Diff line number Diff line change
Expand Up @@ -668,17 +668,31 @@ end

function Snippet:input_enter()
self.active = true

if self.type == types.snippet then
self:set_ext_opts("passive")
end
self.mark:update_opts(self.ext_opts[self.type].active)

self:event(events.enter)
end

function Snippet:input_leave()
self:event(events.leave)

self:update_dependents()
self.active = false

self.mark:update_opts(self.ext_opts[self.type].passive)
if self.type == types.snippet then
self:set_ext_opts("snippet_passive")
end

self.active = false
end

function Snippet:set_ext_opts(opt_name)
for _, node in ipairs(self.nodes) do
node:set_ext_opts(opt_name)
end
end

function Snippet:jump_into(dir, no_move)
Expand Down
32 changes: 21 additions & 11 deletions lua/luasnip/util/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,10 @@ end

local function clear_invalid(opts)
for key, val in pairs(opts) do
local act_group, pas_group = val.active.hl_group, val.passive.hl_group
local act_group, pas_group, snip_pas_group = val.active.hl_group, val.passive.hl_group, val.snippet_passive.hl_group
opts[key].snippet_passive.hl_group = vim.fn.hlexists(snip_pas_group) == 1
and snip_pas_group
or nil
opts[key].passive.hl_group = vim.fn.hlexists(pas_group) == 1
and pas_group
or nil
Expand All @@ -346,21 +349,28 @@ end

local function make_opts_valid(user_opts, default_opts)
local opts = vim.deepcopy(default_opts)
for key, val in pairs(opts) do
for key, default_val in pairs(opts) do
-- prevent nil-indexing error.
user_opts[key] = user_opts[key] or {}

-- override defaults with user for passive.
val.passive = vim.tbl_extend(
-- override defaults with user for snippet_passive.
default_val.snippet_passive = vim.tbl_extend(
"force",
val.passive,
user_opts[key].passive or {}
default_val.snippet_passive,
user_opts[key].snippet_passive or {}
)
-- override default active with user, then fill in missing values from passive.
val.active = vim.tbl_extend(
"keep",
vim.tbl_extend("force", val.active, user_opts[key].active or {}),
val.passive
-- override default-passive with user, get missing values from default
-- snippet_passive
default_val.passive = vim.tbl_extend(
"force",
user_opts[key].snippet_passive or {},
vim.tbl_extend("force", default_val.passive, user_opts[key].passive or {})
)
-- same here, but with passive and active
default_val.active = vim.tbl_extend(
"force",
default_val.passive,
vim.tbl_extend("force", default_val.active, user_opts[key].active or {})
)
end
return opts
Expand Down

0 comments on commit 257f65b

Please sign in to comment.