├── ftdetect └── rego.vim ├── syntax └── rego.vim └── README.md /ftdetect/rego.vim: -------------------------------------------------------------------------------- 1 | autocmd BufRead,BufNewFile *.rego set filetype=rego 2 | 3 | " Use # as a comment prefix 4 | autocmd FileType rego setlocal comments=b:#,fb:- 5 | autocmd FileType rego setlocal commentstring=#\ %s 6 | -------------------------------------------------------------------------------- /syntax/rego.vim: -------------------------------------------------------------------------------- 1 | " Vim syntax file 2 | " Language: Rego (http://github.com/open-policy-agent/opa) 3 | " Maintainers: Torin Sandall 4 | 5 | if version < 600 6 | syntax clear 7 | elseif exists("b:current_syntax") 8 | finish 9 | endif 10 | 11 | syn case match 12 | 13 | " language keywords 14 | syn keyword regoKeyword package import as not with default else some in 15 | 16 | " comments 17 | syn match regoComment "#.*$" contains=regoTodo,@Spell 18 | syn keyword regoTodo FIXME XXX TODO contained 19 | 20 | " data types 21 | syn keyword regoNull null 22 | syn keyword regoBoolean true false 23 | syn match regoNumber "\<\(0[0-7]*\|0[xx]\x\+\|\d\+\)[ll]\=\>" 24 | syn match regoNumber "\(\<\d\+\.\d*\|\.\d\+\)\([ee][-+]\=\d\+\)\=[ffdd]\=" 25 | syn match regoNumber "\<\d\+[ee][-+]\=\d\+[ffdd]\=\>" 26 | syn match regoNumber "\<\d\+\([ee][-+]\=\d\+\)\=[ffdd]\>" 27 | syn region regoString start="\"" skip="\\\"" end="\"" contains=regoStringEscape 28 | syn match regoStringEscape "\\u[0-9a-fA-F]\{4}" contained 29 | syn match regoStringEscape "\\[nrfvb\\\"]" contained 30 | 31 | " rules 32 | syn match regoRuleName "^\(\w\+\)" 33 | syn region regoBody start="{" end="}" transparent 34 | 35 | " operators 36 | syn match regoEquality "=" 37 | syn match regoInequality "[<>!]" 38 | syn match regoArith "[+-/*&|]" 39 | syn match regoBuiltin "\w\+(" nextgroup=regoBuiltinArgs contains=regoBuiltinArgs 40 | syn region regoBuiltinArgs start="(" end=")" contained contains=regoNull,regoBoolean,regoNumber,regoString 41 | 42 | " highlighting 43 | hi link regoKeyword Keyword 44 | hi link regoNull Function 45 | hi link regoBoolean Boolean 46 | hi link regoNumber Number 47 | hi link regoString String 48 | 49 | hi link regoRuleName Function 50 | 51 | hi link regoEquality Keyword 52 | hi link regoInequality Keyword 53 | hi link regoArith Keyword 54 | hi link regoBuiltin Type 55 | 56 | hi link regoComment Comment 57 | hi link regoTodo Todo 58 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vim-rego 2 | 3 | A Vim plugin for the Rego language that includes support for syntax highlighting. 4 | 5 | ## Installation 6 | 7 | The rego-vim plugin can be installed the old fashion way, by copying the files here into your `~/.vim` directory, or 8 | using any of the plugin managers (Vundle, etc). 9 | 10 | ### Vundle Installation 11 | 12 | In your `.vimrc` file add the line `Plugin 'tsandall/vim-rego'`, like so: 13 | 14 | ```viml 15 | call vundle#begin() 16 | Plugin 'VundleVim/Vundle.vim' 17 | " ... other plugins here ... 18 | 19 | Plugin 'tsandall/vim-rego' 20 | 21 | " ... more plugins ... 22 | " All of your Plugins must be added before the following line 23 | call vundle#end() " required 24 | ``` 25 | 26 | ## Formatting 27 | 28 | ### vim-autoformat 29 | 30 | You can configure Vim to automatically format your Rego policies using `opa 31 | fmt`. Install the [vim-autoformat](https://github.com/Chiel92/vim-autoformat) 32 | plugin then put this in your `.vimrc`: 33 | 34 | ```viml 35 | let g:formatdef_rego = '"opa fmt"' 36 | let g:formatters_rego = ['rego'] 37 | let g:autoformat_autoindent = 0 38 | let g:autoformat_retab = 0 39 | au BufWritePre *.rego Autoformat 40 | ``` 41 | 42 | If you want to see parse errors that occurred during formatting put this in your 43 | `.vimrc` as well: 44 | 45 | ```viml 46 | let g:autoformat_verbosemode = 1 47 | ``` 48 | 49 | ### Neoformat 50 | 51 | If you are using [`neoformat`](https://github.com/sbdchd/neoformat) plugin, then put this in your `.vimrc`: 52 | 53 | ```viml 54 | let g:neoformat_rego_opa = { 55 | \ 'exe': 'opa', 56 | \ 'args': ['fmt'], 57 | \ 'stdin': 1, 58 | \ } 59 | 60 | let g:neoformat_enabled_rego = ['opa'] 61 | ``` 62 | 63 | Now, `:Neoformat` command will use `opa` to format rego files. 64 | 65 | To make Neoformat auto-format on save, put this in your `.vimrc`: 66 | 67 | ```viml 68 | augroup fmt 69 | autocmd! 70 | autocmd BufWritePre * undojoin | Neoformat 71 | augroup END 72 | ``` 73 | --------------------------------------------------------------------------------