summaryrefslogtreecommitdiffstats
path: root/indent/typescript.vim
blob: f498298bd0fbc788c9f2ead04872fd31ec4ed173 (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
" Vim indent file, taken from indent/java.vim
" Language:	    Typescript
" Maintainer:	None!  Wanna improve this?
" Last Change:	2015 Mar 07

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

" Use javascript cindent options
setlocal cindent cinoptions& cinoptions+=j1,J1
setlocal indentkeys&

" Load typescript indent function
setlocal indentexpr=GetTypescriptIndent()

let b:undo_indent = "setl cin< cino< indentkeys< indentexpr<"

" Only define the function once
if exists("*GetTypescriptIndent")
    finish
endif

" Make sure we have vim capabilities
let s:keepcpo = &cpo
set cpo&vim

function! TypescriptPrevNonBlankOrComment(lnum)
    let pnum = prevnonblank(a:lnum)
    " skip any comments (either `//`, `/*` or `*`)
    while getline(pnum) =~ '^\s*\(\/\/\|\/\*\|\*\)'
        let pnum = prevnonblank(pnum-1)
    endwhile
    return pnum
endfunction

function GetTypescriptIndent()

    " default value: trust cindent
    let ind = cindent(v:lnum)

    if getline(v:lnum) =~ '^\s*[{}\*]'
        return ind
    endif

    " The last non-empty line
    let prev = TypescriptPrevNonBlankOrComment(v:lnum-1)

    " Check if the previous line consists of a single `<variable> : <type>;`
    " declaration (e.g. in interface definitions)
    if getline(prev) =~ '^\s*\w\+\s*:[^{]\+;\s*$'
        return indent(prev)
    endif

    " Try to find out whether the last `}` ended a `<variable> : {` block
    if getline(prev) =~ '};\s*$'
        " jump to matching `{` bracket
        call cursor(prev, 1)
        silent normal %

        " See if current line is type annotation without closing ';' but open
        " `{` bracket
        let lnum = line('.')
        if getline(lnum) =~ '^\s*\w\+\s*:[^;]\+{'
            let ind = indent(lnum)
        endif
    endif

    return ind

endfunction

" Restore compatibility mode
let &cpo = s:keepcpo
unlet s:keepcpo

" vim: et