From ea060a9f3e1a12ff3b121a01cebf1cbae62509f7 Mon Sep 17 00:00:00 2001 From: pika Date: Sun, 18 Aug 2024 14:02:05 +0200 Subject: [PATCH] addet make_pretty format lsp command keybind --- lua/pika/plugins/lsp/lspconfig.lua | 222 +++++++++++------------------ 1 file changed, 85 insertions(+), 137 deletions(-) diff --git a/lua/pika/plugins/lsp/lspconfig.lua b/lua/pika/plugins/lsp/lspconfig.lua index cf60201..e86cb35 100644 --- a/lua/pika/plugins/lsp/lspconfig.lua +++ b/lua/pika/plugins/lsp/lspconfig.lua @@ -1,147 +1,95 @@ return { - "neovim/nvim-lspconfig", - event = { "BufReadPre", "BufNewFile" }, - dependencies = { - "hrsh7th/cmp-nvim-lsp", - { "antosha417/nvim-lsp-file-operations", config = true }, - { "folke/neodev.nvim", opts = {} }, - }, - config = function() - -- import lspconfig plugin - local lspconfig = require("lspconfig") + "neovim/nvim-lspconfig", + event = { "BufReadPre", "BufNewFile" }, + dependencies = { + "hrsh7th/cmp-nvim-lsp", + { "antosha417/nvim-lsp-file-operations", config = true }, + { "folke/neodev.nvim", opts = {} }, + }, + config = function() + local lspconfig = require("lspconfig") + local mason_lspconfig = require("mason-lspconfig") + local cmp_nvim_lsp = require("cmp_nvim_lsp") + local keymap = vim.keymap - -- import mason_lspconfig plugin - local mason_lspconfig = require("mason-lspconfig") + -- Function to make the buffer or selection pretty + local function make_pretty() + local mode = vim.api.nvim_get_mode().mode + if mode == "v" or mode == "V" then + vim.cmd("'<,'>lua vim.lsp.buf.format()") + else + vim.lsp.buf.format() + end + end - -- import cmp-nvim-lsp plugin - local cmp_nvim_lsp = require("cmp_nvim_lsp") + vim.api.nvim_create_autocmd("LspAttach", { + group = vim.api.nvim_create_augroup("UserLspConfig", {}), + callback = function(ev) + local opts = { buffer = ev.buf, silent = true } - local keymap = vim.keymap -- for conciseness + keymap.set({ "n", "v" }, "ca", vim.lsp.buf.code_action, + { buffer = ev.buf, desc = "See available code actions" }) + keymap.set("n", "rn", vim.lsp.buf.rename, { buffer = ev.buf, desc = "Smart rename" }) + keymap.set("n", "D", "Telescope diagnostics bufnr=0", + { buffer = ev.buf, desc = "Show buffer diagnostics" }) + keymap.set("n", "d", vim.diagnostic.open_float, { buffer = ev.buf, desc = "Show line diagnostics" }) + keymap.set("n", "K", vim.lsp.buf.hover, { buffer = ev.buf, desc = "Show documentation for what is under cursor" }) + keymap.set("n", "rs", ":LspRestart", { buffer = ev.buf, desc = "Restart LSP" }) - vim.api.nvim_create_autocmd("LspAttach", { - group = vim.api.nvim_create_augroup("UserLspConfig", {}), - callback = function(ev) - -- Buffer local mappings. - -- See `:help vim.lsp.*` for documentation on any of the below functions - local opts = { buffer = ev.buf, silent = true } + -- Add the "make pretty" keymap + keymap.set({ "n", "v" }, "mp", make_pretty, { buffer = ev.buf, desc = "Make pretty" }) + end, + }) - opts.desc = "See available code actions" - keymap.set({ "n", "v" }, "ca", vim.lsp.buf.code_action, opts) -- see available code actions, in visual mode will apply to selection + local capabilities = cmp_nvim_lsp.default_capabilities() - opts.desc = "Smart rename" - keymap.set("n", "rn", vim.lsp.buf.rename, opts) -- smart rename + 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 - opts.desc = "Show buffer diagnostics" - keymap.set("n", "D", "Telescope diagnostics bufnr=0", opts) -- show diagnostics for file + local server_configs = { + svelte = { + on_attach = function(client, bufnr) + vim.api.nvim_create_autocmd("BufWritePost", { + pattern = { "*.js", "*.ts" }, + callback = function(ctx) + client.notify("$/onDidChangeTsOrJsFile", { uri = ctx.match }) + end, + }) + end, + }, + graphql = { + filetypes = { "graphql", "gql", "svelte", "typescriptreact", "javascriptreact" }, + }, + emmet_ls = { + filetypes = { "html", "typescriptreact", "javascriptreact", "css", "sass", "scss", "less", "svelte" }, + }, + lua_ls = { + settings = { + Lua = { + diagnostics = { globals = { "vim" } }, + completion = { callSnippet = "Replace" }, + }, + }, + }, + cssls = { + filetypes = { "html", "css", "scss", "php" }, + }, + intelephense = {}, + marksman = {}, + } - opts.desc = "Show line diagnostics" - keymap.set("n", "d", vim.diagnostic.open_float, opts) -- show diagnostics for line - - opts.desc = "Show documentation for what is under cursor" - keymap.set("n", "K", vim.lsp.buf.hover, opts) -- show documentation for what is under cursor - - opts.desc = "Restart LSP" - keymap.set("n", "rs", ":LspRestart", opts) -- mapping to restart lsp if necessary - end, - }) - - -- used to enable autocompletion (assign to every lsp server config) - local capabilities = cmp_nvim_lsp.default_capabilities() - - -- Change the Diagnostic symbols in the sign column (gutter) - -- (not in youtube nvim video) - 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 - - mason_lspconfig.setup_handlers({ - -- default handler for installed servers - function(server_name) - lspconfig[server_name].setup({ - capabilities = capabilities, - }) - end, - ["svelte"] = function() - -- configure svelte server - lspconfig["svelte"].setup({ - capabilities = capabilities, - on_attach = function(client, bufnr) - vim.api.nvim_create_autocmd("BufWritePost", { - pattern = { "*.js", "*.ts" }, - callback = function(ctx) - -- Here use ctx.match instead of ctx.file - client.notify("$/onDidChangeTsOrJsFile", { uri = ctx.match }) - end, - }) - end, - }) - end, - ["graphql"] = function() - -- configure graphql language server - lspconfig["graphql"].setup({ - capabilities = capabilities, - filetypes = { "graphql", "gql", "svelte", "typescriptreact", "javascriptreact" }, - }) - end, - ["emmet_ls"] = function() - -- configure emmet language server - lspconfig["emmet_ls"].setup({ - capabilities = capabilities, - filetypes = { - "html", - "typescriptreact", - "javascriptreact", - "css", - "sass", - "scss", - "less", - "svelte", - }, - }) - end, - ["lua_ls"] = function() - -- ─< configure lua server (with special settings) >──────────────────────────────────── - lspconfig["lua_ls"].setup({ - capabilities = capabilities, - settings = { - Lua = { - -- ─< make the language server recognize "vim" global >───────────────────────────────── - diagnostics = { - globals = { "vim" }, - }, - completion = { - callSnippet = "Replace", - }, - }, - }, - }) - end, - ["cssls"] = function() - -- ─< configure CSS server >──────────────────────────────────────────────────────────── - lspconfig["cssls"].setup({ - capabilities = capabilities, - filetypes = { - "html", - "css", - "scss", - "php", - }, - }) - end, - ["intelephense"] = function() - -- ─< configure PHP server >──────────────────────────────────────────────────────────── - lspconfig["intelephense"].setup({ - capabilities = capabilities, - }) - end, - ["marksman"] = function() - -- ─< configure Markdown server >─────────────────────────────────────────────────────── - lspconfig["marksman"].setup({ - capabilities = capabilities, - }) - end, - }) - end, + mason_lspconfig.setup_handlers({ + function(server_name) + lspconfig[server_name].setup({ + capabilities = capabilities, + on_attach = server_configs[server_name] and server_configs[server_name].on_attach or nil, + filetypes = server_configs[server_name] and server_configs[server_name].filetypes or nil, + settings = server_configs[server_name] and server_configs[server_name].settings or nil, + }) + end, + }) + end, }