Skip to content

Commit 086a9ce

Browse files
committed
fix(rust): .ref, .refm
1 parent b337e0f commit 086a9ce

File tree

1 file changed

+36
-46
lines changed

1 file changed

+36
-46
lines changed

lua/luasnip-snippets/snippets/rust/postfix.lua

Lines changed: 36 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,6 @@ local expr_node_types = {
3939
["string_literal"] = true,
4040
}
4141

42-
---@param trig string
43-
---@param expand string
44-
local function expr_tsp(trig, expand)
45-
local name = ("(%s) %s"):format(trig, expand)
46-
local dscr = ("Wraps an expression with %s"):format(expand)
47-
local replaced = expand:gsub("?", "%%s")
48-
49-
return tsp.treesitter_postfix({
50-
trig = trig,
51-
name = name,
52-
dscr = dscr,
53-
wordTrig = false,
54-
reparseBuffer = "live",
55-
matchTSNode = {
56-
query = expr_query,
57-
query_lang = "rust",
58-
},
59-
}, {
60-
f(function(_, parent)
61-
return Utils.replace_all(parent.snippet.env.LS_TSMATCH, replaced)
62-
end, {}),
63-
})
64-
end
65-
6642
local function expr_or_type_tsp(trig, typename, expr_callback, type_callback)
6743
local name = ("(%s) %s"):format(trig, typename)
6844
local dscr = ("Wrap expression/type with %s"):format(typename)
@@ -91,7 +67,21 @@ local function expr_or_type_tsp(trig, typename, expr_callback, type_callback)
9167
})
9268
end
9369

94-
local function simple_expr_or_type_tsp(trig, typename)
70+
local function result_ok_type_callback(match)
71+
return Utils.replace_all(match, "Result<%s, _>")
72+
end
73+
74+
local function result_err_type_callback(match)
75+
return Utils.replace_all(match, "Result<_, %s>")
76+
end
77+
78+
local function build_simple_replace_callback(replaced)
79+
return function(match)
80+
return Utils.replace_all(match, replaced)
81+
end
82+
end
83+
84+
local function new_expr_or_type_tsp(trig, typename)
9585
local expr_callback = function(match)
9686
return Utils.replace_all(match, typename .. "::new(%s)")
9787
end
@@ -101,37 +91,37 @@ local function simple_expr_or_type_tsp(trig, typename)
10191
return expr_or_type_tsp(trig, typename, expr_callback, type_callback)
10292
end
10393

104-
local function result_type_callback(match)
105-
return Utils.replace_all(match, "Result<%s, _>")
106-
end
107-
108-
local function build_simple_replace_callback(replaced)
109-
return function(match)
110-
return Utils.replace_all(match, replaced)
111-
end
94+
local function both_replace_expr_or_type_tsp(trig, pattern)
95+
local template = pattern:gsub("?", "%%s")
96+
return expr_or_type_tsp(
97+
trig,
98+
pattern,
99+
build_simple_replace_callback(template),
100+
build_simple_replace_callback(template)
101+
)
112102
end
113103

114104
return {
115-
simple_expr_or_type_tsp(".rc", "Rc"),
116-
simple_expr_or_type_tsp(".arc", "Arc"),
117-
simple_expr_or_type_tsp(".box", "Box"),
118-
simple_expr_or_type_tsp(".mu", "Mutex"),
119-
simple_expr_or_type_tsp(".rw", "RwLock"),
120-
simple_expr_or_type_tsp(".cell", "Cell"),
121-
simple_expr_or_type_tsp(".refcell", "RefCell"),
122-
simple_expr_or_type_tsp(".ref", "&?"),
123-
simple_expr_or_type_tsp(".refm", "&mut ?"),
105+
new_expr_or_type_tsp(".rc", "Rc"),
106+
new_expr_or_type_tsp(".arc", "Arc"),
107+
new_expr_or_type_tsp(".box", "Box"),
108+
new_expr_or_type_tsp(".mu", "Mutex"),
109+
new_expr_or_type_tsp(".rw", "RwLock"),
110+
new_expr_or_type_tsp(".cell", "Cell"),
111+
new_expr_or_type_tsp(".refcell", "RefCell"),
112+
both_replace_expr_or_type_tsp(".ref", "&?"),
113+
both_replace_expr_or_type_tsp(".refm", "&mut ?"),
124114
expr_or_type_tsp(
125115
".ok",
126116
"Ok(?)",
127117
build_simple_replace_callback("Ok(%s)"),
128-
result_type_callback
118+
result_ok_type_callback
129119
),
130120
expr_or_type_tsp(
131121
".err",
132122
"Err(?)",
133123
build_simple_replace_callback("Err(%s)"),
134-
result_type_callback
124+
result_err_type_callback
135125
),
136126
expr_or_type_tsp(
137127
".some",
@@ -145,7 +135,7 @@ return {
145135
name = [[(.println) println!("{:?}", ?)]],
146136
dscr = [[Wrap expression with println!("{:?}", ?)]],
147137
wordTrig = false,
148-
reparseBuffer = nil,
138+
reparseBuffer = "live",
149139
matchTSNode = {
150140
query = expr_query,
151141
query_lang = "rust",
@@ -164,7 +154,7 @@ return {
164154
name = [[(.match) match ?]],
165155
dscr = [[Wrap expression with match ? block]],
166156
wordTrig = false,
167-
reparseBuffer = nil,
157+
reparseBuffer = "live",
168158
matchTSNode = {
169159
query = expr_query,
170160
query_lang = "rust",

0 commit comments

Comments
 (0)