summaryrefslogtreecommitdiffstats
path: root/indent/d.vim
blob: ca1994c5ffd1c2e6683545ef93f1dba966a32a5b (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
if polyglot#init#is_disabled(expand('<sfile>:p'), 'dlang', 'indent/d.vim')
  finish
endif

" Vim indent file for the D programming language (version 1.076 and 2.063).
"
" Language:     D
" Maintainer:   Jesse Phillips <Jesse.K.Phillips+D@gmail.com>
" Last Change:  2014 January 19
" Version:      0.26
"
" Please submit bugs/comments/suggestions to the github repo:
" https://github.com/JesseKPhillips/d.vim

" Only load this indent file when no other was loaded.
if exists("b:did_indent")
  finish
endif
let b:did_indent = 1

setlocal cindent
setlocal indentkeys& indentkeys+=0=in indentkeys+=0=out indentkeys+=0=body
setlocal indentexpr=GetDIndent()

if exists("*GetDIndent")
  finish
endif

function! SkipBlanksAndComments(startline)
  let lnum = a:startline
  while lnum > 1
    let lnum = prevnonblank(lnum)
    if getline(lnum) =~ '[*+]/\s*$'
      while getline(lnum) !~ '/[*+]' && lnum > 1
	let lnum = lnum - 1
      endwhile
      if getline(lnum) =~ '^\s*/[*+]'
	let lnum = lnum - 1
      else
	break
      endif
    elseif getline(lnum) =~ '\s*//'
      let lnum = lnum - 1
    else
      break
    endif
  endwhile
  return lnum
endfunction

function GetDIndent()
  let lnum = v:lnum
  let line = getline(lnum)
  let cind = cindent(lnum)

  " Align contract blocks with function signature.
  if line =~ '^\s*\(body\|in\|out\)\>'
    " Skip in/out parameters.
    if getline(lnum - 1) =~ '[(,]\s*$'
      return cind
    endif
    " Find the end of the last block or the function signature.
    if line !~ '^\s*}' && getline(lnum - 1) !~ '('
      while lnum > 1 && getline(lnum - 1) !~ '[(}]'
	let lnum = lnum - 1
      endwhile
    endif
    let lnum = SkipBlanksAndComments(lnum)
    return cindent(lnum - 1)
  endif

  " Align multiline array literals. e.g.:
  " auto a = [
  "   [ 1, 2, 3 ],
  "   [ 4, 5, 6 ],
  if line =~ '^\s*\[' && getline(lnum - 1) =~ '^\s*\['
    return indent(lnum - 1)
  endif

  return cind
endfunction