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
|
|
|
|
2019-09-28 05:27:20 -07:00
|
|
|
setmetatable(M, {
|
|
|
|
__index = function (t, k)
|
|
|
|
if k == "TSHighlighter" then
|
2020-07-14 12:50:57 -07:00
|
|
|
a.nvim_err_writeln("vim.TSHighlighter is deprecated, please use vim.treesitter.highlighter")
|
|
|
|
t[k] = require'vim.treesitter.highlighter'
|
|
|
|
return t[k]
|
|
|
|
elseif k == "highlighter" then
|
|
|
|
t[k] = require'vim.treesitter.highlighter'
|
2019-09-28 05:27:20 -07:00
|
|
|
return t[k]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
})
|
|
|
|
|
2020-07-14 12:50:57 -07:00
|
|
|
--- Creates a new parser.
|
|
|
|
--
|
|
|
|
-- It is not recommended to use this, use vim.treesitter.get_parser() instead.
|
|
|
|
--
|
|
|
|
-- @param bufnr The buffer the parser will be tied to
|
2020-11-04 10:03:36 -07:00
|
|
|
-- @param lang The language of the parser
|
|
|
|
-- @param opts Options to pass to the language tree
|
|
|
|
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)
|
|
|
|
|
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-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-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.
|
|
|
|
--
|
|
|
|
-- If needed this will create the parser.
|
|
|
|
-- Unconditionnally attach the provided callback
|
|
|
|
--
|
|
|
|
-- @param bufnr The buffer the parser should be tied to
|
|
|
|
-- @param ft The filetype of this parser
|
2020-11-04 10:03:36 -07:00
|
|
|
-- @param opts Options object to pass to the parser
|
2020-07-14 12:50:57 -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
|
|
|
|
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
|