lsp-inlayhints.nvim icon indicating copy to clipboard operation
lsp-inlayhints.nvim copied to clipboard

Support inline virtual text

Open tom-anders opened this issue 1 year ago • 42 comments

Inline virtual text will soon be merged into master: https://github.com/neovim/neovim/pull/23426

This Plugin is of course the perfect candidate for implementing this feature.

tom-anders avatar May 09 '23 16:05 tom-anders

Testers needed for the anticonceal branch.

TODO yield every 5ms on the main 'render' loop. Ref https://github.com/neovim/neovim/commit/edf05b005f34f59dd40468c36cc139e217345a71 (l104-120)

lvimuser avatar May 11 '23 11:05 lvimuser

@lvimuser I would like to test it. Do you mind sharing instructions on how to apply the patch on neovim master branch?

In neovim repo directory I do:

$ wget https://patch-diff.githubusercontent.com/raw/neovim/neovim/pull/23426.patch && git apply 23426.patch --allow-overlap --reject --whitespace=fix
$ make CMAKE_BUILD_TYPE=RelWithDebInfo

Fails with error:

mkdir -p build
touch build/.ran-deps-cmake
ninja  -C .deps
ninja: Entering directory `.deps'
[4/10] Performing download step (download, verify and extract) for 'libuv'
FAILED: build/src/libuv-stamp/libuv-download /Users/lukasz.kurpiewski/Projects/neovim/.deps/build/src/libuv-stamp/libuv-download
cd /Users/lukasz.kurpiewski/Projects/neovim/.deps/build/src && /opt/homebrew/Cellar/cmake/3.26.3/bin/cmake -P /Users/lukasz.kurpiewski/Projects/neovim/.deps/build/src/libuv-stamp/download-libuv.cmake && /opt/homebrew/Cellar/cmake/3.26.3/bin/cmake -P /Users/lukasz.kurpiewski/Projects/neovim/.deps/build/src/libuv-stamp/verify-libuv.cmake && /opt/homebrew/Cellar/cmake/3.26.3/bin/cmake -P /Users/lukasz.kurpiewski/Projects/neovim/.deps/build/src/libuv-stamp/extract-libuv.cmake && /opt/homebrew/Cellar/cmake/3.26.3/bin/cmake -E touch /Users/lukasz.kurpiewski/Projects/neovim/.deps/build/src/libuv-stamp/libuv-download
-- Downloading...
   dst='/Users/lukasz.kurpiewski/Projects/neovim/.deps/build/downloads/libuv/62c2374a8c005ce9e42088965f8f8af2532c177b.tar.gz'
   timeout='none'
   inactivity timeout='none'
-- Using src='https://github.com/libuv/libuv/archive/62c2374a8c005ce9e42088965f8f8af2532c177b.tar.gz'
-- verifying file...
       file='/Users/lukasz.kurpiewski/Projects/neovim/.deps/build/downloads/libuv/62c2374a8c005ce9e42088965f8f8af2532c177b.tar.gz'
-- SHA256 hash of
    /Users/lukasz.kurpiewski/Projects/neovim/.deps/build/downloads/libuv/62c2374a8c005ce9e42088965f8f8af2532c177b.tar.gz
  does not match expected value
    expected: 'cd97aff8e7073fb128aef7a45e9183264ccaf07945a6cb430053070bd03ff200'
      actual: 'c7e89137da65a1cb550ba96b892dfeeabea982bf33b9237bcf9bbcd90f2e70a1'
-- Hash mismatch, removing...

(...)

If I just apply the patch without --allow-overlap --reject --whitespace=fix I get:

2023-05-11 15:10:52 (1.35 MB/s) - ‘23426.patch’ saved [137917]

error: patch failed: runtime/doc/news.txt:52
error: runtime/doc/news.txt: patch does not apply

ecosse3 avatar May 11 '23 13:05 ecosse3

@lvimuser I would like to test it. Do you mind sharing instructions on how to apply the patch on neovim master branch?

In neovim repo directory I do:

$ wget https://patch-diff.githubusercontent.com/raw/neovim/neovim/pull/23426.patch && git apply 23426.patch --allow-overlap --reject --whitespace=fix

git am 23426.patch worked without any issues.

lvimuser avatar May 11 '23 13:05 lvimuser

Hey! Thanks for your work on this plugin. In majority of cases that I've tested it works really well. I've been testing this for a while and I found two things:

  1. One thing is that inlay hints does not render on the first line of the buffer:

https://github.com/lvimuser/lsp-inlayhints.nvim/assets/35625949/74f1becb-5d79-4fc1-b77c-f3b56d3db92c

  1. Don't know if it's possible but when we have set set cursorline and on the line that our cursor is at the moment there is a hint with paddingLeft or paddingRight, this padding has different background than the cursorline background. I think it would be nice to have this background the same color as the cursorline: image

KostkaBrukowa avatar May 11 '23 16:05 KostkaBrukowa

1. One thing is that inlay hints does not render on the first line of the buffer:

I've pushed a commit, could you try it out?

3. Don't know if it's possible but when we have set `set cursorline` and on the line that our cursor is at the moment there is a hint with `paddingLeft` or `paddingRight`,  this padding has different background than the `cursorline` background. I think it would be nice to have this background the same color as the cursorline:

Try to experiment with different values of highlight in the formatter function, perhaps "None"

virt_text_formatter = function(label, hint, opts, client_name)
  ...
  local vt = {}
  vt[#vt + 1] = hint.paddingLeft and { " ", "Normal" } or nil
  vt[#vt + 1] = { label, opts.highlight }
  vt[#vt + 1] = hint.paddingRight and { " ", "Normal" } or nil
  return vt
end,

if not, then it's probably https://github.com/lvimuser/lsp-inlayhints.nvim/commit/aa1fee3469f70842fecb0e915fa0d1e5c6784501#diff-35ff978c429cae2ed9217298ef4810beb45307b1baee86f83eb7233f60109fbaL132

lvimuser avatar May 12 '23 09:05 lvimuser

I've checked the newest commit and now it works well. About the 3. you were right:

  vt[#vt + 1] = hint.paddingLeft and { " ", "None" } or nil
  vt[#vt + 1] = { label, opts.highlight }
  vt[#vt + 1] = hint.paddingRight and { " ", "None" } or nil

or even

  vt[#vt + 1] = hint.paddingLeft and { " " } or nil
  vt[#vt + 1] = { label, opts.highlight }
  vt[#vt + 1] = hint.paddingRight and { " " } or nil

works just as intended. Thanks and that all from my side

KostkaBrukowa avatar May 12 '23 11:05 KostkaBrukowa

Hello, I was trying to test this out with neovim built from 23426.

Is there a config that I need to set or some settings I need to update? Or I also need the patch from anticonceal branch?

I am seeing the inlay hints but they are all appended at the end of the line. Below are examples:

Screenshot_2023-05-13-20-18-17_3440x1440

Screenshot_2023-05-13-20-12-08_3440x1440

schizophrenical avatar May 13 '23 12:05 schizophrenical

If you have hints at the end of a line, it means that you have this plugin version from "main" branch and you need version from "anticonceal" branch. Packer has "brach" option you can add in your config to install plugin version from branch ("lazy" should have something similar)

KostkaBrukowa avatar May 13 '23 17:05 KostkaBrukowa

thanks for the info @KostkaBrukowa! Works now.

schizophrenical avatar May 13 '23 18:05 schizophrenical

This seems to work great, with one issue: calling .toggle() hides the inlay hints, but calling it again doesn't re-display the inlay hints.

bnjmnt4n avatar May 14 '23 11:05 bnjmnt4n

The anticonceal feature has been merged to master https://github.com/neovim/neovim/pull/20130

quantum-booty avatar May 22 '23 17:05 quantum-booty

Seems to work very fine. An edge case seems to be when the inlay hints are racing with LSP semantic token highlighting. If the inlay hints resolve first: image

If the semantic tokens resolve first and inlay hints appear after the LSP semantic highlighting they seem to inherit from the LSP highlighting (or is this something else?) image

theHamsta avatar May 22 '23 18:05 theHamsta

woo I am excited for this update

AndreM222 avatar May 22 '23 18:05 AndreM222

If you didn't know, you could use the right_gravity option when placing virtual text. This would, if right_gravity was set to false, when inserting, place the inserted text to the right of the virtual text rather than the left. I found this particularly nice for the parameter type of some LSPs. For example with rust-analyzer

SleepySwords avatar May 23 '23 04:05 SleepySwords

Very nice plugin, thank you. only_current_line is broken though, it shows on current line when buffer is entered, but not when cursor is moved to another line.

Edit: workaround: autocmd CursorHold * silent! lua require('lsp-inlinehints').show(nil, 5)

andrevmatos avatar May 24 '23 17:05 andrevmatos

Tested and works with lua, cpp, TS with anticonceal, was not successful with rust, anyone else got it working ?

EDIT: should probably state the issue, nothing is showing at all with the anticonceal branch everything works fine on master.

A-Lamia avatar May 25 '23 06:05 A-Lamia

Tested on TS also and had no issues, super thanks!

perrin4869 avatar May 25 '23 09:05 perrin4869

Seems to work very fine. An edge case seems to be when the inlay hints are racing with LSP semantic token highlighting. If the inlay hints resolve first: image

If the semantic tokens resolve first and inlay hints appear after the LSP semantic highlighting they seem to inherit from the LSP highlighting (or is this something else?) image

Thanks for the report. I have no idea what's going on there...

lvimuser avatar May 25 '23 10:05 lvimuser

If you didn't know, you could use the right_gravity option when placing virtual text. This would, if right_gravity was set to false, when inserting, place the inserted text to the right of the virtual text rather than the left. I found this particularly nice for the parameter type of some LSPs. For example with rust-analyzer

Thanks, it's way better.

Tested and works with lua, cpp, TS with anticonceal, was not successful with rust, anyone else got it working ?

EDIT: should probably state the issue, nothing is showing at all with the anticonceal branch everything works fine on master.

Not sure... I'll look into it on the weekend.

lvimuser avatar May 25 '23 10:05 lvimuser

Seems to work very fine. An edge case seems to be when the inlay hints are racing with LSP semantic token highlighting. If the inlay hints resolve first: image

If the semantic tokens resolve first and inlay hints appear after the LSP semantic highlighting they seem to inherit from the LSP highlighting (or is this something else?) image

I wonder if this also happens with https://github.com/neovim/neovim/pull/23736...? If so, it's probably a bug in core

tom-anders avatar May 25 '23 10:05 tom-anders

was not successful with rust, anyone else got it working ?

EDIT: should probably state the issue, nothing is showing at all with the anticonceal branch everything works fine on master.

It seems like the hints only show up after making some change to the file. but then they update normally

EDIT: does it have something to do with the fact that when inlayhints tries to render initially, rust_analyzer is probably still stuck indexing, so it fails, and then only updates on edit. is there a change related to that in the anticonceal branch?

IndianBoy42 avatar May 25 '23 14:05 IndianBoy42

@IndianBoy42 Yes you are correct, editing the file does resolve the problem, interesting.

A-Lamia avatar May 25 '23 15:05 A-Lamia

@A-Lamia You can try https://github.com/junnplus/lsp-setup.nvim#inlay-hints, I have tested it on Rust and it should work.

junnplus avatar May 26 '23 07:05 junnplus

Things are working pretty well, but with a few caveats:

  • The toggle function is erratic. Most times, hints don't come back after toggling them back on unless I modify the buffer I'm editing. I'm guessing that's because the LSP doesn't emit them unless the file is modified, but I figure the plugin should be caching or hiding them instead of outright deleting everything when disabling the hints with the toggle function.
  • I don't know what the plugin is doing with the highlights but it seems it's not using the highlight group provided as-is. For example, if I specify no background, it will somehow always display with the general background instead of whatever other extmark is present there. Although maybe that's a neovim bug? Extmarks have been finicky lately.
  • Is there any way to choose which display mode we want for the hints? I would like to have them as-is for most languages, but switch them back to the old way (at the end of the line) for rust-analyzer, as I find the hits much too disruptive visually.

calops avatar May 26 '23 07:05 calops

It might also be worth it to think about the future of this plugin when this pr exists

IndianBoy42 avatar May 26 '23 12:05 IndianBoy42

@A-Lamia You can try junnplus/lsp-setup.nvim#inlay-hints, I have tested it on Rust and it should work.

Seem to not have any luck with I'm guessing the rust configuration, still the same problem where you need to edit to get things to show up.

A-Lamia avatar May 27 '23 02:05 A-Lamia

@ rust-analyzer. Does it happen only on the first buffer, or on each new buffer?

lvimuser avatar May 30 '23 13:05 lvimuser

Huh, I dont know what happened but now it just works from the first buffer

IndianBoy42 avatar May 30 '23 13:05 IndianBoy42

Using anticonceal branch on nightly (NVIM v0.10.0-dev-411+g9dd48f783), rust-analyzer inlay hints don't seem to work until rust-analyzer is stopped and started again. Anyone experience something similar, or have a fix?

Config:

{
    "lvimuser/lsp-inlayhints.nvim",
    event = "LspAttach",
    branch = "anticonceal",
    opts = {},
    config = function(_, opts)
      require("lsp-inlayhints").setup(opts)
      vim.api.nvim_create_autocmd("LspAttach", {
        group = vim.api.nvim_create_augroup("LspAttach_inlayhints", {}),
        callback = function(args)
          if not (args.data and args.data.client_id) then
            return
          end
          local client = vim.lsp.get_client_by_id(args.data.client_id)
          require("lsp-inlayhints").on_attach(client, args.buf)
        end,
      })
    end,
  },

kothavade avatar May 30 '23 21:05 kothavade

Using anticonceal branch on nightly (NVIM v0.10.0-dev-411+g9dd48f783), rust-analyzer inlay hints don't seem to work until rust-analyzer is stopped and started again.

You're creating the autocommand after the LspAttach event, so it'll only trigger on subsequent events... Think about the loading order. You can

  1. set lazy = true and remove the event key; or
  2. use ~~setup~~ init instead of config.
Details

{
   "lvimuser/lsp-inlayhints.nvim",
   branch = "anticonceal",
   opts = {},
   lazy = true,
   init = function()
     vim.api.nvim_create_autocmd("LspAttach", {
     group = vim.api.nvim_create_augroup("LspAttach_inlayhints", {}),
     callback = function(args)
       if not (args.data and args.data.client_id) then
         return
       end
       local client = vim.lsp.get_client_by_id(args.data.client_id)
       require("lsp-inlayhints").on_attach(client, args.buf)
     end,
   })
  end,
}

lvimuser avatar May 31 '23 10:05 lvimuser