Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request - more special server commands #69

Closed
weilbith opened this issue Mar 6, 2024 · 2 comments
Closed

Feature Request - more special server commands #69

weilbith opened this issue Mar 6, 2024 · 2 comments

Comments

@weilbith
Copy link

weilbith commented Mar 6, 2024

Hey 馃憢

Here the author of #56. You solved the requested feature pretty fast and nicely by adding the register_method. And I wonder if this feature could be extended. But it doesn't seem too obvious how to extend, so I would like to discuss the solution approach first before thinking about writing a PR.

So I have more LSP server specific commands which are somewhat location related (or could be interpreted like this). So for example the rust-analyzer allows to show a list of tests that are using/related to the piece of code under the cursor. It additionally provides information how to run each individual test etc. pp. Which means it is unfortunately not a simple response with Location[]. Though the request parameter are still just TextDocumentPositionParams.
In this exact case, the response structure of the rust-analyzer looks something like this:

{
  result = {
    {
      runnable = {
        args = { --[[ ... ]] },
        kind = "...",
        label = "...",
        location = { --[[ ... ]] },
      }
    },
    {
      runnable = {
        args = { --[[ ... ]] },
        kind = "...",
        label = "...",
        location = { --[[ ... ]] },
      }
    },
  }
}

So if you take this response and map each entry it to the nested location attribute of it, this would be a regular LSP Location[] data structure again.

Taking this example, I thought about a generic solution approach that puts as less burden as possible on the plugin. And my proposal would be to extend your method representation by adding an optional property called response_preprocessor_callback or something similar (just trying to be expressive). The idea would then be that after the server has responded, the respective logic checks if such a callback is defined and if so, calls it and takes the output of that function to continue processing as always. That would mean for any existing method and all already in use custom ones, nothing changes at all. So it is fully backwards compatible. It also should require not many locations of code to touch and remain hopefully a quite minimal addition.

So an example configuration of a user like me would look the following:

require('glance').register_method(
  name = 'rust-analyzer-related-tests',
  label = 'Related Tests',
  method = 'rust-analyzer/relatedTests',
  response_preprocessor_callback = function(response)
    return vim.tbl_map(function(entry)
        return entry.runnable.location
    end, response.result or {})
  end
)

What do you think?

@DNLHC
Copy link
Owner

DNLHC commented Mar 31, 2024

Hey, sorry for not responding sooner I've been busy lately

You can try to do the preprocessing you need with the before_open hook.

  hooks = {
    before_open = function(results, open, jump, method)
      if method == 'rust-analyzer-related-tests' then
        open(vim.tbl_map(function(entry)
          return entry.runnable.location
        end, results))
      else
        open(results)
      end
    end,
  }

Does this work for you?

@weilbith
Copy link
Author

weilbith commented Apr 2, 2024

No worries maintaining plugins is a horror. I have no expectations. 馃檪

Nice! That works! Sorry, haven't thought about this. 馃檲
Thank you very much! 馃檹馃従

@weilbith weilbith closed this as completed Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants