From f9c4d63afb7f47813e064efb03a8cd49d0474d52 Mon Sep 17 00:00:00 2001 From: pro100ton Date: Fri, 31 Jan 2025 23:45:06 +0300 Subject: [PATCH] Add completion basic settings --- lua/custom/lsp.lua | 193 ++++++++++++++++++++++++++++++ lua/custom/plugins/completion.lua | 1 + lua/custom/plugins/lsp.lua | 35 ++++++ 3 files changed, 229 insertions(+) create mode 100644 lua/custom/lsp.lua create mode 100644 lua/custom/plugins/lsp.lua diff --git a/lua/custom/lsp.lua b/lua/custom/lsp.lua new file mode 100644 index 0000000..752259b --- /dev/null +++ b/lua/custom/lsp.lua @@ -0,0 +1,193 @@ +local lspconfig = require("lspconfig") +local mason_lspconfig = require("mason-lspconfig") +local cmp_nvim_lsp = require("cmp_nvim_lsp") + +local keymap = vim.keymap + +local capabilities = nil +if pcall(require, "cmp_nvim_lsp") then + capabilities = require("cmp_nvim_lsp").default_capabilities() +end + + +local servers = { + gopls = { + settings = { + gopls = { + hints = { + assignVariableTypes = true, + compositeLiteralFields = true, + compositeLiteralTypes = true, + constantValues = true, + functionTypeParameters = true, + parameterNames = true, + rangeVariableTypes = true, + }, + }, + }, + }, + ruff = { + init_options = { + settings = { + -- Ruff language server settings go here + logLevel = "debug", + }, + }, + }, + pyright = { + settings = { + pyright = { + -- Using Ruff's import organizer + disableOrganizeImports = true, + }, + python = { + analysis = { + -- Ignore all files for analysis to exclusively use Ruff for linting + ignore = { "*" }, + diagnosticMode = "off", + typeCheckingMode = "off", + }, + }, + }, + }, + lua_ls = { + capabilities = capabilities, + settings = { + Lua = { + -- make the language server recognize "vim" global + diagnostics = { + globals = { "vim" }, + }, + completion = { + callSnippet = "Replace", + }, + }, + }, + }, + clangd = { + filetypes = { "c" }, + cmd = { + "clangd", + "--offset-encoding=utf-16", + }, + }, +} + +local servers_to_install = vim.tbl_filter(function(key) + local t = servers[key] + if type(t) == "table" then + return not t.manual_install + else + return t + end +end, vim.tbl_keys(servers)) + + + +require("mason").setup() +local ensure_installed = { + "stylua", + "lua_ls", + "ruff", + "gopls", + "gofumpt", + "golines", + "prettier", + "pyright" +} + +vim.list_extend(ensure_installed, servers_to_install) +require("mason-tool-installer").setup { ensure_installed = ensure_installed } + +for name, config in pairs(servers) do + if config == true then + config = {} + end + config = vim.tbl_deep_extend("force", {}, { + capabilities = capabilities, + }, config) + + lspconfig[name].setup(config) +end + +for name, config in pairs(servers) do + if config == true then + config = {} + end + config = vim.tbl_deep_extend("force", {}, { + capabilities = capabilities, + }, config) + + lspconfig[name].setup(config) +end + +-- Here it is used the LSP server attaches to file +vim.api.nvim_create_autocmd("LspAttach", { + -- Grouping together autocommands. + -- Here we creating new group and calling it "UserLspConfig" + group = vim.api.nvim_create_augroup("UserLspConfig", {}), + -- callback defining logic to execute on the event + 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 } + + -- set keybinds + -- show definition, references + opts.desc = "Show LSP references" + keymap.set("n", "gR", "Telescope lsp_references", opts) + + -- go to declaration + opts.desc = "Go to declaration" + keymap.set("n", "gD", vim.lsp.buf.declaration, opts) + + -- show lsp definitions + opts.desc = "Show LSP definitions" + keymap.set("n", "gd", "Telescope lsp_definitions", opts) + + -- show lsp implementations + opts.desc = "Show LSP implementations" + keymap.set("n", "gi", "Telescope lsp_implementations", opts) + + -- show lsp type definitions + opts.desc = "Show LSP type definitions" + keymap.set("n", "gt", "Telescope lsp_type_definitions", opts) + + -- see available code actions, in visual mode will apply to selection + opts.desc = "See available code actions" + keymap.set({ "n", "v" }, "ca", vim.lsp.buf.code_action, opts) + + -- smart rename + opts.desc = "Smart rename" + keymap.set("n", "rn", vim.lsp.buf.rename, opts) + + -- show diagnostics for file + opts.desc = "Show buffer diagnostics" + keymap.set("n", "D", "Telescope diagnostics bufnr=0", opts) + + -- show diagnostics for line + opts.desc = "Show line diagnostics" + keymap.set("n", "d", vim.diagnostic.open_float, opts) + + -- jump to previous diagnostic in buffer + opts.desc = "Go to previous diagnostic" + keymap.set("n", "[d", vim.diagnostic.goto_prev, opts) + + -- jump to next diagnostic in buffer + opts.desc = "Go to next diagnostic" + keymap.set("n", "]d", vim.diagnostic.goto_next, opts) + + -- show documentation for what is under cursor + opts.desc = "Show documentation for what is under cursor" + keymap.set("n", "K", vim.lsp.buf.hover, opts) + + -- mapping to restart lsp if necessary + opts.desc = "Restart LSP" + keymap.set("n", "rs", ":LspRestart", opts) + + opts.desc = "Run formatting on current buffer" + keymap.set("n", "f", function() + vim.lsp.buf.format({ async = true }) + end, opts) + end, +}) diff --git a/lua/custom/plugins/completion.lua b/lua/custom/plugins/completion.lua index 482e700..2628a4c 100644 --- a/lua/custom/plugins/completion.lua +++ b/lua/custom/plugins/completion.lua @@ -12,6 +12,7 @@ return { "saadparwaiz1/cmp_luasnip", -- for autocompletion "rafamadriz/friendly-snippets", -- useful snippets "onsails/lspkind.nvim", -- vs-code like pictograms + "hrsh7th/cmp-nvim-lsp", }, config = function() require "custom/completion" diff --git a/lua/custom/plugins/lsp.lua b/lua/custom/plugins/lsp.lua new file mode 100644 index 0000000..022c2eb --- /dev/null +++ b/lua/custom/plugins/lsp.lua @@ -0,0 +1,35 @@ +return { + { + "neovim/nvim-lspconfig", + dependencies = { + { + --`lazydev` configures Lua LSP for your Neovim config, runtime and plugins + -- used for completion, annotations and signatures of Neovim apis + "folke/lazydev.nvim", + ft = "lua", + opts = { + library = { + -- Load luvit types when the `vim.uv` word is found + { path = "luvit-meta/library", words = { "vim%.uv" } }, + }, + }, + }, + { "Bilal2453/luvit-meta", lazy = true }, + "williamboman/mason.nvim", + "williamboman/mason-lspconfig.nvim", + "WhoIsSethDaniel/mason-tool-installer.nvim", + + -- Some enhancements for notifications in bottom-right corner + { "j-hui/fidget.nvim", opts = {} }, + + -- Following plugin enables enhancements for diagnostics info + -- { "https://git.sr.ht/~whynothugo/lsp_lines.nvim" }, + + -- Autoformatting + "stevearc/conform.nvim", + }, + config = function() + require "custom/lsp" + end, + }, +}