A small CLI to autoformat Gherkin feature files.
In order to install and use the formatter, it is required to have NodeJS runtime environment installed.
In order to build the formatter, follow these steps:
- Clone the repo to your machine
- Go into the repo directory and execute
npm install - When the previou step finishes, execute
npm run build - Install the formatter locally into the folder which exists in
$PATH, e.g. to install into~/.local/binexecutenpm install -g ./ --prefix ~/.local
The last step will create symbolic links to the artifacts of the build step.
After installation one can simply call it as a CLI script
vim-gherkin-formatter my_file.featureOne can even specify a pattern to look for, e.g.
vim-gherkin-formatter *.featureto format multiple files. This will overwrite the unformatted file(s) with their formatted version.
Create a file: ~/.config/nvim/after/ftplugin/cucumber.vim with content
" Only do this when not done yet for this buffer
if exists("b:did_ft_cucumber_formatter")
finish
endif
let b:did_ft_cucumber_formatter = 1
" Gherkin formatter
function s:DoFormatGherkin()
let l:cur_buffer = bufnr('%')
if getbufvar(l:cur_buffer, "&mod")
let l:do_save = confirm("Must save the buffer first.", "&yes\n&no", 1)
if l:do_save == 1
write
else
return
endif
endif
execute "!vim-gherkin-formatter %" | edit
endfunction
augroup GHERKIN
" remove all existing listeners in this group before reattaching them
autocmd!
" Define commands and mappings local to the Gherkin files
autocmd BufEnter *.feature command! GherkinFormat :silent call s:DoFormatGherkin()
autocmd BufLeave *.feature delcommand GherkinFormat
autocmd BufEnter *.feature nnoremap <buffer> <F4> :GherkinFormat<cr>
" autocmd BufWritePost *.feature :GherkinFormat<cr>
augroup ENDAfter this, reload Vim/Neovim so that changes are sourced. The above defines a
:GherkinFormat command and a mapping to it, <F4>, which are available only
when editing the *.feature Gherkin files.