Skip to content

Commit 4fe7846

Browse files
committed
feat(lua): init lua snippets
1 parent dc1ecde commit 4fe7846

File tree

3 files changed

+236
-57
lines changed

3 files changed

+236
-57
lines changed

README.md

Lines changed: 85 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -41,44 +41,72 @@ ls.setup({
4141

4242
</details>
4343

44+
<details>
45+
<summary>Lua</summary>
46+
47+
#### Normal Snippets
48+
49+
| Trig | Desc | Context Required |
50+
| :---: | ------------------------------------ | :--------------: |
51+
| `fn` | Expands to function definition. | No |
52+
| `req` | Expands to `require(...)` statement. | No |
53+
54+
#### Postfix Snippets
55+
56+
```scheme
57+
[
58+
(function_call)
59+
(identifier)
60+
(expression_list)
61+
] @any_expr
62+
```
63+
64+
| Trig | Desc (placehoder: `?`) | Expr before cursor |
65+
| :-------: | ---------------------------------------- | :----------------: |
66+
| `.ipairs` | Expands to `ipairs(?)` for-loop. | `any_expr` |
67+
| `.pairs` | Expands to `pairs(?)` for-loop. | `any_expr` |
68+
| `.isnil` | Expands to `if ? == nil then` statement. | `any_expr` |
69+
70+
</details>
71+
4472
<details>
4573
<summary>Cpp</summary>
4674

4775
#### Normal Snippets
4876

49-
| Trig | Desc | Context Required |
50-
| :---------: | ------------------------------------------------------------------------------------------------ | :--------------: |
51-
| `fn` | Expand to lambda function in argument list or function body, otherwise expand to normal function | No |
52-
| `\|trans` | Expand to ranges::views::transform pipe. | No |
53-
| `\|filter` | Expand to ranges::views::filter pipe. | No |
54-
| `cpo` | Expand to customize point object. | No |
55-
| `ns%s(%S+)` | Expand to namespace block (including comments). | No |
56-
| `itf` | Expand to a struct with default virtual destruction. | No |
57-
| `pvf` | Expand to a pure virtual function declaration. | No |
77+
| Trig | Desc | Context Required |
78+
| :---------: | -------------------------------------------------------------------------------------------------- | :--------------: |
79+
| `fn` | Expands to lambda function in argument list or function body, otherwise expand to normal function. | No |
80+
| `\|trans` | Expands to ranges::views::transform pipe. | No |
81+
| `\|filter` | Expands to ranges::views::filter pipe. | No |
82+
| `cpo` | Expands to customize point object. | No |
83+
| `ns%s(%S+)` | Expands to namespace block (including comments). | No |
84+
| `itf` | Expands to a struct with default virtual destruction. | No |
85+
| `pvf` | Expands to a pure virtual function declaration. | No |
5886

5987
#### Auto-snippets
6088

61-
| Trig | Desc | Context Required |
62-
| :------: | -------------------------------------------------------- | :---------------------------: |
63-
| `ctor!` | Expand to default constructor | In Class |
64-
| `dtor!` | Expand to default destructor | In Class |
65-
| `cc!` | Expand to default copy constructor | In Class |
66-
| `mv!` | Expand to default move constructor | In Class |
67-
| `ncc!` | Expand to delete copy constructor | In Class |
68-
| `nmv!` | Expand to delete move constructor | In Class |
69-
| `ncm!` | Expand to delete copy and move constructor | In Class |
70-
| `once` | Expand to `pragma once` marker at the front of the file. | All lines before are comments |
71-
| `u8` | Expand to `uint8_t`. | No |
72-
| `u16` | Expand to `uint16_t`. | No |
73-
| `u32` | Expand to `uint32_t`. | No |
74-
| `u64` | Expand to `uint64_t`. | No |
75-
| `i8` | Expand to `int8_t`. | No |
76-
| `i16` | Expand to `int16_t`. | No |
77-
| `i32` | Expand to `int32_t`. | No |
78-
| `i64` | Expand to `int64_t`. | No |
79-
| `t(%s)!` | Evaluate (QET) marker, and expand to typename. | No |
80-
| `#"` | Expand to include statement with quotes. `#include ""`. | No |
81-
| `#<` | Expand to include statement with `<>`. `#include <>`. | No |
89+
| Trig | Desc | Context Required |
90+
| :------: | --------------------------------------------------------- | :---------------------------: |
91+
| `ctor!` | Expands to default constructor. | In Class |
92+
| `dtor!` | Expands to default destructor. | In Class |
93+
| `cc!` | Expands to default copy constructor. | In Class |
94+
| `mv!` | Expands to default move constructor. | In Class |
95+
| `ncc!` | Expands to delete copy constructor. | In Class |
96+
| `nmv!` | Expands to delete move constructor. | In Class |
97+
| `ncm!` | Expands to delete copy and move constructor. | In Class |
98+
| `once` | Expands to `pragma once` marker at the front of the file. | All lines before are comments |
99+
| `u8` | Expands to `uint8_t`. | No |
100+
| `u16` | Expands to `uint16_t`. | No |
101+
| `u32` | Expands to `uint32_t`. | No |
102+
| `u64` | Expands to `uint64_t`. | No |
103+
| `i8` | Expands to `int8_t`. | No |
104+
| `i16` | Expands to `int16_t`. | No |
105+
| `i32` | Expands to `int32_t`. | No |
106+
| `i64` | Expands to `int64_t`. | No |
107+
| `t(%s)!` | Evaluates (QET) marker, and expand to typename. | No |
108+
| `#"` | Expands to include statement with quotes. `#include ""`. | No |
109+
| `#<` | Expands to include statement with `<>`. `#include <>`. | No |
82110

83111
##### Quick Expand Type markers
84112

@@ -109,18 +137,18 @@ ls.setup({
109137
] @any_expr
110138
```
111139

112-
| Trig | Desc (placehoder: `?`) | Expr before cursor |
113-
| :-------: | ------------------------------------------------------------------ | :----------------: |
114-
| `.be` | Expand to begin and end exprs | `any_expr` |
115-
| `.mv` | Wraps with `std::move(?)` | `any_expr` |
116-
| `.fwd` | Wraps with `std::forward<decltype(?)>(?)` | `any_expr` |
117-
| `.val` | Wraps with `std::declval<?>()` | `any_expr` |
118-
| `.dt` | Wraps with `decltype(?)` | `any_expr` |
119-
| `.uu` | Wraps with `(void)?` | `any_expr` |
120-
| `.ts` | Switch indent's coding style between `CamelCase` and `snake_case`. | `indent` |
121-
| `.sc` | Wraps with `static_cast<>(?)` | `any_expr` |
122-
| `.single` | Wraps with `ranges::views::single(?)` | `any_expr` |
123-
| `.in` | Expands to `if (...find)` statements. | `any_expr` |
140+
| Trig | Desc (placehoder: `?`) | Expr before cursor |
141+
| :-------: | -------------------------------------------------------------------- | :----------------: |
142+
| `.be` | Expands to begin and end exprs. | `any_expr` |
143+
| `.mv` | Wraps with `std::move(?)`. | `any_expr` |
144+
| `.fwd` | Wraps with `std::forward<decltype(?)>(?)`. | `any_expr` |
145+
| `.val` | Wraps with `std::declval<?>()`. | `any_expr` |
146+
| `.dt` | Wraps with `decltype(?)`. | `any_expr` |
147+
| `.uu` | Wraps with `(void)?`. | `any_expr` |
148+
| `.ts` | Switches indent's coding style between `CamelCase` and `snake_case`. | `indent` |
149+
| `.sc` | Wraps with `static_cast<>(?)`. | `any_expr` |
150+
| `.single` | Wraps with `ranges::views::single(?)`. | `any_expr` |
151+
| `.in` | Expands to `if (...find)` statements. | `any_expr` |
124152

125153
</details>
126154

@@ -149,20 +177,20 @@ ls.setup({
149177
] @expr_or_type
150178
```
151179

152-
| Trig | Desc (placehoder: `?`) | Expr before cursor |
153-
| :--------: | ---------------------------------------------------------- | :----------------: |
154-
| `.rc` | Wraps with `Rc::new(?)` if expr, `Rc<?>` if type | `expr_or_type` |
155-
| `.arc` | Wraps with `Arc::new(?)` if expr, `Arc<?>` if type | `expr_or_type` |
156-
| `.box` | Wraps with `Box::new(?)` if expr, `Box<?>` if type | `expr_or_type` |
157-
| `.mu` | Wraps with `Mutex::new(?)` if expr, `Mutex<?>` if type | `expr_or_type` |
158-
| `.rw` | Wraps with `RwLock::new(?)` if expr, `RwLock<?>` if type | `expr_or_type` |
159-
| `.cell` | Wraps with `Cell::new(?)` if expr, `Cell<?>` if type | `expr_or_type` |
160-
| `.refcell` | Wraps with `RefCell::new(?)` if expr, `RefCell<?>` if type | `expr_or_type` |
161-
| `.ref` | Wraps with `&?` | `expr` |
162-
| `.refm` | Wraps with `&mut ?` | `expr` |
163-
| `.ok` | Wraps with `Ok(?)` | `expr` |
164-
| `.err` | Wraps with `Err(?)` | `expr` |
165-
| `.some` | Wraps with `Some(?)` | `expr` |
166-
| `.println` | Wraps with `println!("{:?}", ?)` | `expr` |
180+
| Trig | Desc (placehoder: `?`) | Expr before cursor |
181+
| :--------: | ----------------------------------------------------------- | :----------------: |
182+
| `.rc` | Wraps with `Rc::new(?)` if expr, `Rc<?>` if type. | `expr_or_type` |
183+
| `.arc` | Wraps with `Arc::new(?)` if expr, `Arc<?>` if type. | `expr_or_type` |
184+
| `.box` | Wraps with `Box::new(?)` if expr, `Box<?>` if type. | `expr_or_type` |
185+
| `.mu` | Wraps with `Mutex::new(?)` if expr, `Mutex<?>` if type. | `expr_or_type` |
186+
| `.rw` | Wraps with `RwLock::new(?)` if expr, `RwLock<?>` if type. | `expr_or_type` |
187+
| `.cell` | Wraps with `Cell::new(?)` if expr, `Cell<?>` if type. | `expr_or_type` |
188+
| `.refcell` | Wraps with `RefCell::new(?)` if expr, `RefCell<?>` if type. | `expr_or_type` |
189+
| `.ref` | Wraps with `&?`. | `expr` |
190+
| `.refm` | Wraps with `&mut ?`. | `expr` |
191+
| `.ok` | Wraps with `Ok(?)`. | `expr` |
192+
| `.err` | Wraps with `Err(?)`. | `expr` |
193+
| `.some` | Wraps with `Some(?)`. | `expr` |
194+
| `.println` | Wraps with `println!("{:?}", ?)`. | `expr` |
167195

168196
</details>

lua/luasnip-snippets/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ function M.setup(opts)
4646
load_and_add_snippet {
4747
"cpp",
4848
"rust",
49+
"lua",
4950
"all",
5051
}
5152
end
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
local function last_lua_module_section(args)
2+
local ls = require("luasnip")
3+
4+
local text = args[1][1] or ""
5+
local split = vim.split(text, ".", { plain = true })
6+
7+
local options = {}
8+
for len = 0, #split - 1 do
9+
local node =
10+
ls.t(table.concat(vim.list_slice(split, #split - len, #split), "_"))
11+
table.insert(options, node)
12+
end
13+
14+
return ls.sn(nil, {
15+
ls.c(1, options),
16+
})
17+
end
18+
19+
local expr_query = [[
20+
[
21+
(function_call)
22+
(identifier)
23+
(expression_list)
24+
] @prefix
25+
]]
26+
27+
return function()
28+
local snippet = require("luasnip-snippets.nodes").construct_snippet
29+
local fmt = require("luasnip.extras.fmt").fmt
30+
local i = require("luasnip-snippets.nodes").insert_node
31+
local ls = require("luasnip")
32+
local f = ls.function_node
33+
local tsp = require("luasnip.extras.treesitter_postfix")
34+
35+
return {
36+
snippet {
37+
"fn",
38+
name = "(fn) function",
39+
dscr = "Expands to function definition",
40+
mode = "w",
41+
nodes = fmt(
42+
[[
43+
function {}({})
44+
{}
45+
end
46+
]],
47+
{
48+
i(1, "function name", { desc = "Function Name" }),
49+
i(2, "arguments", { desc = "Function Arguments" }),
50+
i(0),
51+
}
52+
),
53+
},
54+
55+
snippet {
56+
"req",
57+
name = "require(...)",
58+
dscr = "Require statement",
59+
mode = "wb",
60+
nodes = fmt([[local {} = require("{}")]], {
61+
ls.d(2, last_lua_module_section, { 1 }),
62+
i(1, "module"),
63+
}),
64+
},
65+
66+
tsp.treesitter_postfix(
67+
{
68+
trig = ".ipairs",
69+
name = "(.ipairs) for in ipairs(...)",
70+
dscr = "Expands expression to for in ipairs(...) do ... end",
71+
wordTrig = false,
72+
reparseBuffer = "live",
73+
matchTSNode = {
74+
query = expr_query,
75+
query_lang = "lua",
76+
},
77+
},
78+
fmt(
79+
[[
80+
for i, value in ipairs({}) do
81+
{}
82+
end
83+
]],
84+
{
85+
f(function(_, parent)
86+
local match = parent.snippet.env.LS_TSMATCH
87+
return match
88+
end, {}),
89+
i(0),
90+
}
91+
)
92+
),
93+
94+
tsp.treesitter_postfix(
95+
{
96+
trig = ".pairs",
97+
name = "(.pairs) for in pairs(...)",
98+
dscr = "Expands expression to for in pairs(...) do ... end",
99+
wordTrig = false,
100+
reparseBuffer = "live",
101+
matchTSNode = {
102+
query = expr_query,
103+
query_lang = "lua",
104+
},
105+
},
106+
fmt(
107+
[[
108+
for key, value in pairs({}) do
109+
{}
110+
end
111+
]],
112+
{
113+
f(function(_, parent)
114+
local match = parent.snippet.env.LS_TSMATCH
115+
return match
116+
end, {}),
117+
i(0),
118+
}
119+
)
120+
),
121+
122+
tsp.treesitter_postfix(
123+
{
124+
trig = ".isnil",
125+
name = "(.isnil) if ... == nil",
126+
dscr = "Expands expression to if ... == nil then ... end",
127+
wordTrig = false,
128+
reparseBuffer = "live",
129+
matchTSNode = {
130+
query = expr_query,
131+
query_lang = "lua",
132+
},
133+
},
134+
fmt(
135+
[[
136+
if {} == nil then
137+
{}
138+
end
139+
]],
140+
{
141+
f(function(_, parent)
142+
local match = parent.snippet.env.LS_TSMATCH
143+
return match
144+
end, {}),
145+
i(0, "return"),
146+
}
147+
)
148+
),
149+
}
150+
end

0 commit comments

Comments
 (0)