@@ -3,10 +3,33 @@ local f = ls.function_node
33local tsp = require (" luasnip.extras.treesitter_postfix" )
44local Utils = require (" luasnip-snippets.utils" )
55
6+ local expr_query = [[
7+ [
8+ (struct_expression)
9+ (call_expression)
10+ (identifier)
11+ (field_expression)
12+ ] @prefix
13+ ]]
14+
15+ local expr_or_type_query = [[
16+ [
17+ (struct_expression)
18+ (call_expression)
19+ (identifier)
20+ (field_expression)
21+
22+ (generic_type)
23+ (scoped_type_identifier)
24+ (reference_type)
25+ ] @prefix
26+ ]]
27+
628local expr_node_types = {
7- " struct_expression" ,
8- " call_expression" ,
9- " identifier" ,
29+ [" struct_expression" ] = true ,
30+ [" call_expression" ] = true ,
31+ [" identifier" ] = true ,
32+ [" field_expression" ] = true ,
1033}
1134
1235--- @param trig string
@@ -21,29 +44,76 @@ local function expr_tsp(trig, expand)
2144 name = name ,
2245 dscr = dscr ,
2346 wordTrig = false ,
24- reparseBuffer = nil ,
25- matchTSNode = tsp . builtin . tsnode_matcher . find_topmost_types (
26- expr_node_types ,
27- trig
28- ) ,
47+ reparseBuffer = " live " ,
48+ matchTSNode = {
49+ query = expr_query ,
50+ query_lang = " rust " ,
51+ } ,
2952 }, {
3053 f (function (_ , parent )
3154 return Utils .replace_all (parent .snippet .env .LS_TSMATCH , replaced )
3255 end , {}),
3356 })
3457end
3558
59+ local function expr_or_type_tsp (trig , typename )
60+ local name = (" (%s) %s" ):format (trig , typename )
61+ local dscr = (" Wrap expression/type with %s" ):format (typename )
62+ return tsp .treesitter_postfix ({
63+ trig = trig ,
64+ name = name ,
65+ dscr = dscr ,
66+ wordTrig = false ,
67+ reparseBuffer = " live" ,
68+ matchTSNode = {
69+ query = expr_or_type_query ,
70+ query_lang = " rust" ,
71+ },
72+ }, {
73+ f (function (_ , parent )
74+ local env = parent .snippet .env
75+ local data = env .LS_TSDATA
76+ if expr_node_types [data .prefix .type ] then
77+ -- is expr
78+ return Utils .replace_all (env .LS_TSMATCH , typename .. " ::new(%s)" )
79+ else
80+ -- is type
81+ return Utils .replace_all (env .LS_TSMATCH , typename .. " <%s>" )
82+ end
83+ end ),
84+ })
85+ end
86+
3687return {
37- expr_tsp (" .rc" , " Rc::new(?) " ),
38- expr_tsp (" .arc" , " Arc::new(?) " ),
39- expr_tsp (" .box" , " Box::new(?) " ),
40- expr_tsp (" .mu" , " Mutex::new(?) " ),
41- expr_tsp (" .rw" , " RwLock::new(?) " ),
42- expr_tsp (" .cell" , " Cell::new(?) " ),
43- expr_tsp (" .refcell" , " RefCell::new(?) " ),
88+ expr_or_type_tsp (" .rc" , " Rc" ),
89+ expr_or_type_tsp (" .arc" , " Arc" ),
90+ expr_or_type_tsp (" .box" , " Box" ),
91+ expr_or_type_tsp (" .mu" , " Mutex" ),
92+ expr_or_type_tsp (" .rw" , " RwLock" ),
93+ expr_or_type_tsp (" .cell" , " Cell" ),
94+ expr_or_type_tsp (" .refcell" , " RefCell" ),
4495 expr_tsp (" .ref" , " &?" ),
4596 expr_tsp (" .refm" , " &mut ?" ),
4697 expr_tsp (" .ok" , " Ok(?)" ),
4798 expr_tsp (" .err" , " Err(?)" ),
4899 expr_tsp (" .some" , " Some(?)" ),
100+
101+ tsp .treesitter_postfix ({
102+ trig = " .println" ,
103+ name = [[ (.println) println!("{:?}", ?)]] ,
104+ dscr = [[ Wrap expression with println!("{:?}", ?)]] ,
105+ wordTrig = false ,
106+ reparseBuffer = nil ,
107+ matchTSNode = {
108+ query = expr_query ,
109+ query_lang = " rust" ,
110+ },
111+ }, {
112+ f (function (_ , parent )
113+ return Utils .replace_all (
114+ parent .snippet .env .LS_TSMATCH ,
115+ [[ println!("{:?}", %s)]]
116+ )
117+ end , {}),
118+ }),
49119}
0 commit comments