Yes, you can detect the style class (highlight group) of a text in Vim after it has been highlighted by a plugin or LSP. Vim provides several commands and functions to inspect and query the syntax highlighting of text. Here are a few methods you can use:
1. Using :echo synstack(line("."), col("."))
This command will display the syntax items (highlight groups) applied to the character under the cursor.
:echo synstack(line("."), col("."))
2. Using :echo synIDattr(synID(line("."), col("."), 1), "name")
This command will display the name of the syntax highlight group applied to the character under the cursor.
:echo synIDattr(synID(line("."), col("."), 1), "name")
3. Using :Inspect Plugin
There are plugins available that can help you inspect syntax highlighting more conveniently. One such plugin is vim-highlighter.
Installation
You can install vim-highlighter using a plugin manager like vim-plug:
Plug 'romainl/vim-highlighter'
After installing, you can use the :Inspect command to see the highlight groups applied to the text under the cursor.
4. Using vim-sneak or vim-sneak with vim-highlighter
If you are using plugins like vim-sneak or vim-sneak with vim-highlighter, you can press ? to see the highlight groups of the target character.
5. Custom Function to Display Highlight Group
You can create a custom function to display the highlight group of the text under the cursor:
function! ShowSyntaxUnderCursor()
let lnum = line(".")
let col = col(".")
let syn_stack = synstack(lnum, col)
if len(syn_stack) > 0
let syn_name = synIDattr(syn_stack[-1], "name")
echomsg "Syntax highlight group: " . syn_name
else
echomsg "No syntax highlight group found"
endif
endfunction
nnoremap <leader>sh :call ShowSyntaxUnderCursor()<CR>
After adding this function to your .vimrc, you can press <leader>sh to see the highlight group of the text under the cursor.
Example
Suppose you have the following text highlighted by a plugin:
function HelloWorld()
Place your cursor on the word function and use one of the methods above to inspect the highlight group. For example, using :echo synIDattr(synID(line("."), col("."), 1), "name") might output Function, indicating that the text is highlighted with the Function highlight group.
These methods should help you detect the style class of text in Vim after it has been highlighted by a plugin or LSP.