Skip to content

Commit b337e0f

Browse files
committed
feat(rust): expends .ok, .err, .some
1 parent 663cad4 commit b337e0f

File tree

1 file changed

+50
-15
lines changed

1 file changed

+50
-15
lines changed

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

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ local function expr_tsp(trig, expand)
6363
})
6464
end
6565

66-
local function expr_or_type_tsp(trig, typename)
66+
local function expr_or_type_tsp(trig, typename, expr_callback, type_callback)
6767
local name = ("(%s) %s"):format(trig, typename)
6868
local dscr = ("Wrap expression/type with %s"):format(typename)
6969
return tsp.treesitter_postfix({
@@ -82,28 +82,63 @@ local function expr_or_type_tsp(trig, typename)
8282
local data = env.LS_TSDATA
8383
if expr_node_types[data.prefix.type] then
8484
-- is expr
85-
return Utils.replace_all(env.LS_TSMATCH, typename .. "::new(%s)")
85+
return expr_callback(env.LS_TSMATCH)
8686
else
8787
-- is type
88-
return Utils.replace_all(env.LS_TSMATCH, typename .. "<%s>")
88+
return type_callback(env.LS_TSMATCH)
8989
end
9090
end),
9191
})
9292
end
9393

94+
local function simple_expr_or_type_tsp(trig, typename)
95+
local expr_callback = function(match)
96+
return Utils.replace_all(match, typename .. "::new(%s)")
97+
end
98+
local type_callback = function(match)
99+
return Utils.replace_all(match, typename .. "<%s>")
100+
end
101+
return expr_or_type_tsp(trig, typename, expr_callback, type_callback)
102+
end
103+
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
112+
end
113+
94114
return {
95-
expr_or_type_tsp(".rc", "Rc"),
96-
expr_or_type_tsp(".arc", "Arc"),
97-
expr_or_type_tsp(".box", "Box"),
98-
expr_or_type_tsp(".mu", "Mutex"),
99-
expr_or_type_tsp(".rw", "RwLock"),
100-
expr_or_type_tsp(".cell", "Cell"),
101-
expr_or_type_tsp(".refcell", "RefCell"),
102-
expr_or_type_tsp(".ref", "&?"),
103-
expr_or_type_tsp(".refm", "&mut ?"),
104-
expr_tsp(".ok", "Ok(?)"),
105-
expr_tsp(".err", "Err(?)"),
106-
expr_tsp(".some", "Some(?)"),
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 ?"),
124+
expr_or_type_tsp(
125+
".ok",
126+
"Ok(?)",
127+
build_simple_replace_callback("Ok(%s)"),
128+
result_type_callback
129+
),
130+
expr_or_type_tsp(
131+
".err",
132+
"Err(?)",
133+
build_simple_replace_callback("Err(%s)"),
134+
result_type_callback
135+
),
136+
expr_or_type_tsp(
137+
".some",
138+
"Some(?)",
139+
build_simple_replace_callback("Some(%s)"),
140+
build_simple_replace_callback("Option<%s>")
141+
),
107142

108143
tsp.treesitter_postfix({
109144
trig = ".println",

0 commit comments

Comments
 (0)