Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions after/queries/markdown/matchup.scm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
;; ====================
;; fenced code block ```
;; ====================
; ====================
; fenced code block ```
; ====================
(fenced_code_block
(fenced_code_block_delimiter) @open.code_fence
(fenced_code_block_delimiter) @close.code_fence) @scope.code_fence
(fenced_code_block_delimiter) @close.code_fence) @scope.code_fence
31 changes: 12 additions & 19 deletions after/queries/markdown_inline/matchup.scm
Original file line number Diff line number Diff line change
@@ -1,36 +1,29 @@
;; ===========
;; inline code `
;; ===========
; ===========
; inline code `
; ===========
(code_span
(code_span_delimiter) @open.code
(code_span_delimiter) @close.code) @scope.code

;; ===========================
;; emphasis (italic, *...* / _..._)
;; ===========================
; ===========================
; emphasis (italic, *...* / _..._)
; ===========================
(emphasis
(emphasis_delimiter) @open.emphasis
(emphasis_delimiter) @close.emphasis) @scope.emphasis

;; ===========================
;; strong emphasis (bold, **...** / __...__)
;; ===========================
; ===========================
; strong emphasis (bold, **...** / __...__)
; ===========================
(strong_emphasis
(emphasis_delimiter) @open.strong
(emphasis_delimiter) @open.strong
(emphasis_delimiter) @close.strong
(emphasis_delimiter) @close.strong) @scope.strong

;; ===========================
;; strikethrough (~~...~~)
;; ===========================
; ===========================
; strikethrough (~~...~~)
; ===========================
(strikethrough
(emphasis_delimiter) @open.strikethrough
(emphasis_delimiter) @close.strikethrough) @scope.strikethrough

;; ===========================
;; LaTeX math ($...$ / $$...$$)
;; ===========================
(latex_block
(latex_span_delimiter) @open.math
(latex_span_delimiter) @close.math) @scope.math
36 changes: 19 additions & 17 deletions lua/treesitter-matchup/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ end
---@param lang string
---@return matchup.treesitter.Match[]
local get_memoized_matches = memoize(function(bufnr, root, lang)
local query_name = 'matchup'
local query = ts.query.get(lang, query_name)
local query = ts.query.get(lang, 'matchup')

if not query then
return {}
Expand Down Expand Up @@ -137,28 +136,31 @@ end, buf_root_lang_hash)
---@param bufnr integer
---@return matchup.treesitter.Match[]
M.get_matches = function(bufnr)
local parser = ts.get_parser(bufnr)
local lang_tree = ts.get_parser(bufnr)
local matches = {} ---@type matchup.treesitter.Match[]

if parser then
if lang_tree then
-- NOTE: assummes that we are always parsing the current window. May cause
-- issues if that's not always the case
local win = api.nvim_get_current_win()
local cur_row = unpack(api.nvim_win_get_cursor(win))
local cursor = vim.api.nvim_win_get_cursor(0)
local stopline = vim.g.matchup_treesitter_stopline ---@type integer
local start_row = math.max(cur_row - stopline, 0)
local end_row = math.min(cur_row + stopline, api.nvim_buf_line_count(bufnr))

parser:parse({start_row, end_row})
parser:for_each_tree(function(tree, lang_tree)
if not tree or lang_tree:lang() == 'comment' then
return
local start_row = math.max(cursor[1] - stopline, 0)
local end_row = math.min(cursor[1] + stopline, api.nvim_buf_line_count(bufnr))

lang_tree:parse({ start_row, end_row })
---@type vim.treesitter.LanguageTree?
local nested_lang_tree = lang_tree:language_for_range({ cursor[1]-1, cursor[2], cursor[1]-1, cursor[2] })
while vim.tbl_isempty(matches) and nested_lang_tree ~= nil do
local lang = nested_lang_tree:lang()
if lang ~= 'comment' then
for _, tree in ipairs(nested_lang_tree:trees()) do
local group_results = get_memoized_matches(bufnr, tree:root(), lang)
vim.list_extend(matches, group_results)
end
end

local lang = lang_tree:lang()
local group_results = get_memoized_matches(bufnr, tree:root(), lang)
vim.list_extend(matches, group_results)
end)
nested_lang_tree = nested_lang_tree:parent()
end
end

return matches
Expand Down
Loading