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

Use nvim_set_hl() to set highlights #280

Merged
merged 1 commit into from
Mar 24, 2024
Merged

Use nvim_set_hl() to set highlights #280

merged 1 commit into from
Mar 24, 2024

Conversation

pwnalone
Copy link
Contributor

Description

This PR is related to #278.

Currently, it's not possible to set certain highlight attributes for Sniprun's highlight groups. This is because the code is manually constructing VimScript commands, which it then wraps in calls to vim.cmd(), and it only considers a few attributes.

This PR updates the code to use Neovim's nvim_set_hl() API function, which handles all attributes.

Backward compatibility

This update should be mostly backward compatible with the previous way of setting highlights; however, there are some differences to be aware of.

  1. The VimScript :highlight command updates the highlight group if already set, whereas nvim_set_hl() completely replaces the definition.
  2. The VimScript :highlight command accepts an altfont attribute for GUIs, and nvim_set_hl() does not.

See below for documentation on the attributes/options accepted by these two commands.

VimScript :highlight attributes

attr-list is a comma-separated list (without spaces) of the           
following items (in any order):                                       
	bold                                                          
	underline                                                     
	undercurl	curly underline                               
	underdouble	double underline                              
	underdotted	dotted underline                              
	underdashed	dashed underline                              
	strikethrough                                                 
	reverse                                                       
	inverse		same as reverse                               
	italic                                                        
	standout                                                      
	altfont                                                       
	nocombine	override attributes instead of combining them 
	NONE		no attributes used (used to reset it)         

Lua nvim_set_hl() options

Highlight definition map, accepts the following keys:       
• fg (or foreground): color name or "#RRGGBB", see note.    
• bg (or background): color name or "#RRGGBB", see note.    
• sp (or special): color name or "#RRGGBB"                  
• blend: integer between 0 and 100                          
• bold: boolean                                             
• standout: boolean                                         
• underline: boolean                                        
• undercurl: boolean                                        
• underdouble: boolean                                      
• underdotted: boolean                                      
• underdashed: boolean                                      
• strikethrough: boolean                                    
• italic: boolean                                           
• reverse: boolean                                          
• nocombine: boolean                                        
• link: name of another highlight group to link to, see     
  |:hi-link|.                                               
• default: Don't override existing definition |:hi-default| 
• ctermfg: Sets foreground of cterm color |ctermfg|         
• ctermbg: Sets background of cterm color |ctermbg|         
• cterm: cterm attribute map, like |highlight-args|. If not 
  set, cterm attributes will match those from the attribute 
  map documented above.                                     

Notes

I removed the autocommand that sets the highlights on the ColorScheme event, since it appears to be unnecessary. The author of the previous highlighting code said they were experiencing issues where some colorschemes clear the highlight groups, causing the defaults to be lost (see here), but I tested this with multiple colorschemes and did not experience this issue myself.

As mentioned here, Sniprun's author wants the highlight groups to be defined in this order of priority:

custom config > external colorscheme > default

This PR maintains this priority.

@michaelb
Copy link
Owner

michaelb commented Mar 24, 2024

very nice PR

I'll merge and make a new release sometime today

@michaelb
Copy link
Owner

michaelb commented Mar 24, 2024

I removed the autocommand that sets the highlights on the ColorScheme

Good

The author of the previous highlighting code said they were experiencing issues where some colorschemes clear the highlight

I do too (on Lunarvim), but I was unable to determine what happens: currently no highlight gets applied on my config anyway (so I tested your changes on an default nvim config).
I can 'fix' that by calling setup_highlight() each time a snippet is run, which I'll probably do for v1.3.12, even if it's a bit heavy-handed

@michaelb michaelb merged commit 822890f into michaelb:dev Mar 24, 2024
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

Successfully merging this pull request may close these issues.

2 participants