Skip to content

Commit 8fd1c76

Browse files
committed
Init Project
0 parents  commit 8fd1c76

File tree

6 files changed

+152
-0
lines changed

6 files changed

+152
-0
lines changed

.stylua.toml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
indent_type = "Spaces"
2+
indent_width = 2
3+
column_width = 80
4+
call_parentheses = "NoSingleTable"

lua/luasnip-snippets/init.lua

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
local M = {}
2+
3+
-- dummy
4+
---@class LuaSnip.Snippet
5+
6+
---@alias SnippetOrBuilder LuaSnip.Snippet|fun():SnippetOrBuilder
7+
8+
---Register snippets to luasnip.
9+
---@param ft string filetype
10+
---@param snippets SnippetOrBuilder[]
11+
local function add_snippets(ft, snippets)
12+
local ls = require("luasnip")
13+
local ret = {}
14+
for _, snippet in ipairs(snippets) do
15+
if type(snippet) == "function" then
16+
snippet = snippet()
17+
end
18+
ret[#ret + 1] = snippet
19+
end
20+
ls.add_snippets(ft, ret)
21+
end
22+
23+
---Load snippets from ft module.
24+
---@param ft string
25+
---@return SnippetOrBuilder[]
26+
local function load_snippets(ft)
27+
local snippets = require("luasnip-snippets.snippets." .. ft)
28+
if type(snippets) == "function" then
29+
snippets = snippets()
30+
end
31+
return snippets
32+
end
33+
34+
---Load and register snippets.
35+
---@param fts string[]
36+
local function load_and_add_snippet(fts)
37+
for _, ft in ipairs(fts) do
38+
local snippets = load_snippets(ft)
39+
add_snippets(ft, snippets)
40+
end
41+
end
42+
43+
function M.setup()
44+
-- register snippets
45+
load_and_add_snippet {
46+
"rust",
47+
}
48+
end
49+
50+
return M

lua/luasnip-snippets/snippets/cpp/init.lua

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
local Utils = require("luasnip-snippets.utils")
2+
3+
local function setup()
4+
return Utils.concat_snippets("luasnip-snippets.snippets.rust", {
5+
"postfix",
6+
})
7+
end
8+
9+
return setup
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
local ls = require("luasnip")
2+
local f = ls.function_node
3+
local tsp = require("luasnip.extras.treesitter_postfix")
4+
local Utils = require("luasnip-snippets.utils")
5+
6+
local expr_node_types = {
7+
"struct_expression",
8+
"call_expression",
9+
"identifier",
10+
}
11+
12+
---@param trig string
13+
---@param expand string
14+
local function expr_tsp(trig, expand)
15+
local name = ("(%s) %s"):format(trig, expand)
16+
local dscr = ("Wrap expression with %s"):format(expand)
17+
local replaced = expand:gsub("?", "%%s")
18+
19+
return tsp.treesitter_postfix({
20+
trig = trig,
21+
name = name,
22+
dscr = dscr,
23+
wordTrig = false,
24+
reparseBuffer = nil,
25+
matchTSNode = tsp.builtin.tsnode_matcher.find_topmost_types(
26+
expr_node_types,
27+
trig
28+
),
29+
}, {
30+
f(function(_, parent)
31+
return Utils.replace_all(parent.snippet.env.LS_TSMATCH, replaced)
32+
end, {}),
33+
})
34+
end
35+
36+
return {
37+
expr_tsp(".rc", "Rc::new(?)"),
38+
expr_tsp(".arc", "Arc::new(?)"),
39+
expr_tsp(".box", "Box::new(?)"),
40+
expr_tsp(".mu", "Mutex::new(?)"),
41+
expr_tsp(".rw", "RwLock::new(?)"),
42+
expr_tsp(".cell", "Cell::new(?)"),
43+
expr_tsp(".refcell", "RefCell::new(?)"),
44+
expr_tsp(".ref", "&?"),
45+
expr_tsp(".refm", "&mut ?"),
46+
expr_tsp(".ok", "Ok(?)"),
47+
expr_tsp(".err", "Err(?)"),
48+
expr_tsp(".some", "Some(?)"),
49+
}

lua/luasnip-snippets/utils.lua

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
local M = {}
2+
3+
---Replace all occurences of %s in template with match.
4+
---@param match string|string[]
5+
---@param template string
6+
---@return string[]
7+
function M.replace_all(match, template)
8+
---@type string
9+
local match_str = ""
10+
if type(match) == "table" then
11+
match_str = table.concat(match, "\n")
12+
else
13+
match_str = match
14+
end
15+
16+
local ret = template:gsub("%%s", match_str)
17+
local ret_lines = vim.split(ret, "\n", {
18+
trimempty = false,
19+
})
20+
21+
return ret_lines
22+
end
23+
24+
---Load and concat snippets.
25+
---@param base string
26+
---@param snippets string[]
27+
---@return LuaSnip.Snippet[]
28+
function M.concat_snippets(base, snippets)
29+
local ret = {}
30+
for _, snippet in ipairs(snippets) do
31+
local snippet_module = require(base .. "." .. snippet)
32+
if type(snippet_module) == "function" then
33+
snippet_module = snippet_module()
34+
end
35+
vim.list_extend(ret, snippet_module)
36+
end
37+
return ret
38+
end
39+
40+
return M

0 commit comments

Comments
 (0)