lsp.lua (5461B)
1 ---@diagnostic disable: undefined-global 2 return { 3 "neovim/nvim-lspconfig", 4 dependencies = { 5 "stevearc/conform.nvim", 6 "williamboman/mason.nvim", 7 "williamboman/mason-lspconfig.nvim", 8 "j-hui/fidget.nvim", 9 "saghen/blink.cmp", 10 }, 11 config = function() 12 local map = vim.keymap.set 13 14 local ok_blink, blink = pcall(require, "blink.cmp") 15 if ok_blink then 16 blink.setup({ 17 snippets = { preset = "luasnip" }, 18 sources = { 19 default = { "lsp", "path", "snippets", "buffer" }, 20 }, 21 }) 22 end 23 24 local capabilities = vim.lsp.protocol.make_client_capabilities() 25 if ok_blink and type(blink.get_lsp_capabilities) == "function" then 26 capabilities = vim.tbl_deep_extend("force", capabilities, blink.get_lsp_capabilities()) 27 end 28 29 local ok_endhints, endhints = pcall(require, "lsp-endhints") 30 if ok_endhints then 31 endhints.setup({ 32 icons = { 33 type = "-> ", 34 parameter = "<= ", 35 offspec = "<= ", 36 unknown = "? ", 37 }, 38 label = { 39 truncateAtChars = 50, 40 padding = 1, 41 marginLeft = 0, 42 sameKindSeparator = ", ", 43 }, 44 extmark = { 45 priority = 50, 46 }, 47 autoEnableHints = true, 48 }) 49 endhints.enable() 50 end 51 52 local lspconfig = require("lspconfig") 53 lspconfig.util.default_config.capabilities = capabilities 54 55 require("conform").setup({ 56 formatters_by_ft = { 57 javascript = { "prettier" }, 58 javascriptreact = { "prettier" }, 59 typescript = { "prettier" }, 60 typescriptreact = { "prettier" }, 61 vue = { "prettier" }, 62 css = { "prettier" }, 63 scss = { "prettier" }, 64 lua = { "stylua" }, 65 less = { "prettier" }, 66 html = { "prettier" }, 67 json = { "prettier" }, 68 jsonc = { "prettier" }, 69 yaml = { "prettier" }, 70 markdown = { "prettier" }, 71 graphql = { "prettier" }, 72 svelte = { "prettier" }, 73 astro = { "prettier" }, 74 rust = { "rustfmt" }, 75 }, 76 format_on_save = false, 77 }) 78 79 vim.api.nvim_create_autocmd("LspAttach", { 80 desc = "LSP actions", 81 callback = function(event) 82 local bufnr = event.buf 83 local opts = { buffer = bufnr } 84 85 local client = vim.lsp.get_client_by_id(event.data.client_id) 86 if client and client.server_capabilities.inlayHintProvider then 87 vim.lsp.inlay_hint.enable(true, { bufnr = bufnr }) 88 end 89 90 map("n", "K", vim.lsp.buf.hover, opts) 91 map("n", "gd", vim.lsp.buf.definition, opts) 92 map("n", "gD", vim.lsp.buf.declaration, opts) 93 map("n", "gi", vim.lsp.buf.implementation, opts) 94 map("n", "go", vim.lsp.buf.type_definition, opts) 95 map("n", "gr", vim.lsp.buf.references, opts) 96 map("n", "gs", vim.lsp.buf.signature_help, opts) 97 98 map("n", "gq", function() 99 require("conform").format({ async = true, lsp_fallback = true }) 100 end, opts) 101 102 map("n", "<F2>", vim.lsp.buf.rename, opts) 103 map({ "n", "x" }, "<F3>", function() 104 vim.lsp.buf.format({ async = true }) 105 end, opts) 106 map("n", "<F4>", vim.lsp.buf.code_action, opts) 107 end, 108 }) 109 110 require("fidget").setup({}) 111 112 113 require("mason").setup() 114 require("mason-lspconfig").setup({ 115 handlers = { 116 function(server_name) 117 if server_name == "rust_analyzer" then 118 return 119 end 120 lspconfig[server_name].setup({}) 121 end, 122 123 ["eslint"] = function() 124 lspconfig.eslint.setup({ 125 cmd = { "vscode-eslint-language-server", "--stdio" }, 126 filetypes = { 127 "javascript", 128 "javascriptreact", 129 "javascript.jsx", 130 "typescript", 131 "typescriptreact", 132 "typescript.tsx", 133 "vue", 134 "svelte", 135 "astro", 136 "htmlangular", 137 }, 138 on_attach = function(_, bufnr) 139 vim.bo[bufnr].formatexpr = "v:lua.vim.lsp.formatexpr()" 140 end, 141 settings = { 142 workingDirectory = { mode = "auto" }, 143 }, 144 }) 145 end, 146 147 ["bashls"] = function() 148 lspconfig.bashls.setup({ 149 cmd = { "bash-language-server", "start" }, 150 filetypes = { "zsh", "bash", "sh" }, 151 }) 152 end, 153 154 ["tailwindcss"] = function() 155 lspconfig.tailwindcss.setup({ 156 settings = { 157 tailwindCSS = { 158 includeLanguages = { 159 javascript = "javascript", 160 typescript = "typescript", 161 javascriptreact = "javascriptreact", 162 typescriptreact = "typescriptreact", 163 html = "html", 164 }, 165 }, 166 }, 167 }) 168 end, 169 170 ["lua_ls"] = function() 171 lspconfig.lua_ls.setup({ 172 settings = { 173 Lua = { 174 runtime = { version = "LuaJIT" }, 175 diagnostics = { 176 globals = { "vim", "require" }, 177 }, 178 workspace = { 179 library = vim.api.nvim_get_runtime_file("", true), 180 }, 181 telemetry = { enable = false }, 182 }, 183 }, 184 }) 185 end, 186 }, 187 }) 188 end, 189 }