tree-sitter: implement query functionality and highlighting prototype [skip.lint]

This commit is contained in:
Björn Linse
2019-09-28 14:27:20 +02:00
parent c21511b2f4
commit 440695c296
18 changed files with 1293 additions and 151 deletions

View File

@@ -169,21 +169,21 @@ Boolean nvim_buf_attach(uint64_t channel_id,
goto error;
}
cb.on_lines = v->data.luaref;
v->data.integer = LUA_NOREF;
v->data.luaref = LUA_NOREF;
} else if (is_lua && strequal("on_changedtick", k.data)) {
if (v->type != kObjectTypeLuaRef) {
api_set_error(err, kErrorTypeValidation, "callback is not a function");
goto error;
}
cb.on_changedtick = v->data.luaref;
v->data.integer = LUA_NOREF;
v->data.luaref = LUA_NOREF;
} else if (is_lua && strequal("on_detach", k.data)) {
if (v->type != kObjectTypeLuaRef) {
api_set_error(err, kErrorTypeValidation, "callback is not a function");
goto error;
}
cb.on_detach = v->data.luaref;
v->data.integer = LUA_NOREF;
v->data.luaref = LUA_NOREF;
} else if (is_lua && strequal("utf_sizes", k.data)) {
if (v->type != kObjectTypeBoolean) {
api_set_error(err, kErrorTypeValidation, "utf_sizes must be boolean");
@@ -231,6 +231,90 @@ Boolean nvim_buf_detach(uint64_t channel_id,
return true;
}
static void buf_clear_luahl(buf_T *buf, bool force)
{
if (buf->b_luahl || force) {
executor_free_luaref(buf->b_luahl_start);
executor_free_luaref(buf->b_luahl_window);
executor_free_luaref(buf->b_luahl_line);
executor_free_luaref(buf->b_luahl_end);
}
buf->b_luahl_start = LUA_NOREF;
buf->b_luahl_window = LUA_NOREF;
buf->b_luahl_line = LUA_NOREF;
buf->b_luahl_end = LUA_NOREF;
}
/// Unstabilized interface for defining syntax hl in lua.
///
/// This is not yet safe for general use, lua callbacks will need to
/// be restricted, like textlock and probably other stuff.
///
/// The API on_line/nvim__put_attr is quite raw and not intended to be the
/// final shape. Ideally this should operate on chunks larger than a single
/// line to reduce interpreter overhead, and generate annotation objects
/// (bufhl/virttext) on the fly but using the same representation.
void nvim__buf_set_luahl(uint64_t channel_id, Buffer buffer,
DictionaryOf(LuaRef) opts, Error *err)
FUNC_API_LUA_ONLY
{
buf_T *buf = find_buffer_by_handle(buffer, err);
if (!buf) {
return;
}
redraw_buf_later(buf, NOT_VALID);
buf_clear_luahl(buf, false);
for (size_t i = 0; i < opts.size; i++) {
String k = opts.items[i].key;
Object *v = &opts.items[i].value;
if (strequal("on_start", k.data)) {
if (v->type != kObjectTypeLuaRef) {
api_set_error(err, kErrorTypeValidation, "callback is not a function");
goto error;
}
buf->b_luahl_start = v->data.luaref;
v->data.luaref = LUA_NOREF;
} else if (strequal("on_window", k.data)) {
if (v->type != kObjectTypeLuaRef) {
api_set_error(err, kErrorTypeValidation, "callback is not a function");
goto error;
}
buf->b_luahl_window = v->data.luaref;
v->data.luaref = LUA_NOREF;
} else if (strequal("on_line", k.data)) {
if (v->type != kObjectTypeLuaRef) {
api_set_error(err, kErrorTypeValidation, "callback is not a function");
goto error;
}
buf->b_luahl_line = v->data.luaref;
v->data.luaref = LUA_NOREF;
} else {
api_set_error(err, kErrorTypeValidation, "unexpected key: %s", k.data);
goto error;
}
}
buf->b_luahl = true;
return;
error:
buf_clear_luahl(buf, true);
buf->b_luahl = false;
}
void nvim__buf_redraw_range(Buffer buffer, Integer first, Integer last,
Error *err)
FUNC_API_LUA_ONLY
{
buf_T *buf = find_buffer_by_handle(buffer, err);
if (!buf) {
return;
}
redraw_buf_range_later(buf, (linenr_T)first+1, (linenr_T)last);
}
/// Sets a buffer line
///
/// @deprecated use nvim_buf_set_lines instead.
@@ -1112,7 +1196,6 @@ Array nvim_buf_get_extmarks(Buffer buffer, Integer ns_id, Object start,
return rv;
}
limit = v->data.integer;
v->data.integer = LUA_NOREF;
} else {
api_set_error(err, kErrorTypeValidation, "unexpected key: %s", k.data);
return rv;

View File

@@ -60,6 +60,12 @@
#define ADD(array, item) \
kv_push(array, item)
#define FIXED_TEMP_ARRAY(name, fixsize) \
Array name = ARRAY_DICT_INIT; \
Object name##__items[fixsize]; \
args.size = fixsize; \
args.items = name##__items; \
#define STATIC_CSTR_AS_STRING(s) ((String) {.data = s, .size = sizeof(s) - 1})
/// Create a new String instance, putting data in allocated memory

View File

@@ -189,6 +189,15 @@ Dictionary nvim_get_hl_by_id(Integer hl_id, Boolean rgb, Error *err)
return hl_get_attr_by_id(attrcode, rgb, err);
}
/// Gets a highlight group by name
///
/// similar to |hlID()|, but allocates a new ID if not present.
Integer nvim_get_hl_id_by_name(String name)
FUNC_API_SINCE(7)
{
return syn_check_group((const char_u *)name.data, (int)name.size);
}
/// Sends input-keys to Nvim, subject to various quirks controlled by `mode`
/// flags. This is a blocking call, unlike |nvim_input()|.
///
@@ -2546,3 +2555,27 @@ Array nvim__inspect_cell(Integer grid, Integer row, Integer col, Error *err)
}
return ret;
}
/// Set attrs in nvim__buf_set_lua_hl callbacks
///
/// TODO(bfredl): This is rather pedestrian. The final
/// interface should probably be derived from a reformed
/// bufhl/virttext interface with full support for multi-line
/// ranges etc
void nvim__put_attr(Integer id, Integer c0, Integer c1)
FUNC_API_LUA_ONLY
{
if (!lua_attr_active) {
return;
}
if (id == 0 || syn_get_final_id((int)id) == 0) {
return;
}
int attr = syn_id2attr((int)id);
c0 = MAX(c0, 0);
c1 = MIN(c1, (Integer)lua_attr_bufsize);
for (Integer c = c0; c < c1; c++) {
lua_attr_buf[c] = (sattr_T)hl_combine_attr(lua_attr_buf[c], (int)attr);
}
return;
}