Skip to content
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

Treesitter queries specified in the "inherits:" modeline are always merged, even if there is an overridden query #29348

Open
emonkak opened this issue Jun 15, 2024 · 0 comments
Labels
bug issues reporting wrong behavior needs:discussion issue needs attention from an expert, or PR proposes significant changes to architecture or API treesitter
Milestone

Comments

@emonkak
Copy link
Contributor

emonkak commented Jun 15, 2024

Problem

I created ~/.config/nvim/queries/typescript/folds.scm with the following content to override "folds" query for TypeScript:

[
  (arrow_function)
  (class_declaration)
  (enum_declaration)
  (function_declaration)
  (function_expression)
  (generator_function)
  (generator_function_declaration)
  (interface_declaration)
  (internal_module)
  (method_definition)
  (type_alias_declaration)
] @fold

I expected to overwrite queries provided by other plugins with my query, but that is s not the case when there is "inherits:" modeline present. In nvim-treesitter I have installed, queries/typescript/folds.scm is defined as follows:

; inherits: ecma

[
  (interface_declaration)
  (internal_module)
  (type_alias_declaration)
  (enum_declaration)
] @fold

In this case, my query was being merged with queries/ecma/folds.scm. I understood this from the result of vim.treesitter.query.get_files("typescript", "folds"). The problem with this function is that it does not break loading queries when a non-extension query is found.

for _, filename in ipairs(lang_files) do
local file, err = io.open(filename, 'r')
if not file then
error(err)
end
local extension = false
for modeline in
---@return string
function()
return file:read('*l')
end
do
if not vim.startswith(modeline, ';') then
break
end
local langlist = modeline:match(MODELINE_FORMAT)
if langlist then
---@diagnostic disable-next-line:param-type-mismatch
for _, incllang in ipairs(vim.split(langlist, ',', true)) do
local is_optional = incllang:match('%(.*%)')
if is_optional then
if not is_included then
if add_included_lang(base_langs, lang, incllang:sub(2, #incllang - 1)) then
extension = true
end
end
else
if add_included_lang(base_langs, lang, incllang) then
extension = true
end
end
end
elseif modeline:match(EXTENDS_FORMAT) then
extension = true
end
end
if extension then
table.insert(extensions, filename)
elseif base_query == nil then
base_query = filename
end
io.close(file)
end

Is this behavior intended? I believed that if "extends" modeline is not specified in the query, subsequent queries will not be loaded.

from :help treesitter-query:

Nvim looks for queries as *.scm files in a queries directory under runtimepath, where each file contains queries for a specific language and purpose, e.g., queries/lua/highlights.scm for highlighting Lua files. By default, the first query on runtimepath is used (which usually implies that user config takes precedence over plugins, which take precedence over queries bundled with Nvim). If a query should extend other queries instead of replacing them, use |treesitter-query-modeline-extends|.

Related PR: #20117

Steps to reproduce

mkdir -p rp1/queries/typescript rp2/queries/{ecma,typescript}
touch rp1/queries/typescript/folds.scm
touch rp2/queries/ecma/folds.scm
echo ';inherits: ecma' > rp2/queries/typescript/folds.scm
tree --noreport rp1 rp2
nvim --clean -e -s \
  --cmd 'set runtimepath+=rp1,rp2' \
  --cmd 'verbose lua vim.print(vim.treesitter.query.get_files("typescript", "folds"))'

Output:

rp1
└── queries
    └── typescript
        └── folds.scm
rp2
└── queries
    ├── ecma
    │   └── folds.scm
    └── typescript
        └── folds.scm
{ "rp2/queries/ecma/folds.scm", "rp1/queries/typescript/folds.scm" }

Expected behavior

Expected:

rp1
└── queries
    └── typescript
        └── folds.scm
rp2
└── queries
    ├── ecma
    │   └── folds.scm
    └── typescript
        └── folds.scm
{ "rp1/queries/typescript/folds.scm" }

Neovim version (nvim -v)

NVIM v0.11.0-dev-249+g199d852d9

Vim (not Nvim) behaves the same?

no

Operating system/version

Gentoo Linux

Terminal name/version

alacritty 0.13.2

$TERM environment variable

xterm-256color

Installation

build from repo

@emonkak emonkak added the bug issues reporting wrong behavior label Jun 15, 2024
@justinmk justinmk added this to the backlog milestone Jun 17, 2024
@justinmk justinmk added the needs:discussion issue needs attention from an expert, or PR proposes significant changes to architecture or API label Jun 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug issues reporting wrong behavior needs:discussion issue needs attention from an expert, or PR proposes significant changes to architecture or API treesitter
Projects
None yet
Development

No branches or pull requests

2 participants