Last active
February 26, 2023 19:53
-
-
Save toabr/dae47d074e39a34855a8dab938a946f1 to your computer and use it in GitHub Desktop.
Basic Nvim Lsp Config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
---------------------------------------------------------------- | |
-- Basic Nvim Lsp Config | |
---------------------------------------------------------------- | |
--[[ | |
Requirements: | |
• git | |
• g++ | |
• node | |
Plugins: | |
• packer.nvim | |
• nvim-lspconfig | |
• mason.nvim | |
• mason-lspconfig.nvim | |
• nvim-cmp | |
• cmp-nvim-lsp | |
• cmp_luasnip | |
• LuaSnip | |
• friendly-snippets | |
• nvim-treesitter | |
• nord-vim | |
Install: | |
• clean old nvim configuration | |
$ rm -rf ~/.config/nvim ~/.local/share/nvim ~/.local/state/nvim && mkdir ~/.config/nvim | |
• copy init.lua to ~/.config/nvim/ | |
• open nvim ... wait | |
• restart nvim | |
]]-- | |
---------------------------------------------------------------- | |
-- Packer | |
---------------------------------------------------------------- | |
local ensure_packer = function() | |
local fn = vim.fn | |
local install_path = fn.stdpath('data')..'/site/pack/packer/start/packer.nvim' | |
if fn.empty(fn.glob(install_path)) > 0 then | |
fn.system({'git', 'clone', '--depth', '1', 'https://github.com/wbthomason/packer.nvim', install_path}) | |
vim.cmd [[packadd packer.nvim]] | |
return true | |
end | |
return false | |
end | |
local packer_bootstrap = ensure_packer() | |
require('packer').startup(function(use) | |
use 'wbthomason/packer.nvim' | |
-- lsp support | |
use "neovim/nvim-lspconfig" | |
use "williamboman/mason.nvim" | |
use "williamboman/mason-lspconfig.nvim" | |
use 'hrsh7th/nvim-cmp' -- Autocompletion plugin | |
use 'hrsh7th/cmp-nvim-lsp' -- LSP source | |
use 'saadparwaiz1/cmp_luasnip' -- Snippets source | |
use 'L3MON4D3/LuaSnip' -- Snippets engine | |
use "rafamadriz/friendly-snippets" -- Snippets | |
-- treesitter | |
use 'nvim-treesitter/nvim-treesitter' | |
-- colorscheme | |
use 'arcticicestudio/nord-vim' | |
-- Automatically set up your configuration after cloning packer.nvim | |
-- Put this at the end after all plugins | |
if packer_bootstrap then | |
require('packer').sync() | |
end | |
end) | |
---------------------------------------------------------------- | |
-- LSP settings | |
---------------------------------------------------------------- | |
-- protect us from errors on first boot | |
local status_ok, _ = pcall(require, "lspconfig") | |
if not status_ok then | |
return | |
end | |
-- Diagnostic keymaps | |
local opts = { noremap=true, silent=true } | |
vim.keymap.set('n', '<space>e', vim.diagnostic.open_float, opts) | |
vim.keymap.set('n', '[d', vim.diagnostic.goto_prev, opts) | |
vim.keymap.set('n', ']d', vim.diagnostic.goto_next, opts) | |
vim.keymap.set('n', '<space>q', vim.diagnostic.setloclist, opts) | |
-- This function gets run when an LSP connects to a particular buffer. | |
local on_attach = function(_, bufnr) | |
local nmap = function(keys, func, desc) | |
if desc then | |
desc = 'LSP: ' .. desc | |
end | |
vim.keymap.set('n', keys, func, { buffer = bufnr, desc = desc }) | |
end | |
-- See `:help vim.lsp.*` for documentation | |
nmap('<leader>rn', vim.lsp.buf.rename, '[R]e[n]ame') | |
nmap('<leader>ca', vim.lsp.buf.code_action, '[C]ode [A]ction') | |
nmap('gd', vim.lsp.buf.definition, '[G]oto [D]efinition') | |
nmap('gr', vim.lsp.buf.references, '[G]oto [R]eferences') | |
nmap('gi', vim.lsp.buf.implementation, '[G]oto [I]mplementation') | |
nmap('<leader>D', vim.lsp.buf.type_definition, 'Type [D]efinition') | |
nmap('<leader>ws', vim.lsp.buf.workspace_symbol, '[W]orkspace [S]ymbols') | |
-- See `:help K` for why this keymap | |
nmap('K', vim.lsp.buf.hover, 'Hover Documentation') | |
nmap('<C-k>', vim.lsp.buf.signature_help, 'Signature Documentation') | |
-- Lesser used LSP functionality | |
nmap('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration') | |
nmap('<leader>wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder') | |
nmap('<leader>wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder') | |
nmap('<leader>wl', function() | |
print(vim.inspect(vim.lsp.buf.list_workspace_folders())) | |
end, '[W]orkspace [L]ist Folders') | |
-- Create a command `:Format` local to the LSP buffer | |
vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_) | |
vim.lsp.buf.format() | |
end, { desc = 'Format current buffer with LSP' }) | |
end | |
---------------------------------------------------------------- | |
-- Options | |
---------------------------------------------------------------- | |
vim.g.mapleader = ' ' | |
vim.g.maplocalleader = ' ' | |
vim.o.undofile = true | |
vim.o.signcolumn = "yes" | |
vim.o.number = true | |
vim.cmd.colorscheme 'nord' | |
vim.o.termguicolors = true | |
---------------------------------------------------------------- | |
-- Mason | |
---------------------------------------------------------------- | |
-- Enable the following language servers | |
local servers = { | |
--tsserver = {}, | |
lua_ls = { | |
Lua = { | |
diagnostics = { globals = {'vim'} }, | |
workspace = { checkThirdParty = false }, | |
telemetry = { enable = false }, | |
}, | |
}, | |
} | |
-- Setup mason so it can manage external tooling | |
require("mason").setup() | |
-- Ensure the servers above are installed | |
local mason_lspconfig = require 'mason-lspconfig' | |
mason_lspconfig.setup { | |
ensure_installed = vim.tbl_keys(servers), | |
} | |
-- Add additional capabilities supported by nvim-cmp | |
local capabilities = vim.lsp.protocol.make_client_capabilities() | |
capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities) | |
-- Enable some language servers with the additional completion capabilities | |
mason_lspconfig.setup_handlers { | |
function(server_name) | |
require('lspconfig')[server_name].setup { | |
capabilities = capabilities, | |
on_attach = on_attach, | |
settings = servers[server_name], | |
} | |
end, | |
} | |
---------------------------------------------------------------- | |
-- nvim-cmp setup | |
---------------------------------------------------------------- | |
local cmp = require 'cmp' | |
local luasnip = require 'luasnip' | |
require("luasnip.loaders.from_vscode").lazy_load() | |
-- stop cursor jumps on TAB | |
luasnip.config.set_config { | |
region_check_events = 'InsertEnter' | |
} | |
cmp.setup { | |
snippet = { | |
expand = function(args) | |
luasnip.lsp_expand(args.body) | |
end, | |
}, | |
mapping = cmp.mapping.preset.insert({ | |
['<C-u>'] = cmp.mapping.scroll_docs(-4), -- Up | |
['<C-d>'] = cmp.mapping.scroll_docs(4), -- Down | |
-- C-b (back) C-f (forward) for snippet placeholder navigation. | |
['<C-Space>'] = cmp.mapping.complete(), | |
['<CR>'] = cmp.mapping.confirm { select = false }, | |
['<Tab>'] = cmp.mapping(function(fallback) | |
if cmp.visible() then | |
cmp.select_next_item() | |
elseif luasnip.expand_or_jumpable() then | |
luasnip.expand_or_jump() | |
else | |
fallback() | |
end | |
end, { 'i', 's' }), | |
['<S-Tab>'] = cmp.mapping(function(fallback) | |
if cmp.visible() then | |
cmp.select_prev_item() | |
elseif luasnip.jumpable(-1) then | |
luasnip.jump(-1) | |
else | |
fallback() | |
end | |
end, { 'i', 's' }), | |
}), | |
sources = { | |
{ name = 'nvim_lsp' }, | |
{ name = 'luasnip' }, | |
}, | |
} | |
---------------------------------------------------------------- | |
-- treesitter | |
---------------------------------------------------------------- | |
local status_ok, treesitter = pcall(require, "nvim-treesitter") | |
if not status_ok then | |
return | |
end | |
require'nvim-treesitter.configs'.setup { | |
ensure_installed = { "lua", "vim", "help" }, | |
sync_install = false, | |
auto_install = true, | |
ignore_install = { "javascript" }, | |
highlight = { | |
enable = true, | |
disable = { "css", "scss", "html" }, | |
additional_vim_regex_highlighting = false, | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment