Skip to content

Commit e8f98cd

Browse files
committed
feat(rust): add new snippet 'pm'
1 parent 449df4d commit e8f98cd

File tree

3 files changed

+94
-13
lines changed

3 files changed

+94
-13
lines changed

README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,15 @@ tmss! -> absl::flat_hash_map<std::string, std::string>
231231

232232
#### Normal Snippets
233233

234-
| Trig | Desc | Context Required |
235-
| :--: | -------------------------------------------------------------------------------------------------- | :--------------: |
236-
| `fn` | Expands to lambda function in argument list or function body, otherwise expand to normal function. | No |
237-
| `pc` | Expands to `pub(crate)`. | No |
238-
| `ps` | Expands to `pub(super)`. | No |
239-
| `ii` | Expands to `#[inline]`. | No |
240-
| `ia` | Expands to `#[inline(always)]`. | No |
234+
| Trig | Desc | Context Required |
235+
| :---: | -------------------------------------------------------------------------------------------------------------------------------------------- | :--------------: |
236+
| `fn` | Expands to lambda function in argument list or function body, otherwise expand to normal function. | No |
237+
| `pc` | Expands to `pub(crate)`. | No |
238+
| `ps` | Expands to `pub(super)`. | No |
239+
| `ii` | Expands to `#[inline]`. | No |
240+
| `ia` | Expands to `#[inline(always)]`. | No |
241+
| `tfn` | Expands to a test function. `#[test]` or `#[tokio::test]` supported. With `snippet.rust.rstest_support` enabled, `#[rstest]` also supported. | No |
242+
| `pm` | Expands to a public method definition. | In impl block |
241243

242244
#### Postfix Snippets
243245

@@ -285,12 +287,11 @@ tmss! -> absl::flat_hash_map<std::string, std::string>
285287

286288
#### Normal Snippets
287289

288-
| Trig | Desc | Context Required |
289-
| :---: | -------------------------------------------------------------------------------------------------------------------------------------------- | :--------------: |
290-
| `fn` | Expands to function definition. | No |
291-
| `wfn` | Expands to function definition returns a widget. | No |
292-
| `afn` | Expands to an async function definition. | No |
293-
| `tfn` | Expands to a test function. `#[test]` or `#[tokio::test]` supported. With `snippet.rust.rstest_support` enabled, `#[rstest]` also supported. | No |
290+
| Trig | Desc | Context Required |
291+
| :---: | ------------------------------------------------ | :--------------: |
292+
| `fn` | Expands to function definition. | No |
293+
| `wfn` | Expands to function definition returns a widget. | No |
294+
| `afn` | Expands to an async function definition. | No |
294295

295296
#### Auto-snippets
296297

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
local UtilsTS = require("luasnip-snippets.utils.treesitter")
2+
local ls = require("luasnip")
3+
local t = ls.text_node
4+
local fmta = require("luasnip.extras.fmt").fmta
5+
local i = require("luasnip-snippets.nodes").insert_node
6+
local c = require("luasnip-snippets.nodes").choice_node
7+
8+
local function require_impl_block(_, _, match, captures)
9+
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
10+
local buf = vim.api.nvim_get_current_buf()
11+
12+
return UtilsTS.invoke_after_reparse_buffer(buf, match, function(parser, _)
13+
local pos = {
14+
row - 1,
15+
col - #match,
16+
}
17+
local node = parser:named_node_for_range {
18+
pos[1],
19+
pos[2],
20+
pos[1],
21+
pos[2],
22+
}
23+
24+
local ret = {
25+
trigger = match,
26+
capture = captures,
27+
env_override = {
28+
IMPL_ITEM_START = UtilsTS.start_pos(
29+
UtilsTS.find_first_parent(node, { "impl_item" })
30+
),
31+
FUNCTION_ITEM_START = UtilsTS.start_pos(
32+
UtilsTS.find_first_parent(node, { "function_item" })
33+
),
34+
CLOSURE_EXPRESSION_START = UtilsTS.start_pos(
35+
UtilsTS.find_first_parent(node, { "closure_expression" })
36+
),
37+
},
38+
}
39+
40+
vim.api.nvim_win_set_cursor(0, { row, col })
41+
42+
if
43+
ret.env_override.IMPL_ITEM_START ~= nil
44+
and ret.env_override.FUNCTION_ITEM_START == nil
45+
and ret.env_override.CLOSURE_EXPRESSION_START == nil
46+
then
47+
return ret
48+
end
49+
50+
return nil
51+
end)
52+
end
53+
54+
return {
55+
ls.s(
56+
{
57+
trig = "pm",
58+
wordTrig = true,
59+
name = "(pm) pub method",
60+
resolveExpandParams = require_impl_block,
61+
},
62+
fmta(
63+
[[
64+
pub fn <name>(<_self>) {
65+
<body>
66+
}
67+
]],
68+
{
69+
body = i(0),
70+
name = i(1, "new_fn", { desc = "function name" }),
71+
_self = c(2, {
72+
t("&self"),
73+
t("&mut self"),
74+
t("self"),
75+
}, { desc = "self" }),
76+
}
77+
)
78+
),
79+
}

lua/luasnip-snippets/snippets/rust/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local function setup()
66
"postfix",
77
"lambda_fn",
88
"test_fn",
9+
"impl_block",
910
})
1011
end
1112

0 commit comments

Comments
 (0)