SmiteshP / nvim-navic

Simple winbar/statusline plugin that shows your current code context

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Feature] Extend `get_data(bufnr)` and `get_location()` to accept arbitraries line and column positions

pidgeon777 opened this issue · comments

What I would like to achieve, is to get the breadcrumb scope value for an arbitrary line and column position of a given buffer.

For example, through an extension of the get_data(bufnr) and get_location() functions.

How could this be achieved?

As part of this PR #90 I am exposing some internal logic in form of a library for others to use. It includes the logic that calculates context at cursor position. update_context can take in cursor position as argument. So you should be able to call update_context and then get_data immediately after that to get what you want.
You can try out that branch while I still work on that PR.

I think it should be ~= nil:

arg_cursor_pos ~= nil and arg_cursor_pos or vim.api.nvim_win_get_cursor(0)

Instead, in the current commit it is == nil.

Fixed 👍🏽

I take the opportunity to ask some questions.

From what I understand nvim-navic returns data as a result of require("nvim-navic").attach(client, bufnr).

This means that it must be available:

  • The buffer ID
  • The ID of the LSP server

to which to make the attach.

The question I would like to ask is this, let's imagine that I want to get the scope (breadcrumb) of a file that belongs to the same project as a buffer that is already open in Neovim, and already has an attached LSP server.

This file, of which I know the path, as it has not yet been opened in Neovim, cannot have a buffer number associated with it. The buffer number is assigned once it is opened in Neovim. Then, consequently, since this file is within the same project directory for which there is already an active LSP, then following the opening of the file then the LSP server would automatically be attached to it, leading this also to the attachment of nvim-navic to the file just opened.

The question is, currently I can get the scope information of a given line/column of a buffer by knowing its number (bufnr), but how could I do the same for a file that has not yet been opened in Neovim, thus does not have bufnr, but whose path I know, and to which I also know that nvim-navic and the LSP server would be attached once opened in Neovim?

What I would like to do, is get the navic information for any file belonging to a project for which there is already an active LSP client because a file in the project has already been opened in Neovim. However, in order to extract this breadcrumb information from the files, I would like to avoid having to open them in Neovim first.

If I were instead forced to open the buffer in Neovim, what might be the best way to do it in the background, for example without it being displayed, but just loaded just enough to be able to do all the LSP attachments required by nvim-navic?

I am not sure, But because LSP knows all the jump to definition, references, I think it must be reading in all the files in the project regardless of whether neovim has a buffer open for it or not.

IIRC, the argument on line 231 in lib.lua is the one thats supposed to tell lsp which file to fetch data about. If we can mention which file to use ourself, we might be able to get data for other files in project easily. And I can expose the parse function in lib too, so that can be passed as handler to get the information in nice tree format. I feel this should be doable, Will try to experiment with this and let you know if this can be done.

Just out of curiosity, what are you trying to do?

In short, I had an idea, and it is as follows: I would like, for each item of the quick-fix list, to add breadcrumb information.

I know this is possible for buffers which are already loaded in Neovim: in this case, it would be enough, for each element of the quick-fix list, to ask for data at the given buffer, line and position.

But then, the requirement would be that an attached LSP already exists for the buffer in question, so that it would be possible to call nvim-navic functions.

My issue is how to obtain documentSymbol information for files which are not yet loaded in Neovim as buffers, but which I know to be part of the LSP project/root.

This means that if those files are opened in Neovim, they would automatically be attached to the right LSP server, and so on. But I want to avoid opening them in Neovim.

So, I'm looking for a sort of "manual" documentSymbol request to the wished active LSP passing the file URI as an argument.

So the question would be: how could I obtain the documentSymbol information through nvim-navic for a given file URI without having to open the associated buffer in Neovim first?

Yeah tried this thing out, so passing table with uri = file path works. You can receive file symbols information about any file.

So the question would be: how could I obtain the documentSymbol information through nvim-navic for a given file URI without having to open the associated buffer in Neovim first?

Will add an argument to request function to accept file uri. That should be helpful and then you can pass a handler to do what you want with the data from lsp.

@pidgeon777 Please check the latest commit on refactor branch. You will have to make use of request_symbol function yourself and pass file uri as fourth argument. And in the callback function you can do whatever you want with the data. I have also exposed the parse function that converts the data into nice useable tree structure, you can make use of that in your custom callback function as well

Hi @pidgeon777 !
I hope the changes made satisfy your use case. Closing issue.