mirror of
https://github.com/neovim/neovim.git
synced 2025-09-11 22:08:18 +00:00
tree-sitter: objectify API
This commit is contained in:
@@ -7,7 +7,7 @@ end
|
|||||||
local my_ns = __treesitter_rt_ns
|
local my_ns = __treesitter_rt_ns
|
||||||
|
|
||||||
function ts_inspect_pos(row,col)
|
function ts_inspect_pos(row,col)
|
||||||
local tree = parse_tree(theparser)
|
local tree = theparser:parse_tree()
|
||||||
local root = tree:root()
|
local root = tree:root()
|
||||||
local node = root:descendant_for_point_range(row,col,row,col)
|
local node = root:descendant_for_point_range(row,col,row,col)
|
||||||
show_node(node)
|
show_node(node)
|
||||||
|
@@ -1,47 +1,55 @@
|
|||||||
local a = vim.api
|
local a = vim.api
|
||||||
|
|
||||||
function parse_tree(tsstate, force)
|
local Parser = {}
|
||||||
if tsstate.valid and not force then
|
Parser.__index = Parser
|
||||||
return tsstate.tree
|
|
||||||
|
function Parser:parse_tree(force)
|
||||||
|
if self.valid and not force then
|
||||||
|
return self.tree
|
||||||
end
|
end
|
||||||
tsstate.tree = tsstate.parser:parse_buf(tsstate.bufnr)
|
self.tree = self._parser:parse_buf(self.bufnr)
|
||||||
tsstate.valid = true
|
self.valid = true
|
||||||
return tsstate.tree
|
return self.tree
|
||||||
end
|
end
|
||||||
|
|
||||||
local function change_cb(tsstate, ev, bufnr, tick, start_row, oldstopline, stop_row)
|
local function change_cb(self, ev, bufnr, tick, start_row, oldstopline, stop_row)
|
||||||
local start_byte = a.nvim_buf_get_offset(bufnr,start_row)
|
local start_byte = a.nvim_buf_get_offset(bufnr,start_row)
|
||||||
-- a bit messy, should we expose edited but not reparsed tree?
|
-- a bit messy, should we expose edited but not reparsed tree?
|
||||||
-- are multiple edits safe in general?
|
-- are multiple edits safe in general?
|
||||||
local root = tsstate.parser:tree():root()
|
local root = self._parser:tree():root()
|
||||||
-- TODO: add proper lookup function!
|
-- TODO: add proper lookup function!
|
||||||
local inode = root:descendant_for_point_range(oldstopline+9000,0, oldstopline,0)
|
local inode = root:descendant_for_point_range(oldstopline+9000,0, oldstopline,0)
|
||||||
if inode == nil then
|
if inode == nil then
|
||||||
local stop_byte = a.nvim_buf_get_offset(bufnr,stop_row)
|
local stop_byte = a.nvim_buf_get_offset(bufnr,stop_row)
|
||||||
tsstate.parser:edit(start_byte,stop_byte,stop_byte,start_row,0,stop_row,0,stop_row,0)
|
self._parser:edit(start_byte,stop_byte,stop_byte,start_row,0,stop_row,0,stop_row,0)
|
||||||
else
|
else
|
||||||
local fakeoldstoprow, fakeoldstopcol, fakebyteoldstop = inode:start()
|
local fakeoldstoprow, fakeoldstopcol, fakebyteoldstop = inode:start()
|
||||||
local fake_rows = fakeoldstoprow-oldstopline
|
local fake_rows = fakeoldstoprow-oldstopline
|
||||||
local fakestop = stop_row+fake_rows
|
local fakestop = stop_row+fake_rows
|
||||||
local fakebytestop = a.nvim_buf_get_offset(bufnr,fakestop)+fakeoldstopcol
|
local fakebytestop = a.nvim_buf_get_offset(bufnr,fakestop)+fakeoldstopcol
|
||||||
tsstate.parser:edit(start_byte,fakebyteoldstop,fakebytestop,start_row,0,fakeoldstoprow,fakeoldstopcol,fakestop,fakeoldstopcol)
|
self._parser:edit(start_byte,fakebyteoldstop,fakebytestop,start_row,0,fakeoldstoprow,fakeoldstopcol,fakestop,fakeoldstopcol)
|
||||||
end
|
end
|
||||||
tsstate.valid = false
|
self.valid = false
|
||||||
end
|
end
|
||||||
|
|
||||||
function create_parser(bufnr)
|
local function create_parser(bufnr)
|
||||||
if bufnr == 0 then
|
if bufnr == 0 then
|
||||||
bufnr = a.nvim_get_current_buf()
|
bufnr = a.nvim_get_current_buf()
|
||||||
end
|
end
|
||||||
local ft = a.nvim_buf_get_option(bufnr, "filetype")
|
local ft = a.nvim_buf_get_option(bufnr, "filetype")
|
||||||
local tsstate = {}
|
local self = setmetatable({bufnr=bufnr, valid=false}, Parser)
|
||||||
tsstate.bufnr = bufnr
|
self._parser = vim._create_ts_parser(ft.."_parser.so", ft)
|
||||||
tsstate.parser = vim.ts_parser(ft.."_parser.so", ft)
|
self:parse_tree()
|
||||||
parse_tree(tsstate)
|
|
||||||
local function cb(ev, ...)
|
local function cb(ev, ...)
|
||||||
return change_cb(tsstate, ev, ...)
|
-- TODO: use weakref to self, so that the parser is free'd is no plugin is
|
||||||
|
-- using it.
|
||||||
|
return change_cb(self, ev, ...)
|
||||||
end
|
end
|
||||||
a.nvim_buf_attach(tsstate.bufnr, false, {on_lines=cb})
|
a.nvim_buf_attach(self.bufnr, false, {on_lines=cb})
|
||||||
return tsstate
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- TODO: weak table with reusable parser per buffer.
|
||||||
|
|
||||||
|
return {create_parser=create_parser}
|
||||||
|
|
||||||
|
@@ -6,9 +6,8 @@ func! TSTest()
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
" TODO: module!
|
" TODO: module!
|
||||||
lua require'vim.tree_sitter'
|
lua theparser = require'vim.tree_sitter'.create_parser(0)
|
||||||
lua require'tree_sitter_demo'
|
lua require'tree_sitter_demo'
|
||||||
lua theparser = create_parser(vim.api.nvim_get_current_buf())
|
|
||||||
let g:has_ts = v:true
|
let g:has_ts = v:true
|
||||||
endfunc
|
endfunc
|
||||||
|
|
||||||
|
@@ -822,16 +822,6 @@ void ex_luafile(exarg_T *const eap)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int unsafe_ptr_to_ts_tree(lua_State *L)
|
|
||||||
{
|
|
||||||
if (!lua_gettop(L)) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
TSTree *const *ptr = lua_topointer(L,1);
|
|
||||||
tslua_push_tree(L, *ptr);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int create_tslua_parser(lua_State *L)
|
static int create_tslua_parser(lua_State *L)
|
||||||
{
|
{
|
||||||
if (lua_gettop(L) < 2) {
|
if (lua_gettop(L) < 2) {
|
||||||
@@ -867,9 +857,7 @@ static int create_tslua_parser(lua_State *L)
|
|||||||
static void nlua_add_treesitter(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL
|
static void nlua_add_treesitter(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL
|
||||||
{
|
{
|
||||||
tslua_init(lstate);
|
tslua_init(lstate);
|
||||||
lua_pushcfunction(lstate, unsafe_ptr_to_ts_tree);
|
|
||||||
lua_setfield(lstate, -2, "unsafe_ts_tree");
|
|
||||||
|
|
||||||
lua_pushcfunction(lstate, create_tslua_parser);
|
lua_pushcfunction(lstate, create_tslua_parser);
|
||||||
lua_setfield(lstate, -2, "ts_parser");
|
lua_setfield(lstate, -2, "_create_ts_parser");
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user