Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending vim Fortran Syntax file for pFUnit testing framework

I want to use the pFUnit Fortran Unit Testing Framework with vim. The tests are usually written in a file with the ending .pf, and they use mostly Fortran 95 syntax, except that they have unittesting specific keywords (like @test and @assertEqual)

What is the easiest way to extend the fortran syntax highlighting in vim to also mark these special words only in files with the .pf extension?

Thanks

Example Unit Test:

@test
subroutine test_to_lower_all_A()
    use pfunit_mod
    use mod_str_tools, only: to_lower
    @assertEqual("aaa", to_lower("AAA"))
end subroutine
like image 325
chw21 Avatar asked Oct 26 '25 22:10

chw21


1 Answers

Okay, here's what I did which seems to solve the issue:

1) Create a file in ~/.vim/ftdetect/pFUnit.vim

" ftdetect/pFUnit.vim
au BufRead,BufNewFile *.pf setfiletype pFUnit

This automatically sets the file type according to the file extension

2) Create a file in ~/.vim/ftplugin/pFUnit.vim

" ftplugin/pFUnit.vim
setlocal iskeyword+=@-@
let fortran_free_form=1
so $VIMRUNTIME/ftplugin/fortran.vim

This says that the @ character is part of a keyword, not a separator. It also sets the fortran_free_form tag and loads the fortran plugin.

3) Create a file in ~/.vim/syntax/pFUnit.vim

" Vim syntax file
" Language: pFUnit

if exists("b:current_syntax")
    finish
endif

syn keyword pFUnitTag @test @before @after @suite
syn keyword pFUnitAssert @assertTrue @assertFalse @assertAny @assertAll @assertNone @assertNotAll
syn keyword pFUnitAssert @assertEqual @assertLessThan @assertLessThanOrEqual @assertGreaterThan @assertGreaterThanOrEqual
syn keyword pFUnitAssert @assertRelativelyEqual
syn keyword pFUnitAssert @assertIsNaN @assertIsFinite @assertExceptionRaised @assertSameShape

hi def link pFUnitTag PreProc
hi def link pFUnitAssert Keyword

so $VIMRUNTIME/syntax/fortran.vim

let b:current_syntax = "pFUnit"

This now both defines the new keywords, and sources the original fortran.vim. It is important to source that file before setting b:current_syntax, because otherwise the fortran.vim script exits immediately.

If anyone has a better solution, feel free to share here. Thanks

like image 59
chw21 Avatar answered Oct 28 '25 21:10

chw21



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!