Skip to content

Commit

Permalink
feat(syntax): add wip support for robust-externalize
Browse files Browse the repository at this point in the history
refer: #2977
  • Loading branch information
lervag committed Jul 10, 2024
1 parent 2dc2a54 commit fdb5618
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
67 changes: 67 additions & 0 deletions autoload/vimtex/syntax/p/robust_externalize.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
" VimTeX - LaTeX plugin for Vim
"
" Maintainer: Karl Yngve Lervåg
" Email: karl.yngve@gmail.com
"

function! vimtex#syntax#p#robust_externalize#load(cfg) abort " {{{1
" Match environment boundaries
syntax match texRobExtEnvBgn contained '\\begin{\%(RobExt\)\?CacheMeCode\|CacheMe}'
\ nextgroup=texRobExtEnvOpt,texRobExtEnvArg skipwhite skipnl
\ contains=texCmdEnv
call vimtex#syntax#core#new_opt('texRobExtEnvOpt', {'next': 'texRobExtEnvArg'})
call vimtex#syntax#core#new_arg('texRobExtEnvArg', {'contains': 'texRobExtEnvArg'})

" Match generic environments
call vimtex#syntax#core#new_env({
\ 'name': '\%(RobExt\)\?CacheMeCode\|CacheMe',
\ 'region': 'texRobExtZone',
\ 'contains': 'texCmdEnv,texRobExtEnvBgn',
\})
call vimtex#syntax#core#new_env({
\ 'name': 'PlaceholderPathFromCode',
\ 'region': 'texRobExtZone',
\ 'contains': 'texCmdEnv,texRobExtEnvBgn',
\})
call vimtex#syntax#core#new_env({
\ 'name': 'SetPlaceholderCode\*\?',
\ 'region': 'texRobExtZone',
\ 'contains': 'texCmdEnv,texRobExtEnvBgn',
\})

" Add nested syntax support for supported languages
for [l:preset, l:target] in [
\ ['c', 'c'],
\ ['bash', 'bash'],
\ ['python', 'python'],
\ ['my python', 'python'],
\]
let l:cluster = vimtex#syntax#nested#include(l:target)

let l:name = toupper(l:target[0]) . l:target[1:]
let l:grp_env = 'texRobExtZone' . l:name
let l:options = 'keepend'
let l:contains = 'contains=texCmdEnv,texRobExtEnvBgn'

if empty(l:cluster)
execute 'highlight def link' l:grp_env 'texRobExtZone'
else
let l:contains .= ',' . l:cluster
endif

" Match normal robext environments
execute 'syntax region' l:grp_env
\ 'start="\\begin{\z(\%(RobExt\)\?CacheMeCode\|CacheMe\)}\_s*{' . l:preset . '[ ,}]"'
\ 'end="\\end{\z1}"'
\ l:options
\ l:contains
endfor

" Specify default highlight groups
highlight def link texRobExtEnvArg texSymbol
highlight def link texRobExtEnvArgOpt texOpt
highlight def link texRobExtEnvOpt texOpt
highlight def link texRobExtZone texZone
endfunction

" }}}1
46 changes: 46 additions & 0 deletions test/test-syntax/test-robust-externalize.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
\documentclass{article}
\usepackage{robust-externalize}

\begin{document}

\begin{CacheMeCode}{c}
int main() {
return 1;
}
\end{CacheMeCode}

\begin{CacheMeCode}{bash, tikz terminal}
echo 'test.dat'
exit 0
\end{CacheMeCode}
\begin{figure}
\begin{CacheMeCode}{python matplotlib, add to preamble={\def\hello#1{Hello #1!}}}
def f(p: string):
return [x + p for x in ('a', 'b')]
\end{CacheMeCode}
\end{figure}

\begin{RobExtCacheMeCode}{my python matplotlib,
add to preamble={\def\hello#1{Hello #1!}}
}
def f(p: string):
return [x + p for x in ('a', 'b')]
\end{RobExtCacheMeCode}

\begin{CacheMe}{tikzpicture}[scale=0.6]
\draw[gray, thick] (-1,2) -- (2,-4);
\draw[gray, thick] (-1,-1) -- (2,2);
\filldraw[black] (0,0) circle (2pt) node[anchor=west]{Intersection point};
\end{CacheMe}

\begin{SetPlaceholderCode*}{__PYTHON_TEMP}
def f(p: string):
return [x + p for x in ('a', 'b')]
\end{SetPlaceholderCode*}

\begin{PlaceholderPathFromCode}[.py]{__PYTHON_TEMP}
def f(p: string):
return [x + p for x in ('a', 'b')]
\end{PlaceholderPathFromCode}

\end{document}
7 changes: 7 additions & 0 deletions test/test-syntax/test-robust-externalize.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
source common.vim

Edit test-robust-externalize.tex

if empty($INMAKE) | finish | endif

call vimtex#test#finished()

0 comments on commit fdb5618

Please sign in to comment.