From 0002e01d234c93c765f40a4ea8469769a8a4092b Mon Sep 17 00:00:00 2001 From: hellisabove <59116609+hellisabove@users.noreply.github.com> Date: Thu, 26 Jan 2023 00:50:03 +0200 Subject: [PATCH] updated neovim configuration added telescop and fuzzy finding along with lsp and git integration --- config/nvim/init.lua | 7 ++ config/nvim/lua/gitsign.lua | 6 ++ config/nvim/lua/keymaps.lua | 17 +++++ config/nvim/lua/lsp/lspconfig.lua | 111 ++++++++++++++++++++++++++++++ config/nvim/lua/lsp/lspsaga.lua | 19 +++++ config/nvim/lua/lsp/mason.lua | 28 ++++++++ config/nvim/lua/lsp/null-ls.lua | 41 +++++++++++ config/nvim/lua/nvimcmp.lua | 38 ++++++++++ config/nvim/lua/p-tree.lua | 2 - config/nvim/lua/plugins.lua | 47 ++++++++++++- config/nvim/lua/teleop.lua | 23 +++++++ 11 files changed, 336 insertions(+), 3 deletions(-) create mode 100644 config/nvim/lua/gitsign.lua create mode 100644 config/nvim/lua/lsp/lspconfig.lua create mode 100644 config/nvim/lua/lsp/lspsaga.lua create mode 100644 config/nvim/lua/lsp/mason.lua create mode 100644 config/nvim/lua/lsp/null-ls.lua create mode 100644 config/nvim/lua/nvimcmp.lua create mode 100644 config/nvim/lua/teleop.lua diff --git a/config/nvim/init.lua b/config/nvim/init.lua index fc69b45..378fd77 100644 --- a/config/nvim/init.lua +++ b/config/nvim/init.lua @@ -39,3 +39,10 @@ require('p-tree') require('colorscheme') require('keymaps') require('bar') +require('teleop') +require('nvimcmp') +require('gitsign') +require('lsp.mason') +require('lsp.lspsaga') +require('lsp.lspconfig') +require('lsp.null-ls') diff --git a/config/nvim/lua/gitsign.lua b/config/nvim/lua/gitsign.lua new file mode 100644 index 0000000..c25a8f2 --- /dev/null +++ b/config/nvim/lua/gitsign.lua @@ -0,0 +1,6 @@ +local setup, gitsigns = pcall(require, "gitsigns") +if not setup then + return +end + +gitsigns.setup() diff --git a/config/nvim/lua/keymaps.lua b/config/nvim/lua/keymaps.lua index f3e4ea6..b1f4cc8 100644 --- a/config/nvim/lua/keymaps.lua +++ b/config/nvim/lua/keymaps.lua @@ -12,3 +12,20 @@ keymap.set("n","sv","v") -- split window vertically keymap.set("n","sh","s") -- split window horizontally keymap.set("n","se","=") -- make split windows equal width keymap.set("n","sx",":close") -- close current split window + +-- nvim-tree +keymap.set("n", "e", ":NvimTreeToggle") -- toggle file explorer + +-- telescope +keymap.set("n","ff", "Telescope find_files") +keymap.set("n","fs", "Telescope live_grep") +keymap.set("n","fc", "Telescope grep_string") +keymap.set("n","fb", "Telescope buffers") +keymap.set("n","fh", "Telescope help_tags") +keymap.set("n", "gc", "Telescope git_commits") +keymap.set("n", "gfc", "Telescope git_bcommits") +keymap.set("n", "gb", "Telescope git_branches") +keymap.set("n", "gs", "Telescope git_status") + +-- restart lsp server +keymap.set("n", "rs", ":LspRestart") -- mapping to restart lsp if necessary diff --git a/config/nvim/lua/lsp/lspconfig.lua b/config/nvim/lua/lsp/lspconfig.lua new file mode 100644 index 0000000..78b94a3 --- /dev/null +++ b/config/nvim/lua/lsp/lspconfig.lua @@ -0,0 +1,111 @@ +-- import lspconfig plugin safely +local lspconfig_status, lspconfig = pcall(require, "lspconfig") +if not lspconfig_status then + return +end + +-- import cmp-nvim-lsp plugin safely +local cmp_nvim_lsp_status, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp") +if not cmp_nvim_lsp_status then + return +end + +-- import typescript plugin safely +local typescript_setup, typescript = pcall(require, "typescript") +if not typescript_setup then + return +end + +local keymap = vim.keymap -- for conciseness + +-- enable keybinds only for when lsp server available +local on_attach = function(client, bufnr) + -- keybind options + local opts = { noremap = true, silent = true, buffer = bufnr } + + -- set keybinds + keymap.set("n", "gf", "Lspsaga lsp_finder", opts) -- show definition, references + keymap.set("n", "gD", "lua vim.lsp.buf.declaration()", opts) -- got to declaration + keymap.set("n", "gd", "Lspsaga peek_definition", opts) -- see definition and make edits in window + keymap.set("n", "gi", "lua vim.lsp.buf.implementation()", opts) -- go to implementation + keymap.set("n", "ca", "Lspsaga code_action", opts) -- see available code actions + keymap.set("n", "rn", "Lspsaga rename", opts) -- smart rename + keymap.set("n", "D", "Lspsaga show_line_diagnostics", opts) -- show diagnostics for line + keymap.set("n", "d", "Lspsaga show_cursor_diagnostics", opts) -- show diagnostics for cursor + keymap.set("n", "[d", "Lspsaga diagnostic_jump_prev", opts) -- jump to previous diagnostic in buffer + keymap.set("n", "]d", "Lspsaga diagnostic_jump_next", opts) -- jump to next diagnostic in buffer + keymap.set("n", "K", "Lspsaga hover_doc", opts) -- show documentation for what is under cursor + keymap.set("n", "o", "LSoutlineToggle", opts) -- see outline on right hand side + + -- typescript specific keymaps (e.g. rename file and update imports) + if client.name == "tsserver" then + keymap.set("n", "rf", ":TypescriptRenameFile") -- rename file and update imports + keymap.set("n", "oi", ":TypescriptOrganizeImports") -- organize imports (not in youtube nvim video) + keymap.set("n", "ru", ":TypescriptRemoveUnused") -- remove unused variables (not in youtube nvim video) + end +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 + +-- configure html server +lspconfig["html"].setup({ + capabilities = capabilities, + on_attach = on_attach, +}) + +-- configure typescript server with plugin +typescript.setup({ + server = { + capabilities = capabilities, + on_attach = on_attach, + }, +}) + +-- configure css server +lspconfig["cssls"].setup({ + capabilities = capabilities, + on_attach = on_attach, +}) + +-- configure tailwindcss server +lspconfig["tailwindcss"].setup({ + capabilities = capabilities, + on_attach = on_attach, +}) + +-- configure emmet language server +lspconfig["emmet_ls"].setup({ + capabilities = capabilities, + on_attach = on_attach, + filetypes = { "html", "typescriptreact", "javascriptreact", "css", "sass", "scss", "less", "svelte" }, +}) + +-- configure lua server (with special settings) +lspconfig["sumneko_lua"].setup({ + capabilities = capabilities, + on_attach = on_attach, + settings = { -- custom settings for lua + Lua = { + -- make the language server recognize "vim" global + diagnostics = { + globals = { "vim" }, + }, + workspace = { + -- make language server aware of runtime files + library = { + [vim.fn.expand("$VIMRUNTIME/lua")] = true, + [vim.fn.stdpath("config") .. "/lua"] = true, + }, + }, + }, + }, +}) diff --git a/config/nvim/lua/lsp/lspsaga.lua b/config/nvim/lua/lsp/lspsaga.lua new file mode 100644 index 0000000..cd2274e --- /dev/null +++ b/config/nvim/lua/lsp/lspsaga.lua @@ -0,0 +1,19 @@ +local saga_status, saga = pcall(require, "lspsaga") +if not saga_status then + return +end + +saga.setup({ + scroll_preview = { + scroll_down = "", + scroll_up = "" + }, + definition = { + edit = "" + }, + ui = { + colors = { + normal_bg = "#022746", + }, + }, +}) diff --git a/config/nvim/lua/lsp/mason.lua b/config/nvim/lua/lsp/mason.lua new file mode 100644 index 0000000..eb75d0d --- /dev/null +++ b/config/nvim/lua/lsp/mason.lua @@ -0,0 +1,28 @@ +require("mason").setup({ + ui = { + icons = { + package_installed = "✓", + package_pending = "➜", + package_uninstalled = "✗", + }, + }, +}) + +require("mason-lspconfig").setup({ + ensure_installed = { + "sumneko_lua", + "clangd", + "rust_analyzer", + "vls", + }, + automatic_installation = true, +}) + +require("mason-null-ls").setup({ + ensure_installed = { + "prettier", + "stylua", + "eslint_d", + }, + automatic_installation = true, +}) diff --git a/config/nvim/lua/lsp/null-ls.lua b/config/nvim/lua/lsp/null-ls.lua new file mode 100644 index 0000000..8536d85 --- /dev/null +++ b/config/nvim/lua/lsp/null-ls.lua @@ -0,0 +1,41 @@ +local setup, null_ls = pcall(require, "null-ls") +if not setup then + return +end + +local formatting = null_ls.builtins.formatting +local diagnostics = null_ls.builtins.diagnostics + +local augroup = vim.api.nvim_create_augroup("LspFormatting", {}) + +null_ls.setup({ + sources = { + formatting.prettier, + formatting.stylua, + formatting.eslint_d.with({ + condition = function(utils) + return utils.root_has_file('.eslintrc.js') + end, + }), + }, + on_attach = function(current_client, bufnr) + if current_client.supports_method("textDocument/formatting") then + vim.api.nvim_clear_autocmds({ + group = augroup, + buffer = bufnr + }) + vim.api.nvim_create_autocmd("BufWritePre", { + group = augroup, + buffer = bufnr, + callback = function() + vim.lsp.buf.format({ + filter = function(client) + return client.name == "null-ls" + end, + bufnr = bufnr, + }) + end, + }) + end + end, +}) diff --git a/config/nvim/lua/nvimcmp.lua b/config/nvim/lua/nvimcmp.lua new file mode 100644 index 0000000..161b305 --- /dev/null +++ b/config/nvim/lua/nvimcmp.lua @@ -0,0 +1,38 @@ +local cmp_status, cmp = pcall(require, "cmp") +if not cmp_status then + return +end + +local luasnip_status, luasnip = pcall(require, "luasnip") +if not luasnip_status then + return +end + +-- load friendly-snippets +require("luasnip/loaders/from_vscode").lazy_load() + +vim.opt.completeopt = "menu,menuone,noselect" + +cmp.setup({ + snippet = { + expand = function(args) + luasnip.lsp_expand(args.body) + end, + }, + + mapping = cmp.mapping.preset.insert({ + [""] = cmp.mapping.select_prev_item(), -- previous suggestion + [""] = cmp.mapping.select_next_item(), -- next suggestion + [""] = cmp.mapping.scroll_docs(-4), + [""] = cmp.mapping.scroll_docs(4), + [""] = cmp.mapping.complete(), -- show completion suggestions + [""] = cmp.mapping.abort(), -- close completion window + [""] = cmp.mapping.confirm({ select = false }), + }), + + sources = cmp.config.sources({ + { name = "luasnip" }, + { name = "buffer" }, + { name = "path" }, + }), +}) diff --git a/config/nvim/lua/p-tree.lua b/config/nvim/lua/p-tree.lua index cd124ec..607b7eb 100644 --- a/config/nvim/lua/p-tree.lua +++ b/config/nvim/lua/p-tree.lua @@ -36,5 +36,3 @@ require("nvim-tree").setup({ } } }) - -vim.keymap.set('n','','NvimTreeToggle') diff --git a/config/nvim/lua/plugins.lua b/config/nvim/lua/plugins.lua index 82ccd38..3dcf563 100644 --- a/config/nvim/lua/plugins.lua +++ b/config/nvim/lua/plugins.lua @@ -2,12 +2,41 @@ vim.cmd [[packadd packer.nvim]] return require('packer').startup(function(use) use 'wbthomason/packer.nvim' - use 'folke/tokyonight.nvim' + + -- better bar use 'nvim-lualine/lualine.nvim' + + -- autocpompletion + use 'hrsh7th/nvim-cmp' + use 'hrsh7th/cmp-buffer' + use 'hrsh7th/cmp-path' + + -- snippets + use 'L3MON4D3/LuaSnip' + use 'saadparwaiz1/cmp_luasnip' + use 'rafamadriz/friendly-snippets' + + -- fuzzy finding + use { + 'nvim-telescope/telescope.nvim', + tag = '0.1.1', + requires = { + 'nvim-lua/plenary.nvim', + } + } + use { + 'nvim-telescope/telescope-fzf-native.nvim', + run = 'make' + } + + -- colorschemes + use 'folke/tokyonight.nvim' use { 'catppuccin/nvim', as = 'catppuccin' } + + -- neovim explorer use { 'nvim-tree/nvim-tree.lua', requires = { @@ -15,4 +44,20 @@ return require('packer').startup(function(use) }, tag = 'nightly' } + + -- managing & installing lsp servers + use 'williamboman/mason.nvim' + use 'williamboman/mason-lspconfig.nvim' + -- configuring lsp servers + use 'neovim/nvim-lspconfig' + use 'hrsh7th/cmp-nvim-lsp' + use 'onsails/lspkind.nvim' + use { 'glepnir/lspsaga.nvim', branch = main} + + -- formatting & linting + use 'jose-elias-alvarez/null-ls.nvim' -- configure formatters & linters + use 'jayp0521/mason-null-ls.nvim' -- bridges gap b/w mason & null-ls + + -- git integration + use 'lewis6991/gitsigns.nvim' end) diff --git a/config/nvim/lua/teleop.lua b/config/nvim/lua/teleop.lua new file mode 100644 index 0000000..46f395c --- /dev/null +++ b/config/nvim/lua/teleop.lua @@ -0,0 +1,23 @@ +local telescope_setup, telescope = pcall(require, "telescope") +if not telescope_setup then + return +end + +local actions_setup, actions = pcall(require, "telescope.actions") +if not actions_setup then + return +end + +telescope.setup({ + defaults = { + mappings = { + i = { + [""] = actions.move_selection_previous, + [""] = actions.move_selection_next, + [""] = actions.send_selected_to_qflist + actions.open_qflist, + } + } + } +}) + +telescope.load_extension("fzf")