Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to define treesitter @x.y groups #109

Open
rktjmp opened this issue Oct 16, 2022 · 14 comments
Open

How to define treesitter @x.y groups #109

rktjmp opened this issue Oct 16, 2022 · 14 comments

Comments

@rktjmp
Copy link
Owner

rktjmp commented Oct 16, 2022

This interface is experimental, please subscribe to this issue for notification if the interface changes, also taking suggestions on alterations.

As of Neovim 0.8, highlight groups may contain . and @, in addition to a-Z. These group names are used for treesitter highlights (eg: @type.rust).

@ is an invalid variable name in Lua, so it's not possible to define these groups in the normal way.

Instead you must use a special sym function, which is provided via a table argument to your lush-spec function. sym stands for "symbol". This might change to group or group_name in the future.

-- functions are injected via a table for future extension
local theme = lush(function(injected_functions)
  -- you probably want to alias it locally
  local sym = injected_functions.sym 
  -- then call sym when defining the name
  return {
    Normal { bg = red, fg = blue },
    -- define groups as normal, but replace the group name with the function call
    -- you may omit the function call parenthesis as per lua.
    sym"@type.rust" { fg = green },
    -- You can access these groups in the same manner as normal groups
    X { sym"@type.rust" }, -- link
    Y { sym"@type.rust", bg = gold }, -- inherit 
    Z { fg = sym"@type.rust".fg } -- field access
})

Lushify support

  • You must alias to sym for Lushify to find calls.
    • DO sym("@type.rust")
    • DONT ts_name("@type.rust")
  • You must call with a " or ' string literal, parenthesis may be omitted as per lua, currently no space is supported between sym and the group name
    • DO sym("@type.rust")
    • DO sym('@type.rust')
    • DO sym"@type.rust"
    • DO sym'@type.rust'
    • DONT sym([[@type.rust]])
    • DONT sym(some_string_variable)
@mhanberg
Copy link

I have updated my theme to use this new syntax, but it seems like all highlights defined after the treesitter ones are not having any affect.

I tested this by moving a style above the treesitter definitions and then it worked when i restarted neovim.

I am not sure if this is a lush thing, or if I did something wrong when converting the TSFooBar highlights into sym("@foo.bar").

Changes for reference https://github.com/mhanberg/thicc_forest/pull/1/files

@rktjmp
Copy link
Owner Author

rktjmp commented Oct 17, 2022

Your missing the new argument/sym alias, and one of the names is invalid due to whitespace.

The error reporting is bad, not your fault and the design of Lush is kind of straining here. If you don't assign sym, then it's the same as a "missing symbol lookup" for normal group definitions, so Lush thinks you're doing:

return {
  sym "@some.thing" { fg = ...}, -- => sym("@some.thing")({fg = ...}) <- looks like you want to define a group called `sym` lush/lua
  NextGroup { fg = ... }, -- NextGroup({fg = ...})
}

I should add some error report if a group def is called without a table argument at least though.

Does this diff solve your issue?

@@ -99,7 +99,8 @@ vim.g.VM_Cursor_hl = "Cursor"
 vim.g.VM_Insert_hl = "Cursor"
 
 -- stylua: ignore start
-local theme = lush(function()
+local theme = lush(function(injected_functions)
+  local sym = injected_functions.sym
   return {
     ColorColumn { fg = nil, bg = bg1 }, -- used for the columns set with 'colorcolumn'
     Conceal { fg = grey1, bg = nil }, -- placeholder characters substituted for concealed text (see 'conceallevel')
@@ -332,7 +333,7 @@ local theme = lush(function()
     sym("@boolean")  { Purple },
     sym("@character") { Yellow },
     sym("@comment") { Grey },
-    sym("@conditional ") { Red },
+    sym("@conditional") { Red },
     sym("@constant.builtin") { PurpleItalic },
     sym("@constant.macro") { Purple },
     sym("@constant") { PurpleItalic },

@oncomouse
Copy link

oncomouse commented Oct 17, 2022

I'm trying to implement this in a theme that uses shipwright (specifically require("shipwright.transform.lush").to_vimscript) and none of the new groups are being created in the resulting vimscript colorscheme file.

I'm not sure I defined the groups correctly right, but here's my code: https://github.com/oncomouse/lushwal.nvim/blob/new-ts/lua/lushwal/addons/treesitter.lua

@OkanEsen
Copy link

I may be wrong but I think you have to change the syntax a little bit:

- sym("@preproc")({ PreProc }),
+ sym("@preproc") { PreProc },

@mhanberg
Copy link

Oh sorry, I totally missed the function argument part. I think I was thrown off by the words import and alias. I'll try this later.

@rktjmp
Copy link
Owner Author

rktjmp commented Oct 18, 2022

I'm trying to implement this in a theme that uses shipwright (specifically require("shipwright.transform.lush").to_vimscript) and none of the new groups are being created in the resulting vimscript colorscheme file.

I'm not sure I defined the groups correctly right, but here's my code: https://github.com/oncomouse/lushwal.nvim/blob/new-ts/lua/lushwal/addons/treesitter.lua

If I copy the TS groups into base.lua and build they show up. Maybe addons/treesitter isn't being required before building? @text.note also links to an undefined group SpecialComment (no other refs to that in the repo).

There is also possibly a bug with the vimscript export for X ({ strikethrough = true }),. Works in other theme.

@mhanberg
Copy link

All good on my end now 👍

@oncomouse
Copy link

@rktjmp Thanks for taking a look. Best I can tell, it looks like a couple of the highlight groups were defined twice, which was causing an error in compilation that was only getting reported (for me) when the groups were defined base.lua. I think it's working now.

@chmnchiang
Copy link

Hi, does this feature works with lushwright.to_lua? This produce something like

@variable.builtin = {fg = "#E991B0"},

which is causing syntax error.

@rktjmp
Copy link
Owner Author

rktjmp commented Oct 22, 2022

@chmnchiang Should be fixed!

Actually, it's not!

Actually fixed!

@chmnchiang
Copy link

@chmnchiang Should be fixed!

Actually, it's not!

Actually fixed!

Can confirmed it is fixed. Thanks for the swift update!

Doerge added a commit to Doerge/laserwave.nvim that referenced this issue Nov 7, 2022
shawnohare added a commit to shawnohare/hadalized.nvim that referenced this issue Nov 23, 2022
Replace neovim 0.7.2 treesitter highlight groups of the form `TS<X><Y>`
with the neovim 0.8 analogs of the form `@x.y`.

Implement the suggested lush method for defining these groups
as per rktjmp/lush.nvim#109
shawnohare added a commit to shawnohare/hadalized.nvim that referenced this issue Nov 23, 2022
Deprecate use of neovim 0.7.2

Replace neovim 0.7.2 treesitter highlight groups of the form `TS<X><Y>`
with the neovim 0.8 analogs of the form `@x.y`.

Implement the suggested lush method for defining these groups
as per rktjmp/lush.nvim#109

Fixes #1
@musjj
Copy link
Contributor

musjj commented Dec 12, 2022

For some reason, it's not working for me:

local colors = require("theme.colors")
...
sym("@annotation") { fg = colors.foo }, -- doesn't work
sym("@attribute") { fg = colors.bar }, -- doesn't work either
WhichKey { fg = colors.blue }, -- this line and beyond also stops working
...

sym(...) highlights and any highlight assignments afterwards doesn't work. Once I removed all lines with sym(...), the rest of the highlights are functioning again.
I'm guessing that it's not parsing the table access correctly?

@rockyzhang24
Copy link
Contributor

rockyzhang24 commented Dec 12, 2022

No issues on my side. It works perfectly as usual. Do you adjust it to the code below?

local theme = lush(function(injected_functions)
  local sym = injected_functions.sym
  return {
	....
    sym("@class") { fg = blue_green },
    ....
  }
end)
return theme

FYI: my theme repo https://github.com/rockyzhang24/arctic.nvim.

@musjj
Copy link
Contributor

musjj commented Dec 12, 2022

local sym = injected_functions.sym

Argh, for some reason my eyes skipped that line! Thanks for the quick reply!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants