aboutsummaryrefslogtreecommitdiffstats
path: root/after/plugin/lsp.lua
blob: f6bd37a77da3d49c9d455622d1806d3af34b202a (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
local telescope_mapper = require("alpha.telescope.mappings")

local lsp_formatting = function(bufnr, allowed_clientes)
    vim.lsp.buf.format({
        filter = function(client)
            for _, value in pairs(allowed_clientes) do
                if client.name == value then
                    return true
                end
            end

            return false
        end,
        bufnr = bufnr,
    })
end

-- if you want to set up formatting on save, you can use this as a callback
local augroup = vim.api.nvim_create_augroup("LspFormatting", {})

local format_on_save = function(allowed_clients)
    return function(client, bufnr)
        if client.supports_method("textDocument/formatting") then
            vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
            vim.api.nvim_create_autocmd("BufWritePre", {
                group = augroup,
                buffer = bufnr,
                callback = function()
                    lsp_formatting(bufnr, allowed_clients)
                end,
            })
        end
    end
end

local filetype_attach = setmetatable({
    go = format_on_save({ "gopls" }),
    -- php = format_on_save,
    -- lua = format_on_save,

    gdscript = function(_) end,
}, {
    __index = function()
        return function() end
    end,
})

-- Since I use null os this allows to have the format and actions
-- in all buffers not just configured servers
vim.keymap.set({ "n", "v" }, "<leader>vca", vim.lsp.buf.code_action, {})

vim.keymap.set("n", "<leader>vf", function()
    return vim.lsp.buf.format({ async = true })
end, {})

vim.keymap.set("n", "<leader>vn", vim.diagnostic.goto_next, {})
vim.keymap.set("n", "<leader>vp", vim.diagnostic.goto_prev, {})

local function on_attach(client, bufnr)
    local filetype = vim.api.nvim_buf_get_option(0, "filetype")
    -- keymaps for lsp
    vim.keymap.set("n", "K", vim.lsp.buf.hover, { buffer = 0 })
    vim.keymap.set("n", "<leader>vrn", vim.lsp.buf.rename, { buffer = 0 })
    vim.keymap.set("i", "<c-h>", vim.lsp.buf.signature_help, { buffer = 0 })
    vim.keymap.set("n", "<leader>vo", ":LspRestart<cr>", { noremap = true })

    telescope_mapper("gr", "lsp_references", nil, true)
    telescope_mapper("<leader>pv", "find_symbol", nil, true)
    telescope_mapper("<leader>pd", "lsp_document_symbols", nil, true)
    telescope_mapper("<c-]>", "lsp_definitions", nil, true)

    vim.bo.omnifunc = "v:lua.vim.lsp.omnifunc"

    -- Attach any filetype specific options to the client
    filetype_attach[filetype](client, bufnr)
end

vim.diagnostic.config({
    virtual_text = false,
})

local signs = { Error = "⛔", Warn = "⚠️", Hint = "💡", Info = "ℹ️" }
for type, icon in pairs(signs) do
    local hl = "DiagnosticSign" .. type
    vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = "" })
end

-- Actual configs
local lsp_flags = {
    debounce_text_changes = 150,
}

-- Server configurations

require("lspconfig")["rust_analyzer"].setup({
    on_attach = on_attach,
    flags = lsp_flags,
    -- Server-specific settings...
    settings = {
        ["rust-analyzer"] = {},
    },
})

require("lspconfig").dockerls.setup({
    on_attach = on_attach,
    flags = lsp_flags,
})

-- require("lspconfig")["intelephense"].setup({
-- 	on_attach = on_attach,
-- 	flags = lsp_flags,
-- })

require("lspconfig")["phpactor"].setup({
    on_attach = on_attach,
    flags = lsp_flags,
    filetypes = { "php", "cucumber" },
})

require("lspconfig").jsonls.setup({
    on_attach = on_attach,
    flags = lsp_flags,
})
require("lspconfig").html.setup({
    on_attach = on_attach,
    flags = lsp_flags,
})
require("lspconfig").html.setup({
    on_attach = on_attach,
    flags = lsp_flags,
})

local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities.textDocument.completion.completionItem.snippetSupport = true

require("lspconfig").emmet_ls.setup({
    on_attach = on_attach,
    flags = lsp_flags,
    capabilities = capabilities,
    filetypes = { "html", "typescriptreact", "javascriptreact", "css", "sass", "scss", "less", "blade", "vue" },
})

require("lspconfig").gopls.setup({
    on_attach = on_attach,
    flags = lsp_flags,
})

require("lspconfig").gdscript.setup({
    on_attach = on_attach,
    flags = lsp_flags,
})

require("lspconfig").tsserver.setup({
    on_attach = on_attach,
    flags = lsp_flags,
})

require("lspconfig").volar.setup({
    on_attach = on_attach,
    flags = lsp_flags,
})

require("lspconfig")["sumneko_lua"].setup({
    on_attach = on_attach,
    flags = lsp_flags,
    settings = {
        Lua = {
            runtime = {
                version = "LuaJIT",
            },
            diagnostics = {
                globals = { "vim" },
            },
            workspace = {
                library = vim.api.nvim_get_runtime_file("", true),
            },
            telemetry = {
                enable = false,
            },
        },
    },
})

local lspconfig = require("lspconfig")
local configs = require("lspconfig.configs")

configs.blade = {
    default_config = {
        cmd = { "laravel-dev-tools", "lsp" },
        filetypes = { "blade" },
        root_dir = function(fname)
            return lspconfig.util.find_git_ancestor(fname)
        end,
        settings = {},
    },
}
-- Set it up
lspconfig.blade.setup({
    -- Capabilities is specific to my setup.
    capabilities = capabilities,
    on_attach = on_attach,
    flags = lsp_flags,
})