fix: common index snippets in markup

This commit is contained in:
arne314
2024-12-08 22:10:58 +01:00
parent 5529a29327
commit 628290704e
2 changed files with 9 additions and 10 deletions

View File

@@ -19,10 +19,10 @@ local ts_string_query = ts.query.parse('typst', '(string) @string')
M.in_math = function() M.in_math = function()
local cursor = utils.get_cursor_pos() local cursor = utils.get_cursor_pos()
return utils.cursor_inside_treesitter_query(ts_math_query, cursor) return utils.cursor_within_treesitter_query(ts_math_query, 0, cursor)
and not utils.cursor_inside_treesitter_query(ts_string_query, cursor) and not utils.cursor_within_treesitter_query(ts_string_query, 0, cursor)
end end
M.in_markup = function() return utils.cursor_inside_treesitter_query(ts_markup_query) end M.in_markup = function() return utils.cursor_within_treesitter_query(ts_markup_query, 2) end
M.not_in_math = function() return not M.in_math() end M.not_in_math = function() return not M.in_math() end
M.not_in_markup = function() return not M.in_markup() end M.not_in_markup = function() return not M.in_markup() end
M.snippets_toggle = true M.snippets_toggle = true
@@ -50,8 +50,6 @@ function M.snip(trigger, expand, insert, condition, priority)
trig = trigger, trig = trigger,
trigEngine = M.engine, trigEngine = M.engine,
trigEngineOpts = { condition = condition }, trigEngineOpts = { condition = condition },
regTrig = true,
wordtrig = false,
priority = priority, priority = priority,
snippetType = 'autosnippet' snippetType = 'autosnippet'
}, },

View File

@@ -20,7 +20,7 @@ function M.run_shell_command(cmd)
vim.fn.jobstart(cmd) vim.fn.jobstart(cmd)
end end
function M.cursor_inside_treesitter_query(query, cursor) function M.cursor_within_treesitter_query(query, match_tolerance, cursor)
cursor = cursor or M.get_cursor_pos() cursor = cursor or M.get_cursor_pos()
local bufnr = vim.api.nvim_get_current_buf() local bufnr = vim.api.nvim_get_current_buf()
local root = ts.get_parser(bufnr):parse()[1]:root() local root = ts.get_parser(bufnr):parse()[1]:root()
@@ -28,7 +28,8 @@ function M.cursor_inside_treesitter_query(query, cursor)
if match then if match then
local start_row, start_col, _, _ = match[1]:range() local start_row, start_col, _, _ = match[1]:range()
local _, _, end_row, end_col = match[#match]:range() local _, _, end_row, end_col = match[#match]:range()
local matched = M.cursor_inside_coords(cursor, start_row, end_row, start_col, end_col) local matched = M.cursor_within_coords(cursor, start_row, end_row, start_col, end_col,
match_tolerance)
if matched then if matched then
return true return true
end end
@@ -37,11 +38,11 @@ function M.cursor_inside_treesitter_query(query, cursor)
return false return false
end end
function M.cursor_inside_coords(cursor, start_row, end_row, start_col, end_col) function M.cursor_within_coords(cursor, start_row, end_row, start_col, end_col, match_tolerance)
if start_row <= cursor[1] and end_row >= cursor[1] then if start_row <= cursor[1] and end_row >= cursor[1] then
if start_row == cursor[1] and start_col > cursor[2] then if start_row == cursor[1] and start_col - match_tolerance >= cursor[2] then
return false return false
elseif end_row == cursor[1] and end_col < cursor[2] then elseif end_row == cursor[1] and end_col + match_tolerance <= cursor[2] then
return false return false
end end
return true return true