Jelajahi Sumber

fix: more robust reloading (#1556)

kylo252 3 tahun lalu
induk
melakukan
e22f9a21c1

+ 9 - 27
README.md

@@ -123,43 +123,25 @@ To update LunarVim:
 
 ```bash
 cd ~/.local/share/lunarvim/lvim && git pull
-:PackerSync
+lvim +LvimCacheReset +PackerUpdate
 ```
 ## Known Issues
+
 If you get either of the following errors
 - init.lua:6: module 'bootstrap' not found:
 - /home/user/.config/nvim/config.lua not found, falling back to /home/user/.config/nvim/lv-config.lua
 
-Try the following:
+Try the following methods:
+1. clear up the cache files used by the startup processing. You can either pass it as an argument
 ```bash
-which lvim
-# if output is /usr/local/bin/lvim remove it
-sudo rm /usr/local/bin/lvim
-
-which lvim
-# if output is ~/.local/bin/lvim, open lvim and run :PackerSync.  That should get you to a working state
-
-# otherwise if `which lvim` returns `not found`, 
-Make sure the `lvim` file exists in `~/.local/bin/lvim`.  
-If the file exists,make sure `~/.local/bin` is in your PATH.  If not, [add it](https://www.lunarvim.org/02-after-install.html#add-lvim-to-path)
-either reinstall again or manually add the lunarvim launcher
-
-If the file doesn't exist, create the file
-cd ~/.local/bin
-touch lvim
-chmod 755 lvim
+lvim +LvimCacheReset
 ```
+or just call it manually when inside LunarVim `:LvimCacheReset`
 
-And then add the following to the lvim file you created.  Replace all `torvalds` with your user name
-
+2. make sure your `lvim` binary is up-to-date
 ```bash
-#!/bin/sh
-
-export LUNARVIM_CONFIG_DIR="${LUNARVIM_CONFIG_DIR:-/home/torvalds/.config/lvim}"
-export LUNARVIM_RUNTIME_DIR="${LUNARVIM_RUNTIME_DIR:-/home/torvalds/.local/share/lunarvim}"
-
-exec nvim -u "$LUNARVIM_RUNTIME_DIR/lvim/init.lua" "$@"
-
+LUNARVIM_RUNTIME_DIR="${LUNARVIM_RUNTIME_DIR:-$HOME/.local/share/lunarvim}"
+bash "$LUNARVIM_RUNTIME_DIR/utils/installer/install_bin.sh"
 ```
 
 ## Resources

+ 5 - 6
lua/bootstrap.lua

@@ -38,6 +38,8 @@ function M:init()
   self.cache_path = get_cache_dir()
 
   self.pack_dir = join_paths(self.runtime_dir, "site", "pack")
+  self.packer_install_dir = join_paths(self.runtime_dir, "site", "pack", "packer", "start", "packer.nvim")
+  self.packer_cache_path = join_paths(self.config_dir, "plugin", "packer_compiled.lua")
 
   if os.getenv "LUNARVIM_RUNTIME_DIR" then
     vim.opt.rtp:remove(join_paths(vim.fn.stdpath "data", "site"))
@@ -57,6 +59,7 @@ function M:init()
 
   -- FIXME: currently unreliable in unit-tests
   if not os.getenv "LVIM_TEST_ENV" then
+    vim.fn.mkdir(vim.fn.stdpath "cache", "p")
     require("impatient").setup {
       path = vim.fn.stdpath "cache" .. "/lvim_cache",
       enable_profiling = true,
@@ -69,12 +72,8 @@ function M:init()
   }
 
   require("plugin-loader"):init {
-    cache_path = self.cache_path,
-    runtime_dir = self.runtime_dir,
-    config_dir = self.config_dir,
-    install_path = join_paths(self.runtime_dir, "site", "pack", "packer", "start", "packer.nvim"),
-    package_root = join_paths(self.runtime_dir, "site", "pack"),
-    compile_path = join_paths(self.config_dir, "plugin", "packer_compiled.lua"),
+    package_root = self.pack_dir,
+    install_path = self.packer_install_dir,
   }
 
   return self

+ 1 - 0
lua/core/which-key.lua

@@ -209,6 +209,7 @@ M.config = function()
           },
           P = { "<cmd>edit ~/.cache/nvim/packer.nvim.log<cr>", "Open the Packer logfile" },
         },
+        r = { "<cmd>lua require('utils').reload_lv_config()<cr>", "Reload configurations" },
       },
       s = {
         name = "Search",

+ 19 - 1
lua/plugin-loader.lua

@@ -1,11 +1,15 @@
 local plugin_loader = {}
 
+local utils = require "utils"
+local Log = require "core.log"
+-- we need to reuse this outside of init()
+local compile_path = get_config_dir() .. "/plugin/packer_compiled.lua"
+
 function plugin_loader:init(opts)
   opts = opts or {}
 
   local install_path = opts.install_path or vim.fn.stdpath "data" .. "/site/pack/packer/start/packer.nvim"
   local package_root = opts.package_root or vim.fn.stdpath "data" .. "/site/pack"
-  local compile_path = opts.compile_path or vim.fn.stdpath "config" .. "/plugin/packer_compile.lua"
 
   if vim.fn.empty(vim.fn.glob(install_path)) > 0 then
     vim.fn.system { "git", "clone", "--depth", "1", "https://github.com/wbthomason/packer.nvim", install_path }
@@ -32,6 +36,20 @@ function plugin_loader:init(opts)
   return self
 end
 
+function plugin_loader:cache_clear()
+  if vim.fn.delete(compile_path) == 0 then
+    Log:debug "deleted packer_compiled.lua"
+  end
+end
+
+function plugin_loader:cache_reset()
+  self.cache_clear()
+  require("packer").compile()
+  if utils.is_file(compile_path) then
+    Log:debug "generated packer_compiled.lua"
+  end
+end
+
 function plugin_loader:load(configurations)
   return self.packer.startup(function(use)
     for _, plugins in ipairs(configurations) do

+ 24 - 2
lua/utils/init.lua

@@ -93,9 +93,11 @@ function utils.reload_lv_config()
   vim.cmd("source " .. utils.join_paths(get_runtime_dir(), "lvim", "lua", "plugins.lua"))
   local plugins = require "plugins"
   utils.toggle_autoformat()
-  require("plugin-loader"):load { plugins, lvim.plugins }
-  vim.cmd ":PackerCompile"
+  local plugin_loader = require "plugin-loader"
+  plugin_loader:cache_reset()
+  plugin_loader:load { plugins, lvim.plugins }
   vim.cmd ":PackerInstall"
+  vim.cmd ":PackerCompile"
   -- vim.cmd ":PackerClean"
   local null_ls = require "lsp.null-ls"
   null_ls.setup(vim.bo.filetype, { force_reload = true })
@@ -118,6 +120,18 @@ function utils.gsub_args(args)
   return args
 end
 
+--- Returns a table with the default values that are missing.
+--- either paramter can be empty.
+--@param config (table) table containing entries that take priority over defaults
+--@param default_config (table) table contatining default values if found
+function utils.apply_defaults(config, default_config)
+  config = config or {}
+  default_config = default_config or {}
+  local new_config = vim.tbl_deep_extend("keep", vim.empty_dict(), config)
+  new_config = vim.tbl_deep_extend("keep", new_config, default_config)
+  return new_config
+end
+
 --- Checks whether a given path exists and is a file.
 --@param filename (string) path to check
 --@returns (bool)
@@ -132,6 +146,14 @@ function utils.join_paths(...)
   return result
 end
 
+function utils.lvim_cache_reset()
+  _G.__luacache.clear_cache()
+  _G.__luacache.save_cache()
+  require("plugin-loader"):cache_reset()
+end
+
+vim.cmd [[ command! LvimCacheReset lua require('utils').lvim_cache_reset() ]]
+
 return utils
 
 -- TODO: find a new home for these autocommands

+ 2 - 0
utils/installer/install.sh

@@ -294,6 +294,8 @@ function update_lvim() {
     git -C "$LUNARVIM_RUNTIME_DIR/lvim" merge --ff-only --progress ||
       echo "Unable to guarantee data integrity while updating. Please do that manually instead." && exit 1
   fi
+  echo "Clearing up old startup cache"
+  "$INSTALL_PREFIX/bin/lvim" --headless +LvimCacheReset +q
   echo "Your LunarVim installation is now up to date!"
 }
 

+ 33 - 0
utils/installer/install_bin.sh

@@ -0,0 +1,33 @@
+#!/usr/bin/env bash
+set -eo pipefail
+
+declare -r INSTALL_PREFIX="${INSTALL_PREFIX:-"$HOME/.local"}"
+
+declare -r XDG_DATA_HOME="${XDG_DATA_HOME:-"$HOME/.local/share"}"
+declare -r XDG_CACHE_HOME="${XDG_CACHE_HOME:-"$HOME/.cache"}"
+declare -r XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-"$HOME/.config"}"
+
+declare -r LUNARVIM_RUNTIME_DIR="${LUNARVIM_RUNTIME_DIR:-"$XDG_DATA_HOME/lunarvim"}"
+declare -r LUNARVIM_CONFIG_DIR="${LUNARVIM_CONFIG_DIR:-"$XDG_CONFIG_HOME/lvim"}"
+
+# TODO: Use a dedicated cache directory #1256
+declare -r LUNARVIM_CACHE_DIR="$XDG_CACHE_HOME/nvim"
+
+function setup_shim() {
+  if [ ! -d "$INSTALL_PREFIX/bin" ]; then
+    mkdir -p "$INSTALL_PREFIX/bin"
+  fi
+  cat >"$INSTALL_PREFIX/bin/lvim" <<EOF
+#!/bin/sh
+
+export LUNARVIM_CONFIG_DIR="\${LUNARVIM_CONFIG_DIR:-$LUNARVIM_CONFIG_DIR}"
+export LUNARVIM_RUNTIME_DIR="\${LUNARVIM_RUNTIME_DIR:-$LUNARVIM_RUNTIME_DIR}"
+export LUNARVIM_CACHE_DIR="\${LUNARVIM_CACHE_DIR:-$LUNARVIM_CACHE_DIR}"
+
+exec nvim -u "\$LUNARVIM_RUNTIME_DIR/lvim/init.lua" "\$@"
+EOF
+  chmod +x "$INSTALL_PREFIX/bin/lvim"
+}
+
+setup_shim "$@"
+echo "You can start LunarVim by running: $INSTALL_PREFIX/bin/lvim"