2019-06-07 05:21:00 -07:00
|
|
|
local a = vim.api
|
2020-07-14 12:50:57 -07:00
|
|
|
local query = require'vim.treesitter.query'
|
|
|
|
local language = require'vim.treesitter.language'
|
2020-11-04 10:03:36 -07:00
|
|
|
local LanguageTree = require'vim.treesitter.languagetree'
|
2019-06-07 05:21:00 -07:00
|
|
|
|
2019-06-17 12:46:31 -07:00
|
|
|
-- TODO(bfredl): currently we retain parsers for the lifetime of the buffer.
|
|
|
|
-- Consider use weak references to release parser if all plugins are done with
|
|
|
|
-- it.
|
|
|
|
local parsers = {}
|
|
|
|
|
2020-07-14 12:50:57 -07:00
|
|
|
local M = vim.tbl_extend("error", query, language)
|
2019-09-28 05:04:05 -07:00
|
|
|
|
2021-03-30 13:40:29 -07:00
|
|
|
M.language_version = vim._ts_get_language_version()
|
2022-01-27 04:46:56 -07:00
|
|
|
M.minimum_language_version = vim._ts_get_minimum_language_version()
|
2021-03-30 13:40:29 -07:00
|
|
|
|
2019-09-28 05:27:20 -07:00
|
|
|
setmetatable(M, {
|
|
|
|
__index = function (t, k)
|
2021-03-15 17:41:28 -07:00
|
|
|
if k == "highlighter" then
|
2020-07-14 12:50:57 -07:00
|
|
|
t[k] = require'vim.treesitter.highlighter'
|
2019-09-28 05:27:20 -07:00
|
|
|
return t[k]
|
2021-04-01 07:50:31 -07:00
|
|
|
elseif k == "language" then
|
|
|
|
t[k] = require"vim.treesitter.language"
|
|
|
|
return t[k]
|
2021-08-23 06:34:08 -07:00
|
|
|
elseif k == "query" then
|
|
|
|
t[k] = require"vim.treesitter.query"
|
|
|
|
return t[k]
|
2019-09-28 05:27:20 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
})
|
|
|
|
|
2020-07-14 12:50:57 -07:00
|
|
|
--- Creates a new parser.
|
2021-05-01 05:19:48 -07:00
|
|
|
---
|
|
|
|
--- It is not recommended to use this, use vim.treesitter.get_parser() instead.
|
|
|
|
---
|
2021-08-22 13:55:28 -07:00
|
|
|
---@param bufnr The buffer the parser will be tied to
|
|
|
|
---@param lang The language of the parser
|
|
|
|
---@param opts Options to pass to the created language tree
|
2020-11-04 10:03:36 -07:00
|
|
|
function M._create_parser(bufnr, lang, opts)
|
2020-07-14 12:50:57 -07:00
|
|
|
language.require_language(lang)
|
2019-06-07 05:21:00 -07:00
|
|
|
if bufnr == 0 then
|
|
|
|
bufnr = a.nvim_get_current_buf()
|
|
|
|
end
|
2020-07-10 06:33:27 -07:00
|
|
|
|
|
|
|
vim.fn.bufload(bufnr)
|
|
|
|
|
2020-11-04 10:03:36 -07:00
|
|
|
local self = LanguageTree.new(bufnr, lang, opts)
|
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
---@private
|
2020-03-21 11:55:19 -07:00
|
|
|
local function bytes_cb(_, ...)
|
2020-11-04 10:03:36 -07:00
|
|
|
self:_on_bytes(...)
|
2019-06-07 05:21:00 -07:00
|
|
|
end
|
2020-11-04 10:03:36 -07:00
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
---@private
|
2021-02-06 02:17:40 -07:00
|
|
|
local function detach_cb(_, ...)
|
2020-11-04 10:03:36 -07:00
|
|
|
if parsers[bufnr] == self then
|
|
|
|
parsers[bufnr] = nil
|
2019-06-17 12:46:31 -07:00
|
|
|
end
|
2021-02-06 02:17:40 -07:00
|
|
|
self:_on_detach(...)
|
2019-06-17 12:46:31 -07:00
|
|
|
end
|
2020-11-04 10:03:36 -07:00
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
---@private
|
2021-02-06 23:32:19 -07:00
|
|
|
local function reload_cb(_, ...)
|
|
|
|
self:_on_reload(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
a.nvim_buf_attach(self:source(), false, {on_bytes=bytes_cb, on_detach=detach_cb, on_reload=reload_cb, preview=true})
|
2020-11-04 10:03:36 -07:00
|
|
|
|
|
|
|
self:parse()
|
|
|
|
|
2019-06-07 09:19:59 -07:00
|
|
|
return self
|
2019-06-07 05:21:00 -07:00
|
|
|
end
|
|
|
|
|
2020-07-14 12:50:57 -07:00
|
|
|
--- Gets the parser for this bufnr / ft combination.
|
2021-05-01 05:19:48 -07:00
|
|
|
---
|
|
|
|
--- If needed this will create the parser.
|
2021-11-27 09:10:48 -07:00
|
|
|
--- Unconditionally attach the provided callback
|
2021-05-01 05:19:48 -07:00
|
|
|
---
|
2021-08-22 13:55:28 -07:00
|
|
|
---@param bufnr The buffer the parser should be tied to
|
|
|
|
---@param lang The filetype of this parser
|
|
|
|
---@param opts Options object to pass to the created language tree
|
2021-05-01 05:19:48 -07:00
|
|
|
---
|
2021-08-22 13:55:28 -07:00
|
|
|
---@returns The parser
|
2020-11-04 10:03:36 -07:00
|
|
|
function M.get_parser(bufnr, lang, opts)
|
|
|
|
opts = opts or {}
|
|
|
|
|
2019-06-17 12:46:31 -07:00
|
|
|
if bufnr == nil or bufnr == 0 then
|
|
|
|
bufnr = a.nvim_get_current_buf()
|
|
|
|
end
|
2020-07-14 12:50:57 -07:00
|
|
|
if lang == nil then
|
|
|
|
lang = a.nvim_buf_get_option(bufnr, "filetype")
|
2019-06-17 12:46:31 -07:00
|
|
|
end
|
|
|
|
|
2020-11-04 10:03:36 -07:00
|
|
|
if parsers[bufnr] == nil then
|
|
|
|
parsers[bufnr] = M._create_parser(bufnr, lang, opts)
|
2019-09-28 05:27:20 -07:00
|
|
|
end
|
2020-07-08 13:47:57 -07:00
|
|
|
|
2020-11-04 10:03:36 -07:00
|
|
|
parsers[bufnr]:register_cbs(opts.buf_attach_cbs)
|
2020-07-08 13:47:57 -07:00
|
|
|
|
2020-11-04 10:03:36 -07:00
|
|
|
return parsers[bufnr]
|
2019-06-17 12:46:31 -07:00
|
|
|
end
|
2019-06-07 09:19:59 -07:00
|
|
|
|
2021-05-01 05:19:48 -07:00
|
|
|
--- Gets a string parser
|
|
|
|
---
|
2021-08-22 13:55:28 -07:00
|
|
|
---@param str The string to parse
|
|
|
|
---@param lang The language of this string
|
|
|
|
---@param opts Options to pass to the created language tree
|
2020-11-04 10:03:36 -07:00
|
|
|
function M.get_string_parser(str, lang, opts)
|
2020-09-30 06:32:43 -07:00
|
|
|
vim.validate {
|
|
|
|
str = { str, 'string' },
|
|
|
|
lang = { lang, 'string' }
|
|
|
|
}
|
|
|
|
language.require_language(lang)
|
|
|
|
|
2020-11-04 10:03:36 -07:00
|
|
|
return LanguageTree.new(str, lang, opts)
|
2020-09-30 06:32:43 -07:00
|
|
|
end
|
|
|
|
|
2019-09-28 05:27:20 -07:00
|
|
|
return M
|