nvim-treesitter
nvim-treesitter copied to clipboard
Add support for custom parser install locations
nvim-treesitter provides excellent convenience for managing the installing of both treesitter parsers and modules that use them. I want to use the parsers in projects outside of nvim, but the default install path is not ideal (and changes based on you r package manager.)
It would be nice to have a configuration option which is a path that the parsers will be installed to. Something like:
require'nvim-treesitter.configs'.setup {
-- A list of parser names, or "all"
ensure_installed = { "c", "lua", "rust" },
-- Install parsers synchronously (only applied to `ensure_installed`)
sync_install = false,
-- List of parsers to ignore installing (for "all")
ignore_install = { "javascript" },
-- Path in which to install parsers. Default: "${plugin_dir}/parser"
parser_install_path= "~/treesitter/parsers"
-- ...
}
I have not seriously thought about the details of the option. Like what it should be called, and how much power it needs.
Should it be able to expand environment variables? Expand the home "~", etc.
Also should it be set to a default value, or should it default to lua nil
, which means install in the plugin dir?
I am also happy to try and implement a solution for this issue, but would value some starting guidance.
I think this might be a problem because neovims get_parser
needs the parsers to be installed in the runtimepath i believe.
@theHamsta do you think it would still be feasible ? If so, i'm not sure it adds to much value to be honest.
@dncnmcdougall in the meantime, would a symlink do the trick ?
Hi @kyazdani42, thanks for the reply. Presumably nvim-treesitter adds the current parsers directory to the runtime path? Is there a reason it could not add an arbitrary directory instead?
As to symlinks, I had not thought of them, thank you. In the mean time I hard coded the parser directory into the script I was writing. This feels similar, and works, but is a little hard to automate when moving to a new computer or reinstalling a system.
I'll be waiting on @clason and @theHamsta suggestion about this then
Thanks. I am happy to do the work on this, I think it will be a relatively gentle intro into the workings of all the bits and pieces.
I am very much in favor of this instead of the current approach, which tries the default location and, if that is not writeable, puts parsers in a different, hardcoded location. This has been the source of significant frustration when outdated and incompatible parsers were left over in the fallback location. (See #2300.)
Requiring people to explicitly specify a non-default location if the default one doesn't work for them for whatever reason is the better approach.
(And, yes, the parser directory needs to be put in runtimepath
so that Neovim's core treesitter functions can access them.)
I think it could also help with people using parsers installed from nix packages
Yes, that was the original reason for adding the fallback location.
Does adding a new entry to runtimepath
suffice then?
Doesn't that entirely cover the features requested here?
No, because it's not about where Neovim finds treesitter parsers, but where nvim-treesitter installs them.
I am looking at it and initially I thought it was easy: just update utils.get_parser_install_dir
But this is not designed for accepting options. So with any other plugin I'd add in a g:nvim-treesitter-custom-parer-intall-dir
.
However nvim-treesitter prefers parsing options in through a table in config.setup {...}
.
This raises two questions:
- What is the best way to persist settings through
setup.config {...}
? Would this be to add a local setting to the configs module?
-- lua/nvim-treesitter/utils.lua
M.parser_install_dir = nil
- What is a good name for this option?
-
parser_install_dir
? -
custom_install_dir
? -
install_directory
?
require`nvim-treesitter.configs`.config {
-- directory to install parsers into
parser_install_dir = "~/nvim-treesitter/parsers",
-- A list of parser names, or "all"
ensure_installed = { ... },
...
}
However nvim-treesitter prefers parsing options in through a table in config.setup {...}.
Yes, this is the common pattern throughout the Lua ecosystem.
What is the best way to persist settings through setup.config {...}? Would this be to add a local setting to the configs module?
I would put it in the config
table in configs.lua
, like the ensure_installed
options (and friends). You will have to pass this table through the relevant functions, of course, but this should already be the case (since the other keys also affect the installation process).
What is a good name for this option?
parser_install_dir
, definitely.
closed by #3031