lsp.lua (5613B)
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 = { 57 latexindent = { 58 prepend_args = { "-y=defaultIndent:' '" }, 59 }, 60 }, 61 formatters_by_ft = { 62 javascript = { "prettier" }, 63 javascriptreact = { "prettier" }, 64 typescript = { "prettier" }, 65 typescriptreact = { "prettier" }, 66 vue = { "prettier" }, 67 css = { "prettier" }, 68 scss = { "prettier" }, 69 lua = { "stylua" }, 70 less = { "prettier" }, 71 html = { "prettier" }, 72 json = { "prettier" }, 73 jsonc = { "prettier" }, 74 yaml = { "prettier" }, 75 markdown = { "prettier" }, 76 graphql = { "prettier" }, 77 svelte = { "prettier" }, 78 astro = { "prettier" }, 79 rust = { "rustfmt" }, 80 tex = { "latexindent" }, 81 }, 82 format_on_save = false, 83 }) 84 85 vim.api.nvim_create_autocmd("LspAttach", { 86 desc = "LSP actions", 87 callback = function(event) 88 local bufnr = event.buf 89 local opts = { buffer = bufnr } 90 91 local client = vim.lsp.get_client_by_id(event.data.client_id) 92 if client and client.server_capabilities.inlayHintProvider then 93 vim.lsp.inlay_hint.enable(true, { bufnr = bufnr }) 94 end 95 96 map("n", "K", vim.lsp.buf.hover, opts) 97 map("n", "gd", vim.lsp.buf.definition, opts) 98 map("n", "gD", vim.lsp.buf.declaration, opts) 99 map("n", "gi", vim.lsp.buf.implementation, opts) 100 map("n", "go", vim.lsp.buf.type_definition, opts) 101 map("n", "gr", vim.lsp.buf.references, opts) 102 map("n", "gs", vim.lsp.buf.signature_help, opts) 103 104 map("n", "gq", function() 105 require("conform").format({ async = true, lsp_fallback = true }) 106 end, opts) 107 108 map("n", "<F2>", vim.lsp.buf.rename, opts) 109 map({ "n", "x" }, "<F3>", function() 110 vim.lsp.buf.format({ async = true }) 111 end, opts) 112 map("n", "<F4>", vim.lsp.buf.code_action, opts) 113 end, 114 }) 115 116 require("fidget").setup({}) 117 118 119 require("mason").setup() 120 require("mason-lspconfig").setup({ 121 handlers = { 122 function(server_name) 123 if server_name == "rust_analyzer" then 124 return 125 end 126 lspconfig[server_name].setup({}) 127 end, 128 129 ["eslint"] = function() 130 lspconfig.eslint.setup({ 131 cmd = { "vscode-eslint-language-server", "--stdio" }, 132 filetypes = { 133 "javascript", 134 "javascriptreact", 135 "javascript.jsx", 136 "typescript", 137 "typescriptreact", 138 "typescript.tsx", 139 "vue", 140 "svelte", 141 "astro", 142 "htmlangular", 143 }, 144 on_attach = function(_, bufnr) 145 vim.bo[bufnr].formatexpr = "v:lua.vim.lsp.formatexpr()" 146 end, 147 settings = { 148 workingDirectory = { mode = "auto" }, 149 }, 150 }) 151 end, 152 153 ["bashls"] = function() 154 lspconfig.bashls.setup({ 155 cmd = { "bash-language-server", "start" }, 156 filetypes = { "zsh", "bash", "sh" }, 157 }) 158 end, 159 160 ["tailwindcss"] = function() 161 lspconfig.tailwindcss.setup({ 162 settings = { 163 tailwindCSS = { 164 includeLanguages = { 165 javascript = "javascript", 166 typescript = "typescript", 167 javascriptreact = "javascriptreact", 168 typescriptreact = "typescriptreact", 169 html = "html", 170 }, 171 }, 172 }, 173 }) 174 end, 175 176 ["lua_ls"] = function() 177 lspconfig.lua_ls.setup({ 178 settings = { 179 Lua = { 180 runtime = { version = "LuaJIT" }, 181 diagnostics = { 182 globals = { "vim", "require" }, 183 }, 184 workspace = { 185 library = vim.api.nvim_get_runtime_file("", true), 186 }, 187 telemetry = { enable = false }, 188 }, 189 }, 190 }) 191 end, 192 }, 193 }) 194 end, 195 }