diff options
author | Björn Linse <bjorn.linse@gmail.com> | 2019-06-07 14:21:00 +0200 |
---|---|---|
committer | Björn Linse <bjorn.linse@gmail.com> | 2019-09-28 14:55:43 +0200 |
commit | 005b6d638caa200711bf5960e0c0d70ba5721c94 (patch) | |
tree | f8513f54348ab32ba8ef49646c1be0eda251aa24 | |
parent | c07e1e8696826579f017c9b401fe264d70384ceb (diff) | |
download | rneovim-005b6d638caa200711bf5960e0c0d70ba5721c94.tar.gz rneovim-005b6d638caa200711bf5960e0c0d70ba5721c94.tar.bz2 rneovim-005b6d638caa200711bf5960e0c0d70ba5721c94.zip |
tree-sitter: split tree-sitter lua interface from demo code
-rw-r--r-- | runtime/lua/tree_sitter_demo.lua | 57 | ||||
-rw-r--r-- | runtime/lua/treesitter_rt.lua | 115 | ||||
-rw-r--r-- | runtime/lua/vim/tree_sitter.lua | 47 | ||||
-rw-r--r-- | runtime/plugin/ts_test.vim | 3 |
4 files changed, 106 insertions, 116 deletions
diff --git a/runtime/lua/tree_sitter_demo.lua b/runtime/lua/tree_sitter_demo.lua new file mode 100644 index 0000000000..24a0f3d622 --- /dev/null +++ b/runtime/lua/tree_sitter_demo.lua @@ -0,0 +1,57 @@ +local a = vim.api +_G.a = vim.api + +if __treesitter_rt_ns == nil then + __treesitter_rt_ns = a.nvim_create_namespace("treesitter_demp") +end +local my_ns = __treesitter_rt_ns + +function ts_inspect_pos(row,col) + local tree = parse_tree(theparser) + local root = tree:root() + local node = root:descendant_for_point_range(row,col,row,col) + show_node(node) +end + +function show_node(node) + if node == nil then + return + end + a.nvim_buf_clear_highlight(0, my_ns, 0, -1) + shown_node = node + print(node:type()) + local start_row, start_col, end_row, end_col = node:range() + + a.nvim_buf_add_highlight(0, my_ns, "ErrorMsg", start_row, start_col, start_col+1) + + if end_col >= 1 then + end_col = end_col - 1 + end + a.nvim_buf_add_highlight(0, my_ns, "ErrorMsg", end_row, end_col, end_col+1) +end + +function ts_expand_node() + if shown_node == nil then + return + end + parent = shown_node:parent() + show_node(parent) +end + +function ts_cursor() + local row, col = unpack(a.nvim_win_get_cursor(0)) + ts_inspect_pos(row-1, col) +end + +if false then + ctree = theparser.tree + root = ctree:root() + cursor = root:to_cursor() + node = cursor:forward(5000) if true then return node end + print(#root) + c = root:child(50) + print(require'inspect'{c:extent()}) + type(ctree.__tostring) + root:__tostring() + print(_tslua_debug()) +end diff --git a/runtime/lua/treesitter_rt.lua b/runtime/lua/treesitter_rt.lua deleted file mode 100644 index 1dea22000a..0000000000 --- a/runtime/lua/treesitter_rt.lua +++ /dev/null @@ -1,115 +0,0 @@ -local a = vim.api - -if __treesitter_rt_ns == nil then - __treesitter_rt_ns = a.nvim_buf_add_highlight(0, 0, "", 0, 0, 0) - __treesitter_rt_syn_ns = a.nvim_buf_add_highlight(0, 0, "", 0, 0, 0) -end -local my_ns = __treesitter_rt_ns -local my_syn_ns = __treesitter_rt_syn_ns - - ---luadev = require'luadev' ---i = require'inspect' - - -function parse_tree(tsstate, force) - if tsstate.valid and not force then - return tsstate.tree - end - tsstate.tree = tsstate.parser:parse_buf(tsstate.bufnr) - tsstate.valid = true - return tsstate.tree -end - -function the_cb(tsstate, ev, bufnr, tick, start_row, oldstopline, stop_row) - local start_byte = a.nvim_buf_get_offset(bufnr,start_row) - -- a bit messy, should we expose edited but not reparsed tree? - -- are multiple edits safe in general? - local root = tsstate.parser:tree():root() - -- TODO: add proper lookup function! - local inode = root:descendant_for_point_range(oldstopline+9000,0, oldstopline,0) - local edit - if inode == nil then - 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) - else - local fakeoldstoprow, fakeoldstopcol, fakebyteoldstop = inode:start() - local fake_rows = fakeoldstoprow-oldstopline - local fakestop = stop_row+fake_rows - local fakebytestop = a.nvim_buf_get_offset(bufnr,fakestop)+fakeoldstopcol - tsstate.parser:edit(start_byte,fakebyteoldstop,fakebytestop,start_row,0,fakeoldstoprow,fakeoldstopcol,fakestop,fakeoldstopcol) - end - tsstate.valid = false - --luadev.append_buf({i{edit.start_byte,edit.old_end_byte,edit.new_end_byte}, - -- i{edit.start_point, edit.old_end_point, edit.new_end_point}}) -end - -function attach_buf(tsstate) - local function cb(ev, ...) - return the_cb(tsstate, ev, ...) - end - a.nvim_buf_attach(tsstate.bufnr, false, {on_lines=cb}) -end - -function create_parser(bufnr) - if bufnr == 0 then - bufnr = a.nvim_get_current_buf() - end - local ft = a.nvim_buf_get_option(bufnr, "filetype") - local tsstate = {} - tsstate.bufnr = bufnr - tsstate.parser = vim.ts_parser(ft.."_parser.so", ft) - parse_tree(tsstate) - attach_buf(tsstate) - return tsstate -end - -function ts_inspect_pos(row,col) - local tree = parse_tree(theparser) - local root = tree:root() - local node = root:descendant_for_point_range(row,col,row,col) - show_node(node) -end - -function show_node(node) - if node == nil then - return - end - a.nvim_buf_clear_highlight(0, my_ns, 0, -1) - shown_node = node - print(node:type()) - local start_row, start_col, end_row, end_col = node:range() - - a.nvim_buf_add_highlight(0, my_ns, "ErrorMsg", start_row, start_col, start_col+1) - - if end_col >= 1 then - end_col = end_col - 1 - end - a.nvim_buf_add_highlight(0, my_ns, "ErrorMsg", end_row, end_col, end_col+1) -end - -function ts_expand_node() - if shown_node == nil then - return - end - parent = shown_node:parent() - show_node(parent) -end - -function ts_cursor() - local row, col = unpack(a.nvim_win_get_cursor(0)) - ts_inspect_pos(row-1, col) -end - -if false then - ctree = theparser.tree - root = ctree:root() - cursor = root:to_cursor() - node = cursor:forward(5000) if true then return node end - print(#root) - c = root:child(50) - print(require'inspect'{c:extent()}) - type(ctree.__tostring) - root:__tostring() - print(_tslua_debug()) -end diff --git a/runtime/lua/vim/tree_sitter.lua b/runtime/lua/vim/tree_sitter.lua new file mode 100644 index 0000000000..a4cb3f3db6 --- /dev/null +++ b/runtime/lua/vim/tree_sitter.lua @@ -0,0 +1,47 @@ +local a = vim.api + +function parse_tree(tsstate, force) + if tsstate.valid and not force then + return tsstate.tree + end + tsstate.tree = tsstate.parser:parse_buf(tsstate.bufnr) + tsstate.valid = true + return tsstate.tree +end + +local function change_cb(tsstate, ev, bufnr, tick, start_row, oldstopline, stop_row) + local start_byte = a.nvim_buf_get_offset(bufnr,start_row) + -- a bit messy, should we expose edited but not reparsed tree? + -- are multiple edits safe in general? + local root = tsstate.parser:tree():root() + -- TODO: add proper lookup function! + local inode = root:descendant_for_point_range(oldstopline+9000,0, oldstopline,0) + if inode == nil then + 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) + else + local fakeoldstoprow, fakeoldstopcol, fakebyteoldstop = inode:start() + local fake_rows = fakeoldstoprow-oldstopline + local fakestop = stop_row+fake_rows + local fakebytestop = a.nvim_buf_get_offset(bufnr,fakestop)+fakeoldstopcol + tsstate.parser:edit(start_byte,fakebyteoldstop,fakebytestop,start_row,0,fakeoldstoprow,fakeoldstopcol,fakestop,fakeoldstopcol) + end + tsstate.valid = false +end + +function create_parser(bufnr) + if bufnr == 0 then + bufnr = a.nvim_get_current_buf() + end + local ft = a.nvim_buf_get_option(bufnr, "filetype") + local tsstate = {} + tsstate.bufnr = bufnr + tsstate.parser = vim.ts_parser(ft.."_parser.so", ft) + parse_tree(tsstate) + local function cb(ev, ...) + return change_cb(tsstate, ev, ...) + end + a.nvim_buf_attach(tsstate.bufnr, false, {on_lines=cb}) + return tsstate +end + diff --git a/runtime/plugin/ts_test.vim b/runtime/plugin/ts_test.vim index e40e2792e1..d048dbe7c8 100644 --- a/runtime/plugin/ts_test.vim +++ b/runtime/plugin/ts_test.vim @@ -6,7 +6,8 @@ func! TSTest() return end " TODO: module! - lua require'treesitter_rt' + lua require'vim.tree_sitter' + lua require'tree_sitter_demo' lua theparser = create_parser(vim.api.nvim_get_current_buf()) let g:has_ts = v:true endfunc |