Skip to content

Commit ca469d5

Browse files
committed
feat(nix): add .split postfix to bindings
1 parent a74ff6f commit ca469d5

File tree

2 files changed

+77
-4
lines changed

2 files changed

+77
-4
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,17 @@ Snippets with `*` are available only when `vim_snippet` is enabled.
283283
```scheme
284284
[
285285
(identifier)
286-
] @prefix
286+
] @identifier
287+
[
288+
((binding
289+
expression: (_) @expr
290+
))
291+
] @binding
287292
```
288293

289-
| Trig | Desc (placehoder: `?`) | Expr before cursor |
290-
| :---: | ----------------------------------- | :----------------: |
291-
| `.on` | Expands to enable option statement. | `any_expr` |
294+
| Trig | Desc (placehoder: `?`) | Expr before cursor |
295+
| :------: | --------------------------------------- | :----------------: |
296+
| `.on` | Expands to enable option statement. | `identifier` |
297+
| `.split` | Expands bindings to full attrset style. | `binding` |
292298

293299
</details>

lua/luasnip-snippets/snippets/nix.lua

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ local rep = extras.rep
77
local i = require("luasnip-snippets.nodes").insert_node
88
local tsp = require("luasnip.extras.treesitter_postfix")
99
local Utils = require("luasnip-snippets.utils")
10+
---@type luasnip-snippets.utils.treesitter
11+
local UtilsTS = require("luasnip-snippets.utils.treesitter")
1012

1113
local identifier_query = [[
1214
[
@@ -40,6 +42,37 @@ local function identifier_tsp(trig, expand, dscr)
4042
})
4143
end
4244

45+
local bind_query = [[
46+
[
47+
((binding
48+
expression: (_) @expr
49+
))
50+
] @prefix
51+
]]
52+
53+
---@param context LSSnippets.ProcessMatchesContext
54+
---@param previous any
55+
local function inject_bind_matches(context, previous)
56+
vim.print("???")
57+
local node = context.prefix_node
58+
local attr_path = node:field("attrpath")[1]
59+
local attrs_nodes = attr_path:field("attr")
60+
local attrs = {}
61+
62+
for _, attr in ipairs(attrs_nodes) do
63+
local attr_text = context.ts_parser:get_node_text(attr)
64+
attrs[#attrs + 1] = attr_text
65+
end
66+
67+
previous = vim.tbl_deep_extend("force", previous, {
68+
env_override = {
69+
ATTRS = attrs,
70+
},
71+
})
72+
73+
return previous
74+
end
75+
4376
return {
4477
snippet {
4578
"@module",
@@ -76,4 +109,38 @@ return {
76109
"? = { enable = true; };",
77110
"Completes an identifier with an enable option"
78111
),
112+
113+
UtilsTS.treesitter_postfix({
114+
trig = ".split",
115+
name = "(.split) foo.bar = xxx; -> foo = { bar = xxx; };",
116+
dscr = "Split a dot expression into full attrset declaration",
117+
wordTrig = false,
118+
reparseBuffer = "live",
119+
matchTSNode = {
120+
query = bind_query,
121+
query_lang = "nix",
122+
},
123+
injectMatches = inject_bind_matches,
124+
}, {
125+
f(function(_, parent)
126+
vim.print(parent.snippet.env)
127+
local attrs = parent.snippet.env.ATTRS
128+
local expr = table.concat(parent.snippet.env.LS_TSCAPTURE_EXPR, "\n")
129+
Utils.reverse_list(attrs)
130+
131+
local generate_bindings = function(first, attr, previous)
132+
if first then
133+
return ("%s = %s;"):format(attr, previous)
134+
else
135+
return ("%s = { %s };"):format(attr, previous)
136+
end
137+
end
138+
139+
for j, attr in ipairs(attrs) do
140+
expr = generate_bindings(j == 1, attr, expr)
141+
end
142+
143+
return expr
144+
end, {}),
145+
}),
79146
}

0 commit comments

Comments
 (0)