summaryrefslogtreecommitdiff
path: root/autoload/ale/filetypes.vim
blob: 340a9c4e908d78cac2af4c6629d35f78e7119281 (plain)
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
" Author: w0rp <devw0rp@gmail.com>
" Description: This file handles guessing file extensions for filetypes, etc.

function! ale#filetypes#LoadExtensionMap() abort
    " Output includes:
    "    '*.erl setf erlang'
    let l:output = execute('exec "autocmd"')

    let l:map = {}

    for l:line in split(l:output, "\n")
        " Parse filetypes, like so:
        "
        "    *.erl setf erlang
        " *.md      set filetype=markdown
        " *.snippet setlocal filetype=snippets
        let l:match = matchlist(l:line, '\v^ *\*(\.[^ ]+).*set(f *| *filetype=|local *filetype=)([^ ]+)')

        if !empty(l:match)
            let l:map[substitute(l:match[3], '^=', '', '')] = l:match[1]
        endif
    endfor

    return l:map
endfunction

let s:cached_map = {}

function! s:GetCachedExtensionMap() abort
    if empty(s:cached_map)
        let s:cached_map = ale#filetypes#LoadExtensionMap()
    endif

    return s:cached_map
endfunction

function! ale#filetypes#GuessExtension(filetype) abort
    let l:map = s:GetCachedExtensionMap()
    let l:ext = get(l:map, a:filetype, '')

    " If we have an exact match, like something for javascript.jsx, use that.
    if !empty(l:ext)
        return l:ext
    endif

    " If we don't have an exact match, use the first filetype in the compound
    " filetype.
    for l:part in split(a:filetype, '\.')
        let l:ext = get(l:map, l:part, '')

        if !empty(l:ext)
            return l:ext
        endif
    endfor

    " Return an empty string if we don't find anything.
    return ''
endfunction