Skip to content

Commit e7e034d

Browse files
committed
feat(cpp): add two qt-related snippets, and a option to disable these
1 parent 1ddc00a commit e7e034d

File tree

4 files changed

+93
-34
lines changed

4 files changed

+93
-34
lines changed

README.md

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ Config Example:
5656
-- use `std::unordered_map` instead of `absl::flat_hash_map`
5757
extra_trig = {
5858
{ trig = 'm', params = 2, template = 'std::unordered_map<%s, %s>' }
59-
}
59+
},
60+
-- enable qt-related snippets
61+
qt = true,
6062
},
6163
},
6264
rust = {
@@ -139,39 +141,41 @@ Snippets with `*` are available only when `vim_snippet` is enabled.
139141

140142
#### Normal Snippets
141143

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

152155
#### Auto-snippets
153156

154-
| Trig | Desc | Context Required | Could Disable AutoExpansion |
155-
| :------: | --------------------------------------------------------- | :---------------------------: | :-------------------------: |
156-
| `ctor!` | Expands to default constructor. | In Class | No |
157-
| `dtor!` | Expands to default destructor. | In Class | No |
158-
| `cc!` | Expands to default copy constructor. | In Class | No |
159-
| `mv!` | Expands to default move constructor. | In Class | No |
160-
| `ncc!` | Expands to delete copy constructor. | In Class | No |
161-
| `nmv!` | Expands to delete move constructor. | In Class | No |
162-
| `ncm!` | Expands to delete copy and move constructor. | In Class | No |
163-
| `once` | Expands to `pragma once` marker at the front of the file. | All lines before are comments | Yes |
164-
| `u8` | Expands to `uint8_t`. | No | Yes |
165-
| `u16` | Expands to `uint16_t`. | No | Yes |
166-
| `u32` | Expands to `uint32_t`. | No | Yes |
167-
| `u64` | Expands to `uint64_t`. | No | Yes |
168-
| `i8` | Expands to `int8_t`. | No | Yes |
169-
| `i16` | Expands to `int16_t`. | No | Yes |
170-
| `i32` | Expands to `int32_t`. | No | Yes |
171-
| `i64` | Expands to `int64_t`. | No | Yes |
172-
| `t(%s)!` | Evaluates (QET) marker, and expand to typename. | No | No |
173-
| `#"` | Expands to include statement with quotes. `#include ""`. | No | Yes |
174-
| `#<` | Expands to include statement with `<>`. `#include <>`. | No | Yes |
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 |
175179

176180
##### Quick Expand Type markers
177181

@@ -220,7 +224,7 @@ tmss! -> absl::flat_hash_map<std::string, std::string>
220224
| `.uu` | Wraps with `(void)?`. | `any_expr` |
221225
| `.ts` | Switches indent's coding style between `CamelCase` and `snake_case`. | `indent` |
222226
| `.sc` | Wraps with `static_cast<>(?)`. | `any_expr` |
223-
| `.rc` | Wraps with `reinterpret_cast<>(?)`. | `any_expr` |
227+
| `.rc` | Wraps with `reinterpret_cast<>(?)`. | `any_expr` |
224228
| `.single` | Wraps with `ranges::views::single(?)`. | `any_expr` |
225229
| `.await` | Expands to `co_await ?`. | `any_expr` |
226230
| `.in` | Expands to `if (...find)` statements. | `any_expr` |

lua/luasnip-snippets/config.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
---@class LSSnippets.Config.Snippet.Cpp
1818
---@field quick_type? LSSnippets.Config.Snippet.Cpp.QuickType
19+
---@field qt? boolean Enable Qt related snippets.
1920

2021
---@class LSSnippets.Config.Snippet.Rust
2122
---@field rstest_support? boolean
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
local Utils = require("luasnip-snippets.utils")
22

33
local function setup()
4-
return Utils.concat_snippets("luasnip-snippets.snippets.cpp", {
4+
local collections = {
55
"statements",
66
"lambda_fn",
77
"postfix",
88
"default",
9-
})
9+
}
10+
11+
local Config = require("luasnip-snippets.config")
12+
local qt_enabled = vim.F.if_nil(Config.get("snippet.cpp.qt"), true)
13+
14+
if qt_enabled then
15+
collections[#collections + 1] = "qt"
16+
end
17+
18+
return Utils.concat_snippets("luasnip-snippets.snippets.cpp", collections)
1019
end
1120

1221
return setup
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
local ls = require("luasnip")
2+
local UtilsTS = require("luasnip-snippets.utils.treesitter")
3+
local d = ls.dynamic_node
4+
local sn = ls.snippet_node
5+
local t = ls.text_node
6+
local fmta = require("luasnip.extras.fmt").fmta
7+
local snippet = require("luasnip-snippets.nodes").construct_snippet
8+
local i = require("luasnip-snippets.nodes").insert_node
9+
local rep = require("luasnip.extras").rep
10+
11+
return {
12+
snippet {
13+
"qcls",
14+
name = "Q_OBJECT class",
15+
dscr = "Declare a class with Q_OBJECT macro",
16+
mode = "bw",
17+
nodes = fmta(
18+
[[
19+
class <> : public QObject {
20+
Q_OBJECT
21+
22+
public:
23+
~<>() = default;
24+
};
25+
]],
26+
{
27+
i(1, "Class Name"),
28+
rep(1),
29+
}
30+
),
31+
},
32+
snippet {
33+
"#q",
34+
name = "include qt MOC",
35+
dscr = "#include qt generated MOC file",
36+
mode = "bA",
37+
lang = "cpp",
38+
nodes = {
39+
t((function()
40+
local filename = vim.fn.expand("%:t")
41+
return ('#include "moc_%s"'):format(filename)
42+
end)()),
43+
},
44+
},
45+
}

0 commit comments

Comments
 (0)