zigtools / zls

A Zig language server supporting Zig developers with features like autocomplete and goto definition
MIT License
2.98k stars 296 forks source link

Special characters not highlighted in strings #1928

Open Av1d1ty opened 4 months ago

Av1d1ty commented 4 months ago

Zig Version

0.12.1

Zig Language Server Version

0.13.0, 0.12.0

Client / Code Editor / Extensions

nvim 0.10, lspconfig

Steps to Reproduce and Observed Behavior

Open file with a special character like \n or \t in a string literal. See that all characters have the same color. image

Expected Behavior

Special characters highlighted differently image

Image above shows what it looks like when ZLS is turned off.

Relevant log output

No response

Techatrix commented 4 months ago

Above Terminal is NeoVim 0.10.0, Bottom Terminal is Neovim 0.9.5

With ZLS

with-zls

Without ZLS

without-zls

In case this is hard to see, the escape characters are highlighted except with ZLS on NeoVim 0.10.0. Everything else was as expected. I tried out a few of the default colorschemes in NeoVim 0.10.0 but that didn't help. I did not observe any changes when enabling the zig.vim extension.

So it seems like something is going on with NeoVim 0.10.0... :thinking:

The `init.lua` being used ```lua local vim = vim local Plug = vim.fn['plug#'] vim.call('plug#begin') Plug('neovim/nvim-lspconfig') -- https://github.com/neovim/nvim-lspconfig -- Plug('ziglang/zig.vim') -- https://github.com/ziglang/zig.vim vim.call('plug#end') -- I disabled ZLS by removing everything below here local lspconfig = require('lspconfig') lspconfig.zls.setup { cmd = { '/home/techatrix/repos/zls/zig-out/bin/zls' }, settings = { zls = { zig_exe_path = '/nix/store/s3nq31mhm8gxkq691p5w6q61ficw1hvr-zig-0.13.0/bin/zig', } } } ```
Av1d1ty commented 4 months ago

Also special characters are displayed correctly in Lua files with lua-ls active.

WillLillis commented 4 months ago

It seems like neovim's semantic token highlighting are overriding tree-sitter's highlight's for the escaped characters. Adding the following to my config disabled semantic highlighting for zig string tokens, and appears to fix the issue:

vim.api.nvim_set_hl(0, '@lsp.type.string.zig', {})

I'm not sure what a more permanent/ "out of the box" fix would be, as it seems that semantic token highlights have a higher priority than tree-sitter's by default.

Vexu commented 4 months ago

The same thing happens in vscode when semantic_tokens is set to full instead of partial, maybe other editors with grammar based highlighting should also use partial?

Techatrix commented 4 months ago

Perhaps it is more reasonable to assume that the editor already provides syntax based highlighting for Zig code? Then we could make partial the default which also fixes this issue.

Av1d1ty commented 4 months ago

Can confirm that setting semantic tokens to "partial" does fix the issue, but at the same time highlighting changes in places where I prefer it would not

Semantic tokens set to "full" image

Semantic tokens set to "partial" image

As you can see, function parameters are no longer highlighted distinctly in function body.

Av1d1ty commented 4 months ago

Also, on a side note, Mason tells me that default value for semantic tokens in "partial", while in fact it is set to "full"

image

Av1d1ty commented 4 months ago

Side note 2: Mason shows config names in camelCase, while in fact they need to be set in snake_case in my config files. Not sure if it is an actual issue. Maybe I lack understanding about how Mason and configs work for language servers.

Techatrix commented 4 months ago

Mason's package.yaml for ZLS uses vscode-zig's package.json to provide information about config options. This should not be done because vscode-zig options don't exactly match what ZLS uses. I will look into resolving that.

Techatrix commented 4 months ago

Can confirm that setting semantic tokens to "partial" does fix the issue, but at the same time highlighting changes in places where I prefer it would not

This should be fixed by 2e5a2a5959c0ba645fd6bfbf93dc467c32458465.

I used folke/tokyonight.nvim and set semantic_tokens to partial. Here is how it changed:

Before

Screenshot from 2024-06-22 12-31-42

After

Screenshot from 2024-06-22 12-31-09

Av1d1ty commented 4 months ago

Awesome, thanks!