|
| 1 | +local Env = require('render-markdown.lib.env') |
| 2 | + |
| 3 | +---@class render.md.integ.TreeSitter |
| 4 | +---@field private initialized boolean |
| 5 | +---@field private queries table<string, vim.treesitter.Query> |
| 6 | +local M = { |
| 7 | + initialized = false, |
| 8 | + queries = {}, |
| 9 | +} |
| 10 | + |
| 11 | +---Should only be called from manager on initial buffer attach |
| 12 | +function M.setup() |
| 13 | + if M.initialized then |
| 14 | + return |
| 15 | + end |
| 16 | + M.initialized = true |
| 17 | + local state = require('render-markdown.state') |
| 18 | + for _, language in ipairs(state.file_types) do |
| 19 | + M.disable(language, state.patterns[language]) |
| 20 | + end |
| 21 | +end |
| 22 | + |
| 23 | +---@param language string |
| 24 | +---@param query string |
| 25 | +---@return vim.treesitter.Query |
| 26 | +function M.parse(language, query) |
| 27 | + local result = M.queries[query] |
| 28 | + if result == nil then |
| 29 | + result = vim.treesitter.query.parse(language, query) |
| 30 | + M.queries[query] = result |
| 31 | + end |
| 32 | + return result |
| 33 | +end |
| 34 | + |
| 35 | +---@param language string |
| 36 | +---@param injection? render.md.Injection |
| 37 | +function M.inject(language, injection) |
| 38 | + if injection == nil or not injection.enabled then |
| 39 | + return |
| 40 | + end |
| 41 | + |
| 42 | + local query = '' |
| 43 | + if Env.has_11 then |
| 44 | + query = query .. ';; extends' .. '\n' |
| 45 | + else |
| 46 | + local files = vim.treesitter.query.get_files(language, 'injections') |
| 47 | + for _, file in ipairs(files) do |
| 48 | + local f = io.open(file, 'r') |
| 49 | + if f ~= nil then |
| 50 | + query = query .. f:read('*all') .. '\n' |
| 51 | + f:close() |
| 52 | + end |
| 53 | + end |
| 54 | + end |
| 55 | + query = query .. injection.query |
| 56 | + pcall(vim.treesitter.query.set, language, 'injections', query) |
| 57 | +end |
| 58 | + |
| 59 | +---@private |
| 60 | +---@param language string |
| 61 | +---@param pattern? render.md.Pattern |
| 62 | +function M.disable(language, pattern) |
| 63 | + if not Env.has_11 then |
| 64 | + return |
| 65 | + end |
| 66 | + if pattern == nil or not pattern.disable then |
| 67 | + return |
| 68 | + end |
| 69 | + local query = vim.treesitter.query.get(language, 'highlights') |
| 70 | + if query == nil then |
| 71 | + return |
| 72 | + end |
| 73 | + local query_directives = query.info.patterns |
| 74 | + for _, directive in ipairs(pattern.directives) do |
| 75 | + local query_directive = query_directives[directive.id] |
| 76 | + if M.has_directive(directive.name, query_directive) then |
| 77 | + query.query:disable_pattern(directive.id) |
| 78 | + end |
| 79 | + end |
| 80 | +end |
| 81 | + |
| 82 | +---@private |
| 83 | +---@param name string |
| 84 | +---@param directives? (string|integer)[][] |
| 85 | +---@return boolean |
| 86 | +function M.has_directive(name, directives) |
| 87 | + if directives == nil then |
| 88 | + return false |
| 89 | + end |
| 90 | + for _, directive in ipairs(directives) do |
| 91 | + if directive[1] == 'set!' and directive[2] == name then |
| 92 | + return true |
| 93 | + end |
| 94 | + end |
| 95 | + return false |
| 96 | +end |
| 97 | + |
| 98 | +return M |
0 commit comments