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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

turn inlay hints on by default? #948

Closed
damccull opened this issue May 23, 2024 · 13 comments
Closed

turn inlay hints on by default? #948

damccull opened this issue May 23, 2024 · 13 comments

Comments

@damccull
Copy link

I just updated to the most recent commit for kickstart.nvim and I'm happy to see a toggle now for inlay hints.

However, I want it on by default and able to be toggled off. I understand the <leader>th keymap will toggle to the inverse of the current state, so I tried adding this to my init.lua near my vim.opts.

-- Enable inlay hints by default; toggle off with <leader>th
vim.lsp.inlay_hint.enable()

The toggle will still work if I toggle it twice (I'm assuming the first one is setting it to 'off' after my code messes up the state), but I can't get it to default on. How do I fix this?

@eljamm
Copy link

eljamm commented May 23, 2024

How about adding it like this, instead?

if client and client.server_capabilities.inlayHintProvider and vim.lsp.inlay_hint then
  -- Toggle inlay hints
  map('<leader>th', function()
    vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled())
  end, '[T]oggle Inlay [H]ints')

  -- Enable inlay hints by default
  vim.lsp.inlay_hint.enable()
end

This way, inlay hints will be on by default and you can toggle them with <leader>th

@damccull
Copy link
Author

Oh, wow. That's great, thanks. Exactly what I was trying to do and in a much better way.

@damccull damccull reopened this May 23, 2024
@damccull
Copy link
Author

Well, I thought this should work, but inlay hints are not enabling until I toggle it twice, still. Am I using the enable() function wrong?

@damccull
Copy link
Author

Ok, so it does seem to be activating inlay hints. However, they aren't showing up until something in the code changes. If I force rust_analyzer to reevaluate the code by changing whitespace above or below where I should see an inlay, for instance, it shows up. Otherwise I need to toggle it twice. Is there an additional code I need to make it display them automatically without needing to change something or manually toggle it twice?

@eljamm
Copy link

eljamm commented May 24, 2024

I'm using rustaceanvim and it works normally.

I guess the issue you're describing is related to this, which rustaceanvim is working around here.

@sergeken
Copy link

sergeken commented Jun 4, 2024

Would be great to have a commented section with

-- Remove comment to enable inlay hints by default
-- vim.lsp.inlay_hint.enable()

@dffuller
Copy link

dffuller commented Jun 20, 2024

It appears some of this code made it into the last release.

if client and client.server_capabilities.inlayHintProvider and vim.lsp.inlay_hint then
  -- Toggle inlay hints
  map('<leader>th', function()
    vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled())
  end, '[T]oggle Inlay [H]ints')

  -- Enable inlay hints by default
  vim.lsp.inlay_hint.enable()
end

When I go through diagnostic errors in a (basically) new kickstart.nvim file, I am getting an error stating "This function requires 1 argument but instead it is receiving 0 for the is_enabled() call. The diagnostic error goes away when I add a table { bufnr = 0} inside the call, but I'm not sure this is what is desired.

Edit: It looks like you can just drop the parens after is_enabled. They seem to be throwing the diagnostic error.

@eljamm
Copy link

eljamm commented Jun 20, 2024

Curious why that doesn't work for you considering that's how it's written in the Neovim docs. It still works for me, but deleting the parens works as well.

Is it a difference in versions? I'm using Neovim v0.10.0

@dffuller
Copy link

I, too, am using v0.10.0. I can't say for certain that it "doesn't work" but I was getting diagnostic errors for the issue.

@eljamm
Copy link

eljamm commented Jun 20, 2024

Oh, that's just a warning not an error, so you can safely ignore it. I didn't see it before because I had turned off some neodev diagnostics.

It's worth noting that I thought removing the parens worked at first, but with that the toggle only works once, then it stops working. Replacing the parens with curly braces worked, though, and the warning disappeared as well:

    vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled {})

Don't know what the ramifications of this is are, though.

@VlaDexa
Copy link
Contributor

VlaDexa commented Jun 22, 2024

I was getting diagnostic errors for the issue.

This was a neovim docs issue

@feoh
Copy link
Collaborator

feoh commented Jul 22, 2024

Fixed in a recent merge.

@feoh feoh closed this as completed Jul 22, 2024
@VlaDexa
Copy link
Contributor

VlaDexa commented Jul 22, 2024

Not quite fixed (because the initial issue wasn't a bug, more of a question), but I think it's overall resolved apart from the neovim issue.

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

6 participants