Skip to content

Commit

Permalink
Add vim-elixir
Browse files Browse the repository at this point in the history
  • Loading branch information
kerryb committed Jun 21, 2013
1 parent bfad3fe commit 4d5cb75
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 0 deletions.
21 changes: 21 additions & 0 deletions bundle/vim-elixir/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# vim-elixir

This project contains some Vim configuration files to work with [Elixir](http://elixir-lang.org).

So far it's included:

* Syntax highlighting

* Filetype detection

* Auto indentation

## Install

* Copy the files to your `~/.vim` directory.

* If you use vim-pathogen you can clone this repo into `~/.vim/bundle`

## Snippets

If you are looking for snipmate snippets take a look at: [elixir-snippets](https://github.com/carlosgaldino/elixir-snippets)
2 changes: 2 additions & 0 deletions bundle/vim-elixir/ftdetect/elixir.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
au BufRead,BufNewFile *.ex,*.exs set filetype=elixir
au FileType elixir setl sw=2 sts=2 et
12 changes: 12 additions & 0 deletions bundle/vim-elixir/ftplugin/elixir.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
" Vim filetype plugin
" Language: Elixir
" Maintainer: Carlos Galdino <[email protected]>
" URL: https://github.com/elixir-lang/vim-elixir

if (exists("b:did_ftplugin"))
finish
endif
let b:did_ftplugin = 1

setlocal comments=:#
setlocal commentstring=#\ %s
76 changes: 76 additions & 0 deletions bundle/vim-elixir/indent/elixir.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
" Vim indent file
" Language: Elixir
" Maintainer: Carlos Galdino <[email protected]>
" Last Change: 2013 Apr 24

if exists("b:did_indent")
finish
endif
let b:did_indent = 1

setlocal nosmartindent

setlocal indentexpr=GetElixirIndent(v:lnum)
setlocal indentkeys+==end,=else:,=match:,=elsif:,=catch:,=after:,=rescue:

if exists("*GetElixirIndent")
finish
endif

let s:elixir_indent_keywords = '\%(\<\(case\|if\|unless\|try\|loop\|receive\)\>\|' .
\ '^\s*\(defmodule\|defimpl\|defmacro\|defdelegate\|defexception\|defcallback\|defoverridable\|defp\|def\|test\|[a-z]\w*\(:\)\@=\)\|' .
\ 'fn(.*)\s\(do\|->\)$\)'

let s:elixir_clauses = '\(else\|match\|elsif\|catch\|after\|rescue\):\|end'

function! s:BlockStarter(lnum, block_start_re)
let lnum = a:lnum
let maxindent = 10000
while lnum > 1
let lnum = prevnonblank(lnum - 1)
if indent(lnum) < maxindent
if getline(lnum) =~ a:block_start_re
return lnum
else
let maxindent = indent(lnum)
if maxindent == 0
return -1
endif
endif
endif
endwhile
return -1
endfunction

function! GetElixirIndent(line_num)
" don't indent if it's the first line of the file
if a:line_num == 0
return 0
endif

let this_line = getline(a:line_num)

if this_line =~ s:elixir_clauses
let bslnum = s:BlockStarter(a:line_num, s:elixir_indent_keywords)
if bslnum > 0
return indent(bslnum)
else
return -1
endif
endif

let plnum = a:line_num - 1
let previous_line = getline(plnum)

if previous_line =~ '\(do\|when\|->\)$\|^\s*\(if\>\|[a-z]\w*\(:\)\@=\)'
return indent(plnum) + &sw
endif

" blank lines are indented based on the previous not blank line"
if previous_line =~ '^\s*$'
let nonblank = prevnonblank(a:line_num)
return indent(nonblank)
endif

return indent(plnum)
endfunction
91 changes: 91 additions & 0 deletions bundle/vim-elixir/syntax/elixir.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
" Vim syntax file
" Language: Elixir
" Maintainer: Carlos Galdino <[email protected]>
" Last Change: 2013 Apr 24

if exists("b:current_syntax")
finish
endif

" syncing starts 2000 lines before top line so docstrings don't screw things up
syn sync minlines=2000

syn cluster elixirNotTop contains=@elixirRegexSpecial,@elixirStringContained,elixirTodo

syn match elixirComment '#.*' contains=elixirTodo
syn keyword elixirTodo FIXME NOTE TODO OPTIMIZE XXX HACK contained

syn match elixirKeyword '\<\%(case\|cond\|bc\|lc\|inlist\|inbits\|if\|unless\|try\|loop\|receive\|function\)\>[?!]\@!'
syn match elixirKeyword '\<\%(defmodule\|defprotocol\|defimpl\|defrecordp\?\|defmacrop\?\|defdelegate\|defoverridable\|defexception\|defcallback\|defp\?\)\>[?!]\@!'
syn match elixirKeyword '\<\%(exit\|raise\|throw\|after\|rescue\|catch\|else\)\>[?!]\@!'
syn match elixirKeyword '\<\%(->\)\>\s*'
syn match elixirKeyword '\<\%(use\|recur\|quote\|unquote\|super\|alias\)\>[?!]\@!'

syn keyword elixirInclude import require

syn match elixirOperator '\<\%(and\|not\|or\|when\|xor\|in\)\>'
syn match elixirOperator '%=\|\*=\|\*\*=\|+=\|-=\|\^=\|||='
syn match elixirOperator "\%(<=>\|<\%(<\|=\)\@!\|>\%(<\|=\|>\)\@!\|<=\|>=\|===\|==\|=\~\|!=\|!\~\|?[ \t]\@=\)"
syn match elixirOperator "!+[ \t]\@=\|&&\|||\|\^\|\*\|+\|-\|/"
syn match elixirOperator "|\|++\|--\|\*\*\|\/\/\|<-\|<>\|<<\|>>\|=\|\."

syn match elixirSymbol '\(:\)\@<!:\%([a-zA-Z_]\w*\%([?!]\|=[>=]\@!\)\?\|<>\|===\?\|>=\?\|<=\?\)'
syn match elixirSymbol '\(:\)\@<!:\%(<=>\|&&\?\|%\(()\|\[\]\|{}\)\|++\?\|--\?\|||\?\|!\|//\|[%&`/|]\)'
syn match elixirSymbol "\%([a-zA-Z_]\w*\([?!]\)\?\):\(:\)\@!"

syn match elixirName '\<nil\>[?!]\@!\|\<[A-Z]\w*\>'

syn match elixirBoolean 'true\|false'

syn match elixirVariable '@[a-zA-Z_]\w*\|&\d'

syn match elixirPseudoVariable '\<__\%(FILE\|DIR\|MODULE\|ENV\|CALLER\)__\>[?!]\@!'

syn match elixirNumber '\<\d\(_\?\d\)*\(\.[^[:space:][:digit:]]\@!\(_\?\d\)*\)\?\([eE][-+]\?\d\(_\?\d\)*\)\?\>'
syn match elixirNumber '\<0[xX][0-9A-Fa-f]\+\>'
syn match elixirNumber '\<0[bB][01]\+\>'

syn match elixirRegexEscape "\\\\\|\\[aAbBcdDefGhHnrsStvVwW]\|\\\d\{3}\|\\x[0-9a-fA-F]\{2}" contained
syn match elixirRegexEscapePunctuation "?\|\\.\|*\|\\\[\|\\\]\|+\|\\^\|\\\$\|\\|\|\\(\|\\)\|\\{\|\\}" contained
syn match elixirRegexQuantifier "[*?+][?+]\=" contained display
syn match elixirRegexQuantifier "{\d\+\%(,\d*\)\=}?\=" contained display
syn match elixirRegexCharClass "\[:\(alnum\|alpha\|ascii\|blank\|cntrl\|digit\|graph\|lower\|print\|punct\|space\|upper\|word\|xdigit\):\]" contained display

syn region elixirRegex matchgroup=elixirDelimiter start="%r/" end="/[uiomxfr]*" skip="\\\\" contains=@elixirRegexSpecial

syn cluster elixirRegexSpecial contains=elixirRegexEscape,elixirRegexCharClass,elixirRegexQuantifier,elixirRegexEscapePunctuation
syn cluster elixirStringContained contains=elixirInterpolation,elixirRegexEscape,elixirRegexCharClass

syn region elixirString matchgroup=elixirDelimiter start="'" end="'" skip="\\'"
syn region elixirString matchgroup=elixirDelimiter start='"' end='"' skip='\\"' contains=@elixirStringContained
syn region elixirInterpolation matchgroup=elixirDelimiter start="#{" end="}" contained contains=ALLBUT,elixirComment,@elixirNotTop
syn region elixirDocString start=+"""+ end=+"""+ contains=elixirTodo fold
syn region elixirDocString start=+'''+ end=+'''+ contains=elixirTodo fold

syn match elixirSymbolInterpolated ':\("\)\@=' contains=elixirString
syn match elixirString "\(\w\)\@<!?\%(\\\(x\d{1,2}\|\h{1,2}\h\@!\>\|0[0-7]{0,2}[0-7]\@!\>\|[^x0MC]\)\|(\\[MC]-)+\w\|[^\s\\]\)"

syn region elixirBlock matchgroup=elixirKeyword start="\<do\>\(:\)\@!" end="\<end\>" contains=ALLBUT,@elixirNotTop fold
syn region elixirAnonymousFunction matchgroup=elixirKeyword start="\<fn\>" end="\<end\>" contains=ALLBUT,@elixirNotTop fold

hi def link elixirInclude Include
hi def link elixirComment Comment
hi def link elixirTodo Todo
hi def link elixirKeyword Keyword
hi def link elixirOperator Operator
hi def link elixirSymbol Constant
hi def link elixirPseudoVariable Constant
hi def link elixirName Type
hi def link elixirBoolean Boolean
hi def link elixirVariable Identifier
hi def link elixirNumber Number
hi def link elixirDocString Comment
hi def link elixirSymbolInterpolated elixirSymbol
hi def link elixirRegex elixirString
hi def link elixirRegexEscape elixirSpecial
hi def link elixirRegexEscapePunctuation elixirSpecial
hi def link elixirRegexCharClass elixirSpecial
hi def link elixirRegexQuantifier elixirSpecial
hi def link elixirSpecial Special
hi def link elixirString String
hi def link elixirDelimiter Delimiter

0 comments on commit 4d5cb75

Please sign in to comment.