ray-x / go.nvim

G'day Nvimer, Joyful Gopher: Discover the Feature-Rich Go Plugin for Neovim
MIT License
2.15k stars 125 forks source link

Ability to disable go.nvim provided inlay hints #356

Open westernwontons opened 1 year ago

westernwontons commented 1 year ago

On Neovim nightly the LSP native inlay hints are available now. go.nvim provides the following configuration for enabling/disabling inlay hints:

lsp_inlay_hints = {
    enabled = true|false

but if it's disabled here, then it's disabled for both. I would like to have the ability to configure which to use. Currently, with AstroNvim, I have the following configuration for go.nvim:

return {
  "ray-x/go.nvim",
  dependencies = {
    "ray-x/guihua.lua",
    "neovim/nvim-lspconfig",
    "nvim-treesitter/nvim-treesitter",
  },
  event = { "CmdlineEnter" },
  ft = { "go", "gomod" },
  build = ':lua require("go.install").update_all_sync()', -- if you need to install/update all binaries
  config = function()
    require("go").setup {
      max_line_len = 80,
      lsp_inlay_hints = {
        enable = true,
      },
      lsp_cfg = true,
      lsp_on_attach = function(client, bufnr)
        -- disable gopls_inlay autocmd to not duplicate inlay hints
        vim.api.nvim_clear_autocmds {
          group = "gopls_inlay",
        }

        -- go.nvim has a deferred callback for enabling inlay hints
        -- but I want the neovim native LSP hints
        -- if the go.nvim defined inlay hints are not disabled, the hints are duplicated
        vim.defer_fn(function() require("go.inlay").disable_inlay_hints() end, 1100)

        require("astronvim.utils.lsp").on_attach(client, bufnr)

        vim.api.nvim_create_autocmd("BufWritePre", {
          group = vim.api.nvim_create_augroup("GoImport", { clear = true }),
          pattern = "*.go",
          callback = function()
            require("go.format").goimport()
            require("go.format").gofmt()
          end,
        })
      end,
    }
  end,
}

I was able to remove the gopls_inlay autocmd, which allows Neovim to display the native ones. My primary motivation for going down this road is because the go.nvim provided inlay hints are usually either stuck or remain in place after editing/deleting code.

I'm curious if there's a different way to disable it or detect if the running Neovim version supports native inlay hints. With a little guidance I'd be happy to contribute.

zalegrala commented 5 months ago

After upgrading to neovim 10, my custom gopls configuration through lspconfig has stopped working. It seems that enable/disable is supported, but disable on go.nvim for inlay hints means that I can't update them with lspconfig.

https://github.com/ray-x/go.nvim/blob/master/lua/go/gopls.lua#L433

I'm using the following to configure the server.

require("lspconfig").gopls.setup(require("user.lsp.settings.gopls"))
ray-x commented 4 months ago

@zalegrala Maybe

require("lspconfig").gopls.setup(require('go.lsp').config())
zalegrala commented 4 months ago

Hmm, that seems to still leave in place all the inlay hints, but what I'd like is to be able to turn off certain ones using the gopls config with lspconfig.

For context, the lua/user/lsp/settings/gopls.lua contains the following, which you can see some of the hints are disabled.

local opts = {
  settings = {
    gopls = {
      -- buildFlags = { "-tags=requires_docker" }, -- custom build flags
      experimentalPostfixCompletions = true,
      analyses = {
        fieldalignment = true,
        -- nilness = true, -- default on
        shadow = true,
        -- unusedparams = true, -- default on
        unusedvariable = true,
        -- unusedwrite = true, -- default on
        useany = true,
      },
      staticcheck = true,
      semanticTokens = true,
      codelenses = {
        gc_details = true,
        upgrade_dependency = true,
        run_govulncheck = true,
        tidy = true,
        vendor = true,
        test = true,
      },
      hints = {
        assignVariableTypes = false,
        compositeLiteralFields = false,
        compositeLiteralTypes = false,
        constantValues = false,
        functionTypeParameters = true,
        parameterNames = false,
        rangeVariableTypes = false,
      },

      gofumpt = true,
    },
  },
  init_options = {
    usePlaceholders = true,
  },
}

return opts
ray-x commented 4 months ago

go.nvim side. You need to disable the lsp by setting lsp_cfg = false

zalegrala commented 4 months ago

@ray-x thank you that worked. First set lsp_config=false and then the following.

require("lspconfig").gopls.setup(require("user.lsp.settings.gopls"))

I needed to skip require("lspconfig").gopls.setup(require('go.lsp').config()) it seems

This allowed me to tune the inlay hints. :+1: