Skip to content

Commit e98adc1

Browse files
committed
feat: i$ and a$ text objects fallback to next group
refer: #3219
1 parent 7975318 commit e98adc1

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

autoload/vimtex/delim.vim

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,15 @@ function! vimtex#delim#get_surrounding(type) abort " {{{1
453453
\ : s:get_surrounding_delim(a:type)
454454
endfunction
455455

456+
" }}}1
457+
function! vimtex#delim#get_surrounding_or_next(type) abort " {{{1
458+
" This is split, because we need some extra conditions to ensure that
459+
" delimiters are matched properly.
460+
return a:type =~# '^env'
461+
\ ? s:get_surrounding_or_next_env(a:type)
462+
\ : s:get_surrounding_or_next_delim(a:type)
463+
endfunction
464+
456465
" }}}1
457466

458467
function! s:get_surrounding_env(type) abort " {{{1
@@ -527,6 +536,34 @@ function! s:get_surrounding_delim(type) abort " {{{1
527536
return [{}, {}]
528537
endfunction
529538

539+
" }}}1
540+
function! s:get_surrounding_or_next_env(type) abort " {{{1
541+
let [l:open, l:close] = s:get_surrounding_env(a:type)
542+
if !empty(l:open)
543+
return [l:open, l:close]
544+
endif
545+
546+
let l:open = vimtex#delim#get_next(a:type, 'open')
547+
if empty(l:open) | return [{}, {}] | endif
548+
549+
let l:close = vimtex#delim#get_matching(l:open)
550+
return [l:open, l:close]
551+
endfunction
552+
553+
" }}}1
554+
function! s:get_surrounding_or_next_delim(type) abort " {{{1
555+
let [l:open, l:close] = s:get_surrounding_delim(a:type)
556+
if !empty(l:open)
557+
return [l:open, l:close]
558+
endif
559+
560+
let l:next = vimtex#delim#get_next(a:type, 'open')
561+
if empty(l:open) | return [{}, {}] | endif
562+
563+
let l:close = vimtex#delim#get_matching(l:open)
564+
return [l:open, l:close]
565+
endfunction
566+
530567
" }}}1
531568

532569
function! s:operator_setup(operator) abort " {{{1

autoload/vimtex/env.vim

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,25 @@ function! vimtex#env#get_surrounding(type) abort
7373
endwhile
7474
endfunction
7575

76+
function! vimtex#env#get_surrounding_or_next(type) abort
77+
if a:type ==# 'normal'
78+
return vimtex#delim#get_surrounding_or_next('env_tex')
79+
endif
80+
81+
if a:type !=# 'math'
82+
call vimtex#log#error('Wrong argument!')
83+
return [{}, {}]
84+
endif
85+
86+
" First check for special math env delimiters
87+
let [l:open, l:close] = vimtex#delim#get_surrounding_or_next('env_math')
88+
if !empty(l:open) | return [l:open, l:close] | endif
89+
90+
" Finally check for standard math environments
91+
let [l:open, l:close] = vimtex#delim#get_surrounding_or_next('env_tex')
92+
return [l:open, l:close]
93+
endfunction
94+
7695
let s:math_envs = [
7796
\ 'align',
7897
\ 'alignat',

autoload/vimtex/text_obj.vim

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ function! vimtex#text_obj#delimited(is_inner, mode, type) abort " {{{1
122122
if a:mode
123123
let l:object = s:get_sel_delimited_visual(a:is_inner, a:type, l:startpos)
124124
else
125-
let [l:open, l:close] = s:get_surrounding(a:type)
125+
let [l:open, l:close] = s:get_surrounding_or_next(a:type)
126126
let l:object = empty(l:open)
127127
\ ? {} : s:get_sel_delimited(l:open, l:close, a:is_inner)
128128
endif
@@ -463,6 +463,15 @@ endfunction
463463

464464
" }}}1
465465

466+
function! s:get_surrounding_or_next(type) abort " {{{1
467+
if a:type ==# 'delims'
468+
return vimtex#delim#get_surrounding_or_next('delim_all')
469+
else
470+
return vimtex#env#get_surrounding_or_next(a:type)
471+
endif
472+
endfunction
473+
474+
" }}}1
466475
function! s:get_surrounding(type) abort " {{{1
467476
if a:type ==# 'delims'
468477
return vimtex#delim#get_surrounding('delim_all')

test/test-textobj/test-other.vim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,12 @@ call vimtex#test#keys('f\dac',
2323
\ 'a + \; f',
2424
\ 'a + f')
2525

26+
call vimtex#test#keys('di$',
27+
\ 'Hello world! $(x)$',
28+
\ 'Hello world! $$')
29+
30+
call vimtex#test#keys('da$',
31+
\ 'Hello world! $(x)$',
32+
\ 'Hello world! ')
33+
2634
call vimtex#test#finished()

0 commit comments

Comments
 (0)