fix: update interface following languagetree merge (#687)

This commit is contained in:
Thomas Vigouroux 2020-11-23 19:46:27 +01:00 committed by GitHub
parent 3c07232d07
commit 808765a6c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 16 additions and 204 deletions

View file

@ -130,7 +130,6 @@ lua <<EOF
require'nvim-treesitter.configs'.setup {
highlight = {
enable = true,
use_languagetree = false, -- Use this to enable language injection (this is very unstable)
custom_captures = {
-- Highlight the @foo.bar capture group with the "Identifier" highlight group.
["foo.bar"] = "Identifier",

View file

@ -1,5 +1,6 @@
--TODO(theHamsta): remove once stabilized!
if not pcall(require,"vim.treesitter.query") then
if not pcall(require,"vim.treesitter.languagetree")
and vim.fn.getenv('CI') == 0 then -- TODO(vigoux): remove once new nightlies are built
error("nvim-treesitter requires a more recent Neovim nightly version!")
end

View file

@ -81,19 +81,14 @@ function M.attach(bufnr, lang)
local parser = parsers.get_parser(bufnr, lang)
local config = configs.get_module('highlight')
if config.use_languagetree then
local ltree = require'nvim-treesitter.languagetree'
ltree.new(bufnr, lang)
else
for k, v in pairs(config.custom_captures) do
hlmap[k] = v
end
local query = queries.get_query(lang, "highlights")
if not query then return end
M.highlighters[bufnr] = ts.highlighter.new(parser, query)
for k, v in pairs(config.custom_captures) do
hlmap[k] = v
end
local query = queries.get_query(lang, "highlights")
if not query then return end
M.highlighters[bufnr] = ts.highlighter.new(parser, query)
end
function M.detach(bufnr)

View file

@ -43,7 +43,7 @@ local function select_incremental(get_parent)
local csrow, cscol, cerow, cecol = visual_selection_range()
-- Initialize incremental selection with current selection
if not nodes or #nodes == 0 or not range_matches(nodes[#nodes]) then
local root = parsers.get_parser():parse():root()
local root = parsers.get_parser():parse()[1]:root()
local node = root:named_descendant_for_range(csrow, cscol, cerow, cecol)
ts_utils.update_selection(buf, node)
if nodes and #nodes > 0 then

View file

@ -40,7 +40,7 @@ function M.get_indent(lnum)
local parser = parsers.get_parser()
if not parser or not lnum then return -1 end
local node = get_node_at_line(parser:parse():root(), lnum-1)
local node = get_node_at_line(parser:parse()[1]:root(), lnum-1)
local indent_queries = get_indents(vim.api.nvim_get_current_buf())
local indents = indent_queries.indents
local branches = indent_queries.branches

View file

@ -1,183 +0,0 @@
local parsers = require'nvim-treesitter.parsers'
local queries = require'nvim-treesitter.query'
local tsutils = require'nvim-treesitter.ts_utils'
local TSHighlighter = require'vim.treesitter.highlighter'
local ns = vim.api.nvim_create_namespace("treesitter/highlighter")
local LanguageTree = {}
LanguageTree.__index = LanguageTree
local trees = { }
function LanguageTree.new(bufnr, lang, not_root)
local buf
if not bufnr or bufnr == 0 then
buf = vim.api.nvim_get_current_buf()
else
buf = bufnr
end
local parser = parsers.get_parser(buf, parsers.ft_to_lang(lang))
if not parser then return end
local query = queries.get_query(lang, "highlights")
if not query then return end
local self = setmetatable(
{
highlighter = TSHighlighter.new(parser, query),
parser = parser,
children = {}
},
LanguageTree)
if not not_root then
trees[buf] = self
self.parser:register_cbs{
on_bytes = function() self:update() end
}
end
-- First setup
self:update()
return self
end
function LanguageTree:add_child(lang, child)
if not vim.tbl_contains(self.children, child) then
table.insert(self.children, child)
end
end
function LanguageTree:remove_child(lang)
self.children[lang] = nil
end
function LanguageTree:node_for_range(range)
for _, child in pairs(self.children) do
if child:contains(range) then
return child:node_for_range(range)
end
end
end
function LanguageTree:nodes_for_line(range, result)
result = result or {}
if self:contains(range, true) then
table.insert(result, self)
end
for _, child in pairs(self.children) do
if child:contains(range, true) then
child:nodes_for_line(range, result)
end
end
return result
end
local function range_contains_line(source, dest)
return source[1] <= dest[1] and source[3] >= dest[3]
end
local function range_contains(source, dest)
local start_fits = source[1] < dest[1] or (source[1] == dest[1] and source[2] <= dest[2])
local end_fits = source[3] > dest[3] or (source[3] == dest[3] and source[4] >= dest[4])
return start_fits and end_fits
end
function LanguageTree:contains(range, line_only)
for _, source in pairs(self.parser:included_ranges()) do
local contains_fn = line_only and range_contains_line or range_contains
if contains_fn(source, range) then
return true
end
end
return false
end
function LanguageTree:update()
local query = queries.get_query(self.parser.lang, "injections")
if not query then return end
local root = self.parser:parse():root()
local startl, _, stopl, _ = root:range()
local injections = {}
-- Find injections
for inj in queries.iter_prepared_matches(query, root, self.parser.bufnr, startl, stopl+1) do
local lang = inj.lang
if type(lang) ~= "string" then
lang = tsutils.get_node_text(lang.node, self.parser.bufnr)[1]
end
if not lang or not inj.injection.node then
vim.api.nvim_err_writeln("Invalid match encountered")
return nil
end
if not injections[lang] then
injections[lang] = {}
end
table.insert(injections[lang], inj.injection.node)
end
local seen = {}
-- Update each child accordingly
-- TODO(vigoux): for now avoid languages that include themselves, will
-- be fixed when managing our own parsers
for lang, ranges in pairs(injections) do
if lang ~= self.parser.lang then
if not self.children[lang] then
self.children[lang] = LanguageTree.new(self.parser.bufnr, lang, true)
end
if self.children[lang] then
self.children[lang].parser:set_included_ranges(ranges)
self.children[lang]:update()
seen[lang] = true
end
end
end
-- Clean up unused parsers
for lang, _ in pairs(self.children) do
if not seen[lang] then
self:remove_child(lang)
end
end
end
function LanguageTree._on_line(_, _win, buf, line)
local tree = trees[buf]
if not tree then return end
local line_len = #(vim.api.nvim_buf_get_lines(buf, line, line + 1, false)[1])
local matches = tree:nodes_for_line { line, 0, line, line_len } -- TODO proper search here
-- Matches are from least specific to most specific range (parent -> child)
for _, match in ipairs(matches) do
TSHighlighter._on_line("line", _win, buf, line, match.highlighter)
end
end
vim.api.nvim_set_decoration_provider(ns, {
on_buf = TSHighlighter._on_buf;
on_win = TSHighlighter._on_win;
on_line = LanguageTree._on_line;
})
return LanguageTree

View file

@ -235,7 +235,7 @@ function M.find_usages(node, scope_node, bufnr)
if not node_text or #node_text < 1 then return {} end
local scope_node = scope_node or parsers.get_parser(bufnr):parse():root()
local scope_node = scope_node or parsers.get_parser(bufnr):parse()[1]:root()
local usages = {}
for match in M.iter_locals(bufnr, scope_node) do

View file

@ -406,7 +406,7 @@ end
function M.get_tree_root(bufnr)
local bufnr = bufnr or api.nvim_get_current_buf()
return M.get_parser(bufnr):parse():root()
return M.get_parser(bufnr):parse()[1]:root()
end
-- get language of given buffer

View file

@ -179,7 +179,7 @@ function M.iter_group_results(bufnr, query_group, root)
local parser = parsers.get_parser(bufnr, lang)
if not parser then return function() end end
local root = root or parser:parse():root()
local root = root or parser:parse()[1]:root()
local start_row, _, end_row, _ = root:range()
-- The end row is exclusive so we need to add 1 to it.

View file

@ -116,7 +116,7 @@ end
function M.get_node_at_cursor(winnr)
if not parsers.has_parser() then return end
local cursor = api.nvim_win_get_cursor(winnr or 0)
local root = parsers.get_parser():parse():root()
local root = parsers.get_parser():parse()[1]:root()
return root:named_descendant_for_range(cursor[1]-1,cursor[2],cursor[1]-1,cursor[2])
end

View file

@ -57,7 +57,7 @@ end
function TSRange:parent(range)
local parser = parsers.get_parser(self.buf, parsers.get_buf_lang(range))
local root = parser:parse():root()
local root = parser:parse()[1]:root()
return root:named_descendant_for_range(self.start_pos[1], self.start_pos[2], self.end_pos[1], self.end_pos[2])
end