From 32cacdd82f6c84d4336eab1464bf42a8dbb597b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Av=C3=A9?= Date: Mon, 8 May 2023 17:03:45 +0200 Subject: [PATCH] Prevent neovim from crashing when no plugins are installed yet --- nvim/init.lua | 49 ++++++++++++----------------------- nvim/lua/autopairs_config.lua | 13 ++++++---- nvim/lua/lsp_config.lua | 5 +++- nvim/lua/lualine_config.lua | 7 ++++- nvim/lua/nvim_tree_config.lua | 26 +++++++++++++++++++ nvim/lua/plugins.lua | 11 +++++--- nvim/lua/telescope_config.lua | 6 ++++- nvim/lua/trim_config.lua | 13 ++++++++++ 8 files changed, 87 insertions(+), 43 deletions(-) create mode 100644 nvim/lua/nvim_tree_config.lua create mode 100644 nvim/lua/trim_config.lua diff --git a/nvim/init.lua b/nvim/init.lua index c4003a7..8a6834f 100644 --- a/nvim/init.lua +++ b/nvim/init.lua @@ -5,41 +5,26 @@ require('lsp_config') require('telescope_config') require('lualine_config') require('mappings') +require('trim_config') +require('nvim_tree_config') -require('trim').setup({ - ft_blocklist= {"markdown", "vimwiki"}, - patterns = { - [[%s/\s\+$//e]], -- remove unwanted spaces - [[%s/\($\n\s*\)\+\%$//]], -- trim last line - [[%s/\%^\n\+//]], -- trim first line - }, -}) +local hasleap, leap = pcall(require, 'leap') +if hasleap then + leap.set_default_keymaps() +end -require('leap').set_default_keymaps() -require('Comment').setup() -require("nvim-tree").setup({ - sort_by = "case_sensitive", - sync_root_with_cwd = true, - update_focused_file = { - update_root = true, - }, - view = { - adaptive_size = true, - mappings = { - list = { - {key = "cd", action = "cd"} - } - }, - }, - renderer = { - group_empty = true, - }, - filters = { - dotfiles = true, - }, -}) +local hasComment, Comment = pcall(require, 'Comment') +if hasComment then + Comment.setup() +end + +local hastrouble, trouble = pcall(require, 'trouble') +if hastrouble then + trouble.setup() +end + +pcall(function() vim.cmd("colorscheme material") end) -vim.cmd("colorscheme material") vim.api.nvim_create_autocmd("FileType", { pattern = "make", command = [[set tabstop=8 shiftwidth=8 softtabstop=0 noexpandtab]] }) vim.api.nvim_create_autocmd("FileType", { pattern = "vimwiki", command = [[setlocal shiftwidth=2 softtabstop=2 expandtab]] }) vim.api.nvim_create_autocmd("FileType", { pattern = "vimwiki", command = [[setlocal filetype=markdown]] }) diff --git a/nvim/lua/autopairs_config.lua b/nvim/lua/autopairs_config.lua index 8b9052b..10566ad 100644 --- a/nvim/lua/autopairs_config.lua +++ b/nvim/lua/autopairs_config.lua @@ -1,5 +1,8 @@ -local npairs = require "nvim-autopairs" -npairs.setup { - check_ts = true, -} -npairs.add_rules(require "nvim-autopairs.rules.endwise-lua") +local hasnpairs, npairs = pcall(require, "nvim-autopairs") + +if hasnpairs then + npairs.setup { + check_ts = true, + } + npairs.add_rules(require "nvim-autopairs.rules.endwise-lua") +end diff --git a/nvim/lua/lsp_config.lua b/nvim/lua/lsp_config.lua index 5e718aa..e6b4492 100644 --- a/nvim/lua/lsp_config.lua +++ b/nvim/lua/lsp_config.lua @@ -1,5 +1,8 @@ -- Setup nvim-cmp.lsp -local cmp = require'cmp' +local hascmp, cmp = pcall(require, "cmp") +if not hascmp then + return +end local feedkey = function(key, mode) vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, true, true), mode, true) diff --git a/nvim/lua/lualine_config.lua b/nvim/lua/lualine_config.lua index fa21c8d..749546e 100644 --- a/nvim/lua/lualine_config.lua +++ b/nvim/lua/lualine_config.lua @@ -1,8 +1,13 @@ +local haslualine, lualine = pcall(require, "lualine") +if not haslualine then + return +end + local function getWords() return tostring(vim.fn.wordcount().words) .. " Words" end -require('lualine').setup({ +lualine.setup({ options = { theme = 'nightfly', }, diff --git a/nvim/lua/nvim_tree_config.lua b/nvim/lua/nvim_tree_config.lua new file mode 100644 index 0000000..28963fc --- /dev/null +++ b/nvim/lua/nvim_tree_config.lua @@ -0,0 +1,26 @@ +local hastree, tree = pcall(require, "nvim-tree") +if not hastree then + return +end + +require("nvim-tree").setup({ + sort_by = "case_sensitive", + sync_root_with_cwd = true, + update_focused_file = { + update_root = true, + }, + view = { + adaptive_size = true, + mappings = { + list = { + {key = "cd", action = "cd"} + } + }, + }, + renderer = { + group_empty = true, + }, + filters = { + dotfiles = true, + }, +}) diff --git a/nvim/lua/plugins.lua b/nvim/lua/plugins.lua index 51b652f..e856df8 100644 --- a/nvim/lua/plugins.lua +++ b/nvim/lua/plugins.lua @@ -31,11 +31,16 @@ require('packer').startup(function(use) use {'nvim-lualine/lualine.nvim', requires = { 'kyazdani42/nvim-web-devicons', opt = true }} use {'nvim-telescope/telescope-fzf-native.nvim', run = 'cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Release && cmake --build build --config Release && cmake --install build --prefix build'} use {'nvim-telescope/telescope.nvim', requires = {{'nvim-lua/plenary.nvim'}}} - use {'nvim-treesitter/nvim-treesitter', run = ':TSUpdate'} + use { + 'nvim-treesitter/nvim-treesitter', + run = function() + local ts_update = require('nvim-treesitter.install').update({ with_sync = true }) + ts_update() + end, + } use { "folke/trouble.nvim", - requires = "kyazdani42/nvim-web-devicons", - config = function() require("trouble").setup {} end + requires = "kyazdani42/nvim-web-devicons" } use { 'kyazdani42/nvim-tree.lua', diff --git a/nvim/lua/telescope_config.lua b/nvim/lua/telescope_config.lua index e086df5..b8aaf9a 100644 --- a/nvim/lua/telescope_config.lua +++ b/nvim/lua/telescope_config.lua @@ -1,6 +1,10 @@ local M = {} -local telescope = require("telescope") +local hastelescope, telescope= pcall(require, "telescope") + +if not hastelescope then + return M +end M.project_files = function() local opts = require('telescope.themes').get_ivy({}) -- define here if you want to define something diff --git a/nvim/lua/trim_config.lua b/nvim/lua/trim_config.lua new file mode 100644 index 0000000..1033553 --- /dev/null +++ b/nvim/lua/trim_config.lua @@ -0,0 +1,13 @@ +local hastrim, trim = pcall(require, "trim") +if not hastrim then + return +end + +trim.setup({ + ft_blocklist= {"markdown", "vimwiki"}, + patterns = { + [[%s/\s\+$//e]], -- remove unwanted spaces + [[%s/\($\n\s*\)\+\%$//]], -- trim last line + [[%s/\%^\n\+//]], -- trim first line + }, +})