Skip to content

Commit fa0ffc8

Browse files
authored
Merge pull request rodjek#115 from lelutin/detect_epp
Detect and highlight epp syntax
2 parents 5f67926 + 74b4e5e commit fa0ffc8

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

ftdetect/puppet.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
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 syntax 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)