-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(treesitter): add filetype
-> lang
API
#22207
Conversation
local fname = 'parser/' .. lang .. '.*' | ||
local paths = a.nvim_get_runtime_file(fname, false) | ||
if #paths == 0 then | ||
for _, l in ipairs {lang, symbol_name} do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather have the user decide what they want instead of nvim trying out multiple paths. If the SO-filename should be configurable, it could be an extra parameter.
As I understood nvim-treesitter/nvim-treesitter#4296 (comment), this to enable ft
to lang
mapping like the Lua table that we have in nvim-treesitter. Please note that when you're moving the functionality of mapping ft
to parser to the treesitter.c
hashmap, you will load the same parser multiple times as the same SO-parser file can be loaded for multiple lang
s (lang
being the key for a loaded parser). Isn't this something that the caller of require_language
can handle via a simple Lua table mapping? At nvim-treesitter, we always tried to have ft
and lang
be separate concepts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather have the user decide what they want instead of nvim trying out multiple paths. If the SO-filename should be configurable, it could be an extra parameter.
It already is as the second path
argument.
Please note that when you're moving the functionality of mapping ft to parser to the treesitter.c hashmap, you will load the same parser multiple times as the same SO-parser file can be loaded for multiple langs (lang being the key for a loaded parser).
I'm aware of this, and this is just a small implementation deficiency that should be easy enough to resolve. In practice, this will just mean that users with a bash
and zsh
buffer open in the same session will have the sh
parser loaded multiple times. For the majority, this shouldn't be an issue since filetype
usually maps directly to lang
.
At nvim-treesitter, we always tried to have ft and lang be separate concepts.
I've seen no benefit in keeping this distinction, and for me has only ever caused problems.
For example, I've currently had to introduce some awkward logic in #22191 because get_parser
defaults to filetype
for lang
despite a parser already being associated for a buffer.
The end goal I'm working to is that the whole filetype
-> lang
relationship is encapsulated to parser installation and management. Everywhere else in any treesitter logic, only filetype should be considered.
In this PR I've only looked in both paths (for when path
isn't provided) just to maintain some compatibility for users who will continue to pass lang
as the first argument. Once downstream has adapted, which we won't give much time for, we then only search for parsers in parser/{symbol_name}.so
I think the distinction between I think the problem here could be the understanding what
So basically the parser identification goes by the name the grammar author has chosen and have nvim-treesitter a configurable mapping from
and configure a mapping to the I would be glad in nvim-treesitter when I wouldn't need to care about filetypes. I don't want to create |
86077c8
to
ac8baa7
Compare
I've not been clear about the direction yet, but this is not the intention. The Initially I planned to keep this filetype knowledge in the I've updated the PR to add a This means add('bash', { filetype = { 'sh', 'csh', 'tcsh', 'bash', 'zsh' } } As opposed to just: require_language('bash') |
ac8baa7
to
91552ce
Compare
lang
correctly
lang
correctlylang
correctly
94e3b4e
to
4db8621
Compare
lang
correctlyvim.treesitter.language.add()
ad7a572
to
a068813
Compare
Should the new function be used by Also, is the plan to deprecate the "old" functions? |
a068813
to
dc25c85
Compare
Sure.
The plan is to at least deprecated |
dc25c85
to
31cc43c
Compare
31cc43c
to
bf5cf43
Compare
I've added a |
85bd563
to
38ee119
Compare
vim.treesitter.language.add()
filetype
-> lang
API
Marking ready, but I'm sure there are some aspects that might need iterating based on feedback. |
f4b4740
to
36f4088
Compare
6ca05f8
to
6487a8b
Compare
Problem: vim.treesitter does not know how to map a specific filetype to a parser. This creates problems since in a few places (including in vim.treesitter itself), the filetype is incorrectly used in place of lang. Solution: Add an API to enable this: - Add vim.treesitter.language.add() as a replacement for vim.treesitter.language.require_language(). - Optional arguments are now passed via an opts table. - Also takes a filetype (or list of filetypes) so we can keep track of what filetypes are associated with which langs. - Deprecated vim.treesitter.language.require_language(). - Add vim.treesitter.language.get_lang() which returns the associated lang for a given filetype. - Add vim.treesitter.language.register() to associate filetypes to a lang without loading the parser.
6487a8b
to
d6fa559
Compare
@lewis6991 I think this introduced a regression in
(The parsers work, and show up as Can you reproduce this? |
Problem
vim.treesitter
does not know how to map a specific filetype to a parser.This creates problems since in a few places (including in
vim.treesitter
itself), thefiletype
is incorrectly used in place oflang
.Solution
Add an API to enable this:
vim.treesitter.language.add()
as a replacement forvim.treesitter.language.require_language()
.opts
table.vim.treesitter.language.require_language()
.vim.treesitter.language.get_lang()
which returns the associatedlang
for a givenfiletype
.vim.treesitter.language.register()
to associate filetypes to a lang without loading the parser.Note: This change works on the principle that
filetype
->lang
is a many to one relationship.nvim-treesitter downstream PR: nvim-treesitter/nvim-treesitter#4296