summaryrefslogtreecommitdiff
path: root/autoload/ale/handlers/deno.vim
blob: b762f9831ea4c1d8e47399ebe02335f5af148310 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
" Author: Mohammed Chelouti - https://github.com/motato1
"         Arnold Chand <creativenull@outlook.com>
" Description: Handler functions for Deno.

call ale#Set('deno_executable', 'deno')
call ale#Set('deno_unstable', 0)
call ale#Set('deno_importMap', 'import_map.json')
call ale#Set('deno_lsp_project_root', '')

function! ale#handlers#deno#GetExecutable(buffer) abort
    return ale#Var(a:buffer, 'deno_executable')
endfunction

" Find project root for Deno's language server.
"
" Deno projects do not require a project or configuration file at the project root.
" This means the root directory has to be guessed,
" unless it is explicitly specified by the user.
"
" The project root is determined by ...
" 1. using a user-specified value from deno_lsp_project_root
" 2. looking for common top-level files/dirs
" 3. using the buffer's directory
function! ale#handlers#deno#GetProjectRoot(buffer) abort
    let l:project_root = ale#Var(a:buffer, 'deno_lsp_project_root')

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

    let l:possible_project_roots = [
    \   'deno.json',
    \   'deno.jsonc',
    \   'tsconfig.json',
    \   '.git',
    \   bufname(a:buffer),
    \]

    for l:possible_root in l:possible_project_roots
        let l:project_root = ale#path#FindNearestFile(a:buffer, l:possible_root)

        if empty(l:project_root)
            let l:project_root = ale#path#FindNearestDirectory(a:buffer, l:possible_root)
        endif

        if !empty(l:project_root)
            " dir:p expands to /full/path/to/dir/ whereas
            " file:p expands to /full/path/to/file (no trailing slash)
            " Appending '/' ensures that :h:h removes the path's last segment
            " regardless of whether it is a directory or not.
            return fnamemodify(l:project_root . '/', ':p:h:h')
        endif
    endfor

    return ''
endfunction

" Initialization Options for deno, for javascript and typescript
function! ale#handlers#deno#GetInitializationOptions(buffer) abort
    let l:options = {
    \   'enable': v:true,
    \   'lint': v:true,
    \   'unstable': v:false,
    \   'importMap': ale#path#FindNearestFile(a:buffer, 'import_map.json'),
    \   }

    if ale#Var(a:buffer, 'deno_unstable')
        let l:options.unstable = v:true
    endif

    if ale#Var(a:buffer, 'deno_importMap') isnot# ''
        let l:options.importMap = ale#path#FindNearestFile(a:buffer, ale#Var(a:buffer, 'deno_importMap'))
    endif

    return l:options
endfunction