Skip to content

Commit 942d42b

Browse files
committed
feat(cpp): add ns
1 parent c676017 commit 942d42b

File tree

3 files changed

+70
-7
lines changed

3 files changed

+70
-7
lines changed

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@ ls.setup({
4646

4747
#### Normal Snippets
4848

49-
| Trig | Desc | Context Required |
50-
| :--------: | ------------------------------------------------------------------------------------------------ | :--------------: |
51-
| `fn` | Expand to lambda function in argument list or function body, otherwise expand to normal function | No |
52-
| `\|trans` | Expand to ranges::views::transform pipe. | No |
53-
| `\|filter` | Expand to ranges::views::filter pipe. | No |
54-
| `cpo` | Expand to customize point object. | No |
49+
| Trig | Desc | Context Required |
50+
| :---------: | ------------------------------------------------------------------------------------------------ | :--------------: |
51+
| `fn` | Expand to lambda function in argument list or function body, otherwise expand to normal function | No |
52+
| `\|trans` | Expand to ranges::views::transform pipe. | No |
53+
| `\|filter` | Expand to ranges::views::filter pipe. | No |
54+
| `cpo` | Expand to customize point object. | No |
55+
| `ns%s(%S+)` | Expand to namespace block (including comments). | No |
5556

5657
#### Auto-snippets
5758

@@ -115,6 +116,7 @@ ls.setup({
115116
| `.ts` | Switch indent's coding style between `CamelCase` and `snake_case`. | `indent` |
116117
| `.sc` | Wraps with `static_cast<>(?)` | `any_expr` |
117118
| `.single` | Wraps with `ranges::views::single(?)` | `any_expr` |
119+
| `.in` | Expands to `if (...find)` statements. | `any_expr` |
118120

119121
</details>
120122

lua/luasnip-snippets/snippets/cpp/default.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,36 @@ return {
207207
return quick_type(shortcut)
208208
end),
209209
}),
210+
211+
snippet {
212+
"ns%s+(%S+)",
213+
name = "namespace",
214+
dscr = "namespace",
215+
mode = "br",
216+
nodes = fmta(
217+
[[
218+
namespace <name> {
219+
<body>
220+
} // namespace <name>
221+
]],
222+
{
223+
body = i(0),
224+
name = f(function(_, snip)
225+
local parts = vim.split(snip.captures[1], "::", {
226+
plain = true,
227+
trimempty = true,
228+
})
229+
local names = {}
230+
for _, part in ipairs(parts) do
231+
local nest_parts = vim.split(part, ".", {
232+
plain = true,
233+
trimempty = true,
234+
})
235+
vim.list_extend(names, nest_parts)
236+
end
237+
return table.concat(names, "::")
238+
end),
239+
}
240+
),
241+
},
210242
}

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local f = ls.function_node
33
local tsp = require("luasnip.extras.treesitter_postfix")
44
local Utils = require("luasnip-snippets.utils")
55
local fmt = require("luasnip.extras.fmt").fmt
6+
local fmta = require("luasnip.extras.fmt").fmta
67
local i = require("luasnip-snippets.nodes").insert_node
78

89
local expr_query = [[
@@ -101,7 +102,7 @@ return {
101102
tsp.treesitter_postfix(
102103
{
103104
trig = ".sc",
104-
name = "static_cast<TYPE>(?)",
105+
name = "(.sc) static_cast<TYPE>(?)",
105106
dscr = "Wraps an expression with static_cast<TYPE>(?)",
106107
wordTrig = false,
107108
reparseBuffer = "live",
@@ -123,4 +124,32 @@ return {
123124
}
124125
)
125126
),
127+
128+
tsp.treesitter_postfix(
129+
{
130+
trig = ".in",
131+
name = "(.in) if (...find)",
132+
dscr = "Expands to an if-expr to find an element in map-like object",
133+
wordTrig = false,
134+
reparseBuffer = "live",
135+
matchTSNode = {
136+
query = expr_query,
137+
query_lang = "cpp",
138+
},
139+
},
140+
fmta(
141+
[[
142+
if (auto it = <expr>.find(<key>); it != <expr>.end()) {
143+
<cursor>
144+
}
145+
]],
146+
{
147+
cursor = i(0),
148+
key = i(1, "Key"),
149+
expr = f(function(_, parent)
150+
return Utils.replace_all(parent.snippet.env.LS_TSMATCH, "%s")
151+
end, {}),
152+
}
153+
)
154+
),
126155
}

0 commit comments

Comments
 (0)