Skip to content

Commit bc2191d

Browse files
committed
feat(cpp,config): new config to add more quick_type shortcuts
1 parent 392bb02 commit bc2191d

File tree

3 files changed

+105
-60
lines changed

3 files changed

+105
-60
lines changed

README.md

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ ls.setup({
3737

3838
# ⚙️ Configuration
3939

40+
Config Example:
4041
```lua
4142
---@type LSSnippets.Config
4243
{
@@ -48,7 +49,19 @@ ls.setup({
4849
lua = {
4950
-- enable neovim related snippets in lua
5051
vim_snippet = false,
51-
}
52+
},
53+
cpp = {
54+
quick_type = {
55+
-- use `std::unordered_map` instead of `absl::flat_hash_map`
56+
extra_trig = {
57+
{ trig = 'm', params = 2, template = 'std::unordered_map<%s, %s>' }
58+
}
59+
},
60+
},
61+
},
62+
disable_auto_expansion = {
63+
-- disable these snippets' auto expansion
64+
cpp = { "i32", "i64" },
5265
}
5366
}
5467
```
@@ -123,27 +136,27 @@ Snippets with `*` are available only when `vim_snippet` is enabled.
123136

124137
#### Auto-snippets
125138

126-
| Trig | Desc | Context Required |
127-
| :------: | --------------------------------------------------------- | :---------------------------: |
128-
| `ctor!` | Expands to default constructor. | In Class |
129-
| `dtor!` | Expands to default destructor. | In Class |
130-
| `cc!` | Expands to default copy constructor. | In Class |
131-
| `mv!` | Expands to default move constructor. | In Class |
132-
| `ncc!` | Expands to delete copy constructor. | In Class |
133-
| `nmv!` | Expands to delete move constructor. | In Class |
134-
| `ncm!` | Expands to delete copy and move constructor. | In Class |
135-
| `once` | Expands to `pragma once` marker at the front of the file. | All lines before are comments |
136-
| `u8` | Expands to `uint8_t`. | No |
137-
| `u16` | Expands to `uint16_t`. | No |
138-
| `u32` | Expands to `uint32_t`. | No |
139-
| `u64` | Expands to `uint64_t`. | No |
140-
| `i8` | Expands to `int8_t`. | No |
141-
| `i16` | Expands to `int16_t`. | No |
142-
| `i32` | Expands to `int32_t`. | No |
143-
| `i64` | Expands to `int64_t`. | No |
144-
| `t(%s)!` | Evaluates (QET) marker, and expand to typename. | No |
145-
| `#"` | Expands to include statement with quotes. `#include ""`. | No |
146-
| `#<` | Expands to include statement with `<>`. `#include <>`. | No |
139+
| Trig | Desc | Context Required | Could Disable AutoExpansion |
140+
| :------: | --------------------------------------------------------- | :---------------------------: | :-------------------------: |
141+
| `ctor!` | Expands to default constructor. | In Class | No |
142+
| `dtor!` | Expands to default destructor. | In Class | No |
143+
| `cc!` | Expands to default copy constructor. | In Class | No |
144+
| `mv!` | Expands to default move constructor. | In Class | No |
145+
| `ncc!` | Expands to delete copy constructor. | In Class | No |
146+
| `nmv!` | Expands to delete move constructor. | In Class | No |
147+
| `ncm!` | Expands to delete copy and move constructor. | In Class | No |
148+
| `once` | Expands to `pragma once` marker at the front of the file. | All lines before are comments | Yes |
149+
| `u8` | Expands to `uint8_t`. | No | Yes |
150+
| `u16` | Expands to `uint16_t`. | No | Yes |
151+
| `u32` | Expands to `uint32_t`. | No | Yes |
152+
| `u64` | Expands to `uint64_t`. | No | Yes |
153+
| `i8` | Expands to `int8_t`. | No | Yes |
154+
| `i16` | Expands to `int16_t`. | No | Yes |
155+
| `i32` | Expands to `int32_t`. | No | Yes |
156+
| `i64` | Expands to `int64_t`. | No | Yes |
157+
| `t(%s)!` | Evaluates (QET) marker, and expand to typename. | No | No |
158+
| `#"` | Expands to include statement with quotes. `#include ""`. | No | Yes |
159+
| `#<` | Expands to include statement with `<>`. `#include <>`. | No | Yes |
147160

148161
##### Quick Expand Type markers
149162

@@ -156,6 +169,13 @@ Snippets with `*` are available only when `vim_snippet` is enabled.
156169
| `m` | `absl::flat_hash_map` | 2 |
157170
| `t` | `std::tuple` | `*` |
158171

172+
Example:
173+
174+
```
175+
tvi! -> std::vector<int32_t>
176+
tmss! -> absl::flat_hash_map<std::string, std::string>
177+
```
178+
159179
#### Postfix Snippets
160180

161181
```scheme

lua/luasnip-snippets/config.lua

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,23 @@
66
---@field vim_snippet? boolean
77
---@field cond? fun():boolean
88

9+
---@class LSSnippets.Config.Snippet.Cpp.QuickType
10+
---@field extra_trig? LSSnippets.Config.Snippet.Cpp.QuickType.Shortcut[]
11+
12+
---@class LSSnippets.Config.Snippet.Cpp.QuickType.Shortcut
13+
---@field trig string One character trigger. Supports lowercase letters only.
14+
---@field params number Number of template parameters.
15+
---@field template string Template string.
16+
17+
---@class LSSnippets.Config.Snippet.Cpp
18+
---@field quick_type? LSSnippets.Config.Snippet.Cpp.QuickType
19+
920
---@alias LSSnippets.Config.Snippet.DisableSnippets string[]
1021
---@alias LSSnippets.SupportLangs 'cpp'|'dart'|'lua'|'rust'|'nix'|'typescript'|'*'
1122

1223
---@class LSSnippets.Config.Snippet
13-
---@field lua LSSnippets.Config.Snippet.Lua
24+
---@field lua? LSSnippets.Config.Snippet.Lua
25+
---@field cpp? LSSnippets.Config.Snippet.Cpp
1426

1527
---@class LSSnippets.Config
1628
---@field copyright_header? string

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

Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -98,68 +98,81 @@ local all_lines_before_are_all_comments =
9898
"^%s*$",
9999
}
100100

101+
local default_quick_markers = {
102+
v = { params = 1, template = "std::vector<%s>" },
103+
i = { params = 0, template = "int32_t" },
104+
s = { params = 0, template = "std::string" },
105+
u = { params = 0, template = "uint32_t" },
106+
m = { params = 2, template = "absl::flat_hash_map<%s, %s>" },
107+
t = { params = -1, template = "std::tuple<%s>" },
108+
}
109+
101110
---@param shortcut string
102111
---@return string?
103112
local function quick_type(shortcut)
104-
-- v = std::vector, 1
105-
-- i = int32_t, 0
106-
-- s = std::string, 0
107-
-- u = uint32_t, 0
108-
-- m = absl::flat_hash_map, 2
109-
-- t = std::tuple, *
113+
---@type luasnip-snippets.config
114+
local Config = require("luasnip-snippets.config")
115+
local quick_markers = Config.get("snippet.cpp.quick_type.extra_trig") or {}
116+
local markers = vim.deepcopy(default_quick_markers)
117+
for _, marker in ipairs(quick_markers) do
118+
markers[marker.trig] = {
119+
params = marker.params,
120+
template = marker.template,
121+
}
122+
end
123+
110124
---@param s string
111125
---@return string?, string?
112126
local function expect_typename(s)
113127
local first, rest = s:match("^(%l)(.*)$")
114128
if first == nil then
115129
return nil, nil
116130
end
117-
if first == "v" then
118-
local typename, sub_rest = expect_typename(rest)
119-
if typename == nil then
120-
return "std::vector", rest
121-
else
122-
return ("std::vector<%s>"):format(typename), sub_rest
123-
end
124-
elseif first == "i" then
125-
return "int32_t", rest
126-
elseif first == "s" then
127-
return "std::string", rest
128-
elseif first == "u" then
129-
return "uint32_t", rest
130-
elseif first == "m" then
131-
local key_type, key_rest = expect_typename(rest)
132-
if key_type == nil or key_rest == nil then
133-
return "absl::flat_hash_map", rest
134-
end
135-
local value_type, value_rest = expect_typename(key_rest)
136-
if value_type == nil or value_rest == nil then
137-
return "absl::flat_hash_map", rest
138-
end
139-
return ("absl::flat_hash_map<%s, %s>"):format(key_type, value_type),
140-
value_rest
141-
elseif first == "t" then
131+
132+
local trig = markers[first]
133+
if trig == nil then
134+
return nil, nil
135+
end
136+
137+
if trig.params == -1 then
142138
local parameters = {}
143139
while #rest > 0 do
144140
local typename, sub_rest = expect_typename(rest)
145141
if typename == nil or sub_rest == nil then
146-
if #parameters == 0 then
147-
return "std::tuple", rest
148-
end
149-
return ("std::tuple<%s>"):format(table.concat(parameters, ", ")), rest
142+
break
150143
end
151144
parameters[#parameters + 1] = typename
152145
rest = sub_rest
153146
end
154-
return ("std::tuple<%s>"):format(table.concat(parameters, ", ")), rest
147+
return (trig.template):format(table.concat(parameters, ", ")), rest
148+
end
149+
150+
if trig.params == 0 then
151+
return trig.template, rest
152+
end
153+
154+
local parameters = {}
155+
for _ = 1, trig.params do
156+
local typename, sub_rest = expect_typename(rest)
157+
if typename == nil or sub_rest == nil then
158+
return nil, rest
159+
end
160+
parameters[#parameters + 1] = typename
161+
rest = sub_rest
155162
end
163+
164+
return string.format(trig.template, unpack(parameters)), rest
156165
end
157166

158167
local result, rest = expect_typename(shortcut)
159168
if rest and #rest > 0 then
160169
print(("After QET eval, rest not empty: %s"):format(rest))
161170
end
162-
return result
171+
if result == nil then
172+
return shortcut
173+
else
174+
return result
175+
end
163176
end
164177

165178
return {

0 commit comments

Comments
 (0)