@@ -7,6 +7,8 @@ local rep = extras.rep
77local i = require (" luasnip-snippets.nodes" ).insert_node
88local tsp = require (" luasnip.extras.treesitter_postfix" )
99local Utils = require (" luasnip-snippets.utils" )
10+ --- @type luasnip-snippets.utils.treesitter
11+ local UtilsTS = require (" luasnip-snippets.utils.treesitter" )
1012
1113local identifier_query = [[
1214[
@@ -40,6 +42,37 @@ local function identifier_tsp(trig, expand, dscr)
4042 })
4143end
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+
4376return {
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