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

feat(colors): optionally get the cursor color from the hl group at the cursor #30

Merged
merged 4 commits into from
Nov 29, 2024

Conversation

folke
Copy link
Contributor

@folke folke commented Nov 28, 2024

When cursor_color = "none", we try using the color at the cursor's hl group as the cursor color.
This is similar to how kitty does the smearing cursors, with cursor none, meaning it takes the color of what's at the current cell as the cursor color.

When getting the color, we first try treesitter captures if treesitter highlighting is enabled in the buffer and then fallback to extmarks. Regular treesitter extmarks are ephemeral, so they can't be retrieved with the get extmarks function.

Everything is cached, so should not impact performance.

I believe you tried something similar, as there was a non-used function called M.get_hl_group already :)

I used some metatable magic to prevent making changes in other places where the hl group lookup tables were used. Let me know if you'd rather have those places call the function directly.

Changes

Mainly changes to the color module, where hl groups are now cached and dynamically created when needed.

image

lua/smear_cursor/color.lua Outdated Show resolved Hide resolved
local cursor = vim.api.nvim_win_get_cursor(0)
local ts_hl_group ---@type string?
for _, capture in pairs(vim.treesitter.get_captures_at_pos(0, cursor[1] - 1, cursor[2])) do
ts_hl_group = "@" .. capture.capture .. "." .. capture.lang
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to loop over all captures. The last one is the one we want, so no early break here.

}
or {
fg = _cursor_color,
bg = "none", -- _normal_bg,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the old code, for level=0, it used to be "none" and for level>0 it was _normal_bg.
I assume this was a bug? And you always want none here?

@folke

This comment was marked as resolved.

@folke folke marked this pull request as draft November 28, 2024 19:10
@folke
Copy link
Contributor Author

folke commented Nov 28, 2024

That issue is now fixed.
Will also add support for other extmarks.

@folke folke marked this pull request as ready for review November 28, 2024 19:37
@folke
Copy link
Contributor Author

folke commented Nov 28, 2024

Added support for both treesitter and extmarks

@folke folke marked this pull request as draft November 28, 2024 19:47
@folke
Copy link
Contributor Author

folke commented Nov 28, 2024

converted to draft again. Need to optimize the code a bit first

@folke
Copy link
Contributor Author

folke commented Nov 28, 2024

Changed it so that the color at the cursor only gets updated on CursorMoved.

Much better for performance
image

@folke folke marked this pull request as ready for review November 28, 2024 20:02
@sphamba
Copy link
Owner

sphamba commented Nov 29, 2024

Thanks a lot, it works great!
I'll merge this and add an entry in the Readme. I'll also remove the metatables and directly call the get_hl_group function, but thanks for keeping the scope of your changes to the color module :)

@sphamba sphamba merged commit 5f0faf1 into sphamba:main Nov 29, 2024
1 check passed
@sphamba sphamba added the enhancement New feature or enhancement label Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants