Skip to content

Commit 2bc761c

Browse files
committed
add snippet for all ft
1 parent 76b63ad commit 2bc761c

File tree

7 files changed

+392
-14
lines changed

7 files changed

+392
-14
lines changed

README.md

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,38 @@ ls.setup({
3030

3131
Snippets which trigger ends with "!" are `autosnippet`.
3232

33+
<details>
34+
<summary>All</summary>
35+
36+
#### Normal Snippets
37+
38+
| Trig | Desc |
39+
| :-----: | ---------------------------------- |
40+
| `todo` | Expand to linewise `TODO` comment |
41+
| `fixme` | Expand to linewise `FIXME` comment |
42+
| `note` | Expand to linewise `NOTE` comment |
43+
44+
</details>
45+
3346
<details>
3447
<summary>Cpp</summary>
3548

3649
#### Normal Snippets
3750

38-
| Trig | Desc | Context Required |
39-
| :--------: | ------------------------------------------------------------------------------------------------ | :--------------: |
40-
| `ctor!` | Expand to default constructor | In Class |
41-
| `dtor!` | Expand to default destructor | In Class |
42-
| `cc!` | Expand to default copy constructor | In Class |
43-
| `mv!` | Expand to default move constructor | In Class |
44-
| `ncc!` | Expand to delete copy constructor | In Class |
45-
| `nmv!` | Expand to delete move constructor | In Class |
46-
| `ncm!` | Expand to delete copy and move constructor | In Class |
47-
| `fn` | Expand to lambda function in argument list or function body, otherwise expand to normal function | No |
48-
| `\|trans` | Expand to ranges::views::transform pipe. | No |
49-
| `\|filter` | Expand to ranges::views::filter pipe. | No |
50-
| `cpo` | Expand to customize point object. | No |
51+
| Trig | Desc | Context Required |
52+
| :--------: | ------------------------------------------------------------------------------------------------ | :---------------------------: |
53+
| `ctor!` | Expand to default constructor | In Class |
54+
| `dtor!` | Expand to default destructor | In Class |
55+
| `cc!` | Expand to default copy constructor | In Class |
56+
| `mv!` | Expand to default move constructor | In Class |
57+
| `ncc!` | Expand to delete copy constructor | In Class |
58+
| `nmv!` | Expand to delete move constructor | In Class |
59+
| `ncm!` | Expand to delete copy and move constructor | In Class |
60+
| `fn` | Expand to lambda function in argument list or function body, otherwise expand to normal function | No |
61+
| `\|trans` | Expand to ranges::views::transform pipe. | No |
62+
| `\|filter` | Expand to ranges::views::filter pipe. | No |
63+
| `cpo` | Expand to customize point object. | No |
64+
| `once` | Expand to `pragma once` marker at the front of the file. | All lines before are comments |
5165

5266
#### Postfix Snippets
5367

lua/luasnip-snippets/config.lua

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---@class LSSnippets.Config.User
2+
---@field name? string
3+
---@field email? string
4+
5+
---@class LSSnippets.Config
6+
---@field copyright_header? string
7+
---@field user? LSSnippets.Config.User
8+
local config = {}
9+
10+
---@param opts? LSSnippets.Config
11+
local function setup(opts)
12+
opts = opts or {}
13+
config = vim.tbl_extend("force", config, opts)
14+
end
15+
16+
---@return any
17+
local function get(key)
18+
local keys = vim.split(key, ".", {
19+
plain = true,
20+
trimempty = true,
21+
})
22+
local value = config
23+
for _, k in ipairs(keys) do
24+
value = value[k]
25+
if value == nil then
26+
return nil
27+
end
28+
end
29+
return value
30+
end
31+
32+
return {
33+
setup = setup,
34+
get = get,
35+
}

lua/luasnip-snippets/init.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,16 @@ local function load_and_add_snippet(fts)
3737
end
3838
end
3939

40-
function M.setup()
40+
---@param opts? LSSnippets.Config
41+
function M.setup(opts)
42+
local Config = require("luasnip-snippets.config")
43+
Config.setup(opts)
44+
4145
-- register snippets
4246
load_and_add_snippet {
4347
"cpp",
4448
"rust",
49+
"all",
4550
}
4651
end
4752

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---@param keyword string
2+
local todo_comment = function(keyword)
3+
local ls = require("luasnip")
4+
local f = ls.function_node
5+
local snippet = require("luasnip-snippets.nodes").construct_snippet
6+
local Config = require("luasnip-snippets.config")
7+
8+
return snippet {
9+
keyword,
10+
mode = "bw",
11+
nodes = {
12+
f(function()
13+
local CommentFt = require("luasnip-snippets.utils.comment")
14+
local ft = vim.api.nvim_get_option_value("filetype", {
15+
buf = 0,
16+
})
17+
local pattern = CommentFt.get(ft, 1)
18+
local name = Config.get("user.name")
19+
if pattern == nil then
20+
-- keep the input
21+
pattern = "%s"
22+
end
23+
if type(pattern) == "table" then
24+
pattern = pattern[1]
25+
end
26+
local marker
27+
if name == nil then
28+
marker = (" %s: "):format(keyword:upper())
29+
else
30+
marker = (" %s(%s): "):format(keyword:upper(), name)
31+
end
32+
return pattern:format(marker)
33+
end, {}),
34+
},
35+
}
36+
end
37+
38+
return {
39+
todo_comment("todo"),
40+
todo_comment("fixme"),
41+
todo_comment("note"),
42+
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local c = require("luasnip-snippets.nodes").choice_node
55
local fmta = require("luasnip.extras.fmt").fmta
66
local f = ls.function_node
77
local t = ls.text_node
8+
local CommonCond = require("luasnip-snippets.utils.common_cond")
89

910
local function cpo_snippet()
1011
local function cpo_func_to_namespace(name)
@@ -71,9 +72,27 @@ local function ranges_views_snippet(trig, func)
7172
}
7273
end
7374

75+
local all_lines_before_are_all_comments =
76+
CommonCond.generate_all_lines_before_match_cond {
77+
"^%s*//.*$",
78+
"^%s*$",
79+
}
80+
7481
return {
7582
cpo_snippet,
7683

7784
ranges_views_snippet("|trans", "transform"),
7885
ranges_views_snippet("|filter", "filter"),
86+
87+
-- progma once
88+
snippet {
89+
"once",
90+
name = "(once) #Progma once",
91+
dscr = "Expands to progma once with comments",
92+
mode = "bwA",
93+
cond = all_lines_before_are_all_comments,
94+
nodes = {
95+
t { "#pragma once // NOLINT(build/header_guard)", "" },
96+
},
97+
},
7998
}

0 commit comments

Comments
 (0)