Skip to content

x/tools/gopls: Highlighting/Semantic Tokens: Gopls is not generating tokens for constants/readonly or packages/namespaces. #72880

@ChrisPittsley

Description

@ChrisPittsley

I am running Neovim v0.10.4 with gopls v0.17.1.

EDIT: manually updated the gopls version installed by mason to v0.18.1 and see the same behavior.

For example, in the code below the constant "myUrl" will be highlighted as a constant when it is declared (:Inspect = @constant) but not when it is used in main() (:Inspect = @variable). My understanding is that there's supposed to be a group/modifier called @lsp.mod.readonly that gopls will mark a constant with, but this does not appear in the list returned by :highlight.

The usages of http and log are also marked as @variable.go when they're used within the two functions, except when http (or any other package) is used to specify a type like in the function signature for printStatus() or the declaration of resp in main() (:Inspect = @module). My understanding is that gopls should be marking these as "namespace", which is listed by :highlight.

Image

I know at least some information is making it from gopls to Treesitter/the neovim highlighter; if I disable gopls, most of the coloring of things like function calls and non-builtin types goes away.

Here's my LSP configuration, should be pretty much the same as the one suggested by LazyVim. Not sure if that workaround for semantic token support is still needed, but I see the same problem with the defaults (gopls.setup({})).

require('mason').setup({
    ensure_installed = {
        "goimports",
        "gofumpt",
        "gomodifytags",
        "impl",
        "delve"
    }
})

require('mason-lspconfig').setup({
    ensure_installed = {'gopls'}
})

local lspconfig = require("lspconfig")

lspconfig.gopls.setup({
  opts = {
    servers = {
      gopls = {
        settings = {
          gopls = {
            gofumpt = true,
              codelenses = {
                gc_details = false,
                generate = true,
                regenerate_cgo = true,
                run_govulncheck = true,
                test = true,
                tidy = true,
                upgrade_dependency = true,
                vendor = true,
          },
          hints = {
            assignVariableTypes = true,
            compositeLiteralFields = true,
            compositeLiteralTypes = true,
            constantValues = true,
            functionTypeParameters = true,
            parameterNames = true,
            rangeVariableTypes = true,
          },
          analyses = {
            nilness = true,
            unusedparams = true,
            unusedwrite = true,
            useany = true,
          },
          usePlaceholders = true,
          completeUnimported = true,
          staticcheck = true,
          directoryFilters = { "-.git", "-.vscode", "-.idea", "-.vscode-test", "-node_modules" },
          semanticTokens = true,
          },
        },
      },
    },
  },
  setup = {
    gopls = function(_, opts)
      -- workaround for gopls not supporting semanticTokensProvider
      -- https://github.com/golang/go/issues/54531#issuecomment-1464982242
      LazyVim.lsp.on_attach(function(client, _)
        if not client.server_capabilities.semanticTokensProvider then
          local semantic = client.config.capabilities.textDocument.semanticTokens
          client.server_capabilities.semanticTokensProvider = {
            full = true,
            legend = {
              tokenTypes = semantic.tokenTypes,
              tokenModifiers = semantic.tokenModifiers,
            },
            range = true,
          }
        end
      end, "gopls")
      -- end workaround
    end,
  },
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.ToolsThis label describes issues relating to any tools in the x/tools repository.goplsIssues related to the Go language server, gopls.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions