nixvim icon indicating copy to clipboard operation
nixvim copied to clipboard

[Feature] LSP inlay-hints

Open MattSturgeon opened this issue 1 year ago • 6 comments

Field Description
Plugin lsp-inlayhints.nvim (archived)
Homepage https://github.com/lvimuser/lsp-inlayhints.nvim
Nixpkgs vimPlugins.lsp-inlayhints-nvim

Extra Information

I'm not sure if this should be considered a plugin request or a feature request... Starting with neovim 0.10 plugins are no longer required to achieve this, and lsp-inlayhints.nvim has therefore been archived.

Upstream docs here.

This comment documents one way the native functionality can be used directly:

vim.api.nvim_create_autocmd('LspAttach', {
    group = vim.api.nvim_create_augroup('UserLspConfig', {}),
    callback = function(ev)
        local client = vim.lsp.get_client_by_id(ev.data.client_id)
        if client.server_capabilities.inlayHintProvider and vim.lsp.inlay_hint then
            vim.lsp.inlay_hint.enable(ev.buf, true)
        end
    end,
)

For nixvim this could use autoCmd:

autoGroups.LspInlayhints = {};
autoCmd = [
  {
    event = "LspAttach";
    group = "LspInlayhints";
    callback = helpers.mkRaw ''
      function(e)
        local client = vim.lsp.get_client_by_id(e.data.client_id)
        if client.server_capabilities.inlayHintProvider and vim.lsp.inlay_hint then
            vim.lsp.inlay_hint.enable(e.buf, true)
        end
      end
    '';
  }
];

Or better yet, plugins.lsp.onAttach:

plugins.lsp.onAttach = ''
  if client.server_capabilities.inlayHintProvider and vim.lsp.inlay_hint then
      vim.lsp.inlay_hint.enable(bufnr, true)
  end
'';

This could be exposed as an extra-option in lsp, for example pluigins.lsp.inlayHints = true.

This could replace, or perhaps augment, the current clangd and rust-tools inlay-hint support.

MattSturgeon avatar Mar 22 '24 18:03 MattSturgeon

For more context, the rust-tools project was unfortunately archived early this year, and one of it's successors, rustaceanvim, only provides inlay hints for nvim >=0.10 by default. The old plugin must hooked manually for the rust hints to work on the current stable version (0.9 atm)

ozwaldorf avatar Mar 22 '24 19:03 ozwaldorf

To detect the nvim version at build time (for warnings/assertions), we can probably do something like:

min = "0.10.0";
version = lib.getVersion config.finalPackage;
isSemver = lib.hasInfix "." version;
supported = isSemver -> lib.versionAtLeast version min;

Unfortunately, any nightly version (even one from years ago) will be "supported", because version will look like "849d82b" so lib.versionAtLeast won't be able to handle it.

~~I don't think there's a sane way~~ to get a (semver) "target version" from a nightly (hash) version...?

EDIT: we can do something like this:

version = let
  exe = getExe config.package;
  out = pkgs.runCommand "neovim-version" {} "${exe} --version > $out";
  lines = splitString "\n" (readFile out);
in removePrefix "NVIM v" (head lines);

supported = versionAtLeast version "0.10.0";
Example

nix-repl> nixpkgs = import <nixpkgs> {}

nix-repl> lib = nixpkgs.lib

nix-repl> :lf github:neovim/neovim?dir=contrib
Added 15 variables.

nix-repl> nvim9 = nixpkgs.neovim

nix-repl> nvim10 = outputs.defaultPackage.x86_64-linux

nix-repl> lib.getVersion nvim9
"0.9.5"

nix-repl> lib.getVersion nvim10
"849d82b"

nix-repl> wrapped9 = nixpkgs.wrapNeovimUnstable nvim9 (nixpkgs.neovimUtils.makeNeovimConfig {})

nix-repl> lib.getVersion wrapped9
"0.9.5"

nix-repl> wrapped10 = nixpkgs.wrapNeovimUnstable nvim10 (nixpkgs.neovimUtils.makeNeovimConfig {})

nix-repl> lib.getVersion wrapped10
"849d82b"

MattSturgeon avatar Mar 22 '24 20:03 MattSturgeon

This comment documents one way the native functionality can be used directly:

So this snippet only works for nvim >= 0.10 ?

GaetanLepage avatar Mar 23 '24 12:03 GaetanLepage

This comment documents one way the native functionality can be used directly:

So this snippet only works for nvim >= 0.10 ?

Yes. It was added with this PR although the API's changed a couple times since.

MattSturgeon avatar Mar 23 '24 13:03 MattSturgeon

It seems like neovim 0.10.0 is just around the corner. Let's just wait for it so that we don't have to bother with a conditional implementation.

GaetanLepage avatar Apr 16 '24 16:04 GaetanLepage