Skip to content
This repository was archived by the owner on Jan 3, 2024. It is now read-only.

Fix inlay hints crash when label field is a table #307

Merged
merged 1 commit into from
Jan 8, 2023
Merged

Fix inlay hints crash when label field is a table #307

merged 1 commit into from
Jan 8, 2023

Conversation

kdarkhan
Copy link
Contributor

@kdarkhan kdarkhan commented Dec 29, 2022

According to LSP spec, InlayHint.label can be either a string or an array. Previously, rust-analyzer was always returning strings. Most likely after this recent PR it started returning arrays.

Based on my testing, it seems only inlay hints with kind 1 are returning arrays but I am preemptively handling kind 2 as well in case it changes in the future.

According to LSP spec,
[InlayHint.label](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#inlayHint)
can be either a string or an array. Previously, `rust-analyzer` was
always returning strings. Most likely after
[this recent PR](rust-lang/rust-analyzer#13699)
it started returning arrays.

Based on my testing, it seems only inlay hints with `kind` 1
are returning arrays but I am preemptively handling `kind` 2
as well in case it changes in the future.
@kdarkhan kdarkhan changed the title Fix inlay hints crash in simrat39/rust-tools.nvim#300 Fix inlay hints crash when label field is a table Dec 29, 2022
@FrankReh
Copy link

FrankReh commented Jan 6, 2023

This fixes the problem for many of us. I lost time because I didn't zero in on a problem with rust-tools right away and my search didn't find this PR or the issues that refer to it as soon as I would have liked. I'm including the error message in case it helps the next person find this more quickly.

Error executing vim.schedule lua callback: ...im/bundle/rust-tools.nvim/lua/rust-tools/inlay_hints.lua:216: bad argument
 #1 to 'sub' (string expected, got table)
stack traceback:
        [C]: in function 'sub'
        ...im/bundle/rust-tools.nvim/lua/rust-tools/inlay_hints.lua:216: in function 'render_line'
        ...im/bundle/rust-tools.nvim/lua/rust-tools/inlay_hints.lua:260: in function 'render'
        ...im/bundle/rust-tools.nvim/lua/rust-tools/inlay_hints.lua:170: in function 'handler'
        ...l/Cellar/neovim/0.8.2/share/nvim/runtime/lua/vim/lsp.lua:1383: in function ''
        vim/_editor.lua: in function <vim/_editor.lua:0>

@Ghasak
Copy link

Ghasak commented Jan 7, 2023

Why is this still an open issue?, please fix, it's so simple
just replace other_hints to other_hints[1]

I fixed it manually, and I hate to waste debugging for 30 mins for something so simple as this.

---------------- How to fix ---------------------
go to the ~/.local/share/nvim/site/pack/packer/opt/rust-tools.nvim/lua/rust-tools/inlya_hints.lua
then on line 215 change the other_hints to other_hints[1]
the function becomes

  -- show other hints with commas and a thicc arrow
  if not vim.tbl_isempty(other_hints) then
    virt_text = virt_text .. opts.other_hints_prefix
    for i, o_hint in ipairs(other_hints[1]) do      --- <<<- this line just needed to be changed. 
      if string.sub(o_hint.label, 1, 2)== ": " then
        virt_text = virt_text .. o_hint.label:sub(3)
      else
        virt_text = virt_text .. o_hint.label
      end
      if i ~= #other_hints then
        virt_text = virt_text .. ", "
      end
    end
  end

@rsignavong
Copy link

Is this repository still maintained?

@simrat39 simrat39 merged commit 32546b3 into simrat39:master Jan 8, 2023
@simrat39
Copy link
Owner

simrat39 commented Jan 8, 2023

Sorry I have been very busy with life but I'll try to find time to maintain the repo. PR looks good.

jordanisaacs added a commit to jordanisaacs/neovim-flake that referenced this pull request Jan 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants