michaelb / sniprun

A neovim plugin to run lines/blocs of code (independently of the rest of the file), supporting multiples languages
MIT License
1.43k stars 46 forks source link

Use `nvim_set_hl()` to set highlights #279

Closed pwnalone closed 5 months ago

pwnalone commented 5 months ago

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.

pwnalone commented 5 months ago

Sorry, I just realized that I made my PR for the master branch. I will close this and open another for the dev branch.