mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
feat(treesitter): allow customizing language symbol name
This commit is contained in:
parent
15a768eeb0
commit
3c1d70f20b
@ -387,16 +387,20 @@ inspect_language({lang}) *inspect_language()*
|
|||||||
Parameters: ~
|
Parameters: ~
|
||||||
{lang} The language.
|
{lang} The language.
|
||||||
|
|
||||||
require_language({lang}, {path}, {silent}) *require_language()*
|
*require_language()*
|
||||||
|
require_language({lang}, {path}, {silent}, {symbol_name})
|
||||||
Asserts that the provided language is installed, and optionally provide a
|
Asserts that the provided language is installed, and optionally provide a
|
||||||
path for the parser
|
path for the parser
|
||||||
|
|
||||||
Parsers are searched in the `parser` runtime directory.
|
Parsers are searched in the `parser` runtime directory.
|
||||||
|
|
||||||
Parameters: ~
|
Parameters: ~
|
||||||
{lang} The language the parser should parse
|
{lang} (string) The language the parser should parse
|
||||||
{path} Optional path the parser is located at
|
{path} (string|nil) Optional path the parser is located at
|
||||||
{silent} Don't throw an error if language not found
|
{silent} (boolean|nil) Don't throw an error if language not
|
||||||
|
found
|
||||||
|
{symbol_name} (string|nil) Internal symbol name for the language to
|
||||||
|
load
|
||||||
|
|
||||||
|
|
||||||
==============================================================================
|
==============================================================================
|
||||||
|
@ -6,10 +6,11 @@ local M = {}
|
|||||||
---
|
---
|
||||||
--- Parsers are searched in the `parser` runtime directory.
|
--- Parsers are searched in the `parser` runtime directory.
|
||||||
---
|
---
|
||||||
---@param lang The language the parser should parse
|
---@param lang string The language the parser should parse
|
||||||
---@param path Optional path the parser is located at
|
---@param path string|nil Optional path the parser is located at
|
||||||
---@param silent Don't throw an error if language not found
|
---@param silent boolean|nil Don't throw an error if language not found
|
||||||
function M.require_language(lang, path, silent)
|
---@param symbol_name string|nil Internal symbol name for the language to load
|
||||||
|
function M.require_language(lang, path, silent, symbol_name)
|
||||||
if vim._ts_has_language(lang) then
|
if vim._ts_has_language(lang) then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
@ -21,7 +22,6 @@ function M.require_language(lang, path, silent)
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
-- TODO(bfredl): help tag?
|
|
||||||
error("no parser for '" .. lang .. "' language, see :help treesitter-parsers")
|
error("no parser for '" .. lang .. "' language, see :help treesitter-parsers")
|
||||||
end
|
end
|
||||||
path = paths[1]
|
path = paths[1]
|
||||||
@ -29,10 +29,10 @@ function M.require_language(lang, path, silent)
|
|||||||
|
|
||||||
if silent then
|
if silent then
|
||||||
return pcall(function()
|
return pcall(function()
|
||||||
vim._ts_add_language(path, lang)
|
vim._ts_add_language(path, lang, symbol_name)
|
||||||
end)
|
end)
|
||||||
else
|
else
|
||||||
vim._ts_add_language(path, lang)
|
vim._ts_add_language(path, lang, symbol_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
@ -14,10 +14,12 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <uv.h>
|
||||||
|
|
||||||
#include "nvim/api/private/helpers.h"
|
#include "nvim/api/private/helpers.h"
|
||||||
#include "nvim/buffer.h"
|
#include "nvim/buffer.h"
|
||||||
#include "nvim/lib/kvec.h"
|
#include "nvim/lib/kvec.h"
|
||||||
|
#include "nvim/log.h"
|
||||||
#include "nvim/lua/treesitter.h"
|
#include "nvim/lua/treesitter.h"
|
||||||
#include "nvim/memline.h"
|
#include "nvim/memline.h"
|
||||||
#include "tree_sitter/api.h"
|
#include "tree_sitter/api.h"
|
||||||
@ -145,18 +147,27 @@ int tslua_has_language(lua_State *L)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Creates the language into the internal language map.
|
||||||
|
//
|
||||||
|
// Returns true if the language is correctly loaded in the language map
|
||||||
int tslua_add_language(lua_State *L)
|
int tslua_add_language(lua_State *L)
|
||||||
{
|
{
|
||||||
const char *path = luaL_checkstring(L, 1);
|
const char *path = luaL_checkstring(L, 1);
|
||||||
const char *lang_name = luaL_checkstring(L, 2);
|
const char *lang_name = luaL_checkstring(L, 2);
|
||||||
|
const char *symbol_name = lang_name;
|
||||||
|
|
||||||
|
if (lua_gettop(L) >= 3 && !lua_isnil(L, 3)) {
|
||||||
|
symbol_name = luaL_checkstring(L, 3);
|
||||||
|
}
|
||||||
|
|
||||||
if (pmap_has(cstr_t)(&langs, lang_name)) {
|
if (pmap_has(cstr_t)(&langs, lang_name)) {
|
||||||
return 0;
|
lua_pushboolean(L, true);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define BUFSIZE 128
|
#define BUFSIZE 128
|
||||||
char symbol_buf[BUFSIZE];
|
char symbol_buf[BUFSIZE];
|
||||||
snprintf(symbol_buf, BUFSIZE, "tree_sitter_%s", lang_name);
|
snprintf(symbol_buf, BUFSIZE, "tree_sitter_%s", symbol_name);
|
||||||
#undef BUFSIZE
|
#undef BUFSIZE
|
||||||
|
|
||||||
uv_lib_t lib;
|
uv_lib_t lib;
|
||||||
@ -179,6 +190,7 @@ int tslua_add_language(lua_State *L)
|
|||||||
|
|
||||||
TSLanguage *lang = lang_parser();
|
TSLanguage *lang = lang_parser();
|
||||||
if (lang == NULL) {
|
if (lang == NULL) {
|
||||||
|
uv_dlclose(&lib);
|
||||||
return luaL_error(L, "Failed to load parser %s: internal error", path);
|
return luaL_error(L, "Failed to load parser %s: internal error", path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ local pending_c_parser = helpers.pending_c_parser
|
|||||||
|
|
||||||
before_each(clear)
|
before_each(clear)
|
||||||
|
|
||||||
describe('treesitter API', function()
|
describe('treesitter language API', function()
|
||||||
-- error tests not requiring a parser library
|
-- error tests not requiring a parser library
|
||||||
it('handles missing language', function()
|
it('handles missing language', function()
|
||||||
eq("Error executing lua: .../language.lua:0: no parser for 'borklang' language, see :help treesitter-parsers",
|
eq("Error executing lua: .../language.lua:0: no parser for 'borklang' language, see :help treesitter-parsers",
|
||||||
@ -26,6 +26,9 @@ describe('treesitter API', function()
|
|||||||
|
|
||||||
eq("Error executing lua: .../language.lua:0: no parser for 'borklang' language, see :help treesitter-parsers",
|
eq("Error executing lua: .../language.lua:0: no parser for 'borklang' language, see :help treesitter-parsers",
|
||||||
pcall_err(exec_lua, "parser = vim.treesitter.inspect_language('borklang')"))
|
pcall_err(exec_lua, "parser = vim.treesitter.inspect_language('borklang')"))
|
||||||
|
|
||||||
|
matches("Error executing lua: Failed to load parser: uv_dlsym: .+",
|
||||||
|
pcall_err(exec_lua, "parser = vim.treesitter.require_language('c', nil, false, 'borklang')"))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('inspects language', function()
|
it('inspects language', function()
|
||||||
|
Loading…
Reference in New Issue
Block a user