Skip to content

Commit 780b004

Browse files
committed
feat(cpp): add selection version to most of postfix snippets
1 parent 9d06ca5 commit 780b004

File tree

5 files changed

+120
-43
lines changed

5 files changed

+120
-43
lines changed

README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -221,21 +221,21 @@ tmss! -> absl::flat_hash_map<std::string, std::string>
221221
] @any_expr
222222
```
223223

224-
| Trig | Desc (placehoder: `?`) | Expr before cursor |
225-
| :-------: | -------------------------------------------------------------------- | :----------------: |
226-
| `.be` | Expands to begin and end exprs. | `any_expr` |
227-
| `.cbe` | Expands to cbegin and cend exprs. | `any_expr` |
228-
| `.mv` | Wraps with `std::move(?)`. | `any_expr` |
229-
| `.fwd` | Wraps with `std::forward<decltype(?)>(?)`. | `any_expr` |
230-
| `.val` | Wraps with `std::declval<?>()`. | `any_expr` |
231-
| `.dt` | Wraps with `decltype(?)`. | `any_expr` |
232-
| `.uu` | Wraps with `(void)?`. | `any_expr` |
233-
| `.ts` | Switches indent's coding style between `CamelCase` and `snake_case`. | `indent` |
234-
| `.sc` | Wraps with `static_cast<>(?)`. | `any_expr` |
235-
| `.rc` | Wraps with `reinterpret_cast<>(?)`. | `any_expr` |
236-
| `.single` | Wraps with `ranges::views::single(?)`. | `any_expr` |
237-
| `.await` | Expands to `co_await ?`. | `any_expr` |
238-
| `.in` | Expands to `if (...find)` statements. | `any_expr` |
224+
| Trig | Desc (placehoder: `?`) | Expr before cursor | Selection Version |
225+
| :-------: | -------------------------------------------------------------------- | :----------------: | :---------------: |
226+
| `.be` | Expands to begin and end exprs. | `any_expr` | Yes |
227+
| `.cbe` | Expands to cbegin and cend exprs. | `any_expr` | Yes |
228+
| `.mv` | Wraps with `std::move(?)`. | `any_expr` | Yes |
229+
| `.fwd` | Wraps with `std::forward<decltype(?)>(?)`. | `any_expr` | Yes |
230+
| `.val` | Wraps with `std::declval<?>()`. | `any_expr` | Yes |
231+
| `.dt` | Wraps with `decltype(?)`. | `any_expr` | Yes |
232+
| `.uu` | Wraps with `(void)?`. | `any_expr` | Yes |
233+
| `.ts` | Switches indent's coding style between `CamelCase` and `snake_case`. | `indent` | Yes |
234+
| `.sc` | Wraps with `static_cast<>(?)`. | `any_expr` | Yes |
235+
| `.rc` | Wraps with `reinterpret_cast<>(?)`. | `any_expr` | Yes |
236+
| `.single` | Wraps with `ranges::views::single(?)`. | `any_expr` | Yes |
237+
| `.await` | Expands to `co_await ?`. | `any_expr` | Yes |
238+
| `.in` | Expands to `if (...find)` statements. | `any_expr` | |
239239

240240
#### Cpplint
241241

lua/luasnip-snippets/snippets/cpp/postfix.lua

Lines changed: 87 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ local Utils = require("luasnip-snippets.utils")
55
local fmt = require("luasnip.extras.fmt").fmt
66
local fmta = require("luasnip.extras.fmt").fmta
77
local i = require("luasnip-snippets.nodes").insert_node
8+
---@type luasnip-snippets.nodes
9+
local Nodes = require("luasnip-snippets.nodes")
10+
local snippet = Nodes.construct_snippet
11+
local CommonCond = require("luasnip-snippets.utils.common_cond")
812

913
local expr_query = [[
1014
[
@@ -36,41 +40,57 @@ local function expr_tsp(trig, expand, dscr)
3640
end
3741
local replaced = expand:gsub("?", "%%s")
3842

39-
return tsp.treesitter_postfix({
40-
trig = trig,
41-
name = name,
42-
dscr = dscr,
43-
wordTrig = false,
44-
reparseBuffer = "live",
45-
matchTSNode = {
46-
query = expr_query,
47-
query_lang = "cpp",
43+
return {
44+
snippet {
45+
trig,
46+
name = name,
47+
dscr = dscr,
48+
mode = "w",
49+
lang = "cpp",
50+
cond = CommonCond.has_select_raw,
51+
nodes = {
52+
f(function(_, snip)
53+
local _, env = {}, snip.env
54+
return Utils.replace_all(env.LS_SELECT_RAW, replaced)
55+
end),
56+
},
4857
},
49-
}, {
50-
f(function(_, parent)
51-
return Utils.replace_all(parent.snippet.env.LS_TSMATCH, replaced)
52-
end, {}),
53-
})
58+
tsp.treesitter_postfix({
59+
trig = "." .. trig,
60+
name = name,
61+
dscr = dscr,
62+
wordTrig = false,
63+
reparseBuffer = "live",
64+
matchTSNode = {
65+
query = expr_query,
66+
query_lang = "cpp",
67+
},
68+
}, {
69+
f(function(_, parent)
70+
return Utils.replace_all(parent.snippet.env.LS_TSMATCH, replaced)
71+
end, {}),
72+
}),
73+
}
5474
end
5575

5676
return {
5777
expr_tsp(
58-
".be",
78+
"be",
5979
"?.begin(), ?.end()",
6080
"Completes an expr with both begin() and end()"
6181
),
6282
expr_tsp(
63-
".cbe",
83+
"cbe",
6484
"?.cbegin(), ?.cend()",
6585
"Completes an expr with both cbegin() and cend()"
6686
),
67-
expr_tsp(".mv", "std::move(?)"),
68-
expr_tsp(".fwd", "std::forward<decltype(?)>(?)"),
69-
expr_tsp(".val", "std::declval<?>()"),
70-
expr_tsp(".dt", "decltype(?)"),
71-
expr_tsp(".uu", "(void)?"),
72-
expr_tsp(".single", "ranges::views::single(?)"),
73-
expr_tsp(".await", "co_await ?"),
87+
expr_tsp("mv", "std::move(?)"),
88+
expr_tsp("fwd", "std::forward<decltype(?)>(?)"),
89+
expr_tsp("val", "std::declval<?>()"),
90+
expr_tsp("dt", "decltype(?)"),
91+
expr_tsp("uu", "(void)?"),
92+
expr_tsp("single", "ranges::views::single(?)"),
93+
expr_tsp("await", "co_await ?"),
7494

7595
tsp.treesitter_postfix({
7696
trig = ".ts",
@@ -131,6 +151,28 @@ return {
131151
)
132152
),
133153

154+
snippet {
155+
"sc",
156+
name = "(sc) static_cast<TYPE>(...)",
157+
dscr = "Wraps an expression with static_cast<TYPE>(...)",
158+
mode = "w",
159+
lang = "cpp",
160+
cond = CommonCond.has_select_raw,
161+
nodes = fmt(
162+
[[
163+
static_cast<{body}>({selected}){cursor}
164+
]],
165+
{
166+
cursor = i(0),
167+
body = i(1),
168+
selected = f(function(_, snip)
169+
local _, env = {}, snip.env
170+
return Utils.replace_all(env.LS_SELECT_RAW, "%s")
171+
end),
172+
}
173+
),
174+
},
175+
134176
tsp.treesitter_postfix(
135177
{
136178
trig = ".rc",
@@ -157,6 +199,28 @@ return {
157199
)
158200
),
159201

202+
snippet {
203+
"rc",
204+
name = "(rc) reinterpret_cast<TYPE>(...)",
205+
dscr = "Wraps an expression with reinterpret_cast<TYPE>(...)",
206+
mode = "w",
207+
lang = "cpp",
208+
cond = CommonCond.has_select_raw,
209+
nodes = fmt(
210+
[[
211+
reinterpret_cast<{body}>({selected}){cursor}
212+
]],
213+
{
214+
cursor = i(0),
215+
body = i(1),
216+
selected = f(function(_, snip)
217+
local _, env = {}, snip.env
218+
return Utils.replace_all(env.LS_SELECT_RAW, "%s")
219+
end),
220+
}
221+
),
222+
},
223+
160224
tsp.treesitter_postfix(
161225
{
162226
trig = ".in",

lua/luasnip-snippets/snippets/cpp/selection.lua

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ local Config = require("luasnip-snippets.config")
1717
local Utils = require("luasnip-snippets.utils")
1818
local CppCommons = require("luasnip-snippets.snippets.cpp.commons")
1919

20-
local function has_select_raw_fn(_, _, _)
21-
return Utils.get_buf_var(0, "LUASNIP_SELECT_RAW") ~= nil
22-
end
23-
local has_select_raw = Cond.make_condition(has_select_raw_fn, has_select_raw_fn)
20+
local has_select_raw = CommonCond.has_select_raw
2421

2522
return {
2623
snippet {

lua/luasnip-snippets/utils/common_cond.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local Cond = require("luasnip-snippets.utils.cond")
2+
local Utils = require("luasnip-snippets.utils")
23

34
local function line_begin_cond(line_to_cursor, matched_trigger, _)
45
if matched_trigger == nil or line_to_cursor == nil then
@@ -57,7 +58,13 @@ local function generate_all_lines_before_match_cond(pattern)
5758
return Cond.make_condition(condition, condition)
5859
end
5960

61+
local function has_select_raw_fn(_, _, _)
62+
return Utils.get_buf_var(0, "LUASNIP_SELECT_RAW") ~= nil
63+
end
64+
local has_select_raw = Cond.make_condition(has_select_raw_fn, has_select_raw_fn)
65+
6066
return {
6167
at_line_begin = at_line_begin,
6268
generate_all_lines_before_match_cond = generate_all_lines_before_match_cond,
69+
has_select_raw = has_select_raw,
6370
}

lua/luasnip-snippets/utils/init.lua

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,16 @@ function M.concat_snippets(base, snippets)
3636
end
3737
vim.list_extend(ret, snippet_module)
3838
end
39-
return ret
39+
-- flatten the list
40+
local flat_ret = {}
41+
for _, snippet in ipairs(ret) do
42+
if vim.islist(snippet) then
43+
vim.list_extend(flat_ret, snippet)
44+
else
45+
flat_ret[#flat_ret + 1] = snippet
46+
end
47+
end
48+
return flat_ret
4049
end
4150

4251
function M.reverse_list(lst)

0 commit comments

Comments
 (0)