Skip to content

Commit 51029ed

Browse files
committed
feat(cpp): add selected code wrapper #define
1 parent 6510eab commit 51029ed

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,11 @@ Snippets with `*` are available only when `vim_snippet` is enabled.
153153
| `itf` | Expands to a struct with default virtual destruction. | No | |
154154
| `pvf` | Expands to a pure virtual function declaration. | No | |
155155
| `qcls` | Expands to a class inherts from QObject. | No | Yes |
156-
| `#if` | Expands to `#if` statement. | After cut selection | No |
157-
| `if` | Expands to `if` statement. | After cut selection | No |
158-
| `do` | Expands to `do ... while (0)` statement. | After cut selection | No |
159-
| `while` | Expands to `while` statement. | After cut selection | No |
156+
| `#if` | Wrap selected code in `#if ... #endif` block. | After cut selection | No |
157+
| `if` | Wrap selected code in `if (...)` block. | After cut selection | No |
158+
| `do` | Wrap selected code in `do ... while(0)` block. | After cut selection | No |
159+
| `while` | Wrap selected code in `while (...)` block. | After cut selection | No |
160+
| `#de` | Wrap selected code in `#define ...` block. | After cut selection | No |
160161

161162
#### Auto-snippets
162163

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

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ local function fix_leading_whitespace(lines, indent)
3434
return ret
3535
end
3636

37+
local function add_trailing_slash(lines)
38+
local ret = {}
39+
local max_len = 0
40+
for _, line in ipairs(lines) do
41+
max_len = math.max(max_len, #line)
42+
end
43+
for _, line in ipairs(lines) do
44+
local len = #line
45+
local diff = max_len - len
46+
table.insert(ret, line .. string.rep(" ", diff) .. " \\")
47+
end
48+
return ret
49+
end
50+
3751
local function has_select_raw_fn(_, _, _)
3852
return Utils.get_buf_var(0, "LUASNIP_SELECT_RAW") ~= nil
3953
end
@@ -43,7 +57,7 @@ return {
4357
snippet {
4458
"#if",
4559
name = "(#if) #if ... #endif",
46-
dscr = "Expands to #if ... #endif",
60+
dscr = "Wrap selected code in #if ... #endif block",
4761
mode = "bw",
4862
lang = "cpp",
4963
cond = has_select_raw,
@@ -68,7 +82,7 @@ return {
6882
snippet {
6983
"if",
7084
name = "(if) if (...) { ... }",
71-
dscr = "Expands to if expression",
85+
dscr = "Wrap selected code in if (...) { ... } block",
7286
mode = "bw",
7387
lang = "cpp",
7488
cond = has_select_raw,
@@ -92,7 +106,7 @@ return {
92106
snippet {
93107
"do",
94108
name = "(do) do { ... } while (0)",
95-
dscr = "Expands to do ... while(0) expression",
109+
dscr = "Wrap selected code in do { ... } while (0) block",
96110
mode = "bw",
97111
lang = "cpp",
98112
cond = has_select_raw,
@@ -115,7 +129,7 @@ return {
115129
snippet {
116130
"while",
117131
name = "(while) while (...) { ... }",
118-
dscr = "Expands to while (...) { ... } expression",
132+
dscr = "Wrap selected code in while (...) { ... } block",
119133
mode = "bw",
120134
lang = "cpp",
121135
cond = has_select_raw,
@@ -135,4 +149,27 @@ return {
135149
}
136150
),
137151
},
152+
153+
snippet {
154+
"#de",
155+
name = "(#de) #define ...",
156+
dscr = "Wrap selected code in #define block",
157+
mode = "bw",
158+
lang = "cpp",
159+
cond = has_select_raw,
160+
nodes = fmta(
161+
[[
162+
#define <name>() \
163+
<selected><cursor>
164+
]],
165+
{
166+
name = i(1, "condition"),
167+
cursor = i(0),
168+
selected = f(function(_, snip)
169+
local _, env = {}, snip.env
170+
return fix_leading_whitespace(add_trailing_slash(env.LS_SELECT_RAW))
171+
end),
172+
}
173+
),
174+
},
138175
}

0 commit comments

Comments
 (0)