Skip to content

Commit f4e2a25

Browse files
committed
feat(cpp): add a new snippets, and can disable cpplint comments now
1 parent e7e034d commit f4e2a25

File tree

4 files changed

+101
-25
lines changed

4 files changed

+101
-25
lines changed

README.md

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ Config Example:
5959
},
6060
-- enable qt-related snippets
6161
qt = true,
62+
-- whether to add cpplint related comments in some snippets
63+
cpplint = true,
6264
},
6365
},
6466
rust = {
@@ -154,28 +156,29 @@ Snippets with `*` are available only when `vim_snippet` is enabled.
154156

155157
#### Auto-snippets
156158

157-
| Trig | Desc | Context Required | Could Disable AutoExpansion | Qt |
158-
| :------: | --------------------------------------------------------- | :---------------------------: | :-------------------------: | :-: |
159-
| `ctor!` | Expands to default constructor. | In Class | No | |
160-
| `dtor!` | Expands to default destructor. | In Class | No | |
161-
| `cc!` | Expands to default copy constructor. | In Class | No | |
162-
| `mv!` | Expands to default move constructor. | In Class | No | |
163-
| `ncc!` | Expands to delete copy constructor. | In Class | No | |
164-
| `nmv!` | Expands to delete move constructor. | In Class | No | |
165-
| `ncm!` | Expands to delete copy and move constructor. | In Class | No | |
166-
| `once` | Expands to `pragma once` marker at the front of the file. | All lines before are comments | Yes | |
167-
| `u8` | Expands to `uint8_t`. | No | Yes | |
168-
| `u16` | Expands to `uint16_t`. | No | Yes | |
169-
| `u32` | Expands to `uint32_t`. | No | Yes | |
170-
| `u64` | Expands to `uint64_t`. | No | Yes | |
171-
| `i8` | Expands to `int8_t`. | No | Yes | |
172-
| `i16` | Expands to `int16_t`. | No | Yes | |
173-
| `i32` | Expands to `int32_t`. | No | Yes | |
174-
| `i64` | Expands to `int64_t`. | No | Yes | |
175-
| `t(%s)!` | Evaluates (QET) marker, and expand to typename. | No | No | |
176-
| `#"` | Expands to include statement with quotes. `#include ""`. | No | Yes | |
177-
| `#<` | Expands to include statement with `<>`. `#include <>`. | No | Yes | |
178-
| `#q` | Expands to include qt generated moc file. | No | Yes | Yes |
159+
| Trig | Desc | Context Required | Could Disable AutoExpansion | Qt | Support Selection |
160+
| :------: | --------------------------------------------------------- | :---------------------------: | :-------------------------: | :-: | :---------------: |
161+
| `ctor!` | Expands to default constructor. | In Class | No | | |
162+
| `dtor!` | Expands to default destructor. | In Class | No | | |
163+
| `cc!` | Expands to default copy constructor. | In Class | No | | |
164+
| `mv!` | Expands to default move constructor. | In Class | No | | |
165+
| `ncc!` | Expands to delete copy constructor. | In Class | No | | |
166+
| `nmv!` | Expands to delete move constructor. | In Class | No | | |
167+
| `ncm!` | Expands to delete copy and move constructor. | In Class | No | | |
168+
| `once` | Expands to `pragma once` marker at the front of the file. | All lines before are comments | Yes | | |
169+
| `u8` | Expands to `uint8_t`. | No | Yes | | |
170+
| `u16` | Expands to `uint16_t`. | No | Yes | | |
171+
| `u32` | Expands to `uint32_t`. | No | Yes | | |
172+
| `u64` | Expands to `uint64_t`. | No | Yes | | |
173+
| `i8` | Expands to `int8_t`. | No | Yes | | |
174+
| `i16` | Expands to `int16_t`. | No | Yes | | |
175+
| `i32` | Expands to `int32_t`. | No | Yes | | |
176+
| `i64` | Expands to `int64_t`. | No | Yes | | |
177+
| `t(%s)!` | Evaluates (QET) marker, and expand to typename. | No | No | | |
178+
| `#"` | Expands to include statement with quotes. `#include ""`. | No | Yes | | |
179+
| `#<` | Expands to include statement with `<>`. `#include <>`. | No | Yes | | |
180+
| `#q` | Expands to include qt generated moc file. | No | Yes | Yes | |
181+
| `#?` | Expands to `ifdef ... endif` fragment. | No | Yes | | Yes |
179182

180183
##### Quick Expand Type markers
181184

@@ -229,6 +232,15 @@ tmss! -> absl::flat_hash_map<std::string, std::string>
229232
| `.await` | Expands to `co_await ?`. | `any_expr` |
230233
| `.in` | Expands to `if (...find)` statements. | `any_expr` |
231234

235+
#### Cpplint
236+
237+
Currently, some snippets will be expanded with cpplint related comments, e.g. `once`(which will expand to `#pragma once // NOLINT(build/header_guard)`).
238+
239+
You can control whether to add cpplint related comments in these snippets by:
240+
241+
- Updating `snippet.cpp.cpplint` in your config. This will affect all buffers.
242+
- Setting buffer variable `b:LuasnipSnippetsCppCppLint`. This will only affect the current buffer, and it will override the global setting.
243+
232244
</details>
233245

234246
<details>

lua/luasnip-snippets/config.lua

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
---@class LSSnippets.Config.Snippet.Cpp
1818
---@field quick_type? LSSnippets.Config.Snippet.Cpp.QuickType
1919
---@field qt? boolean Enable Qt related snippets.
20+
---@field cpplint? boolean Whether to add cpplint related comments in some snippets.
2021

2122
---@class LSSnippets.Config.Snippet.Rust
2223
---@field rstest_support? boolean
@@ -59,7 +60,16 @@ local function get(key)
5960
return value
6061
end
6162

62-
---return bool
63+
---@generic T
64+
---@param key string
65+
---@param default T
66+
---@return T
67+
local function get_default(key, default)
68+
local value = get(key)
69+
return value == nil and default or value
70+
end
71+
72+
---@return boolean
6373
local function auto_expansion_disabled(lang, trig)
6474
---@type luasnip-snippets.utils.tbl
6575
local Tbl = require("luasnip-snippets.utils.tbl")
@@ -72,6 +82,7 @@ end
7282
local M = {
7383
setup = setup,
7484
get = get,
85+
get_default = get_default,
7586
auto_expansion_disabled = auto_expansion_disabled,
7687
}
7788

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

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ local c = Nodes.choice_node
77
local fmta = require("luasnip.extras.fmt").fmta
88
local f = ls.function_node
99
local t = ls.text_node
10+
local rep = require("luasnip.extras").rep
1011
local CommonCond = require("luasnip-snippets.utils.common_cond")
12+
---@type luasnip-snippets.config
13+
local Config = require("luasnip-snippets.config")
14+
---@type luasnip-snippets.utils
15+
local Utils = require("luasnip-snippets.utils")
1116

1217
local function cpo_snippet()
1318
local function cpo_func_to_namespace(name)
@@ -111,7 +116,6 @@ local default_quick_markers = {
111116
---@return string?
112117
local function quick_type(shortcut)
113118
---@type luasnip-snippets.config
114-
local Config = require("luasnip-snippets.config")
115119
local quick_markers = Config.get("snippet.cpp.quick_type.extra_trig") or {}
116120
local markers = vim.deepcopy(default_quick_markers)
117121
for _, marker in ipairs(quick_markers) do
@@ -190,7 +194,20 @@ return {
190194
lang = "cpp",
191195
cond = all_lines_before_are_all_comments,
192196
nodes = {
193-
t { "#pragma once // NOLINT(build/header_guard)", "" },
197+
f(function()
198+
local buffer_settings =
199+
Utils.get_buf_var(0, "LuasnipSnippetsCppCppLint")
200+
201+
local use_cpplint = vim.F.if_nil(
202+
buffer_settings,
203+
Config.get_default("snippet.cpp.cpplint", true)
204+
)
205+
if use_cpplint then
206+
return { "#pragma once // NOLINT(build/header_guard)", "" }
207+
else
208+
return { "#pragma once", "" }
209+
end
210+
end),
194211
},
195212
},
196213

@@ -220,6 +237,35 @@ return {
220237
},
221238
},
222239

240+
-- preprocessor directives short cuts
241+
snippet {
242+
"#?",
243+
name = "#ifdef",
244+
dscr = "Expands to #ifdef ... #endif",
245+
mode = "bA",
246+
lang = "cpp",
247+
nodes = fmta(
248+
[[
249+
#<directive> <name>
250+
<selected><cursor>
251+
#endif // <name_r>
252+
]],
253+
{
254+
directive = c(1, {
255+
t("ifdef"),
256+
t("ifndef"),
257+
}, { desc = "Directive" }),
258+
name = i(2, "name"),
259+
cursor = i(0),
260+
selected = f(function(_, snip)
261+
local _, env = {}, snip.env
262+
return env.LS_SELECT_RAW
263+
end),
264+
name_r = rep(2),
265+
}
266+
),
267+
},
268+
223269
-- fast int types
224270
int_type_snippet(8, true),
225271
int_type_snippet(8, false),

lua/luasnip-snippets/utils/init.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,11 @@ function M.reverse_list(lst)
4646
end
4747
end
4848

49+
function M.get_buf_var(bufnr, key)
50+
local succ, value = pcall(vim.api.nvim_buf_get_var, bufnr, key)
51+
if succ then
52+
return value
53+
end
54+
end
55+
4956
return M

0 commit comments

Comments
 (0)