Skip to content

Commit 7787835

Browse files
committed
Add postfixes
1 parent 81b551f commit 7787835

File tree

5 files changed

+88
-7
lines changed

5 files changed

+88
-7
lines changed

lua/luasnip-snippets/snippets/cpp/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ local function setup()
44
return Utils.concat_snippets("luasnip-snippets.snippets.cpp", {
55
"statements",
66
"lambda_fn",
7+
"postfix",
78
})
89
end
910

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ local UtilsTS = require("luasnip-snippets.utils.treesitter")
33
local d = ls.dynamic_node
44
local sn = ls.snippet_node
55
local t = ls.text_node
6-
local f = ls.function_node
76
local fmta = require("luasnip.extras.fmt").fmta
87
local CppCommons = require("luasnip-snippets.snippets.cpp.commons")
98
local i = ls.insert_node
@@ -55,7 +54,7 @@ local function is_qualified_function(node)
5554
return false
5655
end
5756

58-
local function inject_expanding_environment(_, line_to_cursor, match, captures)
57+
local function inject_expanding_environment(_, _, match, captures)
5958
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
6059
local buf = vim.api.nvim_get_current_buf()
6160

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
local ls = require("luasnip")
2+
local f = ls.function_node
3+
local tsp = require("luasnip.extras.treesitter_postfix")
4+
local Utils = require("luasnip-snippets.utils")
5+
local fmt = require("luasnip.extras.fmt").fmt
6+
local i = require("luasnip-snippets.nodes").insert_node
7+
local c = require("luasnip-snippets.nodes").choice_node
8+
local t = ls.text_node
9+
10+
local expr_query = [[
11+
[
12+
(call_expression)
13+
(identifier)
14+
(template_function)
15+
(subscript_expression)
16+
(field_expression)
17+
(user_defined_literal)
18+
] @prefix
19+
]]
20+
21+
---@param trig string
22+
---@param expand string
23+
---@param dscr string?
24+
local function expr_tsp(trig, expand, dscr)
25+
local name = ("(%s) %s"):format(trig, expand)
26+
if dscr == nil then
27+
dscr = ("Wraps an expression with %s"):format(expand)
28+
else
29+
dscr = dscr:format(expand)
30+
end
31+
local replaced = expand:gsub("?", "%%s")
32+
33+
return tsp.treesitter_postfix({
34+
trig = trig,
35+
name = name,
36+
dscr = dscr,
37+
wordTrig = false,
38+
reparseBuffer = "live",
39+
matchTSNode = {
40+
query = expr_query,
41+
query_lang = "cpp",
42+
},
43+
}, {
44+
f(function(_, parent)
45+
return Utils.replace_all(parent.snippet.env.LS_TSMATCH, replaced)
46+
end, {}),
47+
})
48+
end
49+
50+
return {
51+
expr_tsp(
52+
".be",
53+
"?.begin(), ?.end()",
54+
"Completes an expr with both begin() and end()"
55+
),
56+
expr_tsp(".mv", "std::move(?)"),
57+
expr_tsp(".fwd", "std::forward<decltype(?)>(?)"),
58+
expr_tsp(".val", "std::declval<?>()"),
59+
expr_tsp(".dt", "decltype(?)"),
60+
tsp.treesitter_postfix(
61+
{
62+
trig = ".sc",
63+
name = "static_cast<TYPE>(?)",
64+
dscr = "Wraps an expression with static_cast<TYPE>(?)",
65+
wordTrig = false,
66+
reparseBuffer = "live",
67+
matchTSNode = {
68+
query = expr_query,
69+
query_lang = "cpp",
70+
},
71+
},
72+
fmt(
73+
[[
74+
static_cast<{body}>({expr}){end}
75+
]],
76+
{
77+
body = i(1),
78+
expr = f(function(_, parent)
79+
return Utils.replace_all(parent.snippet.env.LS_TSMATCH, "%s")
80+
end, {}),
81+
["end"] = i(0),
82+
}
83+
)
84+
),
85+
}

lua/luasnip-snippets/snippets/cpp/statements.lua

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ local UtilsTS = require("luasnip-snippets.utils.treesitter")
33
local d = ls.dynamic_node
44
local sn = ls.snippet_node
55
local t = ls.text_node
6-
local f = ls.function_node
76
local fmta = require("luasnip.extras.fmt").fmta
8-
local CppCommons = require("luasnip-snippets.snippets.cpp.commons")
9-
local i = ls.insert_node
10-
local c = ls.choice_node
117

128
local function inject_class_name(_, line_to_cursor, match, captures)
139
-- check if at the line begin

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ local expr_node_types = {
3636
---@param expand string
3737
local function expr_tsp(trig, expand)
3838
local name = ("(%s) %s"):format(trig, expand)
39-
local dscr = ("Wrap expression with %s"):format(expand)
39+
local dscr = ("Wraps an expression with %s"):format(expand)
4040
local replaced = expand:gsub("?", "%%s")
4141

4242
return tsp.treesitter_postfix({

0 commit comments

Comments
 (0)