summaryrefslogtreecommitdiff
path: root/plugin/lsp.lua
blob: 90f187716727cad2f1d14a4cb44d7306a48b95bd (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
local lspconfig = require("lspconfig")

--------------------------
-- LSP keybindings
--------------------------
vim.api.nvim_create_autocmd("LspAttach", {
    desc = "LSP actions",
    callback = function(event)
        local opts = { buffer = event.buf }
        vim.keymap.set("n", "K", function()
            vim.lsp.buf.hover()
        end, opts)

        vim.keymap.set("n", "]e", function()
            vim.diagnostic.goto_next()
        end, opts)

        vim.keymap.set("n", "[e", function()
            vim.diagnostic.goto_prev()
        end, opts)

        vim.keymap.set({ "n", "x" }, "<C-i>",
            "<cmd>lua vim.lsp.buf.format({async = true})<cr>",
            opts)
    end,
})

--------------------------
-- Auto completion setup
--------------------------
local lsp_capabilities = require("cmp_nvim_lsp").default_capabilities()
local default_setup = function(server)
    lspconfig[server].setup({ capabilities = lsp_capabilities })
end

local cmp = require("cmp")
cmp.setup({
    sources = {
        { name = "nvim_lsp" },
    },
    mapping = cmp.mapping.preset.insert({
        -- Enter key confirms completion item
        ["<CR>"] = cmp.mapping.confirm({ select = false }),

        -- Ctrl + space triggers completion menu
        ["<C-Space>"] = cmp.mapping.complete(),
    }),
    snippet = {
        expand = function(args)
            require("luasnip").lsp_expand(args.body)
        end,
    },
    window = {
        completion = {
            winhighlight = "Normal:CmpNormal",
        },
        documentation = {
            winhighlight = "Normal:CmpDocNormal",
        },
    },
})

--------------------------
-- Setup mason
--------------------------
require("mason").setup({})
require("mason-lspconfig").setup({
    ensure_installed = {
        "lua_ls",
        "rust_analyzer",
        "gopls",
        "ruff_lsp",
        "tsserver",
        "clangd",
        "taplo",
        "bashls",
        "yamlls",
        "dockerls",
        "texlab",
        "cssls",
        "html",
    },
    handlers = {
        default_setup,
    },
})

-- Disable the annoying warning "undefined vim"
lspconfig.lua_ls.setup({
    capabilities = lsp_capabilities,
    settings = {
        Lua = {
            runtime = {
                version = "LuaJIT",
            },
            diagnostics = {
                globals = { "vim" },
            },
            workspace = {
                library = {
                    vim.env.VIMRUNTIME,
                },
            },
        },
    },
})

-- rust_analyzer setup
lspconfig.rust_analyzer.setup({
    -- Server-specific settings. See `:help lspconfig-setup`
    -- settings = {
    -- 	["rust-analyzer"] = {
    -- 		cargo = {
    -- 			allFeatures = true,
    -- 		},
    -- 	},
    -- },
})

-- dartls setup
lspconfig.dartls.setup({})