Skip to content

Commit 9815a55

Browse files
j-xellajunegunn
andauthored
Resolve syntax highlighting group in neovim/treesitter (#160)
Co-authored-by: Junegunn Choi <[email protected]>
1 parent 12dd631 commit 9815a55

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ highlighted as code comments or strings are ignored.
406406
```vim
407407
" Default:
408408
" If a delimiter is in a highlight group whose name matches
409-
" any of the followings, it will be ignored.
409+
" any of the following regular expressions, it will be ignored.
410410
let g:easy_align_ignore_groups = ['Comment', 'String']
411411
```
412412

@@ -463,6 +463,19 @@ If a pattern in `ignore_groups` is prepended by a `!`, it will have the opposite
463463
meaning. For instance, if `ignore_groups` is given as `['!Comment']`, delimiters
464464
that are *not* highlighted as Comment will be ignored during the alignment.
465465

466+
To make `ignore_groups` work, and to debug the related issues, it is useful to
467+
know which highlight group a certain location in a file belongs to. A special
468+
function exists for this purpose, returning exactly the name of the highlight
469+
group that is used by the easy align plugin.
470+
471+
```vim
472+
" Highlight group name of the cursor position
473+
echo easy_align#get_highlight_group_name()
474+
475+
" Highlight group name of the line 10, column 20
476+
echo easy_align#get_highlight_group_name(10, 20)
477+
```
478+
466479
### Ignoring unmatched lines
467480

468481
`ignore_unmatched` option determines how EasyAlign command processes lines that

autoload/easy_align.vim

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,30 @@ function! s:floor2(v)
8989
return a:v % 2 == 0 ? a:v : a:v - 1
9090
endfunction
9191

92+
function! s:get_highlight_group_name(line, col)
93+
let hl = synIDattr(synID(a:line, a:col, 0), 'name')
94+
95+
if hl == '' && has('nvim-0.9.0')
96+
let insp = luaeval('vim.inspect_pos and vim.inspect_pos( nil, ' .. (a:line-1) .. ', ' .. (a:col-1) .. ' ) or { treesitter = {} }')
97+
if !empty(insp.treesitter)
98+
let hl = insp.treesitter[0].hl_group_link
99+
endif
100+
endif
101+
102+
" and, finally
103+
return hl
104+
endfunction
105+
106+
function! easy_align#get_highlight_group_name(...)
107+
let l = get(a:, 1, line('.'))
108+
let c = get(a:, 2, col('.'))
109+
let hl = s:get_highlight_group_name(l, c)
110+
return { 'line': l, 'column': c, 'group': hl }
111+
endfunction
112+
92113
function! s:highlighted_as(line, col, groups)
93114
if empty(a:groups) | return 0 | endif
94-
let hl = synIDattr(synID(a:line, a:col, 0), 'name')
115+
let hl = s:get_highlight_group_name(a:line, a:col)
95116
for grp in a:groups
96117
if grp[0] == '!'
97118
if hl !~# grp[1:-1]
@@ -1146,3 +1167,4 @@ endfunction
11461167
let &cpo = s:cpo_save
11471168
unlet s:cpo_save
11481169

1170+
" vim: set et sw=2 :

doc/easy_align.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ highlighted as code comments or strings are ignored.
517517
>
518518
" Default:
519519
" If a delimiter is in a highlight group whose name matches
520-
" any of the followings, it will be ignored.
520+
" any of the following regular expressions, it will be ignored.
521521
let g:easy_align_ignore_groups = ['Comment', 'String']
522522
<
523523
For example, the following paragraph
@@ -567,6 +567,17 @@ opposite meaning. For instance, if `ignore_groups` is given as `['!Comment']`,
567567
delimiters that are not highlighted as Comment will be ignored during the
568568
alignment.
569569

570+
To make `ignore_groups` work, and to debug the related issues, it is useful to
571+
know which highlight group a certain location in a file belongs to. A special
572+
function exists for this purpose, returning exactly the name of the highlight
573+
group that is used by the easy align plugin.
574+
>
575+
" Highlight group name of the cursor position
576+
echo easy_align#get_highlight_group_name()
577+
578+
" Highlight group name of the line 10, column 20
579+
echo easy_align#get_highlight_group_name(10, 20)
580+
<
570581

571582
< Ignoring unmatched lines >__________________________________________________~
572583
*easy-align-ignoring-unmatched-lines*

plugin/easy_align.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,4 @@ vnoremap <silent> <Plug>(EasyAlignRepeat) :<C-U>call <SID>repeat_in_visual()<Ent
140140
" Backward-compatibility (deprecated)
141141
nnoremap <silent> <Plug>(EasyAlignOperator) :set opfunc=<SID>easy_align_op<Enter>g@
142142
143+
" vim: set et sw=2 :

0 commit comments

Comments
 (0)