mirror of
https://github.com/chenasraf/nvim-treesitter.git
synced 2026-05-18 01:39:00 +00:00
32
README.md
32
README.md
@@ -357,6 +357,38 @@ This will respect your `foldminlines` and `foldnestmax` settings.
|
||||
|
||||
# Advanced setup
|
||||
|
||||
## Changing the parser install directory
|
||||
|
||||
If you want to install the parsers to a custom directory you can specify this
|
||||
directory with `parser_install_dir` option in that is passed to `setup`.
|
||||
`nvim-treesitter` will then install the parser files into this directory.
|
||||
|
||||
This directory must be writeable and must be explicitly added to the
|
||||
`runtimepath`. For example:
|
||||
|
||||
``` lua
|
||||
require'nvim-treesitter.configs'.setup {
|
||||
parser_install_dir = "/some/path/to/store/parsers",
|
||||
|
||||
...
|
||||
|
||||
}
|
||||
vim.opt.runtimepath:append("/some/path/to/store/parsers")
|
||||
```
|
||||
|
||||
If this option is not included in the setup options, or is explicitly set to
|
||||
`nil` then the default install directories will be used. If this value is set
|
||||
the default directories will be ignored.
|
||||
|
||||
Bear in mind that any parser installed into a parser folder on the runtime path
|
||||
will still be considered installed. (For example if
|
||||
"~/.local/share/nvim/site/parser/c.so" exists then the "c" parser will be
|
||||
considered installed, even though it is not in `parser_install_dir`)
|
||||
|
||||
The default paths are:
|
||||
1. first the package folder. Where `nvim-treesitter` is installed.
|
||||
2. second the site directory. This is the "site" subdirectory of `stdpath("data")`.
|
||||
|
||||
## Adding parsers
|
||||
|
||||
If you have a parser that is not on the list of supported languages (either as a repository on Github or in a local directory), you can add it manually for use by `nvim-treesitter` as follows:
|
||||
|
||||
@@ -39,6 +39,12 @@ To enable supported features, put this in your `init.lua` file:
|
||||
|
||||
>
|
||||
require'nvim-treesitter.configs'.setup {
|
||||
-- A directory to install the parsers into.
|
||||
-- If this is excluded or nil parsers are installed
|
||||
-- to either the package dir, or the "site" dir.
|
||||
-- If a custom path is used (not nil) it must be added to the runtimepath.
|
||||
parser_install_dir = "/some/path/to/store/parsers",
|
||||
|
||||
-- A list of parser names, or "all"
|
||||
ensure_installed = { "c", "lua", "rust" },
|
||||
|
||||
@@ -62,6 +68,7 @@ To enable supported features, put this in your `init.lua` file:
|
||||
additional_vim_regex_highlighting = false,
|
||||
},
|
||||
}
|
||||
vim.opt.runtimepath:append("/some/path/to/store/parsers")
|
||||
<
|
||||
|
||||
See |nvim-treesitter-modules| for a list of all available modules and its options.
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
local api = vim.api
|
||||
local luv = vim.loop
|
||||
|
||||
local queries = require "nvim-treesitter.query"
|
||||
local ts_query = require "vim.treesitter.query"
|
||||
@@ -14,6 +15,7 @@ local config = {
|
||||
ensure_installed = {},
|
||||
ignore_install = {},
|
||||
update_strategy = "lockfile",
|
||||
parser_install_dir = nil,
|
||||
}
|
||||
-- List of modules that need to be setup on initialization.
|
||||
local queued_modules_defs = {}
|
||||
@@ -379,6 +381,10 @@ end
|
||||
function M.setup(user_data)
|
||||
config.modules = vim.tbl_deep_extend("force", config.modules, user_data)
|
||||
config.ignore_install = user_data.ignore_install or {}
|
||||
config.parser_install_dir = user_data.parser_install_dir or nil
|
||||
if config.parser_install_dir then
|
||||
config.parser_install_dir = vim.fn.expand(config.parser_install_dir, ":p")
|
||||
end
|
||||
|
||||
local ensure_installed = user_data.ensure_installed or {}
|
||||
if #ensure_installed > 0 then
|
||||
@@ -528,6 +534,45 @@ function M.init()
|
||||
end
|
||||
end
|
||||
|
||||
-- If parser_install_dir is not nil is used or created.
|
||||
-- If parser_install_dir is nil try the package dir of the nvim-treesitter
|
||||
-- plugin first, followed by the "site" dir from "runtimepath". "site" dir will
|
||||
-- be created if it doesn't exist. Using only the package dir won't work when
|
||||
-- the plugin is installed with Nix, since the "/nix/store" is read-only.
|
||||
function M.get_parser_install_dir(folder_name)
|
||||
folder_name = folder_name or "parser"
|
||||
|
||||
if config.parser_install_dir then
|
||||
local parser_dir = utils.join_path(config.parser_install_dir, folder_name)
|
||||
return utils.create_or_resue_writable_dir(
|
||||
parser_dir,
|
||||
utils.join_space("Could not create parser dir '", parser_dir, "': "),
|
||||
utils.join_space("Parser dir '", parser_dir, "' should be read/write.")
|
||||
)
|
||||
end
|
||||
|
||||
local package_path = utils.get_package_path()
|
||||
local package_path_parser_dir = utils.join_path(package_path, folder_name)
|
||||
|
||||
-- If package_path is read/write, use that
|
||||
if luv.fs_access(package_path_parser_dir, "RW") then
|
||||
return package_path_parser_dir
|
||||
end
|
||||
|
||||
local site_dir = utils.get_site_dir()
|
||||
local parser_dir = utils.join_path(site_dir, folder_name)
|
||||
|
||||
return utils.create_or_resue_writable_dir(
|
||||
parser_dir,
|
||||
nil,
|
||||
utils.join_space("Invalid rights,", package_path, "or", parser_dir, "should be read/write")
|
||||
)
|
||||
end
|
||||
|
||||
function M.get_parser_info_dir(parser_install_dir)
|
||||
return M.get_parser_install_dir "parser-info"
|
||||
end
|
||||
|
||||
function M.get_update_strategy()
|
||||
return config.update_strategy
|
||||
end
|
||||
|
||||
@@ -80,7 +80,7 @@ local function get_revision(lang)
|
||||
end
|
||||
|
||||
local function get_installed_revision(lang)
|
||||
local lang_file = utils.join_path(utils.get_parser_info_dir(), lang .. ".revision")
|
||||
local lang_file = utils.join_path(configs.get_parser_info_dir(), lang .. ".revision")
|
||||
if vim.fn.filereadable(lang_file) == 1 then
|
||||
return vim.fn.readfile(lang_file)[1]
|
||||
end
|
||||
@@ -243,9 +243,9 @@ local function run_install(cache_folder, install_folder, lang, repo, with_sync,
|
||||
compile_location = repo.url
|
||||
else
|
||||
local repo_location = string.gsub(repo.location or project_name, "/", path_sep)
|
||||
compile_location = cache_folder .. path_sep .. repo_location
|
||||
compile_location = utils.join_path(cache_folder, repo_location)
|
||||
end
|
||||
local parser_lib_name = install_folder .. path_sep .. lang .. ".so"
|
||||
local parser_lib_name = utils.join_path(install_folder, lang) .. ".so"
|
||||
|
||||
generate_from_grammar = repo.requires_generate_from_grammar or generate_from_grammar
|
||||
|
||||
@@ -329,7 +329,7 @@ local function run_install(cache_folder, install_folder, lang, repo, with_sync,
|
||||
shell.select_mv_cmd("parser.so", parser_lib_name, compile_location),
|
||||
{
|
||||
cmd = function()
|
||||
vim.fn.writefile({ revision or "" }, utils.join_path(utils.get_parser_info_dir(), lang .. ".revision"))
|
||||
vim.fn.writefile({ revision or "" }, utils.join_path(configs.get_parser_info_dir(), lang .. ".revision"))
|
||||
end,
|
||||
},
|
||||
{ -- auto-attach modules after installation
|
||||
@@ -392,7 +392,7 @@ local function install(options)
|
||||
return api.nvim_err_writeln(err)
|
||||
end
|
||||
|
||||
local install_folder, err = utils.get_parser_install_dir()
|
||||
local install_folder, err = configs.get_parser_install_dir()
|
||||
if err then
|
||||
return api.nvim_err_writeln(err)
|
||||
end
|
||||
@@ -459,11 +459,6 @@ function M.update(options)
|
||||
end
|
||||
|
||||
function M.uninstall(...)
|
||||
local path_sep = "/"
|
||||
if fn.has "win32" == 1 then
|
||||
path_sep = "\\"
|
||||
end
|
||||
|
||||
if vim.tbl_contains({ "all" }, ...) then
|
||||
reset_progress_counter()
|
||||
local installed = info.installed_parsers()
|
||||
@@ -473,12 +468,12 @@ function M.uninstall(...)
|
||||
elseif ... then
|
||||
local languages = vim.tbl_flatten { ... }
|
||||
for _, lang in ipairs(languages) do
|
||||
local install_dir, err = utils.get_parser_install_dir()
|
||||
local install_dir, err = configs.get_parser_install_dir()
|
||||
if err then
|
||||
return api.nvim_err_writeln(err)
|
||||
end
|
||||
|
||||
local parser_lib = install_dir .. path_sep .. lang .. ".so"
|
||||
local parser_lib = utils.join_path(install_dir, lang) .. ".so"
|
||||
|
||||
local command_list = {
|
||||
shell.select_rm_file_cmd(parser_lib, "Uninstalling parser for " .. lang),
|
||||
|
||||
@@ -187,7 +187,7 @@ function M.select_download_commands(repo, project_name, cache_folder, revision,
|
||||
M.select_install_rm_cmd(cache_folder, project_name .. "-tmp"),
|
||||
{
|
||||
cmd = "curl",
|
||||
info = "Downloading...",
|
||||
info = "Downloading " .. project_name .. "...",
|
||||
err = "Error during download, please verify your internet connection",
|
||||
opts = {
|
||||
args = {
|
||||
@@ -204,7 +204,7 @@ function M.select_download_commands(repo, project_name, cache_folder, revision,
|
||||
M.select_mkdir_cmd(project_name .. "-tmp", cache_folder, "Creating temporary directory"),
|
||||
{
|
||||
cmd = "tar",
|
||||
info = "Extracting...",
|
||||
info = "Extracting " .. project_name .. "...",
|
||||
err = "Error during tarball extraction.",
|
||||
opts = {
|
||||
args = {
|
||||
@@ -231,7 +231,7 @@ function M.select_download_commands(repo, project_name, cache_folder, revision,
|
||||
return {
|
||||
{
|
||||
cmd = "git",
|
||||
info = "Downloading...",
|
||||
info = "Downloading " .. project_name .. "...",
|
||||
err = clone_error,
|
||||
opts = {
|
||||
args = {
|
||||
|
||||
@@ -10,6 +10,27 @@ function M.notify(msg, log_level, opts)
|
||||
vim.notify(msg, log_level, vim.tbl_extend("force", default_opts, opts or {}))
|
||||
end
|
||||
|
||||
-- Returns the system specific path seperator.
|
||||
function M.get_path_sep()
|
||||
return fn.has "win32" == 1 and "\\" or "/"
|
||||
end
|
||||
|
||||
-- Returns a function that joins the given arguments with separator. Arguments
|
||||
-- can't be nil. Example:
|
||||
--[[
|
||||
print(M.generate_join(" ")("foo", "bar"))
|
||||
--]]
|
||||
-- prints "foo bar"
|
||||
function M.generate_join(separator)
|
||||
return function(...)
|
||||
return table.concat({ ... }, separator)
|
||||
end
|
||||
end
|
||||
|
||||
M.join_path = M.generate_join(M.get_path_sep())
|
||||
|
||||
M.join_space = M.generate_join " "
|
||||
|
||||
--- Define user defined vim command which calls nvim-treesitter module function
|
||||
--- - If module name is 'mod', it should be defined in hierarchy 'nvim-treesitter.mod'
|
||||
--- - A table with name 'commands' should be defined in 'mod' which needs to be passed as
|
||||
@@ -65,26 +86,28 @@ function M.setup_commands(mod, commands)
|
||||
end
|
||||
end
|
||||
|
||||
function M.get_path_sep()
|
||||
return fn.has "win32" == 1 and "\\" or "/"
|
||||
end
|
||||
function M.create_or_resue_writable_dir(dir, create_err, writeable_err)
|
||||
create_err = create_err or M.join_space("Could not create dir '", dir, "': ")
|
||||
writeable_err = writeable_err or M.join_space("Invalid rights, '", dir, "' should be read/write")
|
||||
-- Try creating and using parser_dir if it doesn't exist
|
||||
if not luv.fs_stat(dir) then
|
||||
local ok, error = pcall(vim.fn.mkdir, dir, "p", "0755")
|
||||
if not ok then
|
||||
return nil, M.join_space(create_err, error)
|
||||
end
|
||||
|
||||
-- Returns a function that joins the given arguments with separator. Arguments
|
||||
-- can't be nil. Example:
|
||||
--[[
|
||||
print(M.generate_join(" ")("foo", "bar"))
|
||||
--]]
|
||||
-- prints "foo bar"
|
||||
function M.generate_join(separator)
|
||||
return function(...)
|
||||
return table.concat({ ... }, separator)
|
||||
return dir
|
||||
end
|
||||
|
||||
-- parser_dir exists, use it if it's read/write
|
||||
if luv.fs_access(dir, "RW") then
|
||||
return dir
|
||||
end
|
||||
|
||||
-- parser_dir exists but isn't read/write, give up
|
||||
return nil, M.join_space(writeable_err, dir, "'")
|
||||
end
|
||||
|
||||
M.join_path = M.generate_join(M.get_path_sep())
|
||||
|
||||
local join_space = M.generate_join " "
|
||||
|
||||
function M.get_package_path()
|
||||
-- Path to this source file, removing the leading '@'
|
||||
local source = string.sub(debug.getinfo(1, "S").source, 2)
|
||||
@@ -102,56 +125,13 @@ function M.get_cache_dir()
|
||||
return "/tmp"
|
||||
end
|
||||
|
||||
return nil, join_space("Invalid cache rights,", fn.stdpath "data", "or /tmp should be read/write")
|
||||
return nil, M.join_space("Invalid cache rights,", fn.stdpath "data", "or /tmp should be read/write")
|
||||
end
|
||||
|
||||
-- Returns $XDG_DATA_HOME/nvim/site, but could use any directory that is in
|
||||
-- runtimepath
|
||||
function M.get_site_dir()
|
||||
local path_sep = M.get_path_sep()
|
||||
return M.join_path(fn.stdpath "data", path_sep, "site")
|
||||
end
|
||||
|
||||
-- Try the package dir of the nvim-treesitter plugin first, followed by the
|
||||
-- "site" dir from "runtimepath". "site" dir will be created if it doesn't
|
||||
-- exist. Using only the package dir won't work when the plugin is installed
|
||||
-- with Nix, since the "/nix/store" is read-only.
|
||||
function M.get_parser_install_dir(folder_name)
|
||||
folder_name = folder_name or "parser"
|
||||
local package_path = M.get_package_path()
|
||||
local package_path_parser_dir = M.join_path(package_path, folder_name)
|
||||
|
||||
-- If package_path is read/write, use that
|
||||
if luv.fs_access(package_path_parser_dir, "RW") then
|
||||
return package_path_parser_dir
|
||||
end
|
||||
|
||||
local site_dir = M.get_site_dir()
|
||||
local path_sep = M.get_path_sep()
|
||||
local parser_dir = M.join_path(site_dir, path_sep, folder_name)
|
||||
|
||||
-- Try creating and using parser_dir if it doesn't exist
|
||||
if not luv.fs_stat(parser_dir) then
|
||||
local ok, error = pcall(vim.fn.mkdir, parser_dir, "p", "0755")
|
||||
if not ok then
|
||||
return nil, join_space("Couldn't create parser dir", parser_dir, ":", error)
|
||||
end
|
||||
|
||||
return parser_dir
|
||||
end
|
||||
|
||||
-- parser_dir exists, use it if it's read/write
|
||||
if luv.fs_access(parser_dir, "RW") then
|
||||
return parser_dir
|
||||
end
|
||||
|
||||
-- package_path isn't read/write, parser_dir exists but isn't read/write
|
||||
-- either, give up
|
||||
return nil, join_space("Invalid cache rights,", package_path, "or", parser_dir, "should be read/write")
|
||||
end
|
||||
|
||||
function M.get_parser_info_dir()
|
||||
return M.get_parser_install_dir "parser-info"
|
||||
return M.join_path(fn.stdpath "data", "site")
|
||||
end
|
||||
|
||||
-- Gets a property at path
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
require "nvim-treesitter.highlight" -- yes, this is necessary to set the hlmap
|
||||
local highlighter = require "vim.treesitter.highlighter"
|
||||
local utils = require "nvim-treesitter.utils"
|
||||
local configs = require "nvim-treesitter.configs"
|
||||
local ts_utils = require "nvim-treesitter.ts_utils"
|
||||
local parsers = require "nvim-treesitter.parsers"
|
||||
|
||||
@@ -16,7 +16,7 @@ local function check_assertions(file)
|
||||
)
|
||||
local assertions = vim.fn.json_decode(
|
||||
vim.fn.system(
|
||||
"highlight-assertions -p '" .. utils.get_parser_install_dir() .. "/" .. lang .. ".so'" .. " -s '" .. file .. "'"
|
||||
"highlight-assertions -p '" .. configs.get_parser_install_dir() .. "/" .. lang .. ".so'" .. " -s '" .. file .. "'"
|
||||
)
|
||||
)
|
||||
local parser = parsers.get_parser(buf, lang)
|
||||
|
||||
Reference in New Issue
Block a user