AstroNvim / astrotheme

The default colorscheme used by AstroNvim
https://AstroNvim.com
GNU General Public License v3.0
97 stars 19 forks source link

Golang syntax highlighting isn't as good as other languages #107

Closed AthirsonSilva closed 7 months ago

AthirsonSilva commented 7 months ago

Is your feature related to a problem?

I migrated from VSCode today and, currently, everything is going smoothly, but one thing that is really bothering me is how the default theme doesn't show the "complete" syntax highlighting for Golang, and Golang only, i tested with Java, Lua, HTML and TypeScript and the syntax highlighting worked well with every single one.

Describe the new feature

Basically, it would be cool to make the syntax highlighting integrate better on Go, here goes a comparation between how the syntax highlighting works with Golang and TypeScript, and how much better it works for TypeScript.

Screenshot from 2024-02-02 19-57-03 Screenshot from 2024-02-02 19-51-57

Additional context

For additional context, i have the Golang LSP installed on my setup.

mehalter commented 7 months ago

https://github.com/golang/vscode-go/issues/2286

Looks like semantic tokens are disabled by default. It will be up to the treesitter parser or the language server to provide semantic tokens for them to be highlighted nicely. Syntax is controlled in a general level and not necessarily language specific

mehalter commented 7 months ago

You probably just need to configure gopls to enable semantic tokens in the settings to get better highlighting

AthirsonSilva commented 7 months ago

Oh, then i will take a look at it, many thanks!

AthirsonSilva commented 7 months ago

I don't know if reopening this issue or making a new post on the Discord would be the correct way, but i tried configuring gopls to enable semantic tokens and it didn't work, here goes my init.lua file:

return {
  opts = {
    clipboard = "unnamedplus",
    setup = {
      gopls = function(_, opts)
        require("lazyvim.util").on_attach(function(client, bufnr)
          if client.name == "gopls" then
            client.server_capabilities.semanticTokens = {
              full = true,
              legend = {
                tokenTypes = {
                  "namespace",
                  "type",
                  "class",
                  "enum",
                  "interface",
                  "struct",
                  "typeParameter",
                  "parameter",
                  "variable",
                  "property",
                  "enumMember",
                  "event",
                  "function",
                  "method",
                  "macro",
                  "keyword",
                  "modifier",
                  "comment",
                  "string",
                  "number",
                  "regexp",
                  "operator",
                  "decorator",
                },
                tokenModifiers = {
                  "declaration",
                  "definition",
                  "readonly",
                  "static",
                  "deprecated",
                  "abstract",
                  "async",
                  "modification",
                  "documentation",
                  "defaultLibrary",
                },
              },
            }
          end
        end)
        opts.settings = {
          gopls = {
            semanticTokens = true,
          },
        }
      end,
    },
  },
  highlights = {
    init = {},
  },
  plugins = {
    "gbprod/cutlass.nvim",
    "AstroNvim/astrocommunity",
    { import = "astrocommunity.motion.mini-move" },
    { import = "astrocommunity.project.nvim-spectre" },
    { import = "astrocommunity.project.projectmgr-nvim" },
  },
  mappings = {
    -- first key is the mode
    n = {
      -- second key is the lefthand side of the map
      -- mappings seen under group name "Buffer"
      ["<leader>bn"] = { "<cmd>tabnew<cr>", desc = "New tab" },
      ["<leader>bD"] = {
        function()
          require("astronvim.utils.status").heirline.buffer_picker(
            function(bufnr) require("astronvim.utils.buffer").close(bufnr) end
          )
        end,
        desc = "Pick to close",
      },
      -- tables with the `name` key will be registered with which-key if it's installed
      -- this is useful for naming menus
      ["<leader>b"] = { name = "Buffers" },
      -- quick save
      ["<C-s>"] = { ":w!<cr>", desc = "Save File" }, -- change description but the same command
      ["d"] = { '"_d', desc = "Deletes current line without copying the contents" },
      -- ["<Del>"] = { '"_d', desc = "Delete visual selection without saving to clipboard" },
    },
    v = {
      -- setting a mapping to false will disable it
      -- ["<esc>"] = false,
      ["<Del>"] = { '"_d', desc = "Delete visual selection without saving to clipboard" },
    },
  },
}

I found this configuration on this Reddit post: https://www.reddit.com/r/neovim/comments/11xekjx/how_to_make_semantic_token_highlighting_work_with/

One interesting thing is that in the post it was mentioned that semantic tokens are built into neovim now, and i am already using a recent version of neovim right now, and since the link @mehalter posted was from 2022, it makes sense to be built in now.

That is also this post that says the same thing: https://www.reddit.com/r/neovim/comments/zg6bvs/lsp_semantic_tokens_support_merged/

NVIM v0.10.0-dev Build type: RelWithDebInfo LuaJIT 2.1.0-beta3 Run "nvim -V1 -v" for more info

mehalter commented 7 months ago

This is incorrect @AthirsonSilva

  1. this is using utilities in LazyVim which aren't available in AstroNvim
  2. Semantic tokens are default in 0.9+ so you don't need to enable the capability.

All you need to do is create a file user/lsp/config/gopls.lua and give it the content:

return {
  settings = {
    gopls = {
      semanticTokens = true,
    },
  },
}
mehalter commented 7 months ago

2024-02-03_10:05:21_screenshot

here is an example with just that added in a base AstroNvim install.