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

Improve the main-file detection #2558

Closed
wants to merge 2 commits into from
Closed
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
20 changes: 10 additions & 10 deletions autoload/vimtex/state.vim
Original file line number Diff line number Diff line change
Expand Up @@ -475,23 +475,23 @@ endfunction
function! s:file_is_main(file) abort " {{{1
if !filereadable(a:file) | return 0 | endif

let l:preamble = vimtex#parser#preamble(a:file, {
\ 'inclusive' : 1,
\ 'root' : fnamemodify(a:file, ':p:h'),
\})

" Check if a:file is a main file by looking for the \documentclass command,
" but ignore the following:
"
" \documentclass[...]{subfiles}
" \documentclass[...]{standalone}
"
let l:lines = readfile(a:file, 0, 50)
" * \documentclass[...]{subfiles}
" * \documentclass[...]{standalone}
let l:lines = copy(l:preamble)
call filter(l:lines, 'v:val =~# ''^\s*\\documentclass\_\s*[\[{]''')
call filter(l:lines, 'v:val !~# ''{subfiles}''')
call filter(l:lines, 'v:val !~# ''{standalone}''')
if len(l:lines) == 0 | return 0 | endif

" A main file contains `\begin{document}`
let l:lines = vimtex#parser#preamble(a:file, {
\ 'inclusive' : 1,
\ 'root' : fnamemodify(a:file, ':p:h'),
\})
" A main file must also contain `\begin{document}`
let l:lines = copy(l:preamble)
call filter(l:lines, 'v:val =~# ''^\s*\\begin\s*{document}''')
return len(l:lines) > 0
endfunction
Expand Down
21 changes: 2 additions & 19 deletions doc/vimtex.txt
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,8 @@ Recursive search~
and parent directories that

1. includes the present file (directly or indirectly),
2. has a `\documentclass` line near the top, and
3. contains `\begin{document}` (or includes a file that does).
2. has a `\documentclass` line near the top of its parsed content, and
3. contains `\begin{document}` in its parsed content.

As long as a project follows these rules, the recursive search should work.
Notice, though, that the first point is important and is where the recursive
Expand All @@ -463,23 +463,6 @@ Recursive search~

Let's consider a common construct that will fail: >

% preamble.tex
\documentclass{article}
\usepackage{mypackages}
...

% main.tex
\input{preamble}
\begin{document}
Hello world
\end{document}
<
In this case, `main.tex` is _not_ recognized as a valid main file, because
it does not include `\documentclass`. Further, no other main file is found
that includes this file. Thus, VimTeX is not able to properly parse it.

Another example where the algorithm may fail: >

path1/main.tex
path2/chapter.tex
<
Expand Down
3 changes: 3 additions & 0 deletions test/test-get-main/test-included-preamble/main.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
\input{preamble}
\begin{document}
\end{document}
1 change: 1 addition & 0 deletions test/test-get-main/test-included-preamble/preamble.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\documentclass{minimal}
5 changes: 5 additions & 0 deletions test/test-get-main/test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,9 @@ call vimtex#test#main('test-bib-alternate/references.bib', 'test-bib-alternate/m
call vimtex#test#main('test-standalone/a/a.tex', 'test-standalone/main.tex')
call vimtex#test#main('test-standalone/a/a.tex', 'test-standalone/a/a.tex', 1)

" Test included preamble
call vimtex#test#main(
\ './test-included-preamble/preamble.tex',
\ './test-included-preamble/main.tex')

call vimtex#test#finished()