Repository: neovim
Issue: `LanguageTree:contains()` does not respect gaps in included regions
Labels: bug,lua,treesitter

### Problem

`LanguageTree:contains(range)` returns `true` if `range` fits between start of first and end of last regions included in `LanguageTree`. This can lead to unexpected results if the regions are far apart with regions from other language trees in between.

The particular issue of determining the "correct" (most local) language tree is currently solved in built-in commenting [by taking "the deepest" tree that contains reference range](https://github.com/neovim/neovim/blob/3fdc4302415972eb5d98ba832372236be3d22572/runtime/lua/vim/_comment.lua#L30-L48) as the most local. This doesn't always work as described in "Steps to reproduce".

I believe #18678 was meant to fix this by additionally iterating through language tree's regions to determine the result. Not sure why it was not merged, but its implementation seems to be outdated at this point.

### Steps to reproduce

Basically, [this part of code](https://github.com/neovim/neovim/blob/3fdc4302415972eb5d98ba832372236be3d22572/runtime/lua/vim/treesitter/languagetree.lua#L1073-L1096). 
 
Here is a more practical example from @ribru17 (minimized from [this comment](https://github.com/neovim/neovim/pull/31950#issuecomment-2585790850)) that currently affects built-in commenting:

1. Install and enabled tree-sitter parsers for ['markdown'](https://github.com/tree-sitter-grammars/tree-sitter-markdown) and ['html'](https://github.com/tree-sitter/tree-sitter-html). For example, through `:TSInstall` from [nvim-treesitter/nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter).
2. Define the following function:
    ```lua
    _G.get_langtree_info = function()
      local pos = vim.api.nvim_win_get_cursor(0)
      local ref_range = { pos[1] - 1, pos[2], pos[1] - 1, pos[2] }

      local res = {}
      local function traverse(lang_tree)
        table.insert(res, {
          contains = lang_tree:contains(ref_range),
          lang = lang_tree:lang(),
          included_regions = lang_tree:included_regions(),
        })
        for _, child_lang_tree in pairs(lang_tree:children()) do
          traverse(child_lang_tree)
        end
      end
      traverse(vim.treesitter.get_parser(0))

      return res
    end
    ```
3. Open the following markdown file and put cursor inside code block (third line):
    ````markdown
    <!-- html -->
    ```lua
    local a = 1
    ```
    <!-- html -->
    ````
4. Execute `:=get_langtree_info()`. It returns the following output:
    ```lua
    { {
        contains = true,
        included_regions = { {} },
        lang = "markdown"
      }, {
        contains = true,
        included_regions = { { { 0, 0, 0, 1, 0, 14 }, { 4, 0, 37, 5, 0, 51 } } },
        lang = "html"
      }, {
        contains = true,
        included_regions = { { { 2, 0, 21, 3, 0, 33 } } },
        lang = "lua"
      } }
    ```

All language trees report that "cursor range" is contained inside them, although only one of them includes a region whose range contains cursor range.

Side note: this particular case does not currently work with built-in commenting because both 'html' and 'lua' language trees are sibling trees. This results in a hard to reproduce issue: sometimes 'lua' will be traversed first and sometimes 'html' (due to no order guarantees in `pairs`).

### Expected behavior

`LanguageTree:contains()` respects its included regions when computing the result. Basically add an extra `for` loop for included regions (either [here](https://github.com/neovim/neovim/blob/3fdc4302415972eb5d98ba832372236be3d22572/runtime/lua/vim/treesitter/languagetree.lua#L1074-L1081) or [here](https://github.com/neovim/neovim/blob/3fdc4302415972eb5d98ba832372236be3d22572/runtime/lua/vim/treesitter/languagetree.lua#L1089-L1093)).

This can also be solved directly in `vim._comment`, but I think it is more reasonable to update `LanguageTree:contains()`.

### Nvim version (nvim -v)

NVIM v0.11.0-dev-1498+g6719276040

### Vim (not Nvim) behaves the same?

No

### Operating system/version

EndeavourOS Linux x86_64, 6.12.9-arch1-1

### Terminal name/version

Ghostty 1.0.1-arch

### $TERM environment variable

xterm-ghostty

### Installation

appimage