From 885ac85fba7e52c6f06fe343a10c23502f2269a2 Mon Sep 17 00:00:00 2001 From: pika Date: Mon, 19 Aug 2024 15:43:55 +0200 Subject: [PATCH] addet formatting and lspconfig --- lua/pika/plugins/lsp/formatting.lua | 41 +++++ lua/pika/plugins/lsp/lspconfig.lua | 222 +++++++++++++++++----------- 2 files changed, 178 insertions(+), 85 deletions(-) create mode 100644 lua/pika/plugins/lsp/formatting.lua diff --git a/lua/pika/plugins/lsp/formatting.lua b/lua/pika/plugins/lsp/formatting.lua new file mode 100644 index 0000000..382906e --- /dev/null +++ b/lua/pika/plugins/lsp/formatting.lua @@ -0,0 +1,41 @@ +return { + "stevearc/conform.nvim", + event = { "BufReadPre", "BufNewFile" }, + config = function() + local conform = require("conform") + + conform.setup({ + formatters_by_ft = { + javascript = { "prettier" }, + typescript = { "prettier" }, + javascriptreact = { "prettier" }, + typescriptreact = { "prettier" }, + svelte = { "prettier" }, + css = { "prettier" }, + -- html = { 'prettier' }, + fish = { "fish_indent" }, + php = { "pretty-php" }, + json = { "yq" }, + yaml = { "yq" }, + markdown = { "prettier" }, + graphql = { "prettier" }, + liquid = { "prettier" }, + sh = { "shfmt" }, + lua = { "stylua" }, + }, + format_on_save = { + lsp_fallback = false, + async = false, + timeout_ms = 1000, + }, + }) + + vim.keymap.set({ "n", "v" }, "mp", function() + conform.format({ + lsp_fallback = true, + async = false, + timeout_ms = 1000, + }) + end, { desc = "Format file or range (in visual mode)" }) + end, +} diff --git a/lua/pika/plugins/lsp/lspconfig.lua b/lua/pika/plugins/lsp/lspconfig.lua index 3b04946..cf60201 100644 --- a/lua/pika/plugins/lsp/lspconfig.lua +++ b/lua/pika/plugins/lsp/lspconfig.lua @@ -1,95 +1,147 @@ 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() - local lspconfig = require("lspconfig") - local mason_lspconfig = require("mason-lspconfig") - local cmp_nvim_lsp = require("cmp_nvim_lsp") - local keymap = vim.keymap + "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") - -- 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 mason_lspconfig plugin + local mason_lspconfig = require("mason-lspconfig") - vim.api.nvim_create_autocmd("LspAttach", { - group = vim.api.nvim_create_augroup("UserLspConfig", {}), - callback = function(ev) - local opts = { buffer = ev.buf, silent = true } + -- import cmp-nvim-lsp plugin + local cmp_nvim_lsp = require("cmp_nvim_lsp") - 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" }) + local keymap = vim.keymap -- for conciseness - -- Add the "make pretty" keymap - keymap.set({ "n", "v" }, "mp", make_pretty, { buffer = ev.buf, desc = "Make pretty" }) - end, - }) + 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 } - local capabilities = cmp_nvim_lsp.default_capabilities() + 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 + opts.desc = "Smart rename" + keymap.set("n", "rn", vim.lsp.buf.rename, opts) -- smart rename + + opts.desc = "Show buffer diagnostics" + keymap.set("n", "D", "Telescope diagnostics bufnr=0", opts) -- show diagnostics for file + + 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 + for type, icon in pairs(signs) do + local hl = "DiagnosticSign" .. type + vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = "" }) + end - 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 = {}, - } - - 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, + 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, }