summaryrefslogtreecommitdiff
path: root/lua/lsp/ts-fmt-lint.lua
blob: 3ca97d71e0dd9d6cd4831908d5a48e04a2eaad51 (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
-- Example configuations here: https://github.com/mattn/efm-langserver
-- You can look for project scope Prettier and Eslint with e.g. vim.fn.glob("node_modules/.bin/prettier") etc. If it is not found revert to global Prettier where needed.
local M = {}

M.setup = function()
    local tsserver_args = {}

    local prettier = {
        formatCommand = "prettier --stdin-filepath ${INPUT}",
        formatStdin = true
    }

    if vim.fn.glob("node_modules/.bin/prettier") then
        prettier = {
            formatCommand = "./node_modules/.bin/prettier --stdin-filepath ${INPUT}",
            formatStdin = true
        }
    end

    -- TODO global eslint?

    local eslint = {
        lintCommand = "./node_modules/.bin/eslint -f unix --stdin --stdin-filename ${INPUT}",
        lintIgnoreExitCode = true,
        lintStdin = true,
        lintFormats = {"%f:%l:%c: %m"},
        -- formatCommand = "./node_modules/.bin/eslint -f unix --fix --stdin-filename ${INPUT}", -- TODO check if eslint is the formatter then add this
        formatStdin = true
    }

    if O.lang.tsserver.formatter == 'prettier' then
        table.insert(tsserver_args, prettier)
    end

    if O.lang.tsserver.linter == 'eslint' then
        table.insert(tsserver_args, eslint)
    end

    require"lspconfig".efm.setup {
        -- init_options = {initializationOptions},
        cmd = {DATA_PATH .. "/lspinstall/efm/efm-langserver"},
        init_options = {documentFormatting = true, codeAction = false},
        filetypes = {
            "javascriptreact", "javascript", "typescript", "typescriptreact",
            "html", "css", "yaml", "vue"
        },
        settings = {
            rootMarkers = {".git/", "package.json"},
            languages = {
                javascript = tsserver_args,
                javascriptreact = tsserver_args,
                typescript = tsserver_args,
                typescriptreact = tsserver_args,
                html = {prettier},
                css = {prettier},
                json = {prettier},
                yaml = {prettier}
                -- javascriptreact = {prettier, eslint},
                -- javascript = {prettier, eslint},
                -- markdown = {markdownPandocFormat, markdownlint},
            }
        }
    }
end

return M