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

feat(fold)!: handle comment specially #2884

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
42 changes: 30 additions & 12 deletions autoload/vimtex/fold.vim
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,37 @@ function! vimtex#fold#init_state(state) abort " {{{1
\ . '|^\s*\]\s*%(\{|$)'
\ . '|^\s*}'
let a:state.fold_re_next = ''
let a:state.fold_re_comment = ''
for l:name in [
\ 'comment_pkg',
\ 'preamble',
\ 'cmd_single',
\ 'cmd_single_opt',
\ 'cmd_multi',
\ 'cmd_addplot',
\ 'sections',
\ 'markers',
\ 'comments',
\ 'items',
\ 'envs',
\ 'env_options',
\]
let l:type = get(a:state.fold_types_dict, l:name, {})
if !empty(l:type)
call add(a:state.fold_types_ordered, l:type)
if exists('l:type.re.fold_re')
let a:state.fold_re .= '|' . l:type.re.fold_re
endif
if exists('l:type.re.fold_re_next')
let a:state.fold_re_next .=
\ (empty(a:state.fold_re_next) ? '\v' : '|')
\ . l:type.re.fold_re_next
endif
if empty(l:type) | continue | endif

call add(a:state.fold_types_ordered, l:type)
if exists('l:type.re.fold_re')
let a:state.fold_re .= '|' .. l:type.re.fold_re
endif

if exists('l:type.re.fold_re_next')
let a:state.fold_re_next .=
\ (empty(a:state.fold_re_next) ? '\v' : '|')
\ .. l:type.re.fold_re_next
endif

if exists('l:type.re.fold_re_comment')
let a:state.fold_re_comment .=
\ (empty(a:state.fold_re_comment) ? '\v' : '|')
\ .. l:type.re.fold_re_comment
endif
endfor
endfunction
Expand Down Expand Up @@ -110,6 +116,18 @@ function! vimtex#fold#level(lnum) abort " {{{1
return '='
endif

" Handle comments by considering the syntax
if vimtex#syntax#in_comment(a:lnum, 1)
\ && l:line !~# b:vimtex.fold_re_comment
if l:line =~# '^\s*\\begin\s*{comment}'
return 'a1'
elseif l:line =~# '^\s*\\end\s*{comment}'
return 's1'
else
return '='
endif
endif

for l:type in b:vimtex.fold_types_ordered
let l:value = l:type.level(l:line, a:lnum)
if !empty(l:value) | return l:value | endif
Expand Down
39 changes: 0 additions & 39 deletions autoload/vimtex/fold/comment_pkg.vim

This file was deleted.

43 changes: 0 additions & 43 deletions autoload/vimtex/fold/comments.vim

This file was deleted.

3 changes: 2 additions & 1 deletion autoload/vimtex/fold/markers.vim
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ function! s:folder.init() abort dict " {{{1
\ ['^.*\ze\s*%', ''],
\]

let self.re.fold_re = escape(self.open . '|' . self.close, '{}%+*.')
let self.re.fold_re = escape(self.open .. '|' .. self.close, '{}%+*.')
let self.re.fold_re_comment = escape(self.open .. '|' .. self.close, '{}%+*.')

return self
endfunction
Expand Down
3 changes: 2 additions & 1 deletion autoload/vimtex/fold/sections.vim
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ function! s:folder.init() abort dict " {{{1
let self.re.secpat1 = self.re.sections . '\*?\s*\{\zs.*'
let self.re.secpat2 = self.re.sections . '\*?\s*\[\zs.*'

let self.re.fold_re = '\\%(' . join(self.parts + self.sections, '|') . ')'
let self.re.fold_re = '\\%(' .. join(self.parts + self.sections, '|') .. ')'
let self.re.fold_re_comment = '^\s*\% Fake'

return self
endfunction
Expand Down
2 changes: 0 additions & 2 deletions autoload/vimtex/options.vim
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ function! vimtex#options#init() abort " {{{1
call s:init_option('vimtex_fold_types_defaults', {
\ 'preamble' : {},
\ 'items' : {},
\ 'comment_pkg' : {},
\ 'comments' : { 'enabled' : 0 },
\ 'envs' : {
\ 'blacklist' : [],
\ 'whitelist' : [],
Expand Down
5 changes: 0 additions & 5 deletions doc/vimtex.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1846,11 +1846,6 @@ OPTIONS *vimtex-options*
corresponding "real" sections. The fold title is the
provided title with the `Fake...` part prepended.

<comment_pkg> Fold `\begin{comments} ... \end{comments}` and disable
folding inside the environment.

<comments> Fold multiline comments. This is disabled by default.

<markers> Fold on vim-style markers inside comments, that is,
pairs of e.g. `{{{` and `}}}` (the default markers).
|regex| patterns for the opening and closing markers
Expand Down
16 changes: 16 additions & 0 deletions test/test-folding/test-comment.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
\documentclass{article}
\usepackage{comment}

\begin{document}

\section{test 1}

\begin{comment}
f(x) = 1
\begin{equation}
f(x) = 1
\end{equation}
\label{sec:test1}
\end{comment}

\end{document}
23 changes: 23 additions & 0 deletions test/test-folding/test-comment.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
set nocompatible
let &rtp = '../..,' . &rtp
filetype plugin on
syntax enable

set fillchars=fold:\
set number
set foldcolumn=4

nnoremap q :qall!<cr>

call vimtex#log#set_silent()

let g:vimtex_fold_enabled = 1

silent edit test-comment.tex

if empty($INMAKE) | finish | endif

call assert_equal(2, foldlevel(9))
call assert_equal(2, foldlevel(11))

call vimtex#test#finished()
3 changes: 1 addition & 2 deletions test/test-folding/test-other.vim
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ nnoremap q :qall!<cr>
call vimtex#log#set_silent()

let g:vimtex_fold_enabled = 1
let g:vimtex_fold_types = {'comments' : {'enabled': 1}}

silent edit test-other.tex

if empty($INMAKE) | finish | endif


call assert_equal(1, foldlevel(1))
call assert_equal(2, foldlevel(2))
call assert_equal(1, foldlevel(2))
call assert_equal(2, foldlevel(34))
call assert_equal(2, foldlevel(48))
call assert_equal(3, foldlevel(128))
Expand Down