Skip to content

Commit 7c06d87

Browse files
committed
feat(cpp): add qet
1 parent 25b69bd commit 7c06d87

File tree

2 files changed

+115
-18
lines changed

2 files changed

+115
-18
lines changed

README.md

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,36 @@ ls.setup({
5555

5656
#### Auto-snippets
5757

58-
| Trig | Desc | Context Required |
59-
| :-----: | -------------------------------------------------------- | :---------------------------: |
60-
| `ctor!` | Expand to default constructor | In Class |
61-
| `dtor!` | Expand to default destructor | In Class |
62-
| `cc!` | Expand to default copy constructor | In Class |
63-
| `mv!` | Expand to default move constructor | In Class |
64-
| `ncc!` | Expand to delete copy constructor | In Class |
65-
| `nmv!` | Expand to delete move constructor | In Class |
66-
| `ncm!` | Expand to delete copy and move constructor | In Class |
67-
| `once` | Expand to `pragma once` marker at the front of the file. | All lines before are comments |
68-
| `u8` | Expand to `uint8_t`. | No |
69-
| `u16` | Expand to `uint16_t`. | No |
70-
| `u32` | Expand to `uint32_t`. | No |
71-
| `u64` | Expand to `uint64_t`. | No |
72-
| `i8` | Expand to `int8_t`. | No |
73-
| `i16` | Expand to `int16_t`. | No |
74-
| `i32` | Expand to `int32_t`. | No |
75-
| `i64` | Expand to `int64_t`. | No |
58+
| Trig | Desc | Context Required |
59+
| :------: | -------------------------------------------------------- | :---------------------------: |
60+
| `ctor!` | Expand to default constructor | In Class |
61+
| `dtor!` | Expand to default destructor | In Class |
62+
| `cc!` | Expand to default copy constructor | In Class |
63+
| `mv!` | Expand to default move constructor | In Class |
64+
| `ncc!` | Expand to delete copy constructor | In Class |
65+
| `nmv!` | Expand to delete move constructor | In Class |
66+
| `ncm!` | Expand to delete copy and move constructor | In Class |
67+
| `once` | Expand to `pragma once` marker at the front of the file. | All lines before are comments |
68+
| `u8` | Expand to `uint8_t`. | No |
69+
| `u16` | Expand to `uint16_t`. | No |
70+
| `u32` | Expand to `uint32_t`. | No |
71+
| `u64` | Expand to `uint64_t`. | No |
72+
| `i8` | Expand to `int8_t`. | No |
73+
| `i16` | Expand to `int16_t`. | No |
74+
| `i32` | Expand to `int32_t`. | No |
75+
| `i64` | Expand to `int64_t`. | No |
76+
| `t(%s)!` | Evaluate (QET) marker, and expand to typename. | No |
77+
78+
##### Quick Expand Type markers
79+
80+
| Marker | Expand Type | Parameter |
81+
| :----: | :-------------------- | :-------: |
82+
| `v` | `std::vector` | 1 |
83+
| `i` | `int32_t` | 0 |
84+
| `u` | `uint32_t` | 0 |
85+
| `s` | `std::string` | 0 |
86+
| `m` | `absl::flat_hash_map` | 2 |
87+
| `t` | `std::tuple` | `*` |
7688

7789
#### Postfix Snippets
7890

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

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,70 @@ local all_lines_before_are_all_comments =
9696
"^%s*$",
9797
}
9898

99+
---@param shortcut string
100+
---@return string?
101+
local function quick_type(shortcut)
102+
-- v = std::vector, 1
103+
-- i = int32_t, 0
104+
-- s = std::string, 0
105+
-- u = uint32_t, 0
106+
-- m = absl::flat_hash_map, 2
107+
-- t = std::tuple, *
108+
---@param s string
109+
---@return string?, string?
110+
local function expect_typename(s)
111+
local first, rest = s:match("^(%l)(.*)$")
112+
if first == nil then
113+
return nil, nil
114+
end
115+
if first == "v" then
116+
local typename, sub_rest = expect_typename(rest)
117+
if typename == nil then
118+
return "std::vector", rest
119+
else
120+
return ("std::vector<%s>"):format(typename), sub_rest
121+
end
122+
elseif first == "i" then
123+
return "int32_t", rest
124+
elseif first == "s" then
125+
return "std::string", rest
126+
elseif first == "u" then
127+
return "uint32_t", rest
128+
elseif first == "m" then
129+
local key_type, key_rest = expect_typename(rest)
130+
if key_type == nil or key_rest == nil then
131+
return "absl::flat_hash_map", rest
132+
end
133+
local value_type, value_rest = expect_typename(key_rest)
134+
if value_type == nil or value_rest == nil then
135+
return "absl::flat_hash_map", rest
136+
end
137+
return ("absl::flat_hash_map<%s, %s>"):format(key_type, value_type),
138+
value_rest
139+
elseif first == "t" then
140+
local parameters = {}
141+
while #rest > 0 do
142+
local typename, sub_rest = expect_typename(rest)
143+
if typename == nil or sub_rest == nil then
144+
if #parameters == 0 then
145+
return "std::tuple", rest
146+
end
147+
return ("std::tuple<%s>"):format(table.concat(parameters, ", ")), rest
148+
end
149+
parameters[#parameters + 1] = typename
150+
rest = sub_rest
151+
end
152+
return ("std::tuple<%s>"):format(table.concat(parameters, ", ")), rest
153+
end
154+
end
155+
156+
local result, rest = expect_typename(shortcut)
157+
if rest and #rest > 0 then
158+
print(("After QET eval, rest not empty: %s"):format(rest))
159+
end
160+
return result
161+
end
162+
99163
return {
100164
cpo_snippet,
101165

@@ -123,4 +187,25 @@ return {
123187
int_type_snippet(32, false),
124188
int_type_snippet(64, true),
125189
int_type_snippet(64, false),
190+
191+
-- quick expand, expand stl types
192+
-- v = std::vector
193+
-- i = int32_t
194+
-- s = std::string
195+
-- u = uint32_t
196+
-- m = absl::flat_hash_map
197+
-- t = std::tuple
198+
ls.s({
199+
trig = "t(%l+)!",
200+
wordTrig = true,
201+
regTrig = true,
202+
snippetType = "autosnippet",
203+
name = "(t) Quick types",
204+
desc = "Expands to a type",
205+
}, {
206+
f(function(_, snip)
207+
local shortcut = snip.captures[1]
208+
return quick_type(shortcut)
209+
end),
210+
}),
126211
}

0 commit comments

Comments
 (0)