Skip to content

Commit 1b56d5a

Browse files
committed
fix: properly handle math env delims in get_surrounding_or_next
refer: #3219
1 parent 958539a commit 1b56d5a

File tree

2 files changed

+55
-20
lines changed

2 files changed

+55
-20
lines changed

autoload/vimtex/env.vim

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,33 +83,50 @@ function! vimtex#env#get_surrounding_or_next(type) abort
8383
return [{}, {}]
8484
endif
8585

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
86+
" Now we check for math envs/regions
87+
let l:pos_saved = vimtex#pos#get_cursor()
88+
let l:posval_saved = vimtex#pos#val(l:pos_saved)
89+
90+
" Check for special math env delimiters ($..$, etc)
91+
let [l:open_sp, l:close_sp] = vimtex#delim#get_surrounding_or_next('env_math')
92+
let l:posval_sp = empty(l:open_sp)
93+
\ ? 500*l:posval_saved
94+
\ : vimtex#pos#val(l:open_sp)
95+
96+
" Early return if this match surrounds the cursor
97+
if l:posval_sp <= l:posval_saved
98+
return [l:open_sp, l:close_sp]
99+
endif
89100

90-
" Finally check for standard math environments
91-
let l:save_pos = vimtex#pos#get_cursor()
92-
let l:pv_current = vimtex#pos#val(l:save_pos)
93-
let l:pv_prev = 0
94-
while v:true && l:pv_current != l:pv_prev
95-
let [l:open, l:close] = vimtex#delim#get_surrounding_or_next('env_tex')
96-
if empty(l:open)
97-
call vimtex#pos#set_cursor(l:save_pos)
98-
return [l:open, l:close]
101+
" Check for standard math environments
102+
let l:posval_prev = 0
103+
let l:posval_env = l:posval_saved
104+
while v:true
105+
let [l:open_env, l:close_env] = vimtex#delim#get_surrounding_or_next('env_tex')
106+
if empty(l:open_env)
107+
call vimtex#pos#set_cursor(l:pos_saved)
108+
return [l:open_sp, l:close_sp]
99109
endif
100110

101-
if index(s:math_envs, substitute(l:open.name, '\*$', '', '')) >= 0
102-
call vimtex#pos#set_cursor(l:save_pos)
103-
return [l:open, l:close]
111+
if index(s:math_envs, substitute(l:open_env.name, '\*$', '', '')) >= 0
112+
call vimtex#pos#set_cursor(l:pos_saved)
113+
break
104114
endif
105115

106-
let l:pos_next = vimtex#pos#prev(l:open)
107-
let l:pv_prev = l:pv_current
108-
let l:pv_current = vimtex#pos#val(l:pos_next)
109-
call vimtex#pos#set_cursor(l:pos_next)
116+
let l:pos_next = vimtex#pos#prev(l:open_env)
117+
let l:posval_prev = l:posval_env
118+
let l:posval_env = vimtex#pos#val(l:pos_next)
119+
if l:posval_env == l:posval_prev
120+
call vimtex#pos#set_cursor(l:pos_saved)
121+
return [l:open_sp, l:close_sp]
122+
else
123+
call vimtex#pos#set_cursor(l:pos_next)
124+
endif
110125
endwhile
111126

112-
return [{}, {}]
127+
return l:posval_env <= l:posval_saved || l:posval_env < l:posval_sp
128+
\ ? [l:open_env, l:close_env]
129+
\ : [l:open_sp, l:close_sp]
113130
endfunction
114131

115132
let s:math_envs = [

test/test-textobj/test-other.vim

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,22 @@ call vimtex#test#keys('jjda$',
4646
\ ],
4747
\)
4848

49+
call vimtex#test#keys('jjfxda$',
50+
\ [
51+
\ '\documentclass{minimal}',
52+
\ '\begin{document}',
53+
\ '\begin{equation} x \end{equation}',
54+
\ '$y$',
55+
\ '\end{document}',
56+
\ ],
57+
\ [
58+
\ '\documentclass{minimal}',
59+
\ '\begin{document}',
60+
\ '',
61+
\ '$y$',
62+
\ '\end{document}',
63+
\ ],
64+
\)
65+
66+
4967
call vimtex#test#finished()

0 commit comments

Comments
 (0)