feat(snip): exclude snippets by trigger

This commit is contained in:
arne314
2025-01-28 17:03:23 +01:00
parent 41d239d5e7
commit d0042ce419
5 changed files with 27 additions and 13 deletions

View File

@@ -28,7 +28,7 @@ Math snippets:
- Wrapping of any mathematical expression (see [operations](./lua/typstar/snippets/visual.lua), works nested, multiline and in visual mode via the [selection key](#installation)): `<expression><operation>` &#8594; `<operation>(<expression>)` (e.g. `(a^2+b^2)rt` &#8594; `sqrt(a^2+b^2)`, `lambdatd` &#8594; `tilde(lambda)`, `(1+1)sQ` &#8594; `[1+1]`, `(1+1)sq` &#8594; `[(1+1)]`)
- Matrices: `<size>ma` and `<size>lma` (e.g. `23ma` &#8594; 2x3 matrix)
Note that you can enable and disable collections of snippets in the [config](#configuration).
Note that you can enable and disable single and modules of snippets in the [config](#configuration).
### Excalidraw
- Use `:TypstarInsertExcalidraw` to create a new drawing using the configured template, insert a figure displaying it and open it in Obsidian.

View File

@@ -6,15 +6,18 @@ local fmta = require('luasnip.extras.fmt').fmta
local lsengines = require('luasnip.nodes.util.trig_engines')
local ts = vim.treesitter
local exclude_triggers_set = {}
local last_keystroke_time = nil
vim.api.nvim_create_autocmd('TextChangedI', {
callback = function() last_keystroke_time = vim.loop.now() end,
})
local lexical_result_cache = {}
local ts_markup_query = ts.query.parse('typst', '(text) @markup')
local ts_math_query = ts.query.parse('typst', '(math) @math')
local ts_string_query = ts.query.parse('typst', '(string) @string')
utils.generate_bool_set(cfg.exclude, exclude_triggers_set)
vim.api.nvim_create_autocmd('TextChangedI', {
callback = function() last_keystroke_time = vim.loop.now() end,
})
M.in_math = function()
local cursor = utils.get_cursor_pos()
return utils.cursor_within_treesitter_query(ts_math_query, 0, cursor)
@@ -121,7 +124,16 @@ function M.setup()
if cfg.enable then
local autosnippets = {}
for _, file in ipairs(cfg.modules) do
vim.list_extend(autosnippets, require(('typstar.snippets.%s'):format(file)))
for _, sn in ipairs(require(('typstar.snippets.%s'):format(file))) do
local exclude
local is_start = sn.trigger:match('^%^%(\\s%*%)')
if is_start then
exclude = exclude_triggers_set[sn.trigger:sub(7)]
else
exclude = exclude_triggers_set[sn.trigger]
end
if not exclude then table.insert(autosnippets, sn) end
end
end
luasnip.add_snippets('typst', autosnippets)
local jsregexp_ok, _ = pcall(require, 'luasnip-jsregexp')

View File

@@ -25,6 +25,7 @@ local default_config = {
'markup',
'visual',
},
exclude = {}, -- list of triggers to exclude
},
}

View File

@@ -4,6 +4,7 @@ local i = ls.insert_node
local s = ls.snippet_node
local t = ls.text_node
local helper = require('typstar.autosnippets')
local utils = require('typstar.utils')
local snip = helper.snip
local cap = helper.cap
local math = helper.in_math
@@ -62,14 +63,8 @@ for latin, greek in pairs(greek_letters_map) do
table.insert(greek_keys, latin:upper())
end
local generate_bool_set = function(arr, target)
for _, val in ipairs(arr) do
target[val] = true
end
end
generate_bool_set(index_conflicts, index_conflicts_set)
generate_bool_set(punctuation_prepend_space, punctuation_prepend_space_set)
utils.generate_bool_set(index_conflicts, index_conflicts_set)
utils.generate_bool_set(punctuation_prepend_space, punctuation_prepend_space_set)
greek_letters_map = greek_full
trigger_greek = table.concat(greek_keys, '|')

View File

@@ -61,6 +61,12 @@ function M.urlencode(url)
return url
end
function M.generate_bool_set(arr, target)
for _, val in ipairs(arr) do
target[val] = true
end
end
function M.get_treesitter_root(bufnr) return ts.get_parser(bufnr):parse()[1]:root() end
function M.treesitter_match_start_end(match)