aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRiley Bruins <ribru17@hotmail.com>2024-09-19 13:08:22 -0700
committerChristian Clason <c.clason@uni-graz.at>2024-10-11 18:15:07 +0200
commitd3193afc2559e7d84ed2d76664a650dc03b4c6ef (patch)
treecfa3e37e11d158713ae01a691e03853509317e39 /src
parent267c7525f738cdd6024c39da758e885c026ffaaa (diff)
downloadrneovim-d3193afc2559e7d84ed2d76664a650dc03b4c6ef.tar.gz
rneovim-d3193afc2559e7d84ed2d76664a650dc03b4c6ef.tar.bz2
rneovim-d3193afc2559e7d84ed2d76664a650dc03b4c6ef.zip
fix(treesitter): remove duplicate symbol names in language.inspect()
**Problems:** - `vim.treesitter.language.inspect()` returns duplicate symbol names, sometimes up to 6 of one kind in the case of `markdown` - The list-like `symbols` table can have holes and is thus not even a valid msgpack table anyway, mentioned in a test **Solution:** Return symbols as a map, rather than a list, where field names are the names of the symbol. The boolean value associated with the field encodes whether or not the symbol is named. Note that anonymous nodes are surrounded with double quotes (`"`) to prevent potential collisions with named counterparts that have the same identifier.
Diffstat (limited to 'src')
-rw-r--r--src/nvim/lua/treesitter.c16
1 files changed, 10 insertions, 6 deletions
diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c
index 819ec41390..3ceb21b61a 100644
--- a/src/nvim/lua/treesitter.c
+++ b/src/nvim/lua/treesitter.c
@@ -271,12 +271,16 @@ int tslua_inspect_lang(lua_State *L)
// not used by the API
continue;
}
- lua_createtable(L, 2, 0); // [retval, symbols, elem]
- lua_pushstring(L, ts_language_symbol_name(lang, (TSSymbol)i));
- lua_rawseti(L, -2, 1);
- lua_pushboolean(L, t == TSSymbolTypeRegular);
- lua_rawseti(L, -2, 2); // [retval, symbols, elem]
- lua_rawseti(L, -2, (int)i); // [retval, symbols]
+ const char *name = ts_language_symbol_name(lang, (TSSymbol)i);
+ bool named = t == TSSymbolTypeRegular;
+ lua_pushboolean(L, named); // [retval, symbols, is_named]
+ if (!named) {
+ char buf[256];
+ snprintf(buf, sizeof(buf), "\"%s\"", name);
+ lua_setfield(L, -2, buf); // [retval, symbols]
+ } else {
+ lua_setfield(L, -2, name); // [retval, symbols]
+ }
}
lua_setfield(L, -2, "symbols"); // [retval]