tree-sitter / tree-sitter-c-sharp

C# Grammar for tree-sitter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can we improve the highlight groups?

jjiangweilan opened this issue · comments

Here is a screenshot from my neovim, comparing c-sharp highlight with c++ highlight
image
I have two issues with c-sharp's highlight

  1. local variable and member variable are the same
  2. a member function call and a function declaration are the same

I could try and fix this, but, do you use tokyo night? My highlight's a bit different..I have the difference between a local variable and variable member, but not with function defs/calls.

If you can show the different highlight groups under the cursor at each important position, that'd help for sure

image

You can use a function like this:

---Show all the items at a given buffer position.
---
---Can also be shown with `:Inspect`. *:Inspect*
---
---@param bufnr? number defaults to the current buffer
---@param row? number row to inspect, 0-based. Defaults to the row of the current cursor
---@param col? number col to inspect, 0-based. Defaults to the col of the current cursor
---@param filter? InspectorFilter (table|nil) see |vim.inspect_pos()|
function vim.show_pos(bufnr, row, col, filter)
  local items = vim.inspect_pos(bufnr, row, col, filter)

  local lines = { {} }

  ---@private
  local function append(str, hl)
    table.insert(lines[#lines], { str, hl })
  end

  ---@private
  local function nl()
    table.insert(lines, {})
  end

  ---@private
  local function item(data, comment)
    append('  - ')
    append(data.hl_group, data.hl_group)
    append(' ')
    if data.hl_group ~= data.hl_group_link then
      append('links to ', 'MoreMsg')
      append(data.hl_group_link, data.hl_group_link)
      append(' ')
    end
    if comment then
      append(comment, 'Comment')
    end
    nl()
  end

  -- treesitter
  if #items.treesitter > 0 then
    append('Treesitter', 'Title')
    nl()
    for _, capture in ipairs(items.treesitter) do
      item(capture, capture.lang)
    end
    nl()
  end

  if #items.semantic_tokens > 0 then
    append('Semantic Tokens', 'Title')
    nl()
    for _, token in ipairs(items.semantic_tokens) do
      local client = vim.lsp.get_client_by_id(token.client_id)
      client = client and (' (' .. client.name .. ')') or ''
      item(token.hl_groups.type, 'type' .. client)
      for _, modifier in ipairs(token.hl_groups.modifiers) do
        item(modifier, 'modifier' .. client)
      end
    end
    nl()
  end

  -- syntax
  if #items.syntax > 0 then
    append('Syntax', 'Title')
    nl()
    for _, syn in ipairs(items.syntax) do
      item(syn)
    end
    nl()
  end
  -- extmarks
  if #items.extmarks > 0 then
    append('Extmarks', 'Title')
    nl()
    for _, extmark in ipairs(items.extmarks) do
      if extmark.opts.hl_group then
        item(extmark.opts, extmark.ns)
      else
        append('  - ')
        append(extmark.ns, 'Comment')
        nl()
      end
    end
    nl()
  end

  if #lines[#lines] == 0 then
    table.remove(lines)
  end

  local chunks = {}
  for _, line in ipairs(lines) do
    vim.list_extend(chunks, line)
    table.insert(chunks, { '\n' })
  end
  if #chunks == 0 then
    chunks = {
      {
        'No items found at position '
          .. items.row
          .. ','
          .. items.col
          .. ' in buffer '
          .. items.buffer,
      },
    }
  end
  vim.api.nvim_echo(chunks, false, {})
end

and then bind it like so:

-- highlights under cursor
if vim.fn.has("nvim-0.9.0") == 1 then
  vim.keymap.set("n", "<leader>hl", vim.show_pos, { desc = "Highlight Groups at cursor" })
end

End result:

image
image

Just want to make sure we have the same hl groups

I will test it after they officially release 0.9. Thanks

I've added a highlight test generator in this PR, it might help with improving the highlight groups.

I could try and fix this, but, do you use tokyo night? My highlight's a bit different..I have the difference between a local variable and variable member, but not with function defs/calls.

If you can show the different highlight groups under the cursor at each important position, that'd help for sure

image

You can use a function like this:

---Show all the items at a given buffer position.
---
---Can also be shown with `:Inspect`. *:Inspect*
---
---@param bufnr? number defaults to the current buffer
---@param row? number row to inspect, 0-based. Defaults to the row of the current cursor
---@param col? number col to inspect, 0-based. Defaults to the col of the current cursor
---@param filter? InspectorFilter (table|nil) see |vim.inspect_pos()|
function vim.show_pos(bufnr, row, col, filter)
  local items = vim.inspect_pos(bufnr, row, col, filter)

  local lines = { {} }

  ---@private
  local function append(str, hl)
    table.insert(lines[#lines], { str, hl })
  end

  ---@private
  local function nl()
    table.insert(lines, {})
  end

  ---@private
  local function item(data, comment)
    append('  - ')
    append(data.hl_group, data.hl_group)
    append(' ')
    if data.hl_group ~= data.hl_group_link then
      append('links to ', 'MoreMsg')
      append(data.hl_group_link, data.hl_group_link)
      append(' ')
    end
    if comment then
      append(comment, 'Comment')
    end
    nl()
  end

  -- treesitter
  if #items.treesitter > 0 then
    append('Treesitter', 'Title')
    nl()
    for _, capture in ipairs(items.treesitter) do
      item(capture, capture.lang)
    end
    nl()
  end

  if #items.semantic_tokens > 0 then
    append('Semantic Tokens', 'Title')
    nl()
    for _, token in ipairs(items.semantic_tokens) do
      local client = vim.lsp.get_client_by_id(token.client_id)
      client = client and (' (' .. client.name .. ')') or ''
      item(token.hl_groups.type, 'type' .. client)
      for _, modifier in ipairs(token.hl_groups.modifiers) do
        item(modifier, 'modifier' .. client)
      end
    end
    nl()
  end

  -- syntax
  if #items.syntax > 0 then
    append('Syntax', 'Title')
    nl()
    for _, syn in ipairs(items.syntax) do
      item(syn)
    end
    nl()
  end
  -- extmarks
  if #items.extmarks > 0 then
    append('Extmarks', 'Title')
    nl()
    for _, extmark in ipairs(items.extmarks) do
      if extmark.opts.hl_group then
        item(extmark.opts, extmark.ns)
      else
        append('  - ')
        append(extmark.ns, 'Comment')
        nl()
      end
    end
    nl()
  end

  if #lines[#lines] == 0 then
    table.remove(lines)
  end

  local chunks = {}
  for _, line in ipairs(lines) do
    vim.list_extend(chunks, line)
    table.insert(chunks, { '\n' })
  end
  if #chunks == 0 then
    chunks = {
      {
        'No items found at position '
          .. items.row
          .. ','
          .. items.col
          .. ' in buffer '
          .. items.buffer,
      },
    }
  end
  vim.api.nvim_echo(chunks, false, {})
end

and then bind it like so:

-- highlights under cursor
if vim.fn.has("nvim-0.9.0") == 1 then
  vim.keymap.set("n", "<leader>hl", vim.show_pos, { desc = "Highlight Groups at cursor" })
end

End result:

image image

Just want to make sure we have the same hl groups

Finally 0.9 is here. Hope it not too late. So I got the same as yours. The one in csharo should be IlluminatedWordWrite, but is reported as IlluminatedWordRead

I've added a highlight test generator in this PR, it might help with improving the highlight groups.

I am still having the problem after upgrading to the lastest nvim and ts:(