diff options
Diffstat (limited to 'lua/lvim/core')
| -rw-r--r-- | lua/lvim/core/autocmds.lua | 85 | ||||
| -rw-r--r-- | lua/lvim/core/dashboard.lua | 2 | ||||
| -rw-r--r-- | lua/lvim/core/info.lua | 42 | ||||
| -rw-r--r-- | lua/lvim/core/log.lua | 123 | ||||
| -rw-r--r-- | lua/lvim/core/lualine/components.lua | 5 | ||||
| -rw-r--r-- | lua/lvim/core/notify.lua | 84 | ||||
| -rw-r--r-- | lua/lvim/core/telescope/custom-finders.lua | 10 | ||||
| -rw-r--r-- | lua/lvim/core/treesitter.lua | 10 | ||||
| -rw-r--r-- | lua/lvim/core/which-key.lua | 22 | 
9 files changed, 238 insertions, 145 deletions
| diff --git a/lua/lvim/core/autocmds.lua b/lua/lvim/core/autocmds.lua index e4577e63..712fd323 100644 --- a/lua/lvim/core/autocmds.lua +++ b/lua/lvim/core/autocmds.lua @@ -78,7 +78,7 @@ local get_format_on_save_opts = function()  end  function M.enable_format_on_save(opts) -  local fmd_cmd = string.format(":silent lua vim.lsp.buf.formatting_sync({}, %s)", opts.timeout_ms) +  local fmd_cmd = string.format(":silent lua vim.lsp.buf.formatting_sync({}, %s)", opts.timeout)    M.define_augroups {      format_on_save = { { "BufWritePre", opts.pattern, fmd_cmd } },    } @@ -86,16 +86,12 @@ function M.enable_format_on_save(opts)  end  function M.disable_format_on_save() -  M.remove_augroup "format_on_save" +  M.disable_augroup "format_on_save"    Log:debug "disabled format-on-save"  end  function M.configure_format_on_save()    if lvim.format_on_save then -    if vim.fn.exists "#format_on_save#BufWritePre" == 1 then -      M.remove_augroup "format_on_save" -      Log:debug "reloading format-on-save configuration" -    end      local opts = get_format_on_save_opts()      M.enable_format_on_save(opts)    else @@ -112,24 +108,73 @@ function M.toggle_format_on_save()    end  end -function M.remove_augroup(name) -  if vim.fn.exists("#" .. name) == 1 then -    vim.cmd("au! " .. name) -  end +function M.enable_lsp_document_highlight(client_id) +  M.define_augroups({ +    lsp_document_highlight = { +      { +        "CursorHold", +        "<buffer>", +        string.format("lua require('lvim.lsp.utils').conditional_document_highlight(%d)", client_id), +      }, +      { +        "CursorMoved", +        "<buffer>", +        "lua vim.lsp.buf.clear_references()", +      }, +    }, +  }, true) +end + +function M.disable_lsp_document_highlight() +  M.disable_augroup "lsp_document_highlight" +end + +function M.enable_code_lens_refresh() +  M.define_augroups({ +    lsp_code_lens_refresh = { +      { +        "InsertLeave ", +        "<buffer>", +        "lua vim.lsp.codelens.refresh()", +      }, +      { +        "InsertLeave ", +        "<buffer>", +        "lua vim.lsp.codelens.display()", +      }, +    }, +  }, true)  end -function M.define_augroups(definitions) -- {{{1 -  -- Create autocommand groups based on the passed definitions -  -- -  -- The key will be the name of the group, and each definition -  -- within the group should have: -  --    1. Trigger -  --    2. Pattern -  --    3. Text -  -- just like how they would normally be defined from Vim itself +function M.disable_code_lens_refresh() +  M.disable_augroup "lsp_code_lens_refresh" +end + +--- Disable autocommand groups if it exists +--- This is more reliable than trying to delete the augroup itself +---@param name string the augroup name +function M.disable_augroup(name) +  -- defer the function in case the autocommand is still in-use +  vim.schedule(function() +    if vim.fn.exists("#" .. name) == 1 then +      vim.cmd("augroup " .. name) +      vim.cmd "autocmd!" +      vim.cmd "augroup END" +    end +  end) +end + +--- Create autocommand groups based on the passed definitions +---@param definitions table contains trigger, pattern and text. The key will be used as a group name +---@param buffer boolean indicate if the augroup should be local to the buffer +function M.define_augroups(definitions, buffer)    for group_name, definition in pairs(definitions) do      vim.cmd("augroup " .. group_name) -    vim.cmd "autocmd!" +    if buffer then +      vim.cmd [[autocmd! * <buffer>]] +    else +      vim.cmd [[autocmd!]] +    end      for _, def in pairs(definition) do        local command = table.concat(vim.tbl_flatten { "autocmd", def }, " ") diff --git a/lua/lvim/core/dashboard.lua b/lua/lvim/core/dashboard.lua index 0f62d973..438b46f3 100644 --- a/lua/lvim/core/dashboard.lua +++ b/lua/lvim/core/dashboard.lua @@ -73,7 +73,7 @@ M.setup = function()    vim.g.dashboard_session_directory = lvim.builtin.dashboard.session_directory    local lvim_site = "lunarvim.org" -  local lvim_version = require("lvim.bootstrap"):get_version "short" +  local lvim_version = require("lvim.utils.git"):get_lvim_version "short"    local num_plugins_loaded = #vim.fn.globpath(get_runtime_dir() .. "/site/pack/packer/start", "*", 0, 1)    local footer = { diff --git a/lua/lvim/core/info.lua b/lua/lvim/core/info.lua index df7b7061..7577f296 100644 --- a/lua/lvim/core/info.lua +++ b/lua/lvim/core/info.lua @@ -19,9 +19,8 @@ end  local function make_formatters_info(ft)    local null_formatters = require "lvim.lsp.null-ls.formatters" -  local registered_formatters = null_formatters.list_registered_providers(ft) -  -- print("reg", vim.inspect(registered_formatters)) -  local supported_formatters = null_formatters.list_available(ft) +  local registered_formatters = null_formatters.list_registered(ft) +  local supported_formatters = null_formatters.list_supported(ft)    local section = {      "Formatters info",      fmt( @@ -37,8 +36,7 @@ end  local function make_code_actions_info(ft)    local null_actions = require "lvim.lsp.null-ls.code_actions" -  local registered_actions = null_actions.list_registered_providers(ft) -  local supported_actions = null_actions.list_available(ft) +  local registered_actions = null_actions.list_registered(ft)    local section = {      "Code actions info",      fmt( @@ -46,7 +44,6 @@ local function make_code_actions_info(ft)        table.concat(registered_actions, "  , "),        vim.tbl_count(registered_actions) > 0 and "  " or ""      ), -    fmt("* Supported: %s", str_list(supported_actions)),    }    return section @@ -54,8 +51,8 @@ end  local function make_linters_info(ft)    local null_linters = require "lvim.lsp.null-ls.linters" -  local supported_linters = null_linters.list_available(ft) -  local registered_linters = null_linters.list_registered_providers(ft) +  local supported_linters = null_linters.list_supported(ft) +  local registered_linters = null_linters.list_registered(ft)    local section = {      "Linters info",      fmt( @@ -168,21 +165,20 @@ function M.toggle_popup(ft)    local function set_syntax_hl()      vim.cmd [[highlight LvimInfoIdentifier gui=bold]]      vim.cmd [[highlight link LvimInfoHeader Type]] -    vim.cmd [[let m=matchadd("LvimInfoHeader", "Treesitter info")]] -    vim.cmd [[let m=matchadd("LvimInfoHeader", "Language Server Protocol (LSP) info")]] -    vim.cmd [[let m=matchadd("LvimInfoHeader", "Formatters info")]] -    vim.cmd [[let m=matchadd("LvimInfoHeader", "Linters info")]] -    vim.cmd [[let m=matchadd("LvimInfoHeader", "Code actions info")]] -    vim.cmd('let m=matchadd("LvimInfoIdentifier", " ' .. ft .. '$")') -    vim.cmd 'let m=matchadd("string", "true")' -    vim.cmd 'let m=matchadd("string", "active")' -    vim.cmd 'let m=matchadd("boolean", "inactive")' -    vim.cmd 'let m=matchadd("string", "")' -    vim.cmd 'let m=matchadd("error", "false")' -    -- tbl_set_highlight(registered_providers, "LvimInfoIdentifier") -    tbl_set_highlight(require("lvim.lsp.null-ls.formatters").list_available(ft), "LvimInfoIdentifier") -    tbl_set_highlight(require("lvim.lsp.null-ls.linters").list_available(ft), "LvimInfoIdentifier") -    tbl_set_highlight(require("lvim.lsp.null-ls.code_actions").list_available(ft), "LvimInfoIdentifier") +    vim.fn.matchadd("LvimInfoHeader", "Treesitter info") +    vim.fn.matchadd("LvimInfoHeader", "Language Server Protocol (LSP) info") +    vim.fn.matchadd("LvimInfoHeader", "Formatters info") +    vim.fn.matchadd("LvimInfoHeader", "Linters info") +    vim.fn.matchadd("LvimInfoHeader", "Code actions info") +    vim.fn.matchadd("LvimInfoIdentifier", " " .. ft .. "$") +    vim.fn.matchadd("string", "true") +    vim.fn.matchadd("string", "active") +    vim.fn.matchadd("string", "") +    vim.fn.matchadd("boolean", "inactive") +    vim.fn.matchadd("error", "false") +    tbl_set_highlight(require("lvim.lsp.null-ls.formatters").list_registered(ft), "LvimInfoIdentifier") +    tbl_set_highlight(require("lvim.lsp.null-ls.linters").list_registered(ft), "LvimInfoIdentifier") +    tbl_set_highlight(require("lvim.lsp.null-ls.code_actions").list_registered(ft), "LvimInfoIdentifier")    end    local Popup = require("lvim.interface.popup"):new { 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/lualine/components.lua b/lua/lvim/core/lualine/components.lua index 9cf67616..b1387afa 100644 --- a/lua/lvim/core/lualine/components.lua +++ b/lua/lvim/core/lualine/components.lua @@ -104,17 +104,16 @@ return {        -- add formatter        local formatters = require "lvim.lsp.null-ls.formatters" -      local supported_formatters = formatters.list_registered_providers(buf_ft) +      local supported_formatters = formatters.list_registered(buf_ft)        vim.list_extend(buf_client_names, supported_formatters)        -- add linter        local linters = require "lvim.lsp.null-ls.linters" -      local supported_linters = linters.list_registered_providers(buf_ft) +      local supported_linters = linters.list_registered(buf_ft)        vim.list_extend(buf_client_names, supported_linters)        return "[" .. table.concat(buf_client_names, ", ") .. "]"      end, -    -- icon = " ",      color = { gui = "bold" },      cond = conditions.hide_in_width,    }, 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/core/telescope/custom-finders.lua b/lua/lvim/core/telescope/custom-finders.lua index 5ce1485c..18307fbd 100644 --- a/lua/lvim/core/telescope/custom-finders.lua +++ b/lua/lvim/core/telescope/custom-finders.lua @@ -82,4 +82,14 @@ function M.view_lunarvim_changelog()    }):find()  end +-- Smartly opens either git_files or find_files, depending on whether the working directory is +-- contained in a Git repo. +function M.find_project_files() +  local ok = pcall(builtin.git_files) + +  if not ok then +    builtin.find_files() +  end +end +  return M diff --git a/lua/lvim/core/treesitter.lua b/lua/lvim/core/treesitter.lua index 8dbbcacb..d8b0c136 100644 --- a/lua/lvim/core/treesitter.lua +++ b/lua/lvim/core/treesitter.lua @@ -74,16 +74,20 @@ M.config = function()  end  M.setup = function() +  -- avoid running in headless mode since it's harder to detect failures +  if #vim.api.nvim_list_uis() == 0 then +    Log:debug "headless mode detected, skipping running setup for treesitter" +    return +  end +    local status_ok, treesitter_configs = pcall(require, "nvim-treesitter.configs")    if not status_ok then -    Log:get_default().error "Failed to load nvim-treesitter.configs" +    Log:error "Failed to load nvim-treesitter.configs"      return    end    local opts = vim.deepcopy(lvim.builtin.treesitter) -  -- avoid running any installers in headless mode since it's harder to detect failures -  opts.ensure_installed = #vim.api.nvim_list_uis() == 0 and {} or opts.ensure_installed    treesitter_configs.setup(opts)    if lvim.builtin.treesitter.on_config_done then diff --git a/lua/lvim/core/which-key.lua b/lua/lvim/core/which-key.lua index 88af028f..8691a9a6 100644 --- a/lua/lvim/core/which-key.lua +++ b/lua/lvim/core/which-key.lua @@ -61,14 +61,14 @@ M.config = function()      -- NOTE: Prefer using : over <cmd> as the latter avoids going back in normal-mode.      -- see https://neovim.io/doc/user/map.html#:map-cmd      vmappings = { -      ["/"] = { "<ESC><CMD>lua require('Comment.api').gc(vim.fn.visualmode())<CR>", "Comment" }, +      ["/"] = { "<ESC><CMD>lua require('Comment.api').toggle_linewise_op(vim.fn.visualmode())<CR>", "Comment" },      },      mappings = {        ["w"] = { "<cmd>w!<CR>", "Save" },        ["q"] = { "<cmd>q!<CR>", "Quit" }, -      ["/"] = { "<cmd>lua require('Comment').toggle()<CR>", "Comment" }, +      ["/"] = { "<cmd>lua require('Comment.api').toggle_current_linewise()<CR>", "Comment" },        ["c"] = { "<cmd>BufferClose!<CR>", "Close Buffer" }, -      ["f"] = { "<cmd>Telescope find_files<CR>", "Find File" }, +      ["f"] = { require("lvim.core.telescope.custom-finders").find_project_files, "Find File" },        ["h"] = { "<cmd>nohlsearch<CR>", "No Highlight" },        b = {          name = "Buffers", @@ -140,23 +140,17 @@ M.config = function()        l = {          name = "LSP",          a = { "<cmd>lua require('lvim.core.telescope').code_actions()<cr>", "Code Action" }, -        d = { -          "<cmd>Telescope lsp_document_diagnostics<cr>", -          "Document Diagnostics", -        }, -        w = { -          "<cmd>Telescope lsp_workspace_diagnostics<cr>", -          "Workspace Diagnostics", -        }, +        d = { "<cmd>Telescope diagnostics bufnr=0 theme=get_ivy<cr>", "Buffer Diagnostics" }, +        w = { "<cmd>Telescope diagnostics<cr>", "Diagnostics" },          f = { "<cmd>lua vim.lsp.buf.formatting()<cr>", "Format" },          i = { "<cmd>LspInfo<cr>", "Info" },          I = { "<cmd>LspInstallInfo<cr>", "Installer Info" },          j = { -          "<cmd>lua vim.lsp.diagnostic.goto_next({popup_opts = {border = lvim.lsp.popup_border}})<cr>", +          "<cmd>lua vim.diagnostic.goto_next()<cr>",            "Next Diagnostic",          },          k = { -          "<cmd>lua vim.lsp.diagnostic.goto_prev({popup_opts = {border = lvim.lsp.popup_border}})<cr>", +          "<cmd>lua vim.diagnostic.goto_prev()<cr>",            "Prev Diagnostic",          },          l = { "<cmd>lua vim.lsp.codelens.run()<cr>", "CodeLens Action" }, @@ -166,7 +160,7 @@ M.config = function()            t = { "<cmd>lua require('lvim.lsp.peek').Peek('typeDefinition')<cr>", "Type Definition" },            i = { "<cmd>lua require('lvim.lsp.peek').Peek('implementation')<cr>", "Implementation" },          }, -        q = { "<cmd>lua vim.lsp.diagnostic.set_loclist()<cr>", "Quickfix" }, +        q = { "<cmd>lua vim.diagnostic.setloclist()<cr>", "Quickfix" },          r = { "<cmd>lua vim.lsp.buf.rename()<cr>", "Rename" },          s = { "<cmd>Telescope lsp_document_symbols<cr>", "Document Symbols" },          S = { | 
