Skip to content

Commit 7a0f4ec

Browse files
authored
Merge pull request #358 from elixir-editors/follow-prev-nb
indent: add follow_prev_nb
2 parents ece9fc6 + b4a9b62 commit 7a0f4ec

File tree

2 files changed

+38
-51
lines changed

2 files changed

+38
-51
lines changed

autoload/elixir/indent.vim

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function! elixir#indent#indent(lnum)
2828
\'inside_nested_construct',
2929
\'starts_with_comment',
3030
\'inside_generic_block',
31-
\'following_prev_end'
31+
\'follow_prev_nb'
3232
\]
3333
for handler in handlers
3434
call s:debug('testing handler elixir#indent#handle_'.handler)
@@ -140,6 +140,39 @@ function! elixir#indent#handle_top_of_file(_lnum, _text, prev_nb_lnum, _prev_nb_
140140
end
141141
endfunction
142142

143+
function! elixir#indent#handle_follow_prev_nb(_lnum, _text, prev_nb_lnum, prev_nb_text)
144+
return s:get_base_indent(a:prev_nb_lnum, a:prev_nb_text)
145+
endfunction
146+
147+
" Given the line at `lnum`, returns the indent of the line that acts as the 'base indent'
148+
" for this line. In particular it traverses backwards up things like pipelines
149+
" to find the beginning of the expression
150+
function! s:get_base_indent(lnum, text)
151+
let prev_nb_lnum = prevnonblank(a:lnum - 1)
152+
let prev_nb_text = getline(prev_nb_lnum)
153+
154+
let binary_operator = '\%(=\|<>\|>>>\|<=\|||\|+\|\~\~\~\|-\|&&\|<<<\|/\|\^\^\^\|\*\)'
155+
let data_structure_close = '\%(\]\|}\|)\)'
156+
let pipe = '|>'
157+
158+
if s:starts_with(a:text, binary_operator, a:lnum)
159+
return s:get_base_indent(prev_nb_lnum, prev_nb_text)
160+
elseif s:starts_with(a:text, pipe, a:lnum)
161+
return s:get_base_indent(prev_nb_lnum, prev_nb_text)
162+
elseif s:ends_with(prev_nb_text, binary_operator, prev_nb_lnum)
163+
return s:get_base_indent(prev_nb_lnum, prev_nb_text)
164+
elseif s:ends_with(a:text, data_structure_close, a:lnum)
165+
let data_structure_open = '\%(\[\|{\|(\)'
166+
let close_match_idx = match(a:text, data_structure_close . '\s*$')
167+
let _move = cursor(a:lnum, close_match_idx + 1)
168+
let [open_match_lnum, open_match_col] = searchpairpos(data_structure_open, '', data_structure_close, 'bnW')
169+
let open_match_text = getline(open_match_lnum)
170+
return s:get_base_indent(open_match_lnum, open_match_text)
171+
else
172+
return indent(a:lnum)
173+
endif
174+
endfunction
175+
143176
" TODO: @jbodah 2017-03-31: remove
144177
function! elixir#indent#handle_following_trailing_do(lnum, text, prev_nb_lnum, prev_nb_text)
145178
if s:ends_with(a:prev_nb_text, s:keyword('do'), a:prev_nb_lnum)

spec/indent/def_spec.rb

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,57 +17,11 @@ def handle_call({:release_lock, key}, _from, state) do
1717
EOF
1818

1919
i <<~EOF
20-
defmodule HiMom do
21-
def hi do
22-
puts "hi"
20+
defmodule Hello do
21+
def hello do
2322
end
24-
25-
26-
27-
28-
29-
30-
31-
32-
33-
34-
35-
36-
37-
38-
39-
40-
41-
42-
43-
44-
45-
46-
47-
48-
49-
50-
51-
52-
53-
54-
55-
56-
57-
58-
59-
60-
61-
62-
63-
64-
65-
66-
67-
68-
69-
def mom do
70-
puts "mom"
23+
#{"\n" * 40}
24+
def world do
7125
end
7226
end
7327
EOF

0 commit comments

Comments
 (0)