Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
edkolev committed Mar 19, 2014
0 parents commit bccec58
Show file tree
Hide file tree
Showing 11 changed files with 480 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
Session.vim
doc/tags
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2014 Evgeni Kolev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

37 changes: 37 additions & 0 deletions README.md
@@ -0,0 +1,37 @@
# erlang-motions.vim

Motions and text objects for erlang!

### Motions

* `]]` go to next function declaration

* `[[` previous function declaration

* `]m` next function clause

* `[m` previous function clause

* And more: `]M`, `[M`, `][`, `[]` go to next/previous clause/declaration **end**

### Text objects

* `im`, `am` inside/around function clause

* `iM`, `aM` inside/around function declaration

### Installation

- [Pathogen][1] `git clone https://github.com/edkolev/erlang-motions.vim ~/.vim/bundle/erlang-motions.vim`
- [Vundle][2] `Bundle 'edkolev/erlang-motions.vim'`
- [NeoBundle][3] `NeoBundle 'edkolev/erlang-motions.vim'`

### Inspired by

* [vim-ruby][4]

[1]: https://github.com/tpope/vim-pathogen
[2]: https://github.com/gmarik/vundle
[3]: https://github.com/Shougo/neobundle.vim
[4]: https://github.com/vim-ruby/vim-ruby/blob/master/doc/vim-ruby.txt
[5]: http://www.erlang.org/doc/reference_manual/functions.html
2 changes: 2 additions & 0 deletions after/syntax/erlang.vim
@@ -0,0 +1,2 @@

syn region erlangSkippableAttributeDeclaration start='^\s*-\w\+' end='\.\s*$' transparent containedin=TOP
87 changes: 87 additions & 0 deletions ftplugin/erlang.vim
@@ -0,0 +1,87 @@

nnoremap <silent> <buffer> ]] :<C-U>call <SID>declaration('', 'n')<CR>
nnoremap <silent> <buffer> [[ :<C-U>call <SID>declaration('b', 'n')<CR>
nnoremap <silent> <buffer> ][ :<C-U>call <SID>declaration_end('', 'n')<CR>
nnoremap <silent> <buffer> [] :<C-U>call <SID>declaration_end('b', 'n')<CR>
xnoremap <silent> <buffer> ]] :<C-U>call <SID>declaration('', 'v')<CR>
xnoremap <silent> <buffer> [[ :<C-U>call <SID>declaration('b', 'v')<CR>
xnoremap <silent> <buffer> ][ :<C-U>call <SID>declaration_end('', 'v')<CR>
xnoremap <silent> <buffer> [] :<C-U>call <SID>declaration_end('b', 'v')<CR>
nnoremap <silent> <buffer> ]m :<C-U>call <SID>clause('', 'n')<CR>
nnoremap <silent> <buffer> [m :<C-U>call <SID>clause('b', 'n')<CR>
nnoremap <silent> <buffer> ]M :<C-U>call <SID>clause_end('', 'n')<CR>
nnoremap <silent> <buffer> [M :<C-U>call <SID>clause_end('b', 'n')<CR>
xnoremap <silent> <buffer> ]m :<C-U>call <SID>clause('', 'v')<CR>
xnoremap <silent> <buffer> [m :<C-U>call <SID>clause('b', 'v')<CR>
xnoremap <silent> <buffer> ]M :<C-U>call <SID>clause_end('', 'v')<CR>
xnoremap <silent> <buffer> [M :<C-U>call <SID>clause_end('b', 'v')<CR>
if maparg('im','n') == ''
xnoremap <silent> <buffer> im :<C-U>call <SID>inside('[m',']M')<CR>
xnoremap <silent> <buffer> am :<C-U>call <SID>around('[m',']M')<CR>
onoremap <silent> <buffer> im :<C-U>call <SID>inside('[m',']M')<CR>
onoremap <silent> <buffer> am :<C-U>call <SID>around('[m',']M')<CR>
endif

if maparg('iM','n') == ''
onoremap <silent> <buffer> iM :<C-U>call <SID>wrap_im('[[','][')<CR>
onoremap <silent> <buffer> aM :<C-U>call <SID>around('[[','][')<CR>
xnoremap <silent> <buffer> iM :<C-U>call <SID>inside('[[','][')<CR>
xnoremap <silent> <buffer> aM :<C-U>call <SID>around('[[','][')<CR>
endif

fun! s:declaration(flags, mode)
call s:go_to('\(\.\|\%^\)\_s*\(%.*\n\|\_s\)*\n*\_^\s*\zs[a-z][a-zA-Z_0-9]*(', a:flags, '', a:mode)
endfun

fun! s:declaration_end(flags, mode)
call s:go_to('\.', a:flags, 'erlangComment\|erlangString\|erlangSkippableAttributeDeclaration', a:mode)
endfun

fun! s:clause(flags, mode)
call s:go_to('\(\.\|\%^\|\;\)\_s*\(%.*\n\)*\n*\_^\s*\zs[a-z][a-zA-Z_0-9]*(', a:flags, '', a:mode)
endfun

fun! s:clause_end(flags, mode)
call s:go_to('\(\.\|[\;\.]\_s*\(%.*\n\)*\n*\_^\s*[a-z][a-zA-Z_0-9]*(\)', a:flags, 'erlangComment\|erlangString\|erlangSkippableAttributeDeclaration', a:mode)
endfun

fun! s:go_to(pattern, flags, skip_syn, mode)
norm! m'
if a:mode ==# 'v'
norm! gv
endif

let line = line('.') | let col = col('.')
let pos = search(a:pattern,'W'.a:flags)

if len(a:skip_syn) > 0
while pos != 0 && s:synname() =~# a:skip_syn
let pos = search(a:pattern,'W'.a:flags)
endwhile
endif

if pos == 0
call cursor(line,col)
return
endif
endfunction

function! s:synname()
return join(map(synstack(line('.'), col('.')), 'synIDattr(v:val,"name")'), ' ')
endfunction

function! s:around(back, forward)
execute 'norm h'
execute 'norm ' . a:forward . a:back . 'V' . a:forward
endfunction

function! s:inside(back, forward)
execute 'norm h'
execute 'norm ' . a:forward . a:back
call search('->\n\?\s*.', 'We')
execute 'norm ' . 'v' . a:forward . 'h'
endfunction
102 changes: 102 additions & 0 deletions test/clause.vader
@@ -0,0 +1,102 @@
Execute (load ftplugin):
source ../ftplugin/erlang.vim

# next clause
Given erlang (]m):
f(1) -> 1;
f(2) -> 2.

Do:
]mx

Expect erlang:
f(1) -> 1;
(2) -> 2.

#
Given erlang (]m with new line):

f(1) -> 1;
f(2) -> 2.

Do:
]mx

Expect erlang:

(1) -> 1;
f(2) -> 2.

#
Given erlang (]m with comments):
% comment
f(1) -> 1;
% comment
f(2) -> 2.

Do:
]m]mx

Expect erlang:
% comment
f(1) -> 1;
% comment
(2) -> 2.

# prev clause
Given erlang ([m):
f(1) -> 1;
f(2) -> 2;
f(3) -> 3.

Do:
]m]m[mx

Expect erlang:
f(1) -> 1;
(2) -> 2;
f(3) -> 3.

#
Given erlang ([m):
f(1) -> 1;
f(2) -> 2.

Do:
]m[mx

Expect erlang:
(1) -> 1;
f(2) -> 2.

#
Given erlang ([m with comments):
% comment
f(1) -> 1;
% comment
f(2) -> 2.

Do:
jjj[mx

Expect erlang:
% comment
(1) -> 1;
% comment
f(2) -> 2.

#
Given erlang (]m with -spec):
-spec convert(tuple()) -> list();
(list()) -> tuple().
convert(Tup) when is_tuple(Tup) -> tuple_to_list(Tup);
convert(L = [_|_]) -> list_to_tuple(L).

Do:
]m]mx

Expect erlang:
-spec convert(tuple()) -> list();
(list()) -> tuple().
convert(Tup) when is_tuple(Tup) -> tuple_to_list(Tup);
onvert(L = [_|_]) -> list_to_tuple(L).
47 changes: 47 additions & 0 deletions test/clause_end.vader
@@ -0,0 +1,47 @@
Execute (load ftplugin):
source ../ftplugin/erlang.vim
source ../after/syntax/erlang.vim

# next clause end
Given erlang (]M):
f(1) -> 1;
f(2) -> 2.

Do:
]Mx

Expect erlang:
f(1) -> 1
f(2) -> 2.

#
Given erlang (]M):
f(1) -> 1;
f(2) -> 2;
f(3) -> 3.

Do:
]M]M]Mx

Expect erlang:
f(1) -> 1;
f(2) -> 2;
f(3) -> 3

# prev clause end
Given erlang ([M):
f(1) -> 1;
f(2) -> 2;
f(3) -> 3.

Do:
G[Mx

Expect erlang:
f(1) -> 1;
f(2) -> 2
f(3) -> 3.

# TODO: after/syntax/erlang.vim
# - -spec
# comment

0 comments on commit bccec58

Please sign in to comment.