Skip to content

Commit 9b91baa

Browse files
committed
make syntax highlighting of epp simple but correct
This work is highly based off of Tim Pope's (and contributors') work on the eruby syntax highlighting. The eruby code automatically detects the underlying file type in order to colorize the template content correctly for multiple types of contents. I didn't quite know how to implement this intelligently for .epp files since most of the times we won't have a good clue what type of file the contents represent. As a first step, I've chosed to use "sh" for the underlying file type since it's probably going to be the most common, and it is generic enough that it won't look too bad for most config files. We can eventually make this better. But simple steps first.
1 parent 6366b8b commit 9b91baa

File tree

4 files changed

+141
-1
lines changed

4 files changed

+141
-1
lines changed

ftdetect/puppet.vim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
au! BufRead,BufNewFile *.pp,*.epp setfiletype puppet
1+
au! BufRead,BufNewFile *.pp setfiletype puppet
2+
au! BufRead,BufNewFile *.epp setfiletype embeddedpuppet
23
au! BufRead,BufNewFile Puppetfile setfiletype ruby

ftplugin/embeddedpuppet.vim

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
" Vim filetype plugin
2+
" Language: embedded puppet
3+
" Maintainer: Gabriel Filion <gabster@lelutin.ca>
4+
" URL: https://github.com/rodjek/vim-puppet
5+
" Last Change: 2019-09-01
6+
7+
" Only do this when not done yet for this buffer
8+
if exists("b:did_ftplugin")
9+
finish
10+
endif
11+
12+
let s:save_cpo = &cpo
13+
set cpo-=C
14+
15+
" Define some defaults in case the included ftplugins don't set them.
16+
let s:undo_ftplugin = ""
17+
let s:browsefilter = "All Files (*.*)\t*.*\n"
18+
let s:match_words = ""
19+
20+
runtime! ftplugin/sh.vim
21+
unlet! b:did_ftplugin
22+
23+
" Override our defaults if these were set by an included ftplugin.
24+
if exists("b:undo_ftplugin")
25+
let s:undo_ftplugin = b:undo_ftplugin
26+
unlet b:undo_ftplugin
27+
endif
28+
if exists("b:browsefilter")
29+
let s:browsefilter = b:browsefilter
30+
unlet b:browsefilter
31+
endif
32+
if exists("b:match_words")
33+
let s:match_words = b:match_words
34+
unlet b:match_words
35+
endif
36+
37+
let s:include = &l:include
38+
let s:path = &l:path
39+
let s:suffixesadd = &l:suffixesadd
40+
41+
runtime! ftplugin/puppet.vim
42+
let b:did_ftplugin = 1
43+
44+
" Combine the new set of values with those previously included.
45+
if exists("b:undo_ftplugin")
46+
let s:undo_ftplugin = b:undo_ftplugin . " | " . s:undo_ftplugin
47+
endif
48+
if exists ("b:browsefilter")
49+
let s:browsefilter = substitute(b:browsefilter,'\cAll Files (\*\.\*)\t\*\.\*\n','','') . s:browsefilter
50+
endif
51+
if exists("b:match_words")
52+
let s:match_words = b:match_words . ',' . s:match_words
53+
endif
54+
55+
if len(s:include)
56+
let &l:include = s:include
57+
endif
58+
let &l:path = s:path . (s:path =~# ',$\|^$' ? '' : ',') . &l:path
59+
let &l:suffixesadd = s:suffixesadd . (s:suffixesadd =~# ',$\|^$' ? '' : ',') . &l:suffixesadd
60+
unlet s:include s:path s:suffixesadd
61+
62+
" Load the combined list of match_words for matchit.vim
63+
if exists("loaded_matchit")
64+
let b:match_words = s:match_words
65+
endif
66+
67+
" TODO: comments=
68+
setlocal commentstring=<%#%s%>
69+
70+
let b:undo_ftplugin = "setl cms< "
71+
\ " | unlet! b:browsefilter b:match_words | " . s:undo_ftplugin
72+
73+
let &cpo = s:save_cpo
74+
unlet s:save_cpo
75+

syntax/embeddedpuppet.vim

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
" Vim filetype plugin
2+
" Language: embedded puppet
3+
" Maintainer: Gabriel Filion <gabster@lelutin.ca>
4+
" URL: https://github.com/rodjek/vim-puppet
5+
" Last Change: 2019-09-01
6+
7+
" quit when a syntax file was already loaded {{{1
8+
if exists("b:current_syntax")
9+
finish
10+
endif
11+
12+
runtime! syntax/sh.vim
13+
unlet! b:current_syntax
14+
15+
syn include @puppetTop syntax/puppet.vim
16+
17+
syn cluster ePuppetRegions contains=ePuppetBlock,ePuppetExpression,ePuppetComment
18+
19+
syn region ePuppetBlock matchgroup=ePuppetDelimiter start="<%%\@!-\=" end="[=-]\=%\@<!%>" contains=@puppetTop containedin=ALLBUT,@ePuppetRegions keepend
20+
syn region ePuppetExpression matchgroup=ePuppetDelimiter start="<%=\{1,4}" end="[=-]\=%\@<!%>" contains=@puppetTop containedin=ALLBUT,@ePuppetRegions keepend
21+
syn region ePuppetComment matchgroup=ePuppetDelimiter start="<%-\=#" end="[=-]\=%\@<!%>" contains=puppetTodo,@Spell containedin=ALLBUT,@ePuppetRegions keepend
22+
23+
" Define the default highlighting.
24+
25+
hi def link ePuppetDelimiter PreProc
26+
hi def link ePuppetComment Comment
27+
28+
let b:current_syntax = "embeddedpuppet"
29+

test/syntax/embeddedpuppet.vader

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Given embeddedpuppet (template with litteral content puppet tags):
2+
# Short litteral comment
3+
<% if $variable == '<%%somevalue%%>' { -%>
4+
MYVAR=<%= $variable %>
5+
<%- if $more_variable {
6+
# This is a puppet comment -%>
7+
MOREVAR=true
8+
<%- } %>
9+
<%# epp comment with TODO mark %>
10+
11+
Execute (litteral content syntax must be correct):
12+
AssertEqual 'shComment', SyntaxOf('litteral comment')
13+
AssertEqual 'shVariable', SyntaxOf('MYVAR')
14+
15+
Execute (epp delimiter syntax must be correct):
16+
-- The error messages here are super confusing since all of the assertions use
17+
-- the same type. This is a shortcoming of the current vader output. To bypass
18+
-- this, let's define our own message.
19+
AssertEqual 'ePuppetDelimiter', SyntaxOf('<%'), "syntax of <% should be 'ePuppetDelimiter', got '".SyntaxOf('<%')."'"
20+
AssertEqual 'ePuppetDelimiter', SyntaxOf('-%>'), "syntax of -%> should be 'ePuppetDelimiter', got '".SyntaxOf('-%>')."'"
21+
AssertEqual 'ePuppetDelimiter', SyntaxOf('<%-'), "syntax of <%- should be 'ePuppetDelimiter', got '".SyntaxOf('<%-')."'"
22+
AssertEqual 'ePuppetComment', SyntaxOf('epp comment')
23+
-- We're testing for a *puppet* syntax here but that's because it's contained
24+
-- in the ePuppetComment syntax region
25+
AssertEqual 'puppetTodo', SyntaxOf('TODO')
26+
27+
Execute (puppet syntax must be correct):
28+
AssertEqual 'puppetControl', SyntaxOf('if')
29+
AssertEqual 'puppetVariable', SyntaxOf('$variable')
30+
-- Again, we're testing multiple times for the same syntaxID. Let's define our
31+
-- message.
32+
AssertEqual 'puppetBrace', SyntaxOf('{'), "for { in first puppet block, expected 'puppetBrace', got '".SyntaxOf('{')."'"
33+
AssertEqual 'puppetBrace', SyntaxOf('{', 2), "for { in second puppet block, expected 'puppetBrace', got '".SyntaxOf('{', 2)."'"
34+
AssertEqual 'puppetBrace', SyntaxOf('}'), "for } in third puppet block, expected 'puppetBrace', got '".SyntaxOf('}')."'"
35+
AssertEqual 'puppetComment', SyntaxOf('puppet comment')

0 commit comments

Comments
 (0)