emmanueltouzery / code-compass.nvim

Neovim plugin: set of functions to navigate code without LSP.
MIT License
16 stars 0 forks source link
java neovim neovim-plugin nvim nvim-plugin

code-compass.nvim

Description

A set of functions to navigate code without LSP.

Code Compass leverages tree-sitter, ripgrep and ast-grep (which is also tree-sitter based) to offer "go to definition" and "find references" code navigation features. We need ast-grep >= 0.23.0.

If multiple candidates are found, the list will open in telescope.nvim to allow you to preview them and pick one.

You can install ast-grep using mason.nvim.

A "naive" implementation could support all languages and have results similar to tags-based implementations, but we're trying to be more ambitious.

We have language-specific support to try to get as much context as possible. Compared to a LSP, we obviously don't have a type-checker, and might give up on some advanced constructs that might be possible to discriminate against with tree-sitter. But compared to a tags-based approach, this should be much better (including support for local variables, not only global ones) and would not require re-generating tags on a regular basis.

The plugin is meant for developers working occasionally with other languages and therefore not willing to set up a LSP for them, and for languages where the LSP is complex to set up (java for instance).

The supported languages currently are:

Adding a language is not trivial because we're really trying to offer as good support as possible, for instance we'll try to resolve this if possible, if you use this.var and there is a field var and a variable var we'll know to jump to the field.

References will also list and allow you to filter on the type of reference, for instance inheritance, instantiation, method reference and so on.

The performance is very good thanks to ripgrep pre-filtering the files for ast-grep, and then ast-grep being very optimised itself. I've tested this on larger codebases without performance issues.

Compared to...

LSP

Ctags

Installation

The plugin depends upon:

How to use

Two functions are exported:

Note that a third function, run_tests is also exported, meant for diagnostics and developers.

You can set buffer-level shortcuts to the shortcuts you normally use for LSP:

-- override LSP for java, not using LSP there
vim.api.nvim_create_autocmd("FileType", {
  pattern = "java",
  callback=function(ev)
    vim.keymap.set('n', 'gd', ":lua require'code_compass'.find_definition()<cr>", {desc="code-compass jump to definition", silent=true, buffer=0})
    vim.keymap.set('n', 'gr', ":lua require'code_compass'.find_references()<cr>", {desc="code-compass find references", silent=true, buffer=0})
  end})

The plugin expects that you set the folder to the root folder of the project (using something like vim-rooter for instance).