nvim-treesitter icon indicating copy to clipboard operation
nvim-treesitter copied to clipboard

Add support for custom parser install locations

Open dncnmcdougall opened this issue 2 years ago • 12 comments

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?

dncnmcdougall avatar May 23 '22 10:05 dncnmcdougall

I am also happy to try and implement a solution for this issue, but would value some starting guidance.

dncnmcdougall avatar May 23 '22 10:05 dncnmcdougall

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 ?

kyazdani42 avatar May 26 '22 10:05 kyazdani42

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.

dncnmcdougall avatar May 26 '22 10:05 dncnmcdougall

I'll be waiting on @clason and @theHamsta suggestion about this then

kyazdani42 avatar May 26 '22 11:05 kyazdani42

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.

dncnmcdougall avatar May 26 '22 11:05 dncnmcdougall

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.)

clason avatar May 26 '22 12:05 clason

I think it could also help with people using parsers installed from nix packages

kyazdani42 avatar May 26 '22 12:05 kyazdani42

Yes, that was the original reason for adding the fallback location.

clason avatar May 26 '22 12:05 clason

Does adding a new entry to runtimepath suffice then?

Doesn't that entirely cover the features requested here?

WhyNotHugo avatar Jun 10 '22 15:06 WhyNotHugo

No, because it's not about where Neovim finds treesitter parsers, but where nvim-treesitter installs them.

clason avatar Jun 10 '22 15:06 clason

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:

  1. 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
  1. What is a good name for this option?
  2. parser_install_dir?
  3. custom_install_dir?
  4. 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 = { ... },
...
}

dncnmcdougall avatar Jun 17 '22 10:06 dncnmcdougall

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.

clason avatar Jun 17 '22 10:06 clason

closed by #3031

clason avatar Sep 12 '22 16:09 clason