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

Resolve syntax highlighting group in neovim/treesitter #160

Merged
merged 6 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ highlighted as code comments or strings are ignored.
```vim
" Default:
" If a delimiter is in a highlight group whose name matches
" any of the followings, it will be ignored.
" any of the following regular expressions, it will be ignored.
let g:easy_align_ignore_groups = ['Comment', 'String']
```

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

To make `ignore_groups` work, and to debug the related issues, it is useful to
know which highlight group a certain location in a file belongs to. A special
function exists for this purpose, returning exactly the name of the highlight
group that is used by the easy align plugin.

```vim
" Highlight group name of the cursor position
echo easy_align#get_highlight_group_name()

" Highlight group name of the line 10, column 20
echo easy_align#get_highlight_group_name(10, 20)
```

### Ignoring unmatched lines

`ignore_unmatched` option determines how EasyAlign command processes lines that
Expand Down
24 changes: 23 additions & 1 deletion autoload/easy_align.vim
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,30 @@ function! s:floor2(v)
return a:v % 2 == 0 ? a:v : a:v - 1
endfunction

function! s:get_highlight_group_name(line, col)
let hl = synIDattr(synID(a:line, a:col, 0), 'name')

if hl == '' && has('nvim-0.9.0')
let insp = luaeval('vim.inspect_pos and vim.inspect_pos( nil, ' .. (a:line-1) .. ', ' .. (a:col-1) .. ' ) or { treesitter = {} }')
if !empty(insp.treesitter)
let hl = insp.treesitter[0].hl_group_link
endif
endif

" and, finally
return hl
endfunction

function! easy_align#get_highlight_group_name(...)
let l = get(a:, 1, line('.'))
let c = get(a:, 2, col('.'))
let hl = s:get_highlight_group_name(l, c)
return { 'line': l, 'column': c, 'group': hl }
endfunction

function! s:highlighted_as(line, col, groups)
if empty(a:groups) | return 0 | endif
let hl = synIDattr(synID(a:line, a:col, 0), 'name')
let hl = s:get_highlight_group_name(a:line, a:col)
for grp in a:groups
if grp[0] == '!'
if hl !~# grp[1:-1]
Expand Down Expand Up @@ -1146,3 +1167,4 @@ endfunction
let &cpo = s:cpo_save
unlet s:cpo_save

" vim: set et sw=2 :
13 changes: 12 additions & 1 deletion doc/easy_align.txt
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ highlighted as code comments or strings are ignored.
>
" Default:
" If a delimiter is in a highlight group whose name matches
" any of the followings, it will be ignored.
" any of the following regular expressions, it will be ignored.
let g:easy_align_ignore_groups = ['Comment', 'String']
<
For example, the following paragraph
Expand Down Expand Up @@ -567,6 +567,17 @@ opposite meaning. For instance, if `ignore_groups` is given as `['!Comment']`,
delimiters that are not highlighted as Comment will be ignored during the
alignment.

To make `ignore_groups` work, and to debug the related issues, it is useful to
know which highlight group a certain location in a file belongs to. A special
function exists for this purpose, returning exactly the name of the highlight
group that is used by the easy align plugin.
>
" Highlight group name of the cursor position
echo easy_align#get_highlight_group_name()

" Highlight group name of the line 10, column 20
echo easy_align#get_highlight_group_name(10, 20)
<

< Ignoring unmatched lines >__________________________________________________~
*easy-align-ignoring-unmatched-lines*
Expand Down
1 change: 1 addition & 0 deletions plugin/easy_align.vim
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,4 @@ vnoremap <silent> <Plug>(EasyAlignRepeat) :<C-U>call <SID>repeat_in_visual()<Ent
" Backward-compatibility (deprecated)
nnoremap <silent> <Plug>(EasyAlignOperator) :set opfunc=<SID>easy_align_op<Enter>g@

" vim: set et sw=2 :