@@ -5,6 +5,10 @@ local Utils = require("luasnip-snippets.utils")
55local fmt = require (" luasnip.extras.fmt" ).fmt
66local fmta = require (" luasnip.extras.fmt" ).fmta
77local i = require (" luasnip-snippets.nodes" ).insert_node
8+ --- @type luasnip-snippets.nodes
9+ local Nodes = require (" luasnip-snippets.nodes" )
10+ local snippet = Nodes .construct_snippet
11+ local CommonCond = require (" luasnip-snippets.utils.common_cond" )
812
913local expr_query = [[
1014[
@@ -36,41 +40,57 @@ local function expr_tsp(trig, expand, dscr)
3640 end
3741 local replaced = expand :gsub (" ?" , " %%s" )
3842
39- return tsp .treesitter_postfix ({
40- trig = trig ,
41- name = name ,
42- dscr = dscr ,
43- wordTrig = false ,
44- reparseBuffer = " live" ,
45- matchTSNode = {
46- query = expr_query ,
47- query_lang = " cpp" ,
43+ return {
44+ snippet {
45+ trig ,
46+ name = name ,
47+ dscr = dscr ,
48+ mode = " w" ,
49+ lang = " cpp" ,
50+ cond = CommonCond .has_select_raw ,
51+ nodes = {
52+ f (function (_ , snip )
53+ local _ , env = {}, snip .env
54+ return Utils .replace_all (env .LS_SELECT_RAW , replaced )
55+ end ),
56+ },
4857 },
49- }, {
50- f (function (_ , parent )
51- return Utils .replace_all (parent .snippet .env .LS_TSMATCH , replaced )
52- end , {}),
53- })
58+ tsp .treesitter_postfix ({
59+ trig = " ." .. trig ,
60+ name = name ,
61+ dscr = dscr ,
62+ wordTrig = false ,
63+ reparseBuffer = " live" ,
64+ matchTSNode = {
65+ query = expr_query ,
66+ query_lang = " cpp" ,
67+ },
68+ }, {
69+ f (function (_ , parent )
70+ return Utils .replace_all (parent .snippet .env .LS_TSMATCH , replaced )
71+ end , {}),
72+ }),
73+ }
5474end
5575
5676return {
5777 expr_tsp (
58- " . be" ,
78+ " be" ,
5979 " ?.begin(), ?.end()" ,
6080 " Completes an expr with both begin() and end()"
6181 ),
6282 expr_tsp (
63- " . cbe" ,
83+ " cbe" ,
6484 " ?.cbegin(), ?.cend()" ,
6585 " Completes an expr with both cbegin() and cend()"
6686 ),
67- expr_tsp (" . mv" , " std::move(?)" ),
68- expr_tsp (" . fwd" , " std::forward<decltype(?)>(?)" ),
69- expr_tsp (" . val" , " std::declval<?>()" ),
70- expr_tsp (" . dt" , " decltype(?)" ),
71- expr_tsp (" . uu" , " (void)?" ),
72- expr_tsp (" . single" , " ranges::views::single(?)" ),
73- expr_tsp (" . await" , " co_await ?" ),
87+ expr_tsp (" mv" , " std::move(?)" ),
88+ expr_tsp (" fwd" , " std::forward<decltype(?)>(?)" ),
89+ expr_tsp (" val" , " std::declval<?>()" ),
90+ expr_tsp (" dt" , " decltype(?)" ),
91+ expr_tsp (" uu" , " (void)?" ),
92+ expr_tsp (" single" , " ranges::views::single(?)" ),
93+ expr_tsp (" await" , " co_await ?" ),
7494
7595 tsp .treesitter_postfix ({
7696 trig = " .ts" ,
@@ -131,6 +151,28 @@ return {
131151 )
132152 ),
133153
154+ snippet {
155+ " sc" ,
156+ name = " (sc) static_cast<TYPE>(...)" ,
157+ dscr = " Wraps an expression with static_cast<TYPE>(...)" ,
158+ mode = " w" ,
159+ lang = " cpp" ,
160+ cond = CommonCond .has_select_raw ,
161+ nodes = fmt (
162+ [[
163+ static_cast<{body}>({selected}){cursor}
164+ ]] ,
165+ {
166+ cursor = i (0 ),
167+ body = i (1 ),
168+ selected = f (function (_ , snip )
169+ local _ , env = {}, snip .env
170+ return Utils .replace_all (env .LS_SELECT_RAW , " %s" )
171+ end ),
172+ }
173+ ),
174+ },
175+
134176 tsp .treesitter_postfix (
135177 {
136178 trig = " .rc" ,
@@ -157,6 +199,28 @@ return {
157199 )
158200 ),
159201
202+ snippet {
203+ " rc" ,
204+ name = " (rc) reinterpret_cast<TYPE>(...)" ,
205+ dscr = " Wraps an expression with reinterpret_cast<TYPE>(...)" ,
206+ mode = " w" ,
207+ lang = " cpp" ,
208+ cond = CommonCond .has_select_raw ,
209+ nodes = fmt (
210+ [[
211+ reinterpret_cast<{body}>({selected}){cursor}
212+ ]] ,
213+ {
214+ cursor = i (0 ),
215+ body = i (1 ),
216+ selected = f (function (_ , snip )
217+ local _ , env = {}, snip .env
218+ return Utils .replace_all (env .LS_SELECT_RAW , " %s" )
219+ end ),
220+ }
221+ ),
222+ },
223+
160224 tsp .treesitter_postfix (
161225 {
162226 trig = " .in" ,
0 commit comments