nvim-treesitter / nvim-treesitter-refactor

Refactor module for nvim-treesitter
Apache License 2.0
407 stars 25 forks source link

highlight_definitions not correctly matching scope #41

Closed RRethy closed 2 years ago

RRethy commented 2 years ago

Describe the bug

highlight_definitions isn't finding the definition of functions because they are being scoped incorrectly.

To Reproduce

init.lua:

require('nvim-treesitter.configs').setup {
    refactor = {
        highlight_definitions = {
            enable = true,
        },
    }
}
  1. Open the following lua file:
function Foo()
end

Foo()
Foo()
  1. Put your cursor on the Foo() function calls.
  2. Foo() function declaration will not be highlighted

Expected behavior

I expect the Foo() function call to be highlighted

Output of :checkhealth nvim_treesitter


nvim-treesitter: require("nvim-treesitter.health").check()
========================================================================
## Installation
  - OK: `tree-sitter` found 0.20.0 (53f094f05e9151362bf463b95b80fc4adb05b74a) (parser generator, only needed for :TSInstallFromGrammar)
  - OK: `node` found v18.6.0 (only needed for :TSInstallFromGrammar)
  - OK: `git` executable found.
  - OK: `cc` executable found. Selected from { vim.NIL, "cc", "gcc", "clang", "cl", "zig" }
    Version: Apple clang version 13.1.6 (clang-1316.0.21.2.5)
  - OK: Neovim was compiled with tree-sitter runtime ABI version 14 (required >=13). Parsers must be compatible with runtime ABI.

## Parser/Features H L F I J
  - regex          ✓ . . . .
  - hack           ✓ . . . .
  - rust           ✓ ✓ ✓ ✓ ✓
  - ruby           ✓ ✓ ✓ ✓ ✓
  - sparql         ✓ ✓ ✓ ✓ ✓
  - rego           ✓ . . . ✓
  - sql            x . . . ✓
  - gdscript       ✓ ✓ . ✓ ✓
  - embedded_template✓ . . . ✓
  - surface        ✓ . ✓ ✓ ✓
  - clojure        ✓ ✓ ✓ . ✓
  - eex            ✓ . . . ✓
  - cuda           ✓ ✓ ✓ ✓ ✓
  - solidity       ✓ . . . .
  - ocaml          ✓ ✓ ✓ . ✓
  - query          ✓ ✓ ✓ ✓ ✓
  - devicetree     ✓ ✓ ✓ ✓ ✓
  - ocaml_interface✓ ✓ ✓ . ✓
  - glsl           ✓ ✓ ✓ ✓ ✓
  - svelte         ✓ . ✓ ✓ ✓
  - ocamllex       ✓ . . . ✓
  - tiger          ✓ ✓ ✓ ✓ ✓
  - org            . . . . .
  - dot            ✓ . . . ✓
  - swift          ✓ ✓ . . .
  - v              ✓ ✓ ✓ ✓ ✓
  - rnoweb         ✓ . ✓ . ✓
  - fusion         ✓ ✓ ✓ ✓ .
  - proto          ✓ . ✓ . .
  - todotxt        ✓ . . . .
  - ledger         ✓ . ✓ ✓ ✓
  - bibtex         ✓ . ✓ ✓ .
  - typescript     ✓ ✓ ✓ ✓ ✓
  - m68k           ✓ ✓ ✓ . ✓
  - json           ✓ ✓ ✓ ✓ .
  - scala          ✓ . ✓ . ✓
  - wgsl           ✓ . ✓ . .
  - css            ✓ . ✓ ✓ ✓
  - supercollider  ✓ ✓ ✓ ✓ ✓
  - gomod          ✓ . . . ✓
  - gowork         ✓ . . . ✓
  - slint          ✓ . . ✓ .
  - cooklang       ✓ . . . .
  - lalrpop        ✓ ✓ . . .
  - graphql        ✓ . . ✓ ✓
  - vala           ✓ . ✓ . .
  - norg           . . . . .
  - foam           ✓ ✓ ✓ ✓ ✓
  - d              ✓ . ✓ ✓ ✓
  - perl           ✓ . ✓ . .
  - lua            ✓ ✓ ✓ ✓ ✓
  - gleam          ✓ ✓ ✓ ✓ ✓
  - prisma         ✓ . . . .
  - markdown_inline✓ . . . ✓
  - fish           ✓ ✓ ✓ ✓ ✓
  - http           ✓ . . . ✓
  - tlaplus        ✓ ✓ ✓ . ✓
  - php            ✓ ✓ ✓ ✓ ✓
  - scheme         ✓ . ✓ . ✓
  - fennel         ✓ ✓ . . ✓
  - toml           ✓ ✓ ✓ ✓ ✓
  - hjson          ✓ ✓ ✓ ✓ ✓
  - kotlin         ✓ ✓ ✓ . ✓
  - dockerfile     ✓ . . . ✓
  - heex           ✓ ✓ ✓ ✓ ✓
  - help           ✓ . . . .
  - pug            ✓ . . . ✓
  - vim            ✓ ✓ ✓ . ✓
  - cmake          ✓ . ✓ . .
  - fortran        ✓ . ✓ ✓ .
  - c              ✓ ✓ ✓ ✓ ✓
  - go             ✓ ✓ ✓ ✓ ✓
  - scss           ✓ . . ✓ .
  - zig            ✓ . ✓ ✓ ✓
  - latex          ✓ . ✓ . ✓
  - erlang         ✓ . ✓ . .
  - beancount      ✓ . ✓ . .
  - r              ✓ ✓ . ✓ ✓
  - elixir         ✓ ✓ ✓ ✓ ✓
  - vue            ✓ . ✓ ✓ ✓
  - javascript     ✓ ✓ ✓ ✓ ✓
  - turtle         ✓ ✓ ✓ ✓ ✓
  - jsonc          ✓ ✓ ✓ ✓ ✓
  - godot_resource ✓ ✓ ✓ . .
  - bash           ✓ ✓ ✓ . ✓
  - elm            ✓ . . . ✓
  - html           ✓ ✓ ✓ ✓ ✓
  - yaml           ✓ ✓ ✓ ✓ ✓
  - tsx            ✓ ✓ ✓ ✓ ✓
  - yang           ✓ . ✓ ✓ .
  - hcl            ✓ . ✓ ✓ ✓
  - jsdoc          ✓ . . . .
  - glimmer        ✓ . . . .
  - julia          ✓ ✓ ✓ ✓ ✓
  - verilog        ✓ ✓ ✓ . ✓
  - nix            ✓ ✓ ✓ . ✓
  - json5          ✓ . . . ✓
  - java           ✓ ✓ . ✓ ✓
  - dart           ✓ ✓ . ✓ ✓
  - pioasm         ✓ . . . ✓
  - markdown       ✓ . ✓ . ✓
  - rst            ✓ ✓ . . ✓
  - ninja          ✓ . ✓ ✓ .
  - c_sharp        ✓ ✓ ✓ . ✓
  - hocon          ✓ . . . ✓
  - teal           ✓ ✓ ✓ ✓ ✓
  - cpp            ✓ ✓ ✓ ✓ ✓
  - llvm           ✓ . . . .
  - ql             ✓ ✓ . ✓ ✓
  - qmljs          ✓ . ✓ . .
  - haskell        ✓ . ✓ . ✓
  - python         ✓ ✓ ✓ ✓ ✓
  - commonlisp     ✓ ✓ ✓ . .
  - pascal         ✓ ✓ ✓ ✓ ✓
  - elvish         ✓ . . . ✓
  - make           ✓ . . . ✓
  - phpdoc         ✓ . . . .
  - astro          ✓ ✓ ✓ ✓ ✓
  - rasi           ✓ ✓ ✓ ✓ .
  - comment        ✓ . . . .

  Legend: H[ighlight], L[ocals], F[olds], I[ndents], In[j]ections
         +) multiple parsers found, only one will be used
         x) errors found in the query, try to run :TSUpdate {lang}

## The following errors have been detected:
  - ERROR: sql(highlights): /usr/local/share/nvim/runtime/lua/vim/treesitter/query.lua:174: query: invalid node type at position 86
    sql(highlights) is concatenated from the following files:
    | [ERROR]:"/Users/adam.regaszrethy/.local/share/nvim/site/pack/backpack/opt/nvim-treesitter/queries/sql/highlights.scm", failed to load: /usr/local/share/nvim/runtime/lua/vim/treesitter/query.lua:174: query: invalid node type at position 86

Output of nvim --version

NVIM v0.8.0-dev-nightly-86-gd15a66d80
Build type: Release
LuaJIT 2.1.0-beta3
Compiled by adam.regaszrethy@Adams-MacBook-Pro.local

Features: +acl +iconv +tui
See ":help feature-compile"

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/local/share/nvim"

Run :checkhealth for more info

Additional context

I took a look through the code and the cause is that find_definition has an incorrect def_lookup. The function is being given the id of k_Foo_0_0_2_3 when it should have an id of k_Foo_0_0_6_0 which would let it have the same id as the function calls to Foo(). This id table gets setup here. If we were to debug this line, we would see node_entry.scope is nil which I believe is the issue since it should probably be parent. Looks like a bug in recurse_local_nodes but I'm not sure what's supposed to be going on in that thing.

RRethy commented 2 years ago

Closing in favour of https://github.com/nvim-treesitter/nvim-treesitter/issues/3280 because this bug is in the source code of that plugin.