summaryrefslogtreecommitdiffstats
path: root/autoload/elixir/indent.vim
blob: 8e0c609c231c818638d1cbabfef500d0f60f0ce6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'elixir') == -1
  
let s:NO_COLON_BEFORE = ':\@<!'
let s:NO_COLON_AFTER = ':\@!'
let s:ENDING_SYMBOLS = '\]\|}\|)'
let s:STARTING_SYMBOLS = '\[\|{\|('
let s:ARROW = '->'
let s:END_WITH_ARROW = s:ARROW.'$'
let s:SKIP_SYNTAX = '\%(Comment\|String\)$'
let s:BLOCK_SKIP = "synIDattr(synID(line('.'),col('.'),1),'name') =~? '".s:SKIP_SYNTAX."'"
let s:DEF = '^\s*def'
let s:FN = '\<fn\>'
let s:MULTILINE_FN = s:FN.'\%(.*end\)\@!'
let s:BLOCK_START = '\%(\<do\>\|'.s:FN.'\)\>'
let s:MULTILINE_BLOCK = '\%(\<do\>'.s:NO_COLON_AFTER.'\|'.s:MULTILINE_FN.'\)'
let s:BLOCK_MIDDLE = '\<\%(else\|match\|elsif\|catch\|after\|rescue\)\>'
let s:BLOCK_END = 'end'
let s:STARTS_WITH_PIPELINE = '^\s*|>.*$'
let s:ENDING_WITH_ASSIGNMENT = '=\s*$'
let s:INDENT_KEYWORDS = s:NO_COLON_BEFORE.'\%('.s:MULTILINE_BLOCK.'\|'.s:BLOCK_MIDDLE.'\)'
let s:DEINDENT_KEYWORDS = '^\s*\<\%('.s:BLOCK_END.'\|'.s:BLOCK_MIDDLE.'\)\>'
let s:PAIR_START = '\<\%('.s:NO_COLON_BEFORE.s:BLOCK_START.'\)\>'.s:NO_COLON_AFTER
let s:PAIR_MIDDLE = '^\s*\%('.s:BLOCK_MIDDLE.'\)\>'.s:NO_COLON_AFTER.'\zs'
let s:PAIR_END = '\<\%('.s:NO_COLON_BEFORE.s:BLOCK_END.'\)\>\zs'

function! s:pending_parenthesis(line)
  if a:line.last.text !~ s:ARROW
    return elixir#util#count_indentable_symbol_diff(a:line.last, '(', '\%(end\s*\)\@<!)')
  end
endfunction

function! s:pending_square_brackets(line)
  if a:line.last.text !~ s:ARROW
    return elixir#util#count_indentable_symbol_diff(a:line.last, '[', ']')
  end
endfunction

function! s:pending_brackets(line)
  if a:line.last.text !~ s:ARROW
    return elixir#util#count_indentable_symbol_diff(a:line.last, '{', '}')
  end
endfunction

function! elixir#indent#deindent_case_arrow(ind, line)
  if get(b:old_ind, 'arrow', 0) > 0
        \ && (a:line.current.text =~ s:ARROW
        \ || a:line.current.text =~ s:BLOCK_END)
    let ind = b:old_ind.arrow
    let b:old_ind.arrow = 0
    return ind
  else
    return a:ind
  end
endfunction

function! elixir#indent#deindent_ending_symbols(ind, line)
  if a:line.current.text =~ '^\s*\('.s:ENDING_SYMBOLS.'\)'
    return a:ind - &sw
  else
    return a:ind
  end
endfunction

function! elixir#indent#deindent_keywords(ind, line)
  if a:line.current.text =~ s:DEINDENT_KEYWORDS
    let bslnum = searchpair(
          \ s:PAIR_START,
          \ s:PAIR_MIDDLE,
          \ s:PAIR_END,
          \ 'nbW',
          \ s:BLOCK_SKIP
          \ )

    return indent(bslnum)
  else
    return a:ind
  end
endfunction

function! elixir#indent#deindent_opened_symbols(ind, line)
  let s:opened_symbol =
        \   s:pending_parenthesis(a:line)
        \ + s:pending_square_brackets(a:line)
        \ + s:pending_brackets(a:line)

  if s:opened_symbol < 0
    let ind = get(b:old_ind, 'symbol', a:ind + (s:opened_symbol * &sw))
    let ind = float2nr(ceil(floor(ind)/&sw)*&sw)
    return ind <= 0 ? 0 : ind
  else
    return a:ind
  end
endfunction

function! elixir#indent#indent_after_pipeline(ind, line)
  if a:line.last.text =~ s:STARTS_WITH_PIPELINE
    if empty(substitute(a:line.current.text, ' ', '', 'g'))
          \ || a:line.current.text =~ s:STARTS_WITH_PIPELINE
      return indent(a:line.last.num)
    elseif a:line.last.text !~ s:INDENT_KEYWORDS
      let ind = b:old_ind.pipeline
      let b:old_ind.pipeline = 0
      return ind
    end
  end

  return a:ind
endfunction

function! elixir#indent#indent_assignment(ind, line)
  if a:line.last.text =~ s:ENDING_WITH_ASSIGNMENT
    let b:old_ind.pipeline = indent(a:line.last.num) " FIXME: side effect
    return a:ind + &sw
  else
    return a:ind
  end
endfunction

function! elixir#indent#indent_brackets(ind, line)
  if s:pending_brackets(a:line) > 0
    return a:ind + &sw
  else
    return a:ind
  end
endfunction

function! elixir#indent#indent_case_arrow(ind, line)
  if a:line.last.text =~ s:END_WITH_ARROW && a:line.last.text !~ '\<fn\>'
    let b:old_ind.arrow = a:ind
    return a:ind + &sw
  else
    return a:ind
  end
endfunction

function! elixir#indent#indent_ending_symbols(ind, line)
  if a:line.last.text =~ '^\s*\('.s:ENDING_SYMBOLS.'\)\s*$'
    return a:ind + &sw
  else
    return a:ind
  end
endfunction

function! elixir#indent#indent_keywords(ind, line)
  if a:line.last.text =~ s:INDENT_KEYWORDS
    return a:ind + &sw
  else
    return a:ind
  end
endfunction

function! elixir#indent#indent_parenthesis(ind, line)
  if s:pending_parenthesis(a:line) > 0
        \ && a:line.last.text !~ s:DEF
        \ && a:line.last.text !~ s:END_WITH_ARROW
    let b:old_ind.symbol = a:ind
    return matchend(a:line.last.text, '(')
  else
    return a:ind
  end
endfunction

function! elixir#indent#indent_pipeline_assignment(ind, line)
  if a:line.current.text =~ s:STARTS_WITH_PIPELINE
        \ && a:line.last.text =~ '^[^=]\+=.\+$'
    let b:old_ind.pipeline = indent(a:line.last.num)
    " if line starts with pipeline
    " and last line is an attribution
    " indents pipeline in same level as attribution
    return match(a:line.last.text, '=\s*\zs[^ ]')
  else
    return a:ind
  end
endfunction

function! elixir#indent#indent_pipeline_continuation(ind, line)
  if a:line.last.text =~ s:STARTS_WITH_PIPELINE
        \ && a:line.current.text =~ s:STARTS_WITH_PIPELINE
    return indent(a:line.last.num)
  else
    return a:ind
  end
endfunction

function! elixir#indent#indent_square_brackets(ind, line)
  if s:pending_square_brackets(a:line) > 0
    if a:line.last.text =~ '[\s*$'
      return a:ind + &sw
    else
      " if start symbol is followed by a character, indent based on the
      " whitespace after the symbol, otherwise use the default shiftwidth
      " Avoid negative indentation index
      return matchend(a:line.last.text, '[\s*')
    end
  else
    return a:ind
  end
endfunction

function! elixir#indent#deindent_case_arrow(ind, line)
  if get(b:old_ind, 'arrow', 0) > 0
        \ && (a:line.current.text =~ s:ARROW
        \ || a:line.current.text =~ s:BLOCK_END)
    let ind = b:old_ind.arrow
    let b:old_ind.arrow = 0
    return ind
  else
    return a:ind
  end
endfunction

endif