summaryrefslogtreecommitdiff
path: root/ale_linters/go/gopls.vim
blob: 23082e9b0530694cbf3980e2632fb1be39666c74 (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
" Author: w0rp <devw0rp@gmail.com>
" Author: Jerko Steiner <https://github.com/jeremija>
" Description: https://github.com/saibing/gopls

call ale#Set('go_gopls_executable', 'gopls')
call ale#Set('go_gopls_options', '--mode stdio')
call ale#Set('go_gopls_init_options', {})
call ale#Set('go_gopls_use_global', get(g:, 'ale_use_global_executables', 0))

function! s:GetGoPathExecutable(suffix) abort
    let l:prefix = $GOPATH

    if !empty($GOPATH)
        let l:prefix = $GOPATH
    elseif has('win32')
        let l:prefix = $USERPROFILE . '/go'
    else
        let l:prefix = $HOME . '/go'
    endif

    return ale#path#Simplify(l:prefix . '/' . a:suffix)
endfunction

function! ale_linters#go#gopls#GetCommand(buffer) abort
    return ale#go#EnvString(a:buffer)
    \   . '%e'
    \   . ale#Pad(ale#Var(a:buffer, 'go_gopls_options'))
endfunction

function! ale_linters#go#gopls#FindProjectRoot(buffer) abort
    let l:go_modules_off = ale#Var(a:buffer, 'go_go111module') is# 'off'
    let l:project_root = l:go_modules_off ?
    \ '' : ale#path#FindNearestFile(a:buffer, 'go.mod')
    let l:mods = ':h'

    if empty(l:project_root)
        let l:project_root = ale#path#FindNearestDirectory(a:buffer, '.git')
        let l:mods = ':h:h'
    endif

    return !empty(l:project_root) ? fnamemodify(l:project_root, l:mods) : ''
endfunction

call ale#linter#Define('go', {
\   'name': 'gopls',
\   'lsp': 'stdio',
\   'executable': {b -> ale#path#FindExecutable(b, 'go_gopls', [
\       s:GetGoPathExecutable('bin/gopls'),
\   ])},
\   'command': function('ale_linters#go#gopls#GetCommand'),
\   'project_root': function('ale_linters#go#gopls#FindProjectRoot'),
\   'initialization_options': {b -> ale#Var(b, 'go_gopls_init_options')},
\})