diff options
| author | kylo252 <[email protected]> | 2022-01-02 10:25:20 +0100 | 
|---|---|---|
| committer | GitHub <[email protected]> | 2022-01-02 10:25:20 +0100 | 
| commit | 9017389766ff1ce31c8f0a21fe667653a7ab6b3a (patch) | |
| tree | 63915a205fb0456ed618317c0a77f006f1cfcfae | |
| parent | 91077623f093bf173ea9b224bb7bde15f3c2a13b (diff) | |
feat: lazyload notify's configuration (#1855)
Co-authored-by: Luc Sinet <[email protected]>
| -rw-r--r-- | README.md | 1 | ||||
| -rw-r--r-- | lua/lvim/core/log.lua | 123 | ||||
| -rw-r--r-- | lua/lvim/core/notify.lua | 84 | ||||
| -rw-r--r-- | lua/lvim/plugins.lua | 4 | ||||
| -rw-r--r-- | utils/installer/config.example.lua | 1 | 
5 files changed, 132 insertions, 81 deletions
| @@ -70,6 +70,7 @@ lvim.keys.normal_mode["<C-s>"] = ":w<cr>"  -- Configure builtin plugins  lvim.builtin.dashboard.active = true +lvim.builtin.notify.active = true  lvim.builtin.terminal.active = true  -- Treesitter parsers change this to a table of the languages you want i.e. {"java", "python", javascript} diff --git a/lua/lvim/core/log.lua b/lua/lvim/core/log.lua index f51b8af6..be7930ba 100644 --- a/lua/lvim/core/log.lua +++ b/lua/lvim/core/log.lua @@ -9,29 +9,16 @@ Log.levels = {    WARN = 4,    ERROR = 5,  } -  vim.tbl_add_reverse_lookup(Log.levels) +local notify_opts = {} +  function Log:init()    local status_ok, structlog = pcall(require, "structlog")    if not status_ok then      return nil    end -  local notify_handler = require "lvim.core.notify" - -  ---Check if notify is available -  ---@return boolean -  local is_notify_available = function() -    local in_headless = #vim.api.nvim_list_uis() == 0 -    --We can't rely on lvim.builtin.notify.active since this can be used before the config loader -    local has_notify_plugin = pcall(require, "notify") -    if not in_headless and has_notify_plugin then -      return true -    end -    return false -  end -    local log_level = Log.levels[(lvim.log.level):upper() or "WARN"]    local lvim_log = {      lvim = { @@ -64,50 +51,94 @@ function Log:init()      },    } -  if is_notify_available() then -    table.insert( -      lvim_log.lvim.sinks, -      structlog.sinks.NvimNotify(Log.levels.INFO, { -        processors = { -          notify_handler.default_namer, -          notify_handler.params_injecter, -        }, -        formatter = structlog.formatters.Format( -- -          "%s", -          { "msg" }, -          { blacklist_all = true } -        ), -        params_map = { -          icon = "icon", -          keep = "keep", -          on_open = "on_open", -          on_close = "on_close", -          timeout = "timeout", -          title = "title", -        }, -      }) -    ) -  end -    structlog.configure(lvim_log) -    local logger = structlog.get_logger "lvim" +  -- Overwrite `vim.notify` to use the logger    if lvim.log.override_notify then -    logger:log(Log.levels.INFO, "Ignoring request to override vim.notify with structlog due to instabilities") +    vim.notify = function(msg, vim_log_level, opts) +      notify_opts = opts or {} + +      -- vim_log_level can be omitted +      if vim_log_level == nil then +        vim_log_level = Log.levels["INFO"] +      elseif type(vim_log_level) == "string" then +        vim_log_level = Log.levels[(vim_log_level):upper()] or Log.levels["INFO"] +      else +        -- https://github.com/neovim/neovim/blob/685cf398130c61c158401b992a1893c2405cd7d2/runtime/lua/vim/lsp/log.lua#L5 +        vim_log_level = vim_log_level + 1 +      end + +      logger:log(vim_log_level, msg) +    end    end    return logger  end +--- Configure the sink in charge of logging notifications +---@param notif_handle table The implementation used by the sink for displaying the notifications +function Log:configure_notifications(notif_handle) +  local status_ok, structlog = pcall(require, "structlog") +  if not status_ok then +    return +  end + +  local default_namer = function(logger, entry) +    entry["title"] = logger.name +    return entry +  end + +  local notify_opts_injecter = function(_, entry) +    for key, value in pairs(notify_opts) do +      entry[key] = value +    end +    notify_opts = {} +    return entry +  end + +  local sink = structlog.sinks.NvimNotify(Log.levels.INFO, { +    processors = { +      default_namer, +      notify_opts_injecter, +    }, +    formatter = structlog.formatters.Format( -- +      "%s", +      { "msg" }, +      { blacklist_all = true } +    ), +    -- This should probably not be hard-coded +    params_map = { +      icon = "icon", +      keep = "keep", +      on_open = "on_open", +      on_close = "on_close", +      timeout = "timeout", +      title = "title", +    }, +    impl = notif_handle, +  }) + +  table.insert(self.__handle.sinks, sink) +end +  --- Adds a log entry using Plenary.log ----@fparam msg any +---@param msg any  ---@param level string [same as vim.log.log_levels]  function Log:add_entry(level, msg, event) -  if self.__handle then -    self.__handle:log(level, vim.inspect(msg), event) +  local logger = self:get_logger() +  if not logger then      return    end +  logger:log(level, vim.inspect(msg), event) +end + +---Retrieves the handle of the logger object +---@return table|nil logger handle if found +function Log:get_logger() +  if self.__handle then +    return self.__handle +  end    local logger = self:init()    if not logger then @@ -115,7 +146,7 @@ function Log:add_entry(level, msg, event)    end    self.__handle = logger -  self.__handle:log(level, vim.inspect(msg), event) +  return logger  end  ---Retrieves the path of the logfile diff --git a/lua/lvim/core/notify.lua b/lua/lvim/core/notify.lua index 5339357b..cb62778f 100644 --- a/lua/lvim/core/notify.lua +++ b/lua/lvim/core/notify.lua @@ -1,45 +1,59 @@  local M = {} -function M.config() -  local pallete = require "onedarker.palette" - -  lvim.builtin.notify = { -    active = false, -    on_config_done = nil, -    -- TODO: update after https://github.com/rcarriga/nvim-notify/pull/24 -    opts = { -      ---@usage Animation style one of { "fade", "slide", "fade_in_slide_out", "static" } -      stages = "slide", - -      ---@usage timeout for notifications in ms, default 5000 -      timeout = 5000, - -      ---@usage highlight behind the window for stages that change opacity -      background_colour = pallete.fg, - -      ---@usage Icons for the different levels -      icons = { -        ERROR = "", -        WARN = "", -        INFO = "", -        DEBUG = "", -        TRACE = "✎", -      }, +local Log = require "lvim.core.log" + +local defaults = { +  active = false, +  on_config_done = nil, +  opts = { +    ---@usage Animation style one of { "fade", "slide", "fade_in_slide_out", "static" } +    stages = "slide", + +    ---@usage Function called when a new window is opened, use for changing win settings/config +    on_open = nil, + +    ---@usage Function called when a window is closed +    on_close = nil, + +    ---@usage timeout for notifications in ms, default 5000 +    timeout = 5000, + +    -- Render function for notifications. See notify-render() +    render = "default", + +    ---@usage highlight behind the window for stages that change opacity +    background_colour = "Normal", + +    ---@usage minimum width for notification windows +    minimum_width = 50, + +    ---@usage Icons for the different levels +    icons = { +      ERROR = "", +      WARN = "", +      INFO = "", +      DEBUG = "", +      TRACE = "✎",      }, -  } +  }, +} + +function M.config() +  lvim.builtin.notify = vim.tbl_deep_extend("force", defaults, lvim.builtin.notify or {})  end -M.params_injecter = function(_, entry) -  -- FIXME: this is currently getting ignored or is not passed correctly -  for key, value in pairs(lvim.builtin.notify.opts) do -    entry[key] = value +function M.setup() +  if #vim.api.nvim_list_uis() == 0 then +    -- no need to configure notifications in headless +    return    end -  return entry -end -M.default_namer = function(logger, entry) -  entry["title"] = logger.name -  return entry +  local opts = lvim.builtin.notify and lvim.builtin.notify.opts or defaults +  local notify = require "notify" + +  notify.setup(opts) +  vim.notify = notify +  Log:configure_notifications(notify)  end  return M diff --git a/lua/lvim/plugins.lua b/lua/lvim/plugins.lua index 7585469f..35b273c5 100644 --- a/lua/lvim/plugins.lua +++ b/lua/lvim/plugins.lua @@ -53,6 +53,10 @@ return {      "rcarriga/nvim-notify",      commit = commit.nvim_notify,      disable = not lvim.builtin.notify.active, +    config = function() +      require("lvim.core.notify").setup() +    end, +    event = "BufRead",    },    { "Tastyep/structlog.nvim", commit = commit.structlog }, diff --git a/utils/installer/config.example.lua b/utils/installer/config.example.lua index 51b5f349..e3d9fa23 100644 --- a/utils/installer/config.example.lua +++ b/utils/installer/config.example.lua @@ -55,6 +55,7 @@ lvim.keys.normal_mode["<C-s>"] = ":w<cr>"  -- TODO: User Config for predefined plugins  -- After changing plugin config exit and reopen LunarVim, Run :PackerInstall :PackerCompile  lvim.builtin.dashboard.active = true +lvim.builtin.notify.active = true  lvim.builtin.terminal.active = true  lvim.builtin.nvimtree.setup.view.side = "left"  lvim.builtin.nvimtree.show_icons.git = 0 | 
