Skip to content

Commit aeac509

Browse files
committed
feat(rust): add more snippets, fn like cpp does and more...
1 parent d33f094 commit aeac509

File tree

6 files changed

+226
-26
lines changed

6 files changed

+226
-26
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ ls.setup({
155155
<details>
156156
<summary>Rust</summary>
157157

158+
#### Normal Snippets
159+
160+
| Trig | Desc | Context Required |
161+
| :--: | -------------------------------------------------------------------------------------------------- | :--------------: |
162+
| `fn` | Expands to lambda function in argument list or function body, otherwise expand to normal function. | No |
163+
| `pc` | Expands to `pub(crate)`. | No |
164+
| `ps` | Expands to `pub(super)`. | No |
165+
| `ii` | Expands to `#[inline]`. | No |
166+
| `ia` | Expands to `#[inline(always)]`. | No |
167+
158168
#### Postfix Snippets
159169

160170
```scheme

lua/luasnip-snippets/snippets/cpp/lambda_fn.lua

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ local sn = ls.snippet_node
55
local t = ls.text_node
66
local fmta = require("luasnip.extras.fmt").fmta
77
local CppCommons = require("luasnip-snippets.snippets.cpp.commons")
8-
local i = ls.insert_node
9-
local c = ls.choice_node
8+
local i = require("luasnip-snippets.nodes").insert_node
9+
local c = require("luasnip-snippets.nodes").choice_node
1010

1111
---@class LSSnippets.Cpp.Fn.Env
1212
---@field CPP_ARGUMENT_START { [1]: number, [2]: number }?
@@ -15,17 +15,6 @@ local c = ls.choice_node
1515
---@field CPP_IN_HEADER_FILE boolean
1616
---@field CPP_IN_QUALIFIED_FUNCTION boolean
1717

18-
---Returns the start pos of a `TSNode`
19-
---@param node TSNode?
20-
---@return { [1]: number, [2]: number }?
21-
local function start_pos(node)
22-
if node == nil then
23-
return nil
24-
end
25-
local start_row, start_col, _, _ = vim.treesitter.get_node_range(node)
26-
return { start_row, start_col }
27-
end
28-
2918
---Returns if the node's declarator is qualified or not.
3019
---@param node TSNode? `function_definition` node
3120
---@return boolean
@@ -74,19 +63,25 @@ local function inject_expanding_environment(_, _, match, captures)
7463
trigger = match,
7564
capture = captures,
7665
env_override = {
77-
CPP_ARGUMENT_START = start_pos(UtilsTS.find_first_parent(node, {
78-
"argument_list",
79-
"parameter_list",
80-
})),
81-
CPP_FUNCTION_BODY_START = start_pos(UtilsTS.find_first_parent(node, {
82-
"function_definition",
83-
"lambda_expression",
84-
"field_declaration",
85-
})),
86-
CPP_CLASS_BODY_START = start_pos(UtilsTS.find_first_parent(node, {
87-
"struct_specifier",
88-
"class_specifier",
89-
})),
66+
CPP_ARGUMENT_START = UtilsTS.start_pos(
67+
UtilsTS.find_first_parent(node, {
68+
"argument_list",
69+
"parameter_list",
70+
})
71+
),
72+
CPP_FUNCTION_BODY_START = UtilsTS.start_pos(
73+
UtilsTS.find_first_parent(node, {
74+
"function_definition",
75+
"lambda_expression",
76+
"field_declaration",
77+
})
78+
),
79+
CPP_CLASS_BODY_START = UtilsTS.start_pos(
80+
UtilsTS.find_first_parent(node, {
81+
"struct_specifier",
82+
"class_specifier",
83+
})
84+
),
9085
CPP_IN_HEADER_FILE = CppCommons.in_header_file(),
9186
CPP_IN_QUALIFIED_FUNCTION = is_qualified_function(
9287
UtilsTS.find_first_parent(node, {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
local ls = require("luasnip")
2+
local t = ls.text_node
3+
local snippet = require("luasnip-snippets.nodes").construct_snippet
4+
5+
return {
6+
snippet {
7+
"pc",
8+
name = "(pc) pub(crate) ...",
9+
dscr = "Expands to pub(crate) visibility",
10+
mode = "bw",
11+
nodes = {
12+
t("pub(crate) "),
13+
},
14+
},
15+
snippet {
16+
"ps",
17+
name = "(ps) pub(super) ...",
18+
dscr = "Expands to pub(super) visibility",
19+
mode = "bw",
20+
nodes = {
21+
t("pub(super) "),
22+
},
23+
},
24+
snippet {
25+
"ii",
26+
name = "(ii) #[inline] ...",
27+
dscr = "Expands to #[inline] attribute",
28+
mode = "bw",
29+
nodes = {
30+
t("#[inline]"),
31+
},
32+
},
33+
snippet {
34+
"ia",
35+
name = "(ia) #[inline(always)] ...",
36+
dscr = "Expands to #[inline(always)] attribute",
37+
mode = "bw",
38+
nodes = {
39+
t("#[inline(always)]"),
40+
},
41+
},
42+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ local Utils = require("luasnip-snippets.utils")
22

33
local function setup()
44
return Utils.concat_snippets("luasnip-snippets.snippets.rust", {
5+
"default",
56
"postfix",
7+
"lambda_fn",
68
})
79
end
810

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
local UtilsTS = require("luasnip-snippets.utils.treesitter")
2+
local ls = require("luasnip")
3+
local d = ls.dynamic_node
4+
local sn = ls.snippet_node
5+
local t = ls.text_node
6+
local fmta = require("luasnip.extras.fmt").fmta
7+
local i = require("luasnip-snippets.nodes").insert_node
8+
local c = require("luasnip-snippets.nodes").choice_node
9+
10+
local function inject_expanding_environment(_, _, match, captures)
11+
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
12+
local buf = vim.api.nvim_get_current_buf()
13+
14+
return UtilsTS.invoke_after_reparse_buffer(buf, match, function(parser, _)
15+
local pos = {
16+
row - 1,
17+
col - #match,
18+
}
19+
local node = parser:named_node_for_range {
20+
pos[1],
21+
pos[2],
22+
pos[1],
23+
pos[2],
24+
}
25+
26+
local ret = {
27+
trigger = match,
28+
capture = captures,
29+
env_override = {
30+
IMPL_ITEM_START = UtilsTS.start_pos(
31+
UtilsTS.find_first_parent(node, { "impl_item" })
32+
),
33+
FUNCTION_ITEM_START = UtilsTS.start_pos(
34+
UtilsTS.find_first_parent(node, { "function_item" })
35+
),
36+
CLOSURE_EXPRESSION_START = UtilsTS.start_pos(
37+
UtilsTS.find_first_parent(node, { "closure_expression" })
38+
),
39+
},
40+
}
41+
42+
vim.api.nvim_win_set_cursor(0, { row, col })
43+
return ret
44+
end)
45+
end
46+
47+
---@class LSSnippets.Rust.Fn.Env
48+
---@field IMPL_ITEM_START? { [1]: number, [2]: number }
49+
---@field FUNCTION_ITEM_START? { [1]: number, [2]: number }
50+
---@field CLOSURE_EXPRESSION_START? { [1]: number, [2]: number }
51+
52+
return {
53+
ls.s(
54+
{
55+
trig = "fn",
56+
wordTrig = true,
57+
name = "(fn) Function-Definition/Lambda",
58+
resolveExpandParams = inject_expanding_environment,
59+
},
60+
d(1, function(_, parent)
61+
local env = parent.env
62+
local last_type, last_type_row, last_type_col
63+
local keys = {
64+
"CPP_ARGUMENT_START",
65+
"CPP_FUNCTION_BODY_START",
66+
"CPP_CLASS_BODY_START",
67+
}
68+
for _, key in ipairs(keys) do
69+
if env[key] ~= nil then
70+
if last_type == nil then
71+
last_type = key
72+
last_type_row = env[key][1]
73+
last_type_col = env[key][2]
74+
else
75+
if
76+
last_type_row < env[key][1]
77+
or (last_type_row == env[key][1] and last_type_col < env[key][2])
78+
then
79+
last_type = key
80+
last_type_row = env[key][1]
81+
last_type_col = env[key][2]
82+
end
83+
end
84+
end
85+
end
86+
87+
if
88+
env.FUNCTION_ITEM_START ~= nil or env.CLOSURE_EXPRESSION_START ~= nil
89+
then
90+
-- closure expression
91+
return sn(
92+
nil,
93+
fmta(
94+
[[
95+
<modifier>|<args>| {
96+
<body>
97+
}
98+
]],
99+
{
100+
modifier = c(1, {
101+
t(""),
102+
t("async "),
103+
t("move "),
104+
}, { desc = "function modifier" }),
105+
args = i(2, "args"),
106+
body = i(0),
107+
}
108+
)
109+
)
110+
else
111+
-- function item
112+
return sn(
113+
nil,
114+
fmta(
115+
[[
116+
<modifier><visible>fn <name>(<args>) {
117+
<body>
118+
}
119+
]],
120+
{
121+
modifier = c(1, {
122+
t(""),
123+
t("async "),
124+
}, { desc = "function modifier" }),
125+
visible = c(2, {
126+
t(""),
127+
t("pub "),
128+
t("pub(crate) "),
129+
t("pub(super) "),
130+
}, { desc = "visibility" }),
131+
name = i(3, "new_fn", { desc = "function name" }),
132+
args = i(4, "args", { desc = "function arguments" }),
133+
body = i(0),
134+
}
135+
)
136+
)
137+
end
138+
end, {})
139+
),
140+
}

lua/luasnip-snippets/utils/treesitter.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,15 @@ function M.find_first_parent(node, types)
7777
return find_parent_impl(node)
7878
end
7979

80+
---Returns the start pos of a `TSNode`
81+
---@param node TSNode?
82+
---@return { [1]: number, [2]: number }?
83+
function M.start_pos(node)
84+
if node == nil then
85+
return nil
86+
end
87+
local start_row, start_col, _, _ = vim.treesitter.get_node_range(node)
88+
return { start_row, start_col }
89+
end
90+
8091
return M

0 commit comments

Comments
 (0)