From 5accc8bf38248087ae04931bcb52a387fd79602c Mon Sep 17 00:00:00 2001 From: josean-dev Date: Fri, 1 Sep 2023 21:40:47 -0400 Subject: [PATCH] New code for lsp configuration video --- .config/nvim/lua/josean/lazy.lua | 2 +- .config/nvim/lua/josean/plugins/dressing.lua | 4 ++ .config/nvim/lua/josean/plugins/init.lua | 2 - .../nvim/lua/josean/plugins/lsp/lspconfig.lua | 38 ++++++------------- .config/nvim/lua/josean/plugins/lsp/mason.lua | 19 +++++++--- .config/nvim/lua/josean/plugins/nvim-cmp.lua | 14 ++++--- .config/nvim/lua/josean/plugins/nvim-tree.lua | 6 ++- .config/nvim/lua/josean/plugins/telescope.lua | 35 +++-------------- 8 files changed, 49 insertions(+), 71 deletions(-) create mode 100644 .config/nvim/lua/josean/plugins/dressing.lua diff --git a/.config/nvim/lua/josean/lazy.lua b/.config/nvim/lua/josean/lazy.lua index 8ee7057..9d548a3 100644 --- a/.config/nvim/lua/josean/lazy.lua +++ b/.config/nvim/lua/josean/lazy.lua @@ -11,7 +11,7 @@ if not vim.loop.fs_stat(lazypath) then end vim.opt.rtp:prepend(lazypath) -require("lazy").setup({ { import = "josean.plugins" }, { import = "josean.plugins.lsp" } }, { +require("lazy").setup({{import = "josean.plugins"}, {import = "josean.plugins.lsp"}}, { install = { colorscheme = { "nightfly" }, }, diff --git a/.config/nvim/lua/josean/plugins/dressing.lua b/.config/nvim/lua/josean/plugins/dressing.lua new file mode 100644 index 0000000..105f7e5 --- /dev/null +++ b/.config/nvim/lua/josean/plugins/dressing.lua @@ -0,0 +1,4 @@ +return { + "stevearc/dressing.nvim", + event = "VeryLazy", +} diff --git a/.config/nvim/lua/josean/plugins/init.lua b/.config/nvim/lua/josean/plugins/init.lua index 74ad14c..eb9e525 100644 --- a/.config/nvim/lua/josean/plugins/init.lua +++ b/.config/nvim/lua/josean/plugins/init.lua @@ -4,6 +4,4 @@ return { "christoomey/vim-tmux-navigator", -- tmux & split window navigation "inkarkat/vim-ReplaceWithRegister", -- replace with register contents using motion (gr + motion) - - "github/copilot.vim", } diff --git a/.config/nvim/lua/josean/plugins/lsp/lspconfig.lua b/.config/nvim/lua/josean/plugins/lsp/lspconfig.lua index 5254f3f..a819e96 100644 --- a/.config/nvim/lua/josean/plugins/lsp/lspconfig.lua +++ b/.config/nvim/lua/josean/plugins/lsp/lspconfig.lua @@ -2,12 +2,8 @@ return { "neovim/nvim-lspconfig", event = { "BufReadPre", "BufNewFile" }, dependencies = { - "jose-elias-alvarez/typescript.nvim", "hrsh7th/cmp-nvim-lsp", - { - "smjonas/inc-rename.nvim", - config = true, - }, + { "antosha417/nvim-lsp-file-operations", config = true }, }, config = function() -- import lspconfig plugin @@ -16,15 +12,11 @@ return { -- import cmp-nvim-lsp plugin local cmp_nvim_lsp = require("cmp_nvim_lsp") - -- import typescript plugin - local typescript = require("typescript") - local keymap = vim.keymap -- for conciseness - -- enable keybinds only for when lsp server available + local opts = { noremap = true, silent = true } local on_attach = function(client, bufnr) - -- keybind options - local opts = { noremap = true, silent = true, buffer = bufnr } + opts.buffer = bufnr -- set keybinds opts.desc = "Show LSP references" @@ -46,7 +38,7 @@ return { 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", ":IncRename ", opts) -- 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 @@ -65,18 +57,6 @@ return { opts.desc = "Restart LSP" keymap.set("n", "rs", ":LspRestart", opts) -- mapping to restart lsp if necessary - - -- typescript specific keymaps (e.g. rename file and update imports) - if client.name == "tsserver" then - opts.desc = "Rename file and update file imports" - keymap.set("n", "rf", ":TypescriptRenameFile") -- rename file and update imports - - opts.desc = "Rename file and update file imports" - keymap.set("n", "oi", ":TypescriptOrganizeImports", opts) -- organize imports (not in youtube nvim video) - - opts.desc = "Remove unused imports" - keymap.set("n", "ru", ":TypescriptRemoveUnused", opts) -- remove unused variables (not in youtube nvim video) - end end -- used to enable autocompletion (assign to every lsp server config) @@ -97,11 +77,9 @@ return { }) -- configure typescript server with plugin - typescript.setup({ - server = { + lspconfig["tsserver"].setup({ capabilities = capabilities, on_attach = on_attach, - }, }) -- configure css server @@ -142,6 +120,12 @@ return { filetypes = { "html", "typescriptreact", "javascriptreact", "css", "sass", "scss", "less", "svelte" }, }) + -- configure python server + lspconfig["pyright"].setup({ + capabilities = capabilities, + on_attach = on_attach, + }) + -- configure lua server (with special settings) lspconfig["lua_ls"].setup({ capabilities = capabilities, diff --git a/.config/nvim/lua/josean/plugins/lsp/mason.lua b/.config/nvim/lua/josean/plugins/lsp/mason.lua index ff8bd92..c1a3c10 100644 --- a/.config/nvim/lua/josean/plugins/lsp/mason.lua +++ b/.config/nvim/lua/josean/plugins/lsp/mason.lua @@ -5,17 +5,25 @@ return { "jayp0521/mason-null-ls.nvim", }, config = function() - -- import mason plugin safely + -- import mason local mason = require("mason") - -- import mason-lspconfig plugin safely + -- import mason-lspconfig local mason_lspconfig = require("mason-lspconfig") - -- import mason-null-ls plugin safely + -- import mason-null-ls local mason_null_ls = require("mason-null-ls") - -- enable mason - mason.setup() + -- enable mason and configure icons + mason.setup({ + ui = { + icons = { + package_installed = "✓", + package_pending = "➜", + package_uninstalled = "✗" + } + } + }) mason_lspconfig.setup({ -- list of servers for mason to install @@ -29,6 +37,7 @@ return { "graphql", "emmet_ls", "prismals", + "pyright" }, -- auto-install configured servers (with lspconfig) automatic_installation = true, -- not the same as ensure_installed diff --git a/.config/nvim/lua/josean/plugins/nvim-cmp.lua b/.config/nvim/lua/josean/plugins/nvim-cmp.lua index e20de43..35fe5bc 100644 --- a/.config/nvim/lua/josean/plugins/nvim-cmp.lua +++ b/.config/nvim/lua/josean/plugins/nvim-cmp.lua @@ -4,10 +4,10 @@ return { dependencies = { "hrsh7th/cmp-buffer", -- source for text in buffer "hrsh7th/cmp-path", -- source for file system paths - "onsails/lspkind.nvim", "L3MON4D3/LuaSnip", -- snippet engine "saadparwaiz1/cmp_luasnip", -- for autocompletion "rafamadriz/friendly-snippets", -- useful snippets + "onsails/lspkind.nvim", -- vs-code like pictograms }, config = function() local cmp = require("cmp") @@ -16,12 +16,14 @@ return { local lspkind = require("lspkind") - -- load vs-code like snippets from plugins (e.g. friendly-snippets) + -- loads vscode style snippets from installed plugins (e.g. friendly-snippets) require("luasnip.loaders.from_vscode").lazy_load() - vim.opt.completeopt = "menu,menuone,noselect" cmp.setup({ - snippet = { + completion = { + completeopt = "menu,menuone,preview,noselect", + }, + snippet = { -- configure how nvim-cmp interacts with snippet engine expand = function(args) luasnip.lsp_expand(args.body) end, @@ -37,12 +39,12 @@ return { }), -- sources for autocompletion sources = cmp.config.sources({ - { name = "nvim_lsp" }, -- lsp + { name = "nvim_lsp" }, { name = "luasnip" }, -- snippets { name = "buffer" }, -- text within current buffer { name = "path" }, -- file system paths }), - -- configure lspkind for vs-code like icons + -- configure lspkind for vs-code like pictograms in completion menu formatting = { format = lspkind.cmp_format({ maxwidth = 50, diff --git a/.config/nvim/lua/josean/plugins/nvim-tree.lua b/.config/nvim/lua/josean/plugins/nvim-tree.lua index 6919e41..10f2da5 100644 --- a/.config/nvim/lua/josean/plugins/nvim-tree.lua +++ b/.config/nvim/lua/josean/plugins/nvim-tree.lua @@ -14,10 +14,14 @@ return { -- configure nvim-tree nvimtree.setup({ view = { - width = 45, + width = 35, + relativenumber = true, }, -- change folder arrow icons renderer = { + indent_markers = { + enable = true + }, icons = { glyphs = { folder = { diff --git a/.config/nvim/lua/josean/plugins/telescope.lua b/.config/nvim/lua/josean/plugins/telescope.lua index 30d3a70..c96b272 100644 --- a/.config/nvim/lua/josean/plugins/telescope.lua +++ b/.config/nvim/lua/josean/plugins/telescope.lua @@ -4,56 +4,33 @@ return { dependencies = { "nvim-lua/plenary.nvim", { "nvim-telescope/telescope-fzf-native.nvim", build = "make" }, - "nvim-telescope/telescope-ui-select.nvim", "nvim-tree/nvim-web-devicons", - "ThePrimeagen/harpoon", }, config = function() - -- import telescope plugin safely local telescope = require("telescope") - - -- import telescope actions safely local actions = require("telescope.actions") - -- import telescope-ui-select safely - local themes = require("telescope.themes") - - -- configure telescope telescope.setup({ - -- configure custom mappings defaults = { - path_display = { "truncate" }, + path_display = { "truncate " }, mappings = { i = { [""] = actions.move_selection_previous, -- move to prev result [""] = actions.move_selection_next, -- move to next result - [""] = actions.send_selected_to_qflist + actions.open_qflist, -- send selected to quickfixlist + [""] = actions.send_selected_to_qflist + actions.open_qflist, }, }, }, - extensions = { - ["ui-select"] = { - themes.get_dropdown({}), - }, - }, }) telescope.load_extension("fzf") - telescope.load_extension("ui-select") - telescope.load_extension("harpoon") -- set keymaps local keymap = vim.keymap -- for conciseness - keymap.set("n", "ff", "Telescope find_files", { desc = "Fuzzy find files in cwd" }) -- find files within current working directory, respects .gitignore - keymap.set("n", "fr", "Telescope oldfiles", { desc = "Fuzzy find recent files" }) -- find previously opened files - keymap.set("n", "fs", "Telescope live_grep", { desc = "Find string in cwd" }) -- find string in current working directory as you type - keymap.set("n", "fc", "Telescope grep_string", { desc = "Find string under cursor in cwd" }) -- find string under cursor in current working directory - keymap.set("n", "fb", "Telescope buffers", { desc = "Show open buffers" }) -- list open buffers in current neovim instance - keymap.set("n", "hf", "Telescope harpoon marks", { desc = "Show harpoon marks" }) -- show harpoon marks - keymap.set("n", "gc", "Telescope git_commits", { desc = "Show git commits" }) -- list all git commits (use to checkout) ["gc" for git commits] - keymap.set("n", "gfc", "Telescope git_bcommits", { desc = "Show git commits for current buffer" }) -- list git commits for current file/buffer (use to checkout) ["gfc" for git file commits] - keymap.set("n", "gb", "Telescope git_branches", { desc = "Show git branches" }) -- list git branches (use to checkout) ["gb" for git branch] - keymap.set("n", "gs", "Telescope git_status", { desc = "Show current git changes per file" }) -- list current changes per file with diff preview ["gs" for git status] + keymap.set("n", "ff", "Telescope find_files", { desc = "Fuzzy find files in cwd" }) + keymap.set("n", "fr", "Telescope oldfiles", { desc = "Fuzzy find recent files" }) + keymap.set("n", "fs", "Telescope live_grep", { desc = "Find string in cwd" }) + keymap.set("n", "fc", "Telescope grep_string", { desc = "Find string under cursor in cwd" }) end, }