Skip to content

Commit a4ce0d0

Browse files
committed
Merge pull request elixir-editors#61 from kassio/master
Improve pipeline indentation
2 parents 001b7b4 + 616e94b commit a4ce0d0

File tree

2 files changed

+33
-37
lines changed

2 files changed

+33
-37
lines changed

indent/elixir.vim

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ let s:block_end = 'end'
2828
let s:arrow = '^.*->$'
2929
let s:pipeline = '^\s*|>.*$'
3030

31-
let s:indent_keywords = '\<\%(' . s:block_start . '\|' . s:block_middle . '\)$'
32-
let s:deindent_keywords = '^\s*\<\%(' . s:block_end . '\|' . s:block_middle . '\)\>'
31+
let s:indent_keywords = '\<\%(' . s:block_start . '\|' . s:block_middle . '\)$' . '\|' . s:arrow
32+
let s:deindent_keywords = '^\s*\<\%(' . s:block_end . '\|' . s:block_middle . '\)\>' . '\|' . s:arrow
3333

3434
function! GetElixirIndent(...)
3535
let lnum = prevnonblank(v:lnum - 1)
@@ -41,43 +41,38 @@ function! GetElixirIndent(...)
4141
endif
4242

4343
if synIDattr(synID(v:lnum, 1, 1), "name") !~ s:skip_syntax
44-
let splited_line = split(getline(lnum), '\zs')
45-
let opened_symbol = 0
44+
let current_line = getline(v:lnum)
45+
let last_line = getline(lnum)
46+
47+
let splited_line = split(last_line, '\zs')
48+
let opened_symbol = 0
4649
let opened_symbol += count(splited_line, '[') - count(splited_line, ']')
4750
let opened_symbol += count(splited_line, '{') - count(splited_line, '}')
4851

4952
let ind += opened_symbol * &sw
5053

51-
if getline(lnum) =~ s:indent_keywords .
52-
\ '\|' . s:arrow
54+
if last_line =~ s:indent_keywords
5355
let ind += &sw
5456
endif
5557

5658
" if line starts with pipeline
57-
" and last line doesn't start with pipeline
58-
if getline(v:lnum) =~ s:pipeline &&
59-
\ getline(lnum) !~ s:pipeline
60-
let ind += &sw
61-
endif
62-
63-
" if last line starts with pipeline
64-
" and currentline doesn't start with pipeline
65-
if getline(lnum) =~ s:pipeline &&
66-
\ getline(v:lnum) !~ s:pipeline
67-
let ind -= &sw
59+
" and last line is an attribution
60+
" indents pipeline in same level as attribution
61+
if current_line =~ s:pipeline &&
62+
\ last_line =~ '^[^=]\+=.\+$'
63+
let b:old_ind = ind
64+
let ind += round(match(last_line, '=') / &sw) * &sw
6865
endif
6966

7067
" if last line starts with pipeline
7168
" and current line doesn't start with pipeline
72-
" but last line started a block
73-
if getline(lnum) =~ s:pipeline &&
74-
\ getline(v:lnum) !~ s:pipeline &&
75-
\ getline(lnum) =~ s:block_start
76-
let ind += &sw
69+
" returns the indentation before the pipeline
70+
if last_line =~ s:pipeline &&
71+
\ current_line !~ s:pipeline
72+
let ind = b:old_ind
7773
endif
7874

79-
if getline(v:lnum) =~ s:deindent_keywords .
80-
\ '\|' . s:arrow
75+
if current_line =~ s:deindent_keywords
8176
let bslnum = searchpair( '\<\%(' . s:block_start . '\):\@!\>',
8277
\ '\<\%(' . s:block_middle . '\):\@!\>\zs',
8378
\ '\<:\@<!' . s:block_end . '\>\zs',
@@ -86,7 +81,8 @@ function! GetElixirIndent(...)
8681
let ind = indent(bslnum)
8782
endif
8883

89-
if getline(v:lnum) =~ s:arrow
84+
" indent case statements '->'
85+
if current_line =~ s:arrow
9086
let ind += &sw
9187
endif
9288
endif

spec/indent/pipeline_spec.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@
44
it "using multiline pipeline" do
55
assert_correct_indenting <<-EOF
66
"a,b,c,d"
7-
|> String.split(",")
8-
|> Enum.reverse
7+
|> String.split(",")
8+
|> Enum.reverse
99
EOF
1010
end
1111

1212
it "attribuition using multline pipeline operator" do
1313
assert_correct_indenting <<-EOF
1414
[ h | t ] = "a,b,c,d"
15-
|> String.split(",")
16-
|> Enum.reverse
15+
|> String.split(",")
16+
|> Enum.reverse
1717
EOF
1818
end
1919

2020
it "function with pipeline operator" do
2121
assert_correct_indenting <<-EOF
2222
def test do
2323
[ h | t ] = "a,b,c,d"
24-
|> String.split(",")
25-
|> Enum.reverse
24+
|> String.split(",")
25+
|> Enum.reverse
2626
2727
{ :ok, h }
2828
end
@@ -33,12 +33,12 @@ def test do
3333
assert_correct_indenting <<-EOF
3434
def test do
3535
"a,b,c,d"
36-
|> String.split(",")
37-
|> Enum.first
38-
|> case do
39-
"a" -> "A"
40-
_ -> "Z"
41-
end
36+
|> String.split(",")
37+
|> Enum.first
38+
|> case do
39+
"a" -> "A"
40+
_ -> "Z"
41+
end
4242
end
4343
EOF
4444
end

0 commit comments

Comments
 (0)