From 5ae09b1226f2ad24348b8231da64dc30891a5bd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Yngve=20Lerv=C3=A5g?= Date: Sun, 11 Feb 2024 21:28:05 +0100 Subject: [PATCH] feat(fold)!: handle comment specially This removes folding of multiline comments. I may add it back later if requested by users. refer: #2882 --- autoload/vimtex/fold.vim | 42 +++++++++++++++++++-------- autoload/vimtex/fold/comment_pkg.vim | 39 ------------------------- autoload/vimtex/fold/comments.vim | 43 ---------------------------- autoload/vimtex/fold/markers.vim | 3 +- autoload/vimtex/fold/sections.vim | 3 +- autoload/vimtex/options.vim | 2 -- doc/vimtex.txt | 5 ---- test/test-folding/test-comment.tex | 16 +++++++++++ test/test-folding/test-comment.vim | 23 +++++++++++++++ test/test-folding/test-other.vim | 3 +- 10 files changed, 74 insertions(+), 105 deletions(-) delete mode 100644 autoload/vimtex/fold/comment_pkg.vim delete mode 100644 autoload/vimtex/fold/comments.vim create mode 100644 test/test-folding/test-comment.tex create mode 100644 test/test-folding/test-comment.vim diff --git a/autoload/vimtex/fold.vim b/autoload/vimtex/fold.vim index eefc00cbe0..cebe210982 100644 --- a/autoload/vimtex/fold.vim +++ b/autoload/vimtex/fold.vim @@ -54,8 +54,8 @@ 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', @@ -63,22 +63,28 @@ function! vimtex#fold#init_state(state) abort " {{{1 \ '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 @@ -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 diff --git a/autoload/vimtex/fold/comment_pkg.vim b/autoload/vimtex/fold/comment_pkg.vim deleted file mode 100644 index 3031655eea..0000000000 --- a/autoload/vimtex/fold/comment_pkg.vim +++ /dev/null @@ -1,39 +0,0 @@ -" VimTeX - LaTeX plugin for Vim -" -" Maintainer: Karl Yngve Lervåg -" Email: karl.yngve@gmail.com -" - -function! vimtex#fold#comment_pkg#new(config) abort " {{{1 - return extend(deepcopy(s:folder), a:config) -endfunction - -" }}}1 - - -let s:folder = { - \ 'name' : 'comment_pkg', - \ 're' : { - \ 'start' : '^\s*\\begin\s*{comment}', - \ 'end' : '^\s*\\end\s*{comment}', - \ }, - \ 'opened' : v:false, - \} -function! s:folder.level(line, lnum) abort dict " {{{1 - if a:line =~# self.re.start - let self.opened = v:true - return 'a1' - elseif a:line =~# self.re.end - let self.opened = v:false - return 's1' - elseif self.opened - return '=' - endif -endfunction - -" }}}1 -function! s:folder.text(line, level) abort dict " {{{1 - return a:line -endfunction - -" }}}1 diff --git a/autoload/vimtex/fold/comments.vim b/autoload/vimtex/fold/comments.vim deleted file mode 100644 index ee03e083ef..0000000000 --- a/autoload/vimtex/fold/comments.vim +++ /dev/null @@ -1,43 +0,0 @@ -" VimTeX - LaTeX plugin for Vim -" -" Maintainer: Karl Yngve Lervåg -" Email: karl.yngve@gmail.com -" - -function! vimtex#fold#comments#new(config) abort " {{{1 - return extend(deepcopy(s:folder), a:config) -endfunction - -" }}}1 - - -let s:folder = { - \ 'name' : 'comments', - \ 're' : {'start' : '^\s*%'}, - \ 'opened' : 0, - \} -function! s:folder.level(line, lnum) abort dict " {{{1 - if exists('b:vimtex.fold_types_dict.markers.opened') - \ && b:vimtex.fold_types_dict.markers.opened | return | endif - - if a:line =~# self.re.start - let l:next = getline(a:lnum-1) !~# self.re.start - let l:prev = getline(a:lnum+1) !~# self.re.start - if l:next && !l:prev - let self.opened = 1 - return 'a1' - elseif l:prev && !l:next - let self.opened = 0 - return 's1' - endif - endif -endfunction - -" }}}1 -function! s:folder.text(line, level) abort dict " {{{1 - let l:lines = map(getline(v:foldstart, v:foldend), - \ {_, x -> matchstr(x, '%\s*\zs.*\ze\s*')}) - return matchstr(a:line, '^.*\s*%') . join(l:lines, ' ') -endfunction - -" }}}1 diff --git a/autoload/vimtex/fold/markers.vim b/autoload/vimtex/fold/markers.vim index ecef7347b4..7faf1f248a 100644 --- a/autoload/vimtex/fold/markers.vim +++ b/autoload/vimtex/fold/markers.vim @@ -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 diff --git a/autoload/vimtex/fold/sections.vim b/autoload/vimtex/fold/sections.vim index c94bf2c946..09e9905ca7 100644 --- a/autoload/vimtex/fold/sections.vim +++ b/autoload/vimtex/fold/sections.vim @@ -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 diff --git a/autoload/vimtex/options.vim b/autoload/vimtex/options.vim index f0030846fb..9a9546c4aa 100644 --- a/autoload/vimtex/options.vim +++ b/autoload/vimtex/options.vim @@ -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' : [], diff --git a/doc/vimtex.txt b/doc/vimtex.txt index 0865422137..15aac7cb69 100644 --- a/doc/vimtex.txt +++ b/doc/vimtex.txt @@ -1846,11 +1846,6 @@ OPTIONS *vimtex-options* corresponding "real" sections. The fold title is the provided title with the `Fake...` part prepended. - Fold `\begin{comments} ... \end{comments}` and disable - folding inside the environment. - - Fold multiline comments. This is disabled by default. - 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 diff --git a/test/test-folding/test-comment.tex b/test/test-folding/test-comment.tex new file mode 100644 index 0000000000..18ed461150 --- /dev/null +++ b/test/test-folding/test-comment.tex @@ -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} diff --git a/test/test-folding/test-comment.vim b/test/test-folding/test-comment.vim new file mode 100644 index 0000000000..bc8d26ac63 --- /dev/null +++ b/test/test-folding/test-comment.vim @@ -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! + +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() diff --git a/test/test-folding/test-other.vim b/test/test-folding/test-other.vim index 98d4abc763..bdebb2623c 100644 --- a/test/test-folding/test-other.vim +++ b/test/test-folding/test-other.vim @@ -11,7 +11,6 @@ nnoremap q :qall! call vimtex#log#set_silent() let g:vimtex_fold_enabled = 1 -let g:vimtex_fold_types = {'comments' : {'enabled': 1}} silent edit test-other.tex @@ -19,7 +18,7 @@ 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))