tree-sitter: fix lint, delete "demo" plugin (replaced by functional tests)

This commit is contained in:
Björn Linse 2019-06-21 14:14:51 +02:00
parent 167a1cfdef
commit 06ee45b9b1
7 changed files with 44 additions and 128 deletions

View File

@ -1,58 +0,0 @@
-- TODO: externalize this
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 = theparser:parse_tree()
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

View File

@ -50,7 +50,7 @@ local function create_parser(bufnr, ft, id)
self:parse()
-- TODO: use weakref to self, so that the parser is free'd is no plugin is
-- using it.
local function lines_cb(ev, ...)
local function lines_cb(_, ...)
return on_lines(self, ...)
end
local detach_cb = nil

View File

@ -1,32 +0,0 @@
let g:ts_test_path = expand("<sfile>:p:h:h")
let g:has_ts = v:false
func! TSTest()
if g:has_ts
return
end
" TODO: module!
lua theparser = vim.treesitter.create_parser(0)
lua require'treesitter_demo'
let g:has_ts = v:true
endfunc
func! TSCursor()
" disable matchparen
NoMatchParen
call TSTest()
au CursorMoved <buffer> lua ts_cursor()
au CursorMovedI <buffer> lua ts_cursor()
map <buffer> <Plug>(ts-expand) <cmd>lua ts_expand_node()<cr>
endfunc
func! TSSyntax()
" disable matchparen
set syntax=
call TSTest()
lua ts_syntax()
endfunc
command! TSTest call TSTest()
command! TSCursor call TSCursor()
command! TSSyntax call TSSyntax()

View File

@ -839,9 +839,9 @@ static void nlua_add_treesitter(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL
lua_pushcfunction(lstate, create_tslua_parser);
lua_setfield(lstate, -2, "_create_ts_parser");
lua_pushcfunction(lstate, ts_lua_register_lang);
lua_pushcfunction(lstate, tslua_register_lang);
lua_setfield(lstate, -2, "_ts_add_language");
lua_pushcfunction(lstate, ts_lua_inspect_lang);
lua_pushcfunction(lstate, tslua_inspect_lang);
lua_setfield(lstate, -2, "_ts_inspect_language");
}

View File

@ -1,9 +1,9 @@
// This is an open source non-commercial project. Dear PVS-Studio, please check
// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
// lua bindings for tree-siter.
// NB: this file should contain a generic lua interface for
// tree-sitter trees and nodes, and could be broken out as a reusable library
// lua bindings for tree-sitter.
// NB: this file mostly contains a generic lua interface for tree-sitter
// trees and nodes, and could be broken out as a reusable lua package
#include <stdbool.h>
#include <stdlib.h>
@ -22,9 +22,9 @@
#include "nvim/memline.h"
typedef struct {
TSParser *parser;
TSTree *tree; // internal tree, used for editing/reparsing
} Tslua_parser;
TSParser *parser;
TSTree *tree; // internal tree, used for editing/reparsing
} TSLua_parser;
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "lua/treesitter.c.generated.h"
@ -68,9 +68,9 @@ static struct luaL_Reg node_meta[] = {
{ NULL, NULL }
};
PMap(cstr_t) *langs;
static PMap(cstr_t) *langs;
void build_meta(lua_State *L, const char *tname, const luaL_Reg *meta)
static void build_meta(lua_State *L, const char *tname, const luaL_Reg *meta)
{
if (luaL_newmetatable(L, tname)) { // [meta]
for (size_t i = 0; meta[i].name != NULL; i++) {
@ -84,8 +84,6 @@ void build_meta(lua_State *L, const char *tname, const luaL_Reg *meta)
lua_pop(L, 1); // [] (don't use it now)
}
/// init the tslua library
///
/// all global state is stored in the regirstry of the lua_State
@ -95,13 +93,11 @@ void tslua_init(lua_State *L)
// type metatables
build_meta(L, "treesitter_parser", parser_meta);
build_meta(L, "treesitter_tree", tree_meta);
build_meta(L, "treesitter_node", node_meta);
}
int ts_lua_register_lang(lua_State *L)
int tslua_register_lang(lua_State *L)
{
if (lua_gettop(L) < 2 || !lua_isstring(L, 1) || !lua_isstring(L, 2)) {
return luaL_error(L, "string expected");
@ -114,22 +110,27 @@ int ts_lua_register_lang(lua_State *L)
return 0;
}
// TODO: unsafe!
char symbol_buf[128] = "tree_sitter_";
STRCAT(symbol_buf, lang_name);
#define BUFSIZE 128
char symbol_buf[BUFSIZE];
snprintf(symbol_buf, BUFSIZE, "tree_sitter_%s", lang_name);
#undef BUFSIZE
// TODO: we should maybe keep the uv_lib_t around, and close them
// at exit, to keep LeakSanitizer happy.
uv_lib_t lib;
if (uv_dlopen(path, &lib)) {
return luaL_error(L, "Failed to load parser: uv_dlopen: %s",
uv_dlerror(&lib));
snprintf((char *)IObuff, IOSIZE, "Failed to load parser: uv_dlopen: %s",
uv_dlerror(&lib));
uv_dlclose(&lib);
lua_pushstring(L, (char *)IObuff);
return lua_error(L);
}
TSLanguage *(*lang_parser)(void);
if (uv_dlsym(&lib, symbol_buf, (void **)&lang_parser)) {
return luaL_error(L, "Failed to load parser: uv_dlsym: %s",
uv_dlerror(&lib));
snprintf((char *)IObuff, IOSIZE, "Failed to load parser: uv_dlsym: %s",
uv_dlerror(&lib));
uv_dlclose(&lib);
lua_pushstring(L, (char *)IObuff);
return lua_error(L);
}
TSLanguage *lang = lang_parser();
@ -143,7 +144,7 @@ int ts_lua_register_lang(lua_State *L)
return 1;
}
int ts_lua_inspect_lang(lua_State *L)
int tslua_inspect_lang(lua_State *L)
{
if (lua_gettop(L) < 1 || !lua_isstring(L, 1)) {
return luaL_error(L, "string expected");
@ -176,7 +177,6 @@ int ts_lua_inspect_lang(lua_State *L)
lua_setfield(L, -2, "symbols"); // [retval]
// TODO: this seems to be empty, what langs have fields?
size_t nfields = (size_t)ts_language_field_count(lang);
lua_createtable(L, nfields-1, 1); // [retval, fields]
for (size_t i = 0; i < nfields; i++) {
@ -190,14 +190,14 @@ int ts_lua_inspect_lang(lua_State *L)
int tslua_push_parser(lua_State *L, const char *lang_name)
{
TSParser *parser = ts_parser_new();
TSLanguage *lang = pmap_get(cstr_t)(langs, lang_name);
if (!lang) {
return luaL_error(L, "no such language: %s", lang_name);
}
TSParser *parser = ts_parser_new();
ts_parser_set_language(parser, lang);
Tslua_parser *p = lua_newuserdata(L, sizeof(Tslua_parser)); // [udata]
TSLua_parser *p = lua_newuserdata(L, sizeof(TSLua_parser)); // [udata]
p->parser = parser;
p->tree = NULL;
@ -206,14 +206,14 @@ int tslua_push_parser(lua_State *L, const char *lang_name)
return 1;
}
static Tslua_parser *parser_check(lua_State *L)
static TSLua_parser *parser_check(lua_State *L)
{
return luaL_checkudata(L, 1, "treesitter_parser");
}
static int parser_gc(lua_State *L)
{
Tslua_parser *p = parser_check(L);
TSLua_parser *p = parser_check(L);
if (!p) {
return 0;
}
@ -238,6 +238,7 @@ static const char *input_cb(void *payload, uint32_t byte_index,
buf_T *bp = payload;
#define BUFSIZE 256
static char buf[BUFSIZE];
if ((linenr_T)position.row >= bp->b_ml.ml_line_count) {
*bytes_read = 0;
return "";
@ -246,10 +247,13 @@ static const char *input_cb(void *payload, uint32_t byte_index,
size_t len = STRLEN(line);
size_t tocopy = MIN(len-position.column, BUFSIZE);
// TODO: translate embedded \n to \000
memcpy(buf, line+position.column, tocopy);
// Translate embedded \n to NUL
memchrsub(buf, '\n', '\0', tocopy);
*bytes_read = (uint32_t)tocopy;
if (tocopy < 200) {
if (tocopy < BUFSIZE) {
// now add the final \n. If it didn't fit, input_cb will be called again
// on the same line with advanced column.
buf[tocopy] = '\n';
(*bytes_read)++;
}
@ -259,7 +263,7 @@ static const char *input_cb(void *payload, uint32_t byte_index,
static int parser_parse_buf(lua_State *L)
{
Tslua_parser *p = parser_check(L);
TSLua_parser *p = parser_check(L);
if (!p) {
return 0;
}
@ -282,7 +286,7 @@ static int parser_parse_buf(lua_State *L)
static int parser_tree(lua_State *L)
{
Tslua_parser *p = parser_check(L);
TSLua_parser *p = parser_check(L);
if (!p) {
return 0;
}
@ -298,7 +302,7 @@ static int parser_edit(lua_State *L)
return lua_error(L);
}
Tslua_parser *p = parser_check(L);
TSLua_parser *p = parser_check(L);
if (!p) {
return 0;
}

View File

@ -1,6 +1,10 @@
#ifndef NVIM_LUA_TREESITTER_H
#define NVIM_LUA_TREESITTER_H
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include "tree_sitter/api.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS

View File

@ -1,11 +1,9 @@
-- Test suite for testing interactions with API bindings
local helpers = require('test.functional.helpers')(after_each)
local meths = helpers.meths
local clear = helpers.clear
local eq = helpers.eq
local insert = helpers.insert
local meth_pcall = helpers.meth_pcall
local exec_lua = helpers.exec_lua
local iswin = helpers.iswin
local feed = helpers.feed