summaryrefslogtreecommitdiff
path: root/ale_linters/scala/metals.vim
blob: da9e855d9ba389b27076f64cf25272d3ed1e2f2f (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
" Author: Jeffrey Lau - https://github.com/zoonfafer
" Description: Metals Language Server for Scala https://scalameta.org/metals/

call ale#Set('scala_metals_executable', 'metals-vim')
call ale#Set('scala_metals_project_root', '')

function! ale_linters#scala#metals#GetProjectRoot(buffer) abort
    let l:project_root = ale#Var(a:buffer, 'scala_metals_project_root')

    if !empty(l:project_root)
        return l:project_root
    endif

    let l:potential_roots = [
    \   'build.sc',
    \   'build.sbt',
    \   '.bloop',
    \   '.metals',
    \]

    for l:root in l:potential_roots
        let l:project_root = ale#path#ResolveLocalPath(
        \   a:buffer,
        \   l:root,
        \   ''
        \)

        if !empty(l:project_root)
            return fnamemodify(
            \   l:project_root,
            \   ':h',
            \)
        endif
    endfor

    return ''
endfunction

function! ale_linters#scala#metals#GetCommand(buffer) abort
    return '%e' . ale#Pad('stdio')
endfunction

call ale#linter#Define('scala', {
\   'name': 'metals',
\   'lsp': 'stdio',
\   'language': 'scala',
\   'executable': {b -> ale#Var(b, 'scala_metals_executable')},
\   'command': function('ale_linters#scala#metals#GetCommand'),
\   'project_root': function('ale_linters#scala#metals#GetProjectRoot'),
\})