Repository: neovim
Issue: Treesitter queries specified in the "inherits:" modeline are always merged, even if there is an overridden query
Labels: bug,needs:discussion,treesitter

### 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.

https://github.com/neovim/neovim/blob/61aabe0730b547b733faaf74ff181ec8c33f8b92/runtime/lua/vim/treesitter/query.lua#L115-L162

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: https://github.com/neovim/neovim/pull/20117

### Steps to reproduce

```shell
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