1

I’m writing a syntax file for Bats. I started my syntax file by loading the bash syntax:

let g:is_bash=1
runtime! syntax/sh.vim
unlet b:current_syntax

I then defined the Bats custom keywords:

syntax match batsTestKeyword "\v\@test"
syntax keyword batsFunction load setup teardown run skip assert_success assert_failure assert_output

Finally, I’m trying to create a region that contains both the Bash syntax and the batsFunction keywords:

syntax region batsTestBlock start='{' end='}' contains=@bash, batsFunction

However when looking at a test file only the Bash syntax is highlighted and not my custom syntax.

Any idea why my custom syntax is not included in the region?

Note: The keyword are linked as follows:

highlight default link batsTestKeyword  Identifier
highlight default link batsFunction Function
4
  • That's very odd. I copied your work, and it seems to work just fine for me. Here's a screenshot: i.andrewradev.com/ee29f978d71e940f6ceaa81c6dfd6191.png. Is it possible that the filetype is not detected correctly? Have you checked that :echo &ft displays "bats"? Commented Mar 30, 2017 at 20:32
  • So this is not exactly what I'm expecting: I also want the bash syntax (expected_version=$(cat $SNAP_ROOT/VERSION)) to be highlighted as it is in a Bash file. :echo &ft correctly display bats. Commented Mar 31, 2017 at 18:03
  • Hi! Did you ever get anywhere with this? I'm getting sick of Syntastic barfing all over my BATS files. Leave a link to your project / code, if possible, for future visitors — thanks! Commented May 10, 2017 at 0:26
  • @ELLIOTTCABLE Yes! Check out the plugin repository here. Commented May 10, 2017 at 8:47

1 Answer 1

2

From what I understand from your comments, the problem is that the @test { } group to include bash syntax. One way I found to achieve this is by using :syntax include (:help :syn-include):

let g:is_bash=1

syntax include @BASH syntax/sh.vim
unlet b:current_syntax

runtime! syntax/sh.vim
unlet b:current_syntax

syntax match batsTestKeyword "\v\@test"
syntax keyword batsFunction load setup teardown run skip assert_success assert_failure assert_output

syntax region batsTestBlock start='{' end='}' contains=@BASH,batsFunction

highlight default link batsTestKeyword Identifier
highlight default link batsFunction Function

At the top, we include the bash syntax twice, once as the @BASH group, and once in the global namespace. The contained group can be added to the contains= list. You'd done that, actually, I assume from looking at some example, but you hadn't included the bash syntax group as that name.

I admit that syntax highlighting is not my strong suite, so there may be a better way of doing this, but this seems to work, in my experiments.

1
  • So for some reason that didn't work either, so I ended up saying that the bats keywords are contained in the shExpr syntax group from bash/sh. (Example) Commented Apr 16, 2017 at 15:55

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.