New code for lsp configuration video

This commit is contained in:
josean-dev 2023-09-01 21:40:47 -04:00
parent 976c03191b
commit 5accc8bf38
8 changed files with 49 additions and 71 deletions

View file

@ -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" },
},

View file

@ -0,0 +1,4 @@
return {
"stevearc/dressing.nvim",
event = "VeryLazy",
}

View file

@ -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",
}

View file

@ -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" }, "<leader>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", "<leader>rn", ":IncRename ", opts) -- smart rename
keymap.set("n", "<leader>rn", vim.lsp.buf.rename, opts) -- smart rename
opts.desc = "Show buffer diagnostics"
keymap.set("n", "<leader>D", "<cmd>Telescope diagnostics bufnr=0<CR>", opts) -- show diagnostics for file
@ -65,18 +57,6 @@ return {
opts.desc = "Restart LSP"
keymap.set("n", "<leader>rs", ":LspRestart<CR>", 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", "<leader>rf", ":TypescriptRenameFile<CR>") -- rename file and update imports
opts.desc = "Rename file and update file imports"
keymap.set("n", "<leader>oi", ":TypescriptOrganizeImports<CR>", opts) -- organize imports (not in youtube nvim video)
opts.desc = "Remove unused imports"
keymap.set("n", "<leader>ru", ":TypescriptRemoveUnused<CR>", 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,

View file

@ -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

View file

@ -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,

View file

@ -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 = {

View file

@ -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 = {
["<C-k>"] = actions.move_selection_previous, -- move to prev result
["<C-j>"] = actions.move_selection_next, -- move to next result
["<C-q>"] = actions.send_selected_to_qflist + actions.open_qflist, -- send selected to quickfixlist
["<C-q>"] = 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", "<leader>ff", "<cmd>Telescope find_files<cr>", { desc = "Fuzzy find files in cwd" }) -- find files within current working directory, respects .gitignore
keymap.set("n", "<leader>fr", "<cmd>Telescope oldfiles<cr>", { desc = "Fuzzy find recent files" }) -- find previously opened files
keymap.set("n", "<leader>fs", "<cmd>Telescope live_grep<cr>", { desc = "Find string in cwd" }) -- find string in current working directory as you type
keymap.set("n", "<leader>fc", "<cmd>Telescope grep_string<cr>", { desc = "Find string under cursor in cwd" }) -- find string under cursor in current working directory
keymap.set("n", "<leader>fb", "<cmd>Telescope buffers<cr>", { desc = "Show open buffers" }) -- list open buffers in current neovim instance
keymap.set("n", "<leader>hf", "<cmd>Telescope harpoon marks<cr>", { desc = "Show harpoon marks" }) -- show harpoon marks
keymap.set("n", "<leader>gc", "<cmd>Telescope git_commits<cr>", { desc = "Show git commits" }) -- list all git commits (use <cr> to checkout) ["gc" for git commits]
keymap.set("n", "<leader>gfc", "<cmd>Telescope git_bcommits<cr>", { desc = "Show git commits for current buffer" }) -- list git commits for current file/buffer (use <cr> to checkout) ["gfc" for git file commits]
keymap.set("n", "<leader>gb", "<cmd>Telescope git_branches<cr>", { desc = "Show git branches" }) -- list git branches (use <cr> to checkout) ["gb" for git branch]
keymap.set("n", "<leader>gs", "<cmd>Telescope git_status<cr>", { desc = "Show current git changes per file" }) -- list current changes per file with diff preview ["gs" for git status]
keymap.set("n", "<leader>ff", "<cmd>Telescope find_files<cr>", { desc = "Fuzzy find files in cwd" })
keymap.set("n", "<leader>fr", "<cmd>Telescope oldfiles<cr>", { desc = "Fuzzy find recent files" })
keymap.set("n", "<leader>fs", "<cmd>Telescope live_grep<cr>", { desc = "Find string in cwd" })
keymap.set("n", "<leader>fc", "<cmd>Telescope grep_string<cr>", { desc = "Find string under cursor in cwd" })
end,
}