From 281b1bd65411ebe19fb0c92e3d1aa5ec38c85cde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Yngve=20Lerv=C3=A5g?= Date: Wed, 7 Feb 2024 23:08:06 +0100 Subject: [PATCH] feat(toc): support for comment package refer: #2882 --- autoload/vimtex/parser/toc.vim | 1 + autoload/vimtex/parser/toc/comment.vim | 31 ++++++++++++++++++++++++++ test/test-toc/test-comment.tex | 21 +++++++++++++++++ test/test-toc/test-comment.vim | 22 ++++++++++++++++++ 4 files changed, 75 insertions(+) create mode 100644 autoload/vimtex/parser/toc/comment.vim create mode 100644 test/test-toc/test-comment.tex create mode 100644 test/test-toc/test-comment.vim diff --git a/autoload/vimtex/parser/toc.vim b/autoload/vimtex/parser/toc.vim index c4894f5b0f..e9bf7b90a5 100644 --- a/autoload/vimtex/parser/toc.vim +++ b/autoload/vimtex/parser/toc.vim @@ -204,6 +204,7 @@ function! vimtex#parser#toc#get_matchers() abort " {{{1 let l:re .= '|' . l:matcher.prefilter_re endif endfor + let l:cmds = vimtex#util#uniq_unsorted(l:cmds) let l:matchers.prefilter = '\v\\%(' . join(l:cmds, '|') . ')' . l:re return l:matchers diff --git a/autoload/vimtex/parser/toc/comment.vim b/autoload/vimtex/parser/toc/comment.vim new file mode 100644 index 0000000000..fed3416d41 --- /dev/null +++ b/autoload/vimtex/parser/toc/comment.vim @@ -0,0 +1,31 @@ +" VimTeX - LaTeX plugin for Vim +" +" Maintainer: Karl Yngve LervÄg +" Email: karl.yngve@gmail.com +" + +function! vimtex#parser#toc#comment#new() abort " {{{1 + return s:matcher +endfunction + +" }}}1 + +let s:matcher = { + \ 'in_preamble' : 1, + \ 'prefilter_cmds': ['begin'], + \ 're': '^\s*\\begin{comment}', + \ 're_end': '^\s*\\end{comment}', + \} +function! s:matcher.get_entry(context) abort dict " {{{1 + let a:context.continue = 'comment' + return {} +endfunction + +" }}}1 +function! s:matcher.continue(context) abort dict " {{{1 + if a:context.line =~# self.re_end + unlet! a:context.continue + endif +endfunction + +" }}}1 diff --git a/test/test-toc/test-comment.tex b/test/test-toc/test-comment.tex new file mode 100644 index 0000000000..f69eb673d4 --- /dev/null +++ b/test/test-toc/test-comment.tex @@ -0,0 +1,21 @@ +\documentclass{article} +\usepackage{comment} + +\begin{document} + +\section{Real section} + +\begin{comment} +\section{Commented section} +\subsection{Commented subsection} +\subsubsection{Commented subsubsection} +\end{comment} + +\begin{comment} +\begin{figure} \label{fig:commented} +\end{figure} +\end{comment} + +\section{Next real section} + +\end{document} diff --git a/test/test-toc/test-comment.vim b/test/test-toc/test-comment.vim new file mode 100644 index 0000000000..0aede153f2 --- /dev/null +++ b/test/test-toc/test-comment.vim @@ -0,0 +1,22 @@ +set nocompatible +let &rtp = '../..,' . &rtp +filetype plugin on + +set nomore + +nnoremap q :qall! + +silent edit test-comment.tex + +if empty($INMAKE) | finish | endif + +let s:toc = vimtex#toc#get_entries() +call assert_equal(len(s:toc), 3) + +" let s:i = 0 +" for s:x in s:toc +" echo s:i '--' s:x.title "\n" +" let s:i += 1 +" endfor + +call vimtex#test#finished()