Permalink
Browse files

Add indent script that respects in/out/body.

  • Loading branch information...
1 parent 5b00074 commit 62cbc9cef8480517eee97119921e13afc0c63914 @commonquail commonquail committed Jan 19, 2014
Showing with 73 additions and 0 deletions.
  1. +73 −0 indent/d.vim
View
73 indent/d.vim
@@ -0,0 +1,73 @@
+" 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
+"
+" Contributors:
+" - Jason Mills: original Maintainer
+" - Mikkel Kjeldsen
+"
+" 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 getline(lnum - 1) !~ '[(}]'
+ let lnum = lnum - 1
+ endwhile
+ endif
+ let lnum = SkipBlanksAndComments(lnum)
+ return cindent(lnum - 1)
+ endif
+
+ return cind
+endfunction

0 comments on commit 62cbc9c

Please sign in to comment.