Skip to content

Commit 392bb02

Browse files
committed
feat(config): all autosnippets without suffix '!' can disable its auto expansion
1 parent ca469d5 commit 392bb02

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

lua/luasnip-snippets/config.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
---@field vim_snippet? boolean
77
---@field cond? fun():boolean
88

9+
---@alias LSSnippets.Config.Snippet.DisableSnippets string[]
10+
---@alias LSSnippets.SupportLangs 'cpp'|'dart'|'lua'|'rust'|'nix'|'typescript'|'*'
11+
912
---@class LSSnippets.Config.Snippet
1013
---@field lua LSSnippets.Config.Snippet.Lua
1114

1215
---@class LSSnippets.Config
1316
---@field copyright_header? string
1417
---@field user? LSSnippets.Config.User
1518
---@field snippet? LSSnippets.Config.Snippet
19+
---@field disable_auto_expansion? table<LSSnippets.SupportLangs, LSSnippets.Config.Snippet.DisableSnippets>
1620
local config = {}
1721

1822
---@param opts? LSSnippets.Config
@@ -37,10 +41,18 @@ local function get(key)
3741
return value
3842
end
3943

44+
---return bool
45+
local function auto_expansion_disabled(lang, trig)
46+
local disabled_trigs =
47+
vim.F.if_nil(vim.tbl_get(config, "disable_auto_expansion", lang), {})
48+
return vim.list_contains(disabled_trigs, trig)
49+
end
50+
4051
---@class luasnip-snippets.config
4152
local M = {
4253
setup = setup,
4354
get = get,
55+
auto_expansion_disabled = auto_expansion_disabled,
4456
}
4557

4658
return M

lua/luasnip-snippets/nodes.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ end
5555
---@field hidden boolean? Hidden from completion, If "h" in mode, defaults to true.
5656
---@field nodes LuaSnip.Node[] Expansion nodes
5757
---@field priority number? Snippet priority
58+
---@field lang string? Language of the snippet
5859
---@field cond LSSnippets.ConditionObject? Condition object, including condition and show_condition
5960
---@field resolveExpandParams nil|fun(snippet: LuaSnip.Snippet, line_to_cursor: string, matched_trigger: string, captures: table): table Function to decide whether the snippet can be expanded or not.
6061
---@field opts table? Other options
@@ -63,6 +64,8 @@ end
6364
local function construct_snippet(opts)
6465
local CommonCond = require("luasnip-snippets.utils.common_cond")
6566
local ls = require("luasnip")
67+
---@type luasnip-snippets.config
68+
local Config = require("luasnip-snippets.config")
6669

6770
local trig = opts[1]
6871
local name = vim.F.if_nil(opts.name, trig)
@@ -75,6 +78,13 @@ local function construct_snippet(opts)
7578
end
7679
local hidden = vim.F.if_nil(opts.hidden, mode:match("h") ~= nil)
7780
local snippetType = mode:match("A") ~= nil and "autosnippet" or "snippet"
81+
if
82+
snippetType == "autosnippet"
83+
and opts.lang ~= nil
84+
and Config.auto_expansion_disabled(opts.lang, trig)
85+
then
86+
snippetType = "snippet"
87+
end
7888
local nodes = opts.nodes
7989
local priority = opts.priority or nil
8090
local cond = opts.cond or nil

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
local ls = require("luasnip")
2-
local snippet = require("luasnip-snippets.nodes").construct_snippet
3-
local i = require("luasnip-snippets.nodes").insert_node
4-
local c = require("luasnip-snippets.nodes").choice_node
2+
---@type luasnip-snippets.nodes
3+
local Nodes = require("luasnip-snippets.nodes")
4+
local snippet = Nodes.construct_snippet
5+
local i = Nodes.insert_node
6+
local c = Nodes.choice_node
57
local fmta = require("luasnip.extras.fmt").fmta
68
local f = ls.function_node
79
local t = ls.text_node
@@ -58,6 +60,7 @@ local function int_type_snippet(bits, unsigned)
5860
name = ("(%s) %s"):format(trig, expand),
5961
desc = ("Expands to %s"):format(expand),
6062
mode = "wA",
63+
lang = "cpp",
6164
nodes = {
6265
t(expand),
6366
},
@@ -171,6 +174,7 @@ return {
171174
name = "(once) #Progma once",
172175
dscr = "Expands to progma once with comments",
173176
mode = "bwA",
177+
lang = "cpp",
174178
cond = all_lines_before_are_all_comments,
175179
nodes = {
176180
t { "#pragma once // NOLINT(build/header_guard)", "" },
@@ -183,6 +187,7 @@ return {
183187
name = 'include ""',
184188
dscr = "#include with quotes",
185189
mode = "bA",
190+
lang = "cpp",
186191
nodes = {
187192
t('#include "'),
188193
i(1, "header"),
@@ -194,6 +199,7 @@ return {
194199
name = "include <>",
195200
dscr = "#include with <>",
196201
mode = "bA",
202+
lang = "cpp",
197203
nodes = {
198204
t("#include <"),
199205
i(1, "header"),

0 commit comments

Comments
 (0)