kylo252 3 лет назад
Родитель
Сommit
0481ec8ddd

+ 0 - 1
.github/workflows/install.yaml

@@ -23,7 +23,6 @@ jobs:
           - runner: macos-10.15
             os: osx
     runs-on: ${{ matrix.runner }}
-    if: github.event.pull_request.draft == false
     steps:
       - uses: actions/checkout@v2
 

+ 20 - 11
lua/lvim/bootstrap.lua

@@ -35,7 +35,7 @@ function _G.get_runtime_dir()
   local lvim_runtime_dir = os.getenv "LUNARVIM_RUNTIME_DIR"
   if not lvim_runtime_dir then
     -- when nvim is used directly
-    return vim.fn.stdpath "data"
+    return vim.call("stdpath", "data")
   end
   return lvim_runtime_dir
 end
@@ -45,7 +45,7 @@ end
 function _G.get_config_dir()
   local lvim_config_dir = os.getenv "LUNARVIM_CONFIG_DIR"
   if not lvim_config_dir then
-    return vim.fn.stdpath "config"
+    return vim.call("stdpath", "config")
   end
   return lvim_config_dir
 end
@@ -55,7 +55,7 @@ end
 function _G.get_cache_dir()
   local lvim_cache_dir = os.getenv "LUNARVIM_CACHE_DIR"
   if not lvim_cache_dir then
-    return vim.fn.stdpath "cache"
+    return vim.call("stdpath", "config")
   end
   return lvim_cache_dir
 end
@@ -70,6 +70,18 @@ function M:init(base_dir)
   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")
 
+  ---@meta overridden to use LUNARVIM_x_DIR instead, since a lot of plugins call this function interally
+  vim.fn.stdpath = function(what)
+    if what == "cache" then
+      return _G.get_cache_dir()
+    elseif what == "data" then
+      return _G.get_runtime_dir()
+    elseif what == "config" then
+      return _G.get_config_dir()
+    end
+    return vim.call("stdpath", what)
+  end
+
   ---Get the full path to LunarVim's base directory
   ---@return string
   function _G.get_lvim_base_dir()
@@ -78,13 +90,13 @@ function M:init(base_dir)
 
   if os.getenv "LUNARVIM_RUNTIME_DIR" then
     -- vim.opt.rtp:append(os.getenv "LUNARVIM_RUNTIME_DIR" .. path_sep .. "lvim")
-    vim.opt.rtp:remove(join_paths(vim.fn.stdpath "data", "site"))
-    vim.opt.rtp:remove(join_paths(vim.fn.stdpath "data", "site", "after"))
+    vim.opt.rtp:remove(join_paths(vim.call("stdpath", "data"), "site"))
+    vim.opt.rtp:remove(join_paths(vim.call("stdpath", "data"), "site", "after"))
     vim.opt.rtp:prepend(join_paths(self.runtime_dir, "site"))
     vim.opt.rtp:append(join_paths(self.runtime_dir, "site", "after"))
 
-    vim.opt.rtp:remove(vim.fn.stdpath "config")
-    vim.opt.rtp:remove(join_paths(vim.fn.stdpath "config", "after"))
+    vim.opt.rtp:remove(vim.call("stdpath", "config"))
+    vim.opt.rtp:remove(join_paths(vim.call("stdpath", "config"), "after"))
     vim.opt.rtp:prepend(self.config_dir)
     vim.opt.rtp:append(join_paths(self.config_dir, "after"))
     -- TODO: we need something like this: vim.opt.packpath = vim.opt.rtp
@@ -95,10 +107,7 @@ function M:init(base_dir)
   -- FIXME: currently unreliable in unit-tests
   if not in_headless then
     _G.PLENARY_DEBUG = false
-    require("lvim.impatient").setup {
-      path = join_paths(self.cache_dir, "lvim_cache"),
-      enable_profiling = true,
-    }
+    require "lvim.impatient"
   end
 
   require("lvim.config"):init()

+ 259 - 259
lua/lvim/impatient.lua

@@ -1,112 +1,46 @@
 -- modified version from https://github.com/lewis6991/impatient.nvim
 
 local vim = vim
-local uv = vim.loop
-local impatient_load_start = uv.hrtime()
 local api = vim.api
-local ffi = require "ffi"
-
-local get_option, set_option = api.nvim_get_option, api.nvim_set_option
-local get_runtime_file = api.nvim_get_runtime_file
+local uv = vim.loop
+local _loadfile = loadfile
+local get_runtime = api.nvim__get_runtime
+local fs_stat = uv.fs_stat
+local mpack = vim.mpack
 
-local impatient_dur
+local appdir = os.getenv "APPDIR"
 
 local M = {
-  cache = {},
-  profile = nil,
-  dirty = false,
-  path = nil,
+  chunks = {
+    cache = {},
+    profile = nil,
+    dirty = false,
+    path = vim.fn.stdpath "cache" .. "/luacache_chunks",
+  },
+  modpaths = {
+    cache = {},
+    profile = nil,
+    dirty = false,
+    path = vim.fn.stdpath "cache" .. "/luacache_modpaths",
+  },
   log = {},
 }
 
 _G.__luacache = M
 
---{{{
-local cachepack = {}
-
--- using double for packing/unpacking numbers has no conversion overhead
--- 32-bit ARM causes a bus error when casting to double, so use int there
-local number_t = jit.arch ~= "arm" and "double" or "int"
-ffi.cdef("typedef " .. number_t .. " number_t;")
-
-local c_number_t = ffi.typeof "number_t[1]"
-local c_sizeof_number_t = ffi.sizeof "number_t"
-
-local out_buf = {}
-
-function out_buf.write_number(buf, num)
-  buf[#buf + 1] = ffi.string(c_number_t(num), c_sizeof_number_t)
-end
-
-function out_buf.write_string(buf, str)
-  out_buf.write_number(buf, #str)
-  buf[#buf + 1] = str
-end
-
-function out_buf.to_string(buf)
-  return table.concat(buf)
-end
-
-local in_buf = {}
-
-function in_buf.read_number(buf)
-  if buf.size < buf.pos then
-    error "buffer access violation"
-  end
-  local res = ffi.cast("number_t*", buf.ptr + buf.pos)[0]
-  buf.pos = buf.pos + c_sizeof_number_t
-  return res
-end
-
-function in_buf.read_string(buf)
-  local len = in_buf.read_number(buf)
-  local res = ffi.string(buf.ptr + buf.pos, len)
-  buf.pos = buf.pos + len
-
-  return res
-end
-
-function cachepack.pack(cache)
-  local total_keys = vim.tbl_count(cache)
-  local buf = {}
-
-  out_buf.write_number(buf, total_keys)
-  for k, v in pairs(cache) do
-    out_buf.write_string(buf, k)
-    out_buf.write_string(buf, v[1] or "")
-    out_buf.write_number(buf, v[2] or 0)
-    out_buf.write_string(buf, v[3] or "")
-  end
-
-  return out_buf.to_string(buf)
-end
-
-function cachepack.unpack(str, raw_buf_size)
-  if raw_buf_size == 0 or str == nil or (raw_buf_size == nil and #str == 0) then
-    return {}
-  end
-
-  local buf = {
-    ptr = raw_buf_size and str or ffi.new("const char[?]", #str, str),
-    pos = 0,
-    size = raw_buf_size or #str,
-  }
-  local cache = {}
-
-  local total_keys = in_buf.read_number(buf)
-  for _ = 1, total_keys do
-    local k = in_buf.read_string(buf)
-    local v = {
-      in_buf.read_string(buf),
-      in_buf.read_number(buf),
-      in_buf.read_string(buf),
-    }
-    cache[k] = v
+if not get_runtime then
+  -- nvim 0.5 compat
+  get_runtime = function(paths, all, _)
+    local r = {}
+    for _, path in ipairs(paths) do
+      local found = api.nvim_get_runtime_file(path, all)
+      for i = 1, #found do
+        r[#r + 1] = found[i]
+      end
+    end
+    return r
   end
-
-  return cache
 end
---}}}
 
 local function log(...)
   M.log[#M.log + 1] = table.concat({ string.format(...) }, " ")
@@ -119,246 +53,312 @@ function M.print_log()
 end
 
 function M.enable_profile()
-  M.profile = {}
+  local P = require "lvim.impatient.profile"
+
+  M.chunks.profile = {}
+  M.modpaths.profile = {}
+
+  P.setup(M.modpaths.profile)
+
   M.print_profile = function()
-    M.profile["lvim.impatient"] = {
-      resolve = 0,
-      load = impatient_dur,
-      loader = "standard",
-    }
-    require("lvim.impatient.profile").print_profile(M.profile)
+    P.print_profile(M)
   end
-  vim.cmd [[command! LuaCacheProfile lua _G.__luacache.print_profile()]]
-end
 
-local function is_cacheable(path)
-  -- Don't cache files in /tmp since they are not likely to persist.
-  -- Note: Appimage versions of Neovim mount $VIMRUNTIME in /tmp in a unique
-  -- directory on each launch.
-  return not vim.startswith(path, "/tmp/")
+  vim.cmd [[command! LuaCacheProfile lua _G.__luacache.print_profile()]]
 end
 
 local function hash(modpath)
-  local stat = uv.fs_stat(modpath)
+  local stat = fs_stat(modpath)
   if stat then
-    return stat.mtime.sec
+    return stat.mtime.sec .. stat.mtime.nsec .. stat.size
   end
+  error("Could not hash " .. modpath)
 end
 
-local function hrtime()
-  if M.profile then
-    return uv.hrtime()
+local function modpath_mangle(modpath)
+  if appdir then
+    modpath = modpath:gsub(appdir, "/$APPDIR")
   end
+  return modpath
 end
 
-local function load_package_with_cache(name, loader)
-  local resolve_start = hrtime()
+local function modpath_unmangle(modpath)
+  if appdir then
+    modpath = modpath:gsub("/$APPDIR", appdir)
+  end
+  return modpath
+end
 
-  local basename = name:gsub("%.", "/")
-  local paths = { "lua/" .. basename .. ".lua", "lua/" .. basename .. "/init.lua" }
+local function profile(m, entry, name, loader)
+  if m.profile then
+    local mp = m.profile
+    mp[entry] = mp[entry] or {}
+    if not mp[entry].loader and loader then
+      mp[entry].loader = loader
+    end
+    if not mp[entry][name] then
+      mp[entry][name] = uv.hrtime()
+    end
+  end
+end
 
-  for _, path in ipairs(paths) do
-    local modpath = get_runtime_file(path, false)[1]
-    if modpath then
-      local load_start = hrtime()
-      local chunk, err = loadfile(modpath)
-
-      if M.profile then
-        M.profile[name] = {
-          resolve = load_start - resolve_start,
-          load = hrtime() - load_start,
-          loader = loader or "standard",
-        }
-      end
+local function mprofile(mod, name, loader)
+  profile(M.modpaths, mod, name, loader)
+end
 
-      if chunk == nil then
-        return err
-      end
+local function cprofile(path, name, loader)
+  profile(M.chunks, path, name, loader)
+end
 
-      if is_cacheable(modpath) then
-        log("Creating cache for module %s", name)
-        M.cache[name] = { modpath, hash(modpath), string.dump(chunk) }
-        M.dirty = true
+local function get_runtime_file(basename, paths)
+  -- Look in the cache to see if we have already loaded a parent module.
+  -- If we have then try looking in the parents directory first.
+  local parents = vim.split(basename, "/")
+  for i = #parents, 1, -1 do
+    local parent = table.concat(vim.list_slice(parents, 1, i), "/")
+    local ppath = M.modpaths.cache[parent]
+    if ppath then
+      if ppath:sub(-9) == "/init.lua" then
+        ppath = ppath:sub(1, -10) -- a/b/init.lua -> a/b
       else
-        log("Unable to cache module %s", name)
+        ppath = ppath:sub(1, -5) -- a/b.lua -> a/b
       end
 
-      return chunk
+      for _, path in ipairs(paths) do
+        -- path should be of form 'a/b/c.lua' or 'a/b/c/init.lua'
+        local modpath = ppath .. "/" .. path:sub(#("lua/" .. parent) + 2)
+        if fs_stat(modpath) then
+          return modpath, "cache(p)"
+        end
+      end
     end
   end
-  return nil
+
+  -- What Neovim does by default; slowest
+  local modpath = get_runtime(paths, false, { is_lua = true })[1]
+  return modpath, "standard"
 end
 
-local reduced_rtp
+local function get_runtime_file_cached(basename, paths)
+  local mp = M.modpaths
+  if mp.cache[basename] then
+    local modpath = mp.cache[basename]
+    if fs_stat(modpath) then
+      mprofile(basename, "resolve_end", "cache")
+      return modpath
+    end
+    mp.cache[basename] = nil
+    mp.dirty = true
+  end
 
--- Speed up non-cached loads by reducing the rtp path during requires
-function M.update_reduced_rtp()
-  local luadirs = get_runtime_file("lua/", true)
+  local modpath, loader = get_runtime_file(basename, paths)
+  if modpath then
+    mprofile(basename, "resolve_end", loader)
+    log("Creating cache for module %s", basename)
+    mp.cache[basename] = modpath_mangle(modpath)
+    mp.dirty = true
+  end
+  return modpath
+end
 
-  for i = 1, #luadirs do
-    luadirs[i] = luadirs[i]:sub(1, -6)
+local function extract_basename(pats)
+  local basename
+
+  -- Deconstruct basename from pats
+  for _, pat in ipairs(pats) do
+    for i, npat in ipairs {
+      -- Ordered by most specific
+      "lua/(.*)/init%.lua",
+      "lua/(.*)%.lua",
+    } do
+      local m = pat:match(npat)
+      if i == 2 and m and m:sub(-4) == "init" then
+        m = m:sub(0, -6)
+      end
+      if not basename then
+        if m then
+          basename = m
+        end
+      elseif m and m ~= basename then
+        -- matches are inconsistent
+        return
+      end
+    end
   end
 
-  reduced_rtp = table.concat(luadirs, ",")
+  return basename
 end
 
-local function load_package_with_cache_reduced_rtp(name)
-  if vim.in_fast_event() then
-    -- Can't set/get options in the fast handler
-    return load_package_with_cache(name, "fast")
+local function get_runtime_cached(pats, all, opts)
+  local fallback = false
+  if all or not opts or not opts.is_lua then
+    -- Fallback
+    fallback = true
   end
-  local orig_rtp = get_option "runtimepath"
-  local orig_ei = get_option "eventignore"
 
-  if not reduced_rtp then
-    M.update_reduced_rtp()
+  local basename
+
+  if not fallback then
+    basename = extract_basename(pats)
   end
 
-  set_option("eventignore", "all")
-  set_option("rtp", reduced_rtp)
+  if fallback or not basename then
+    return get_runtime(pats, all, opts)
+  end
 
-  local found = load_package_with_cache(name, "reduced")
+  return { get_runtime_file_cached(basename, pats) }
+end
 
-  set_option("rtp", orig_rtp)
-  set_option("eventignore", orig_ei)
+-- Copied from neovim/src/nvim/lua/vim.lua with two lines changed
+local function load_package(name)
+  local basename = name:gsub("%.", "/")
+  local paths = { "lua/" .. basename .. ".lua", "lua/" .. basename .. "/init.lua" }
 
-  return found
-end
+  -- Original line:
+  -- local found = vim.api.nvim__get_runtime(paths, false, {is_lua=true})
+  local found = { get_runtime_file_cached(basename, paths) }
+  if #found > 0 then
+    local f, err = loadfile(found[1])
+    return f or error(err)
+  end
 
-local function load_from_cache(name)
-  local resolve_start = hrtime()
-  if M.cache[name] == nil then
-    log("No cache for module %s", name)
-    return "No cache entry"
+  local so_paths = {}
+  for _, trail in ipairs(vim._so_trails) do
+    local path = "lua" .. trail:gsub("?", basename) -- so_trails contains a leading slash
+    table.insert(so_paths, path)
   end
 
-  local modpath, mhash, codes = unpack(M.cache[name])
+  -- Original line:
+  -- found = vim.api.nvim__get_runtime(so_paths, false, {is_lua=true})
+  found = { get_runtime_file_cached(basename, so_paths) }
+  if #found > 0 then
+    -- Making function name in Lua 5.1 (see src/loadlib.c:mkfuncname) is
+    -- a) strip prefix up to and including the first dash, if any
+    -- b) replace all dots by underscores
+    -- c) prepend "luaopen_"
+    -- So "foo-bar.baz" should result in "luaopen_bar_baz"
+    local dash = name:find("-", 1, true)
+    local modname = dash and name:sub(dash + 1) or name
+    local f, err = package.loadlib(found[1], "luaopen_" .. modname:gsub("%.", "_"))
+    return f or error(err)
+  end
+  return nil
+end
 
-  if mhash ~= hash(modpath) then
-    log("Stale cache for module %s", name)
-    M.cache[name] = nil
-    M.dirty = true
-    return "Stale cache"
+local function load_from_cache(path)
+  local mc = M.chunks
+
+  if not mc.cache[path] then
+    return nil, string.format("No cache for path %s", path)
   end
 
-  local load_start = hrtime()
-  local chunk = loadstring(codes)
+  local mhash, codes = unpack(mc.cache[path])
 
-  if M.profile then
-    M.profile[name] = {
-      resolve = load_start - resolve_start,
-      load = hrtime() - load_start,
-      loader = "cache",
-    }
+  if mhash ~= hash(modpath_unmangle(path)) then
+    mc.cache[path] = nil
+    mc.dirty = true
+    return nil, string.format("Stale cache for path %s", path)
   end
 
+  local chunk = loadstring(codes)
+
   if not chunk then
-    M.cache[name] = nil
-    M.dirty = true
-    log("Error loading cache for module. Invalidating", name)
-    return "Cache error"
+    mc.cache[path] = nil
+    mc.dirty = true
+    return nil, string.format("Cache error for path %s", path)
   end
 
   return chunk
 end
 
-function M.save_cache()
-  if M.dirty then
-    log("Updating cache file: %s", M.path)
-    local f = io.open(M.path, "w+b")
-    f:write(cachepack.pack(M.cache))
-    f:flush()
-    M.dirty = false
+local function loadfile_cached(path)
+  cprofile(path, "load_start")
+
+  local chunk, err = load_from_cache(path)
+  if chunk and not err then
+    log("Loaded cache for path %s", path)
+    cprofile(path, "load_end", "cache")
+    return chunk
   end
-end
+  log(err)
 
-function M.clear_cache()
-  M.cache = {}
-  os.remove(M.path)
-end
+  chunk, err = _loadfile(path)
 
-impatient_dur = uv.hrtime() - impatient_load_start
+  if not err then
+    log("Creating cache for path %s", path)
+    M.chunks.cache[modpath_mangle(path)] = { hash(path), string.dump(chunk) }
+    M.chunks.dirty = true
+  end
 
-function M.setup(opts)
-  opts = opts or {}
-  M.path = opts.path or vim.fn.stdpath "cache" .. "/lvim_cache"
+  cprofile(path, "load_end", "standard")
+  return chunk, err
+end
 
-  if opts.enable_profiling then
-    M.enable_profile()
+function M.save_cache()
+  local function _save_cache(t)
+    if t.dirty then
+      log("Updating chunk cache file: %s", t.path)
+      local f = io.open(t.path, "w+b")
+      f:write(mpack.encode(t.cache))
+      f:flush()
+      t.dirty = false
+    end
   end
+  _save_cache(M.chunks)
+  _save_cache(M.modpaths)
+end
 
-  local impatient_setup_start = uv.hrtime()
-  local stat = uv.fs_stat(M.path)
-  if stat then
-    log("Loading cache file %s", M.path)
-    local ok
-    -- Linux/macOS lets us easily mmap the cache file for faster reads without passing to Lua
-    if jit.os == "Linux" or jit.os == "OSX" then
-      local size = stat.size
-
-      local C = ffi.C
-      local O_RDONLY = 0x00
-      local PROT_READ = 0x01
-      local MAP_PRIVATE = 0x02
-
-      ffi.cdef [[
-      int open(const char *pathname, int flags);
-      int close(int fd);
-      void *mmap(void *addr, size_t length, int prot, int flags, int fd, long int offset);
-      int munmap(void *addr, size_t length);
-      ]]
-      local f = C.open(M.path, O_RDONLY)
-
-      local addr = C.mmap(nil, size, PROT_READ, MAP_PRIVATE, f, 0)
-      ok = ffi.cast("intptr_t", addr) ~= -1
-
-      if ok then
-        M.cache = cachepack.unpack(ffi.cast("char *", addr), size)
-        C.munmap(addr, size)
-      end
+function M.clear_cache()
+  local function _clear_cache(t)
+    t.cache = {}
+    os.remove(t.path)
+  end
+  _clear_cache(M.chunks)
+  _clear_cache(M.modpaths)
+end
 
-      C.close(f)
-    else
-      local f = io.open(M.path, "rb")
-      ok, M.cache = pcall(function()
-        return cachepack.unpack(f:read "*a")
+local function init_cache()
+  local function _init_cache(t)
+    if fs_stat(t.path) then
+      log("Loading cache file %s", t.path)
+      local f = io.open(t.path, "rb")
+      local ok
+      ok, t.cache = pcall(function()
+        return mpack.decode(f:read "*a")
       end)
-    end
 
-    if not ok then
-      log("Corrupted cache file, %s. Invalidating...", M.path)
-      os.remove(M.path)
-      M.cache = {}
+      if not ok then
+        log("Corrupted cache file, %s. Invalidating...", t.path)
+        os.remove(t.path)
+        t.cache = {}
+      end
+      t.dirty = not ok
     end
-    M.dirty = not ok
   end
 
-  local insert = table.insert
-  local package = package
+  _init_cache(M.chunks)
+  _init_cache(M.modpaths)
+end
 
-  -- Fix the position of the preloader. This also makes loading modules like 'ffi'
-  -- and 'bit' quicker
-  if package.loaders[1] == vim._load_package then
-    -- Move vim._load_package to the second position
-    local vim_load = table.remove(package.loaders, 1)
-    insert(package.loaders, 2, vim_load)
-  end
+local function setup()
+  init_cache()
 
-  insert(package.loaders, 2, load_from_cache)
-  insert(package.loaders, 3, load_package_with_cache_reduced_rtp)
-  insert(package.loaders, 4, load_package_with_cache)
+  -- Override default functions
+  vim._load_package = load_package
+  vim.api.nvim__get_runtime = get_runtime_cached
+  -- luacheck: ignore 121
+  loadfile = loadfile_cached
 
   vim.cmd [[
     augroup impatient
       autocmd VimEnter,VimLeave * lua _G.__luacache.save_cache()
-      autocmd OptionSet runtimepath lua _G.__luacache.update_reduced_rtp(true)
     augroup END
 
     command! LuaCacheClear lua _G.__luacache.clear_cache()
     command! LuaCacheLog   lua _G.__luacache.print_log()
   ]]
-
-  impatient_dur = impatient_dur + (uv.hrtime() - impatient_setup_start)
 end
 
+setup()
+
 return M

+ 201 - 108
lua/lvim/impatient/profile.lua

@@ -1,145 +1,238 @@
 local M = {}
 
-local api = vim.api
+local api, uv = vim.api, vim.loop
 
-function M.print_profile(profile)
-  if not profile then
+local std_data = vim.fn.stdpath "data"
+local std_config = vim.fn.stdpath "config"
+local vimruntime = os.getenv "VIMRUNTIME"
+
+local function load_buffer(title, lines)
+  local bufnr = api.nvim_create_buf(false, false)
+  api.nvim_buf_set_lines(bufnr, 0, 0, false, lines)
+  api.nvim_buf_set_option(bufnr, "bufhidden", "wipe")
+  api.nvim_buf_set_option(bufnr, "buftype", "nofile")
+  api.nvim_buf_set_option(bufnr, "swapfile", false)
+  api.nvim_buf_set_option(bufnr, "modifiable", false)
+  api.nvim_buf_set_name(bufnr, title)
+  api.nvim_set_current_buf(bufnr)
+end
+
+local function mod_path(path)
+  if not path then
+    return "?"
+  end
+  path = path:gsub(std_data .. "/site/pack/packer/", "<PACKER>/")
+  path = path:gsub(std_data .. "/", "<STD_DATA>/")
+  path = path:gsub(std_config .. "/", "<STD_CONFIG>/")
+  path = path:gsub(vimruntime .. "/", "<VIMRUNTIME>/")
+  return path
+end
+
+local function time_tostr(x)
+  if x == 0 then
+    return "?"
+  end
+  return string.format("%8.3fms", x)
+end
+
+local function mem_tostr(x)
+  local unit = ""
+  for _, u in ipairs { "K", "M", "G" } do
+    if x < 1000 then
+      break
+    end
+    x = x / 1000
+    unit = u
+  end
+  return string.format("%1.1f%s", x, unit)
+end
+
+function M.print_profile(I)
+  local mod_profile = I.modpaths.profile
+  local chunk_profile = I.chunks.profile
+
+  if not mod_profile and not chunk_profile then
     print "Error: profiling was not enabled"
     return
   end
 
   local total_resolve = 0
   local total_load = 0
-  local name_pad = 0
   local modules = {}
-  local plugins = {}
-
-  for module, p in pairs(profile) do
-    p.resolve = p.resolve / 1000000
-    p.load = p.load / 1000000
-    p.total = p.resolve + p.load
-    p.module = module:gsub("/", ".")
-
-    local plugin = p.module:match "([^.]+)"
-    if plugin then
-      if not plugins[plugin] then
-        plugins[plugin] = {
-          module = plugin,
-          resolve = 0,
-          load = 0,
-          total = 0,
-        }
-      end
-      local r = plugins[plugin]
 
-      r.resolve = r.resolve + p.resolve
-      r.load = r.load + p.load
-      r.total = r.total + p.total
+  for path, m in pairs(chunk_profile) do
+    m.load = m.load_end - m.load_start
+    m.load = m.load / 1000000
+    m.path = mod_path(path)
+  end
 
-      if not r.loader then
-        r.loader = p.loader
-      elseif r.loader ~= p.loader then
-        r.loader = "mixed"
-      end
+  local module_content_width = 0
+
+  for module, m in pairs(mod_profile) do
+    m.resolve = 0
+    if m.resolve_end then
+      m.resolve = m.resolve_end - m.resolve_start
+      m.resolve = m.resolve / 1000000
     end
 
-    total_resolve = total_resolve + p.resolve
-    total_load = total_load + p.load
+    m.module = module:gsub("/", ".")
+    m.loader = m.loader or m.loader_guess
+
+    local path = I.modpaths.cache[module]
+    local path_prof = chunk_profile[path]
+    m.path = mod_path(path)
 
-    if #module > name_pad then
-      name_pad = #module
+    if path_prof then
+      chunk_profile[path] = nil
+      m.load = path_prof.load
+      m.ploader = path_prof.loader
+    else
+      m.load = 0
+      m.ploader = "NA"
     end
 
-    modules[#modules + 1] = p
+    total_resolve = total_resolve + m.resolve
+    total_load = total_load + m.load
+
+    if #module > module_content_width then
+      module_content_width = #module
+    end
+
+    modules[#modules + 1] = m
   end
 
   table.sort(modules, function(a, b)
-    return a.module > b.module
+    return (a.resolve + a.load) > (b.resolve + b.load)
   end)
 
-  do
-    local plugins_a = {}
-    for _, v in pairs(plugins) do
-      plugins_a[#plugins_a + 1] = v
-    end
-    plugins = plugins_a
+  local paths = {}
+
+  local total_paths_load = 0
+  for _, m in pairs(chunk_profile) do
+    paths[#paths + 1] = m
+    total_paths_load = total_paths_load + m.load
   end
 
-  table.sort(plugins, function(a, b)
-    return a.total > b.total
+  table.sort(paths, function(a, b)
+    return a.load > b.load
   end)
 
   local lines = {}
-  local function add(...)
-    lines[#lines + 1] = string.format(...)
-  end
+  local function add(fmt, ...)
+    local args = { ... }
+    for i, a in ipairs(args) do
+      if type(a) == "number" then
+        args[i] = time_tostr(a)
+      end
+    end
 
-  local l = string.rep("─", name_pad + 1)
+    lines[#lines + 1] = string.format(fmt, unpack(args))
+  end
 
-  add(
-    "%s┬───────────┬────────────┬────────────┬────────────┐",
-    l
-  )
-  add("%-" .. name_pad .. "s │ Loader    │ Resolve    │ Load       │ Total      │", "")
-  add(
-    "%s┼───────────┼────────────┼────────────┼────────────┤",
-    l
-  )
-  add(
-    "%-" .. name_pad .. "s │           │ %8.4fms │ %8.4fms │ %8.4fms │",
-    "Total",
-    total_resolve,
-    total_load,
-    total_resolve + total_load
-  )
-  add(
-    "%s┴───────────┴────────────┴────────────┴────────────┤",
-    l
+  local time_cell_width = 12
+  local loader_cell_width = 11
+  local time_content_width = time_cell_width - 2
+  local loader_content_width = loader_cell_width - 2
+  local module_cell_width = module_content_width + 2
+
+  local tcwl = string.rep("─", time_cell_width)
+  local lcwl = string.rep("─", loader_cell_width)
+  local mcwl = string.rep("─", module_cell_width + 2)
+
+  local n = string.rep("─", 200)
+
+  local module_cell_format = "%-" .. module_cell_width .. "s"
+  local loader_format = "%-" .. loader_content_width .. "s"
+  local line_format = "%s │ %s │ %s │ %s │ %s │ %s"
+
+  local row_fmt = line_format:format(
+    " %" .. time_content_width .. "s",
+    loader_format,
+    "%" .. time_content_width .. "s",
+    loader_format,
+    module_cell_format,
+    "%s"
   )
-  add("%-" .. name_pad .. "s                                                    │", "By Plugin")
-  add(
-    "%s┬───────────┬────────────┬────────────┬────────────┤",
-    l
+
+  local title_fmt = line_format:format(
+    " %-" .. time_content_width .. "s",
+    loader_format,
+    "%-" .. time_content_width .. "s",
+    loader_format,
+    module_cell_format,
+    "%s"
   )
-  for _, p in ipairs(plugins) do
-    add(
-      "%-" .. name_pad .. "s │ %9s │ %8.4fms │ %8.4fms │ %8.4fms │",
-      p.module,
-      p.loader,
-      p.resolve,
-      p.load,
-      p.total
-    )
+
+  local title1_width = time_cell_width + loader_cell_width - 1
+  local title1_fmt = ("%s │ %s │"):format(" %-" .. title1_width .. "s", "%-" .. title1_width .. "s")
+
+  add "Note: this report is not a measure of startup time. Only use this for comparing"
+  add "between cached and uncached loads of Lua modules"
+  add ""
+
+  add "Cache files:"
+  for _, f in ipairs { I.chunks.path, I.modpaths.path } do
+    local size = vim.loop.fs_stat(f).size
+    add("  %s %s", f, mem_tostr(size))
   end
-  add(
-    "%s┴───────────┴────────────┴────────────┴────────────┤",
-    l
-  )
-  add("%-" .. name_pad .. "s                                                    │", "By Module")
-  add(
-    "%s┬───────────┬────────────┬────────────┬────────────┤",
-    l
-  )
-  for _, p in pairs(modules) do
-    add(
-      "%-" .. name_pad .. "s │ %9s │ %8.4fms │ %8.4fms │ %8.4fms │",
-      p.module,
-      p.loader,
-      p.resolve,
-      p.load,
-      p.total
-    )
+  add ""
+
+  add("%s─%s┬%s─%s┐", tcwl, lcwl, tcwl, lcwl)
+  add(title1_fmt, "Resolve", "Load")
+  add("%s┬%s┼%s┬%s┼%s┬%s", tcwl, lcwl, tcwl, lcwl, mcwl, n)
+  add(title_fmt, "Time", "Method", "Time", "Method", "Module", "Path")
+  add("%s┼%s┼%s┼%s┼%s┼%s", tcwl, lcwl, tcwl, lcwl, mcwl, n)
+  add(row_fmt, total_resolve, "", total_load, "", "Total", "")
+  add("%s┼%s┼%s┼%s┼%s┼%s", tcwl, lcwl, tcwl, lcwl, mcwl, n)
+  for _, p in ipairs(modules) do
+    add(row_fmt, p.resolve, p.loader, p.load, p.ploader, p.module, p.path)
+  end
+  add("%s┴%s┴%s┴%s┴%s┴%s", tcwl, lcwl, tcwl, lcwl, mcwl, n)
+
+  if #paths > 0 then
+    add ""
+    add(n)
+    local f3 = " %" .. time_content_width .. "s │ %" .. loader_content_width .. "s │ %s"
+    add "Files loaded with no associated module"
+    add("%s┬%s┬%s", tcwl, lcwl, n)
+    add(f3, "Time", "Loader", "Path")
+    add("%s┼%s┼%s", tcwl, lcwl, n)
+    add(f3, total_paths_load, "", "Total")
+    add("%s┼%s┼%s", tcwl, lcwl, n)
+    for _, p in ipairs(paths) do
+      add(f3, p.load, p.loader, p.path)
+    end
+    add("%s┴%s┴%s", tcwl, lcwl, n)
+    add ""
   end
-  add(
-    "%s┴───────────┴────────────┴────────────┴────────────┘",
-    l
-  )
 
-  local bufnr = api.nvim_create_buf(false, false)
-  api.nvim_buf_set_lines(bufnr, 0, 0, false, lines)
-  api.nvim_buf_set_option(bufnr, "buftype", "nofile")
-  api.nvim_buf_set_name(bufnr, "Impatient Profile Report")
-  api.nvim_set_current_buf(bufnr)
+  load_buffer("Impatient Profile Report", lines)
+end
+
+M.setup = function(profile)
+  local _require = require
+
+  -- luacheck: ignore 121
+  require = function(mod)
+    local basename = mod:gsub("%.", "/")
+    if not profile[basename] then
+      profile[basename] = {}
+      profile[basename].resolve_start = uv.hrtime()
+      profile[basename].loader_guess = "C"
+    end
+    return _require(mod)
+  end
+
+  -- Add profiling around all the loaders
+  local pl = package.loaders
+  for i = 1, #pl do
+    local l = pl[i]
+    pl[i] = function(mod)
+      local basename = mod:gsub("%.", "/")
+      profile[basename].loader_guess = i == 1 and "preloader" or "loader #" .. i
+      return l(mod)
+    end
+  end
 end
 
 return M

+ 5 - 0
lua/lvim/lsp/init.lua

@@ -149,6 +149,11 @@ function M.setup()
     append_default_schemas = true,
   }
 
+  require("nvim-lsp-installer").settings {
+    -- use the default nvim_data_dir, since the server binaries are independent
+    install_root_dir = utils.join_paths(vim.call("stdpath", "data"), "lsp_servers"),
+  }
+
   require("lvim.lsp.null-ls").setup()
 
   autocmds.configure_format_on_save()

+ 14 - 7
tests/specs/bootstrap_spec.lua

@@ -1,19 +1,26 @@
 local a = require "plenary.async_lib.tests"
+local uv = vim.loop
+local home_dir = uv.os_homedir()
 
 a.describe("initial start", function()
-  local uv = vim.loop
-  local home_dir = uv.os_homedir()
-  local lvim_config_path = get_config_dir() or home_dir .. "/.config/lvim"
-  local lvim_runtime_path = get_runtime_dir() or home_dir .. "/.local/share/lunarvim"
+  local lvim_config_path = get_config_dir()
+  local lvim_runtime_path = get_runtime_dir()
+  local lvim_cache_path = get_cache_dir()
 
   a.it("should be able to detect test environment", function()
     assert.truthy(os.getenv "LVIM_TEST_ENV")
     assert.falsy(package.loaded["lvim.impatient"])
   end)
 
-  a.it("should not be reading default neovim directories in the home directories", function()
-    local rtp_list = vim.opt.rtp:get()
-    assert.falsy(vim.tbl_contains(rtp_list, vim.fn.stdpath "config"))
+  a.it("should be able to use lunarvim directories using vim.fn", function()
+    assert.equal(lvim_runtime_path, vim.fn.stdpath "data")
+    assert.equal(lvim_config_path, vim.fn.stdpath "config")
+    assert.equal(lvim_cache_path, vim.fn.stdpath "cache")
+  end)
+
+  a.it("should be to retrieve default neovim directories", function()
+    local xdg_config = os.getenv "XDG_CONFIG_HOME" or join_paths(home_dir, ".config")
+    assert.equal(join_paths(xdg_config, "nvim"), vim.call("stdpath", "config"))
   end)
 
   a.it("should be able to read lunarvim directories", function()

+ 1 - 3
utils/installer/install.sh

@@ -12,11 +12,9 @@ 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"}"
+declare -r LUNARVIM_CACHE_DIR="${LUNARVIM_CACHE_DIR:-"$XDG_CACHE_HOME/lvim"}"
 declare -r LUNARVIM_BASE_DIR="${LUNARVIM_BASE_DIR:-"$LUNARVIM_RUNTIME_DIR/lvim"}"
 
-# TODO: Use a dedicated cache directory #1256
-declare -r LUNARVIM_CACHE_DIR="$XDG_CACHE_HOME/nvim"
-
 declare BASEDIR
 BASEDIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
 BASEDIR="$(dirname -- "$(dirname -- "$BASEDIR")")"

+ 1 - 1
utils/installer/install_bin.sh

@@ -9,7 +9,7 @@ XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-"$HOME/.config"}"
 
 LUNARVIM_RUNTIME_DIR="${LUNARVIM_RUNTIME_DIR:-"$XDG_DATA_HOME/lunarvim"}"
 LUNARVIM_CONFIG_DIR="${LUNARVIM_CONFIG_DIR:-"$XDG_CONFIG_HOME/lvim"}"
-LUNARVIM_CACHE_DIR="${LUNARVIM_CACHE_DIR:-"$XDG_CACHE_HOME/nvim"}"
+LUNARVIM_CACHE_DIR="${LUNARVIM_CACHE_DIR:-"$XDG_CACHE_HOME/lvim"}"
 
 LUNARVIM_BASE_DIR="${LUNARVIM_BASE_DIR:-"$LUNARVIM_RUNTIME_DIR/lvim"}"
 

+ 1 - 3
utils/installer/uninstall.sh

@@ -9,9 +9,7 @@ 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"
+declare -r LUNARVIM_CACHE_DIR="${LUNARVIM_CACHE_DIR:-"$XDG_CACHE_HOME/lvim"}"
 
 declare -a __lvim_dirs=(
   "$LUNARVIM_CONFIG_DIR"