Skip to content

Commit ba23c2b

Browse files
committed
Toggle style
1 parent 0933b26 commit ba23c2b

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

lua/luasnip-snippets/snippets/cpp/postfix.lua

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ local expr_query = [[
1818
] @prefix
1919
]]
2020

21+
local indent_query = [[
22+
[
23+
(identifier)
24+
(field_identifier)
25+
] @prefix
26+
]]
27+
2128
---@param trig string
2229
---@param expand string
2330
---@param dscr string?
@@ -58,6 +65,40 @@ return {
5865
expr_tsp(".val", "std::declval<?>()"),
5966
expr_tsp(".dt", "decltype(?)"),
6067
expr_tsp(".uu", "(void)?"),
68+
69+
tsp.treesitter_postfix({
70+
trig = ".cs",
71+
name = "(.cs) Change style",
72+
dscr = "Change previous indent's style",
73+
wordTrig = false,
74+
reparseBuffer = "live",
75+
matchTSNode = {
76+
query = indent_query,
77+
query_lang = "cpp",
78+
},
79+
}, {
80+
f(function(_, parent)
81+
-- switch name style from snake to pascal or vice versa
82+
-- name must be a oneline identifier
83+
local name = table.concat(parent.snippet.env.LS_TSMATCH, "\n")
84+
if name:match("^[A-Z]") then
85+
-- is pascal case now, change to snake case
86+
name = name:gsub("(%u+)(%u%l)", "%1_%2")
87+
name = name:gsub("([a-z0-9])([A-Z])", "%1_%2")
88+
name = name:gsub("-", "_")
89+
return name:lower()
90+
else
91+
-- is snake case now, change to pascal case
92+
return name
93+
:gsub("_(%l)", function(s)
94+
return s:upper()
95+
end)
96+
:gsub("^%l", string.upper)
97+
:gsub("_$", "")
98+
end
99+
end, {}),
100+
}),
101+
61102
tsp.treesitter_postfix(
62103
{
63104
trig = ".sc",

0 commit comments

Comments
 (0)