Ver código fonte

[Refactor]: only allow a single logger (#1405)

kylo252 3 anos atrás
pai
commit
27679f988f

+ 3 - 2
.github/workflows/install.yaml

@@ -38,8 +38,9 @@ jobs:
       - name: Install LunarVim
         timeout-minutes: 4
         run: |
-          mkdir -p "$HOME/.local/share/lunarvim"
-          mkdir -p "$HOME/.config/lvim"
+          mkdir -p "$HOME"/.local/share/lunarvim/lvim
+          mkdir -p "$HOME"/.config/lvim
+          ln -s "$PWD"/* "$HOME"/.local/share/lunarvim/lvim/.
           bash ./utils/installer/install.sh
 
       - name: Test LunarVim PackerCompile

+ 1 - 1
init.lua

@@ -25,7 +25,7 @@ local plugins = require "plugins"
 local plugin_loader = require("plugin-loader").init()
 plugin_loader:load { plugins, lvim.plugins }
 
-local Log = require("core.log").new_default()
+local Log = require "core.log"
 Log:info "Starting LunarVim"
 
 vim.g.colors_name = lvim.colorscheme -- Colorscheme must get called after plugins are loaded or it will break new installs.

+ 32 - 33
lua/core/log.lua

@@ -1,60 +1,59 @@
 local Log = {}
 
---- Creates a log handle based on Plenary.log
----@param opts these are passed verbatim to Plenary.log
----@return log handle
-function Log:new(opts)
-  local status_ok, handle = pcall(require, "plenary.log")
-  if not status_ok then
-    vim.notify("Plenary.log is not available. Logging to console only", vim.log.levels.DEBUG)
-  end
-
-  self.__handle = handle
-
-  local path = string.format("%s/%s.log", vim.api.nvim_call_function("stdpath", { "cache" }), opts.plugin)
-
-  self.get_path = function()
-    return path
-  end
-
-  setmetatable({}, Log)
-  return self
-end
-
+--- Adds a log entry using Plenary.log
+---@param msg any
+---@param level string [same as vim.log.log_levels]
 function Log:add_entry(msg, level)
-  local status_ok, _ = pcall(require, "plenary.log")
-  if not status_ok then
-    return vim.notify(msg, vim.log.levels[level])
+  assert(type(level) == "string")
+  if self.__handle then
+    -- plenary uses lower-case log levels
+    self.__handle[level:lower()](msg)
+  end
+  local status_ok, plenary = pcall(require, "plenary")
+  if status_ok then
+    local default_opts = { plugin = "lunarvim", level = lvim.log.level }
+    local handle = plenary.log.new(default_opts)
+    handle[level:lower()](msg)
+    self.__handle = handle
   end
-  -- plenary uses lower-case log levels
-  return self.__handle[level:lower()](msg)
+  -- don't do anything if plenary is not available
 end
 
---- Creates or retrieves a log handle for the default logfile
---- based on Plenary.log
----@return log handle
-function Log:new_default()
-  return Log:new { plugin = "lunarvim", level = lvim.log.level }
+---Retrieves the path of the logfile
+---@return string path of the logfile
+function Log:get_path()
+  return string.format("%s/%s.log", vim.fn.stdpath "cache", "lunarvim")
 end
 
+---Add a log entry at TRACE level
+---@param msg any
 function Log:trace(msg)
   self:add_entry(msg, "TRACE")
 end
 
+---Add a log entry at DEBUG level
+---@param msg any
 function Log:debug(msg)
   self:add_entry(msg, "DEBUG")
 end
 
+---Add a log entry at INFO level
+---@param msg any
 function Log:info(msg)
   self:add_entry(msg, "INFO")
 end
 
+---Add a log entry at WARN level
+---@param msg any
 function Log:warn(msg)
-  self:add_entry(msg, "TRACE")
+  self:add_entry(msg, "WARN")
 end
 
+---Add a log entry at ERROR level
+---@param msg any
 function Log:error(msg)
-  self:add_entry(msg, "TRACE")
+  self:add_entry(msg, "ERROR")
 end
 
+setmetatable({}, Log)
 return Log

+ 1 - 1
lua/core/lualine/styles.lua

@@ -118,7 +118,7 @@ function M.get_style(style)
       "options are: ",
       string.format('"%s"', table.concat(style_keys, '", "'))
     )
-    Log:info '"lvim" style is applied.'
+    Log:debug '"lvim" style is applied.'
     style = "lvim"
   end
 

+ 1 - 1
lua/keymappings.lua

@@ -159,7 +159,7 @@ function M.config()
     lvim.keys.normal_mode["<A-Down>"] = lvim.keys.normal_mode["<C-Down>"]
     lvim.keys.normal_mode["<A-Left>"] = lvim.keys.normal_mode["<C-Left>"]
     lvim.keys.normal_mode["<A-Right>"] = lvim.keys.normal_mode["<C-Right>"]
-    Log:info "Activated mac keymappings"
+    Log:debug "Activated mac keymappings"
   end
 end
 

+ 5 - 3
lua/lsp/init.lua

@@ -99,21 +99,23 @@ end
 function M.common_on_init(client, bufnr)
   if lvim.lsp.on_init_callback then
     lvim.lsp.on_init_callback(client, bufnr)
-    Log:info "Called lsp.on_init_callback"
+    Log:debug "Called lsp.on_init_callback"
     return
   end
 
   local formatters = lvim.lang[vim.bo.filetype].formatters
   if not vim.tbl_isempty(formatters) and formatters[1]["exe"] ~= nil and formatters[1].exe ~= "" then
     client.resolved_capabilities.document_formatting = false
-    Log:info(string.format("Overriding language server [%s] with format provider [%s]", client.name, formatters[1].exe))
+    Log:debug(
+      string.format("Overriding language server [%s] with format provider [%s]", client.name, formatters[1].exe)
+    )
   end
 end
 
 function M.common_on_attach(client, bufnr)
   if lvim.lsp.on_attach_callback then
     lvim.lsp.on_attach_callback(client, bufnr)
-    Log:get_default().info "Called lsp.on_init_callback"
+    Log:debug "Called lsp.on_init_callback"
   end
   lsp_highlight_document(client)
   add_lsp_buffer_keybindings(bufnr)

+ 1 - 1
lua/lsp/null-ls/formatters.lua

@@ -53,7 +53,7 @@ function M.list_configured(formatter_configs)
         Log:warn("Not found:", formatter._opts.command)
         errors[fmt_config.exe] = {} -- Add data here when necessary
       else
-        Log:info("Using formatter:", formatter_cmd)
+        Log:debug("Using formatter:", formatter_cmd)
         formatters[fmt_config.exe] = formatter.with { command = formatter_cmd, extra_args = fmt_config.args }
       end
     end

+ 1 - 1
lua/lsp/null-ls/linters.lua

@@ -53,7 +53,7 @@ function M.list_configured(linter_configs)
         Log:warn("Not found:", linter._opts.command)
         errors[lnt_config.exe] = {} -- Add data here when necessary
       else
-        Log:info("Using linter:", linter_cmd)
+        Log:debug("Using linter:", linter_cmd)
         linters[lnt_config.exe] = linter.with { command = linter_cmd, extra_args = lnt_config.args }
       end
     end

+ 3 - 6
lua/plugin-loader.lua

@@ -1,13 +1,10 @@
 local plugin_loader = {}
 
 function plugin_loader:init()
-  local execute = vim.api.nvim_command
-  local fn = vim.fn
-
   local install_path = "~/.local/share/lunarvim/site/pack/packer/start/packer.nvim"
-  if fn.empty(fn.glob(install_path)) > 0 then
-    execute("!git clone https://github.com/wbthomason/packer.nvim " .. install_path)
-    execute "packadd packer.nvim"
+  if vim.fn.empty(vim.fn.glob(install_path)) > 0 then
+    vim.fn.system { "git", "clone", "https://github.com/wbthomason/packer.nvim", install_path }
+    vim.cmd "packadd packer.nvim"
   end
 
   local packer_ok, packer = pcall(require, "packer")

+ 2 - 2
lua/utils/init.lua

@@ -70,7 +70,7 @@ function utils.toggle_autoformat()
         },
       },
     }
-    Log:info "Format on save active"
+    Log:debug "Format on save active"
   end
 
   if not lvim.format_on_save then
@@ -79,7 +79,7 @@ function utils.toggle_autoformat()
         :autocmd! autoformat
       endif
     ]]
-    Log:info "Format on save off"
+    Log:debug "Format on save off"
   end
 end
 

+ 24 - 26
utils/installer/install.sh

@@ -50,32 +50,34 @@ EOF
   echo "Detecting platform for managing any additional neovim dependencies"
   detect_platform
 
-  # skip this in a Github workflow
-  if [ -z "$GITHUB_ACTIONS" ]; then
-    check_system_deps
+  if [ -n "$GITHUB_ACTIONS" ]; then
+    install_packer
+    setup_lvim
+    exit 0
+  fi
 
-    __add_separator "80"
+  check_system_deps
 
-    echo "Would you like to check lunarvim's NodeJS dependencies?"
-    read -p "[y]es or [n]o (default: no) : " -r answer
-    [ "$answer" != "${answer#[Yy]}" ] && install_nodejs_deps
+  __add_separator "80"
 
-    echo "Would you like to check lunarvim's Python dependencies?"
-    read -p "[y]es or [n]o (default: no) : " -r answer
-    [ "$answer" != "${answer#[Yy]}" ] && install_python_deps
+  echo "Would you like to check lunarvim's NodeJS dependencies?"
+  read -p "[y]es or [n]o (default: no) : " -r answer
+  [ "$answer" != "${answer#[Yy]}" ] && install_nodejs_deps
 
-    echo "Would you like to check lunarvim's Rust dependencies?"
-    read -p "[y]es or [n]o (default: no) : " -r answer
-    [ "$answer" != "${answer#[Yy]}" ] && install_rust_deps
+  echo "Would you like to check lunarvim's Python dependencies?"
+  read -p "[y]es or [n]o (default: no) : " -r answer
+  [ "$answer" != "${answer#[Yy]}" ] && install_python_deps
 
-    __add_separator "80"
+  echo "Would you like to check lunarvim's Rust dependencies?"
+  read -p "[y]es or [n]o (default: no) : " -r answer
+  [ "$answer" != "${answer#[Yy]}" ] && install_rust_deps
 
-    echo "Backing up old LunarVim configuration"
-    backup_old_config
+  __add_separator "80"
 
-    __add_separator "80"
+  echo "Backing up old LunarVim configuration"
+  backup_old_config
 
-  fi
+  __add_separator "80"
 
   case "$@" in
     *--overwrite*)
@@ -219,13 +221,13 @@ function backup_old_config() {
 }
 
 function install_packer() {
-  git clone --progress --depth 1 https://github.com/wbthomason/packer.nvim \
+  git clone --depth 1 https://github.com/wbthomason/packer.nvim \
     "$LUNARVIM_RUNTIME_DIR/site/pack/packer/start/packer.nvim"
 }
 
 function clone_lvim() {
   echo "Cloning LunarVim configuration"
-  if ! git clone --progress --branch "$LV_BRANCH" \
+  if ! git clone --branch "$LV_BRANCH" \
     --depth 1 "https://github.com/${LV_REMOTE}" "$LUNARVIM_RUNTIME_DIR/lvim"; then
     echo "Failed to clone repository. Installation failed."
     exit 1
@@ -258,12 +260,8 @@ function setup_lvim() {
     "$LUNARVIM_CONFIG_DIR/config.lua"
 
   nvim -u "$LUNARVIM_RUNTIME_DIR/lvim/init.lua" --headless \
-    +'autocmd User PackerComplete sleep 100m | qall' \
-    +PackerInstall
-
-  nvim -u "$LUNARVIM_RUNTIME_DIR/lvim/init.lua" --headless \
-    +'autocmd User PackerComplete sleep 100m | qall' \
-    +PackerSync
+    -c 'autocmd User PackerComplete quitall' \
+    -c 'PackerSync'
 
   echo "Packer setup complete"