Skip to content

Commit

Permalink
chore: refactor to prepare for new feature
Browse files Browse the repository at this point in the history
  • Loading branch information
lervag committed Jun 12, 2023
1 parent d98d254 commit 52d903b
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 96 deletions.
2 changes: 1 addition & 1 deletion autoload/vimtex/compiler.vim
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ function! vimtex#compiler#clean(full) abort " {{{1

call b:vimtex.compiler.clean(a:full)
sleep 100m
call b:vimtex.compiler.remove_out_dir()
call b:vimtex.compiler.remove_dirs()
call vimtex#log#info('Compiler clean finished' . (a:full ? ' (full)' : ''))


Expand Down
175 changes: 80 additions & 95 deletions autoload/vimtex/compiler/_template.vim
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,21 @@ function! s:compiler.new(options) abort dict " {{{1

call l:compiler.__check_requirements()

call s:out_dir_materialize(l:compiler)
call vimtex#util#materialize_property(l:compiler, 'out_dir')
call l:compiler.__init()
call s:out_dir_respect_envvar(l:compiler)

" $VIMTEX_OUTPUT_DIRECTORY overrides configured compiler.out_dir
if !empty($VIMTEX_OUTPUT_DIRECTORY)
if !empty(l:compiler.out_dir)
\ && (l:compiler.out_dir !=# $VIMTEX_OUTPUT_DIRECTORY)
call vimtex#log#warning(
\ 'Setting VIMTEX_OUTPUT_DIRECTORY overrides out_dir!',
\ 'Changed out_dir from: ' . l:compiler.out_dir,
\ 'Changed out_dir to: ' . $VIMTEX_OUTPUT_DIRECTORY)
endif

let l:compiler.out_dir = $VIMTEX_OUTPUT_DIRECTORY
endif

" Remove init methods
unlet l:compiler.new
Expand Down Expand Up @@ -98,11 +110,72 @@ endfunction

" }}}1

function! s:compiler._create_build_dir(path) abort dict " {{{1
" Create build dir "path" if it does not exist
" Note: This may need to create a hierarchical structure!
if empty(a:path) | return | endif

if has_key(self.state, 'get_sources')
let l:dirs = self.state.get_sources()
call filter(map(
\ l:dirs, "fnamemodify(v:val, ':h')"),
\ {_, x -> x !=# '.'})
call filter(l:dirs, {_, x -> stridx(x, '../') != 0})
else
let l:dirs = glob(self.state.root . '/**/*.tex', v:false, v:true)
call map(l:dirs, "fnamemodify(v:val, ':h')")
call map(l:dirs, 'strpart(v:val, strlen(self.state.root) + 1)')
endif
call uniq(sort(filter(l:dirs, '!empty(v:val)')))

call map(l:dirs, {_, x ->
\ (vimtex#paths#is_abs(a:path) ? '' : self.state.root . '/')
\ . a:path . '/' . x})
call filter(l:dirs, '!isdirectory(v:val)')
if empty(l:dirs) | return | endif

" Create the non-existing directories
call vimtex#log#warning(["Creating directorie(s):"]
\ + map(copy(l:dirs), {_, x -> '* ' . x}))

for l:dir in l:dirs
call mkdir(l:dir, 'p')
endfor
endfunction

" }}}1
function! s:compiler._remove_dir(path) abort dict " {{{1
if empty(a:path) | return | endif

let l:out_dir = vimtex#paths#is_abs(a:path)
\ ? a:path
\ : self.state.root . '/' . a:path
if !isdirectory(l:out_dir) | return | endif

let l:tree = glob(l:out_dir . '/**/*', 0, 1)
let l:files = filter(copy(l:tree), 'filereadable(v:val)')

if empty(l:files)
for l:dir in sort(l:tree) + [l:out_dir]
call delete(l:dir, 'd')
endfor
endif
endfunction

" }}}1

function! s:compiler.create_dirs() abort dict " {{{1
call self._create_build_dir(self.out_dir)
endfunction

" }}}1
function! s:compiler.remove_dirs() abort dict " {{{1
call self._remove_dir(self.out_dir)
endfunction

" }}}1

function! s:compiler.get_file(ext) abort dict " {{{1
" Find file by considering possible output directories:
" * VIMTEX_OUTPUT_DIRECTORY
" * Compiler settings (e.g. g:vimtex_compiler_latexmk.out_dir)
" * Fallback to the main root directory
for l:root in [
\ $VIMTEX_OUTPUT_DIRECTORY,
\ self.out_dir,
Expand Down Expand Up @@ -141,7 +214,7 @@ endfunction
function! s:compiler.start(...) abort dict " {{{1
if self.is_running() | return | endif

call self.create_out_dir()
call self.create_dirs()

" Initialize output file
call writefile([], self.output, 'a')
Expand Down Expand Up @@ -209,62 +282,6 @@ endfunction

" }}}1

function! s:compiler.create_out_dir() abort dict " {{{1
" Create build dir if it does not exist
" Note: This may need to create a hierarchical structure!
if empty(self.out_dir) | return | endif

if has_key(self.state, 'get_sources')
let l:dirs = self.state.get_sources()
call filter(map(
\ l:dirs, "fnamemodify(v:val, ':h')"),
\ {_, x -> x !=# '.'})
call filter(l:dirs, {_, x -> stridx(x, '../') != 0})
else
let l:dirs = glob(self.state.root . '/**/*.tex', v:false, v:true)
call map(l:dirs, "fnamemodify(v:val, ':h')")
call map(l:dirs, 'strpart(v:val, strlen(self.state.root) + 1)')
endif
call uniq(sort(filter(l:dirs, '!empty(v:val)')))

call map(l:dirs, {_, x ->
\ (vimtex#paths#is_abs(self.out_dir) ? '' : self.state.root . '/')
\ . self.out_dir . '/' . x})
call filter(l:dirs, '!isdirectory(v:val)')
if empty(l:dirs) | return | endif

" Create the non-existing directories
call vimtex#log#warning(["Creating out_dir directorie(s):"]
\ + map(copy(l:dirs), {_, x -> '* ' . x}))

for l:dir in l:dirs
call mkdir(l:dir, 'p')
endfor
endfunction

" }}}1
function! s:compiler.remove_out_dir() abort dict " {{{1
" Remove auxilliary output directories (only if they are empty)
if empty(self.out_dir) | return | endif

if vimtex#paths#is_abs(self.out_dir)
let l:out_dir = self.out_dir
else
let l:out_dir = self.state.root . '/' . self.out_dir
endif

let l:tree = glob(l:out_dir . '/**/*', 0, 1)
let l:files = filter(copy(l:tree), 'filereadable(v:val)')

if empty(l:files)
for l:dir in sort(l:tree) + [l:out_dir]
call delete(l:dir, 'd')
endfor
endif
endfunction

" }}}1


let s:compiler_jobs = {}
function! s:compiler_jobs.exec(cmd) abort dict " {{{1
Expand Down Expand Up @@ -459,38 +476,6 @@ endfunction
" }}}1


function! s:out_dir_materialize(compiler) abort " {{{1
if type(a:compiler.out_dir) != v:t_func | return | endif

try
let a:compiler.out_dir = a:compiler.out_dir()
catch
call vimtex#log#error(
\ 'Could not expand out_dir function!',
\ v:exception)
let a:compiler.out_dir = ''
endtry
endfunction

" }}}1
function! s:out_dir_respect_envvar(compiler) abort " {{{1
" Specifying the out_dir by environment variable should override the
" current value.
if empty($VIMTEX_OUTPUT_DIRECTORY) | return | endif

if !empty(a:compiler.out_dir)
\ && (a:compiler.out_dir !=# $VIMTEX_OUTPUT_DIRECTORY)
call vimtex#log#warning(
\ 'Setting VIMTEX_OUTPUT_DIRECTORY overrides out_dir!',
\ 'Changed out_dir from: ' . a:compiler.out_dir,
\ 'Changed out_dir to: ' . $VIMTEX_OUTPUT_DIRECTORY)
endif

let a:compiler.out_dir = $VIMTEX_OUTPUT_DIRECTORY
endfunction

" }}}1

function! s:check_callback(line) abort " {{{1
let l:status = get(s:callbacks, substitute(a:line, '\r', '', ''))
if l:status <= 0 | return | endif
Expand Down
14 changes: 14 additions & 0 deletions autoload/vimtex/util.vim
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,20 @@ function! vimtex#util#extend_recursive(dict1, dict2, ...) abort " {{{1
return a:dict1
endfunction

" }}}1
function! vimtex#util#materialize_property(dict, name) abort " {{{1
if type(get(a:dict, a:name)) != v:t_func | return | endif

try
let a:dict[a:name] = a:dict[a:name]()
catch
call vimtex#log#error(
\ 'Could not materialize property: ' . a:name,
\ v:exception)
let a:dict[a:name] = ''
endtry
endfunction

" }}}1
function! vimtex#util#shellescape(cmd) abort " {{{1
"
Expand Down

0 comments on commit 52d903b

Please sign in to comment.