Skip to content

Commit 1cfd5ab

Browse files
committed
Merge pull request elixir-editors#145 from LnL7/improved-symbol-indentation
indent symbols based on whitespace
2 parents 85593b1 + 5083b7d commit 1cfd5ab

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

indent/elixir.vim

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ set cpo&vim
1818
let s:no_colon_before = ':\@<!'
1919
let s:no_colon_after = ':\@!'
2020
let s:symbols_end = '\]\|}'
21+
let s:symbols_start = '\[\|{'
2122
let s:arrow = '^.*->$'
2223
let s:pipeline = '^\s*|>.*$'
2324
let s:skip_syntax = '\%(Comment\|String\)$'
@@ -54,12 +55,19 @@ function! GetElixirIndent()
5455
let current_line = getline(v:lnum)
5556
let last_line = getline(lnum)
5657

57-
let splited_line = split(last_line, '\zs')
58+
let split_line = split(last_line, '\zs')
5859
let opened_symbol = 0
59-
let opened_symbol += count(splited_line, '[') - count(splited_line, ']')
60-
let opened_symbol += count(splited_line, '{') - count(splited_line, '}')
61-
62-
let ind += (opened_symbol * &sw)
60+
let opened_symbol += count(split_line, '[') - count(split_line, ']')
61+
let opened_symbol += count(split_line, '{') - count(split_line, '}')
62+
63+
" if start symbol is followed by a character, indent based on the
64+
" whitespace after the symbol, otherwise use the default shiftwidth
65+
if last_line =~ '\('.s:symbols_start.'\).'
66+
let opened_prefix = matchlist(last_line, '\('.s:symbols_start.'\)\s*')[0]
67+
let ind += (opened_symbol * strlen(opened_prefix))
68+
else
69+
let ind += (opened_symbol * &sw)
70+
endif
6371

6472
if last_line =~ '^\s*\('.s:symbols_end.'\)' || last_line =~ s:indent_keywords
6573
let ind += &sw

spec/indent/lists_spec.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,20 @@ def test do
8181
.should be_elixir_indentation
8282
end
8383

84+
specify "lists without whitespace" do
85+
<<-EOF
86+
def project do
87+
[{:bar, path: "deps/umbrella/apps/bar"},
88+
{:umbrella, path: "deps/umbrella"}]
89+
end
90+
EOF
91+
.should be_elixir_indentation
92+
end
93+
8494
specify "lists with line break after square brackets" do
8595
<<-EOF
8696
def project do
87-
deps: [
97+
[
8898
{ :bar, path: "deps/umbrella/apps/bar" },
8999
{ :umbrella, path: "deps/umbrella" }
90100
]

0 commit comments

Comments
 (0)