Skip to content

Commit 78bd1e2

Browse files
committed
feat(cpp): lambda_fn supports selection
1 parent 51029ed commit 78bd1e2

File tree

4 files changed

+65
-53
lines changed

4 files changed

+65
-53
lines changed

README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -143,21 +143,21 @@ Snippets with `*` are available only when `vim_snippet` is enabled.
143143

144144
#### Normal Snippets
145145

146-
| Trig | Desc | Context Required | Qt |
147-
| :---------: | -------------------------------------------------------------------------------------------------- | :-----------------: | :-: |
148-
| `fn` | Expands to lambda function in argument list or function body, otherwise expand to normal function. | No | |
149-
| `\|trans` | Expands to ranges::views::transform pipe. | No | |
150-
| `\|filter` | Expands to ranges::views::filter pipe. | No | |
151-
| `cpo` | Expands to customize point object. | No | |
152-
| `ns%s(%S+)` | Expands to namespace block (including comments). | No | |
153-
| `itf` | Expands to a struct with default virtual destruction. | No | |
154-
| `pvf` | Expands to a pure virtual function declaration. | No | |
155-
| `qcls` | Expands to a class inherts from QObject. | No | Yes |
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 |
146+
| Trig | Desc | Context Required | Qt | Support Selection |
147+
| :---------: | -------------------------------------------------------------------------------------------------- | :-----------------: | :-: | :---------------: |
148+
| `fn` | Expands to lambda function in argument list or function body, otherwise expand to normal function. | No | | Yes |
149+
| `\|trans` | Expands to ranges::views::transform pipe. | No | | |
150+
| `\|filter` | Expands to ranges::views::filter pipe. | No | | |
151+
| `cpo` | Expands to customize point object. | No | | |
152+
| `ns%s(%S+)` | Expands to namespace block (including comments). | No | | |
153+
| `itf` | Expands to a struct with default virtual destruction. | No | | |
154+
| `pvf` | Expands to a pure virtual function declaration. | No | | |
155+
| `qcls` | Expands to a class inherts from QObject. | No | Yes | |
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 | |
161161

162162
#### Auto-snippets
163163

lua/luasnip-snippets/snippets/cpp/commons.lua

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,41 @@ local function in_header_file()
1717
return false
1818
end
1919

20+
---@param lines string[]
21+
---@return string[]
22+
local function fix_leading_whitespace(lines, indent)
23+
indent = vim.F.if_nil(indent, 2)
24+
local leading_whitespace = string.rep(" ", indent)
25+
local ret = {}
26+
local first = true
27+
for _, line in ipairs(lines) do
28+
if not first then
29+
table.insert(ret, leading_whitespace .. line)
30+
else
31+
first = false
32+
table.insert(ret, line)
33+
end
34+
end
35+
return ret
36+
end
37+
38+
local function add_trailing_slash(lines)
39+
local ret = {}
40+
local max_len = 0
41+
for _, line in ipairs(lines) do
42+
max_len = math.max(max_len, #line)
43+
end
44+
for _, line in ipairs(lines) do
45+
local len = #line
46+
local diff = max_len - len
47+
table.insert(ret, line .. string.rep(" ", diff) .. " \\")
48+
end
49+
return ret
50+
end
51+
2052
return {
2153
header_ext = header_ext,
2254
in_header_file = in_header_file,
55+
fix_leading_whitespace = fix_leading_whitespace,
56+
add_trailing_slash = add_trailing_slash,
2357
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local fmta = require("luasnip.extras.fmt").fmta
77
local CppCommons = require("luasnip-snippets.snippets.cpp.commons")
88
local i = require("luasnip-snippets.nodes").insert_node
99
local c = require("luasnip-snippets.nodes").choice_node
10+
local f = ls.function_node
1011

1112
---@class LSSnippets.Cpp.Fn.Env
1213
---@field CPP_ARGUMENT_START { [1]: number, [2]: number }?
@@ -115,6 +116,9 @@ local function make_lambda_snippet_node(env)
115116
t(""),
116117
t(" mutable"),
117118
}),
119+
selected = f(function()
120+
return CppCommons.fix_leading_whitespace(env.LS_SELECT_RAW)
121+
end),
118122
args = i(2),
119123
}
120124

@@ -123,7 +127,7 @@ local function make_lambda_snippet_node(env)
123127
fmta(
124128
[[
125129
[<captures>](<args>)<specifier> {
126-
<body>
130+
<selected><body>
127131
}
128132
]],
129133
fmt_args
@@ -162,12 +166,15 @@ local function make_function_snippet_node(env)
162166
fmt_args.name = i(3, "name", { desc = "function name" })
163167
fmt_args.args = i(4, "args", { desc = "function arguments" })
164168
fmt_args.specifier = c(5, specifiers, { desc = "specifier" })
169+
fmt_args.selected = f(function()
170+
return CppCommons.fix_leading_whitespace(env.LS_SELECT_RAW)
171+
end)
165172
return sn(
166173
nil,
167174
fmta(
168175
[[
169176
<storage_specifier><inline_inline>auto <name>(<args>)<specifier> ->> <ret> {
170-
<body>
177+
<selected><body>
171178
}
172179
]],
173180
fmt_args

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

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,7 @@ local Cond = require("luasnip-snippets.utils.cond")
1515
local Config = require("luasnip-snippets.config")
1616
---@type luasnip-snippets.utils
1717
local Utils = require("luasnip-snippets.utils")
18-
19-
---@param lines string[]
20-
---@return string[]
21-
local function fix_leading_whitespace(lines, indent)
22-
indent = vim.F.if_nil(indent, 2)
23-
local leading_whitespace = string.rep(" ", indent)
24-
local ret = {}
25-
local first = true
26-
for _, line in ipairs(lines) do
27-
if not first then
28-
table.insert(ret, leading_whitespace .. line)
29-
else
30-
first = false
31-
table.insert(ret, line)
32-
end
33-
end
34-
return ret
35-
end
36-
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
18+
local CppCommons = require("luasnip-snippets.snippets.cpp.commons")
5019

5120
local function has_select_raw_fn(_, _, _)
5221
return Utils.get_buf_var(0, "LUASNIP_SELECT_RAW") ~= nil
@@ -97,7 +66,7 @@ return {
9766
cursor = i(0),
9867
selected = f(function(_, snip)
9968
local _, env = {}, snip.env
100-
return fix_leading_whitespace(env.LS_SELECT_RAW)
69+
return CppCommons.fix_leading_whitespace(env.LS_SELECT_RAW)
10170
end),
10271
}
10372
),
@@ -120,7 +89,7 @@ return {
12089
cursor = i(0),
12190
selected = f(function(_, snip)
12291
local _, env = {}, snip.env
123-
return fix_leading_whitespace(env.LS_SELECT_RAW)
92+
return CppCommons.fix_leading_whitespace(env.LS_SELECT_RAW)
12493
end),
12594
}
12695
),
@@ -144,7 +113,7 @@ return {
144113
cursor = i(0),
145114
selected = f(function(_, snip)
146115
local _, env = {}, snip.env
147-
return fix_leading_whitespace(env.LS_SELECT_RAW)
116+
return CppCommons.fix_leading_whitespace(env.LS_SELECT_RAW)
148117
end),
149118
}
150119
),
@@ -167,7 +136,9 @@ return {
167136
cursor = i(0),
168137
selected = f(function(_, snip)
169138
local _, env = {}, snip.env
170-
return fix_leading_whitespace(add_trailing_slash(env.LS_SELECT_RAW))
139+
return CppCommons.fix_leading_whitespace(
140+
CppCommons.add_trailing_slash(env.LS_SELECT_RAW)
141+
)
171142
end),
172143
}
173144
),

0 commit comments

Comments
 (0)