Repository: nvim-lualine/lualine.nvim
Pull request #1196 (2024-02-18 06:19:29)

fix: mason extension lazy loading
Fixed mason extension to only load mason-registry when the lualine section is
displayed (on 'mason' filetype). This prevents loading mason when lualine is
loaded.



Patch:
diff --git a/examples/bubbles.lua b/examples/bubbles.lua
index c1ecf31..88edf57 100644
--- a/examples/bubbles.lua
+++ b/examples/bubbles.lua
@@ -17,7 +17,7 @@ local bubbles_theme = {
   normal = {
     a = { fg = colors.black, bg = colors.violet },
     b = { fg = colors.white, bg = colors.grey },
-    c = { fg = colors.black, bg = colors.black },
+    c = { fg = colors.white },
   },
 
   insert = { a = { fg = colors.black, bg = colors.blue } },
@@ -27,22 +27,22 @@ local bubbles_theme = {
   inactive = {
     a = { fg = colors.white, bg = colors.black },
     b = { fg = colors.white, bg = colors.black },
-    c = { fg = colors.black, bg = colors.black },
+    c = { fg = colors.white },
   },
 }
 
 require('lualine').setup {
   options = {
     theme = bubbles_theme,
-    component_separators = '|',
+    component_separators = '',
     section_separators = { left = '', right = '' },
   },
   sections = {
-    lualine_a = {
-      { 'mode', separator = { left = '' }, right_padding = 2 },
-    },
+    lualine_a = { { 'mode', separator = { left = '' }, right_padding = 2 } },
     lualine_b = { 'filename', 'branch' },
-    lualine_c = { 'fileformat' },
+    lualine_c = {
+      '%=', --[[ add your center compoentnts here in place of this comment ]]
+    },
     lualine_x = {},
     lualine_y = { 'filetype', 'progress' },
     lualine_z = {
diff --git a/lua/lualine.lua b/lua/lualine.lua
index fa89a3e..6093049 100644
--- a/lua/lualine.lua
+++ b/lua/lualine.lua
@@ -27,7 +27,7 @@ local refresh_real_curwin
 
 -- The events on which lualine redraws itself
 local default_refresh_events =
-  'WinEnter,BufEnter,SessionLoadPost,FileChangedShellPost,VimResized,Filetype,CursorMoved,CursorMovedI,ModeChanged'
+  'WinEnter,BufEnter,BufWritePost,SessionLoadPost,FileChangedShellPost,VimResized,Filetype,CursorMoved,CursorMovedI,ModeChanged'
 -- Helper for apply_transitional_separators()
 --- finds first applied highlight group after str_checked in status
 ---@param status string : unprocessed statusline string
diff --git a/lua/lualine/components/branch/git_branch.lua b/lua/lualine/components/branch/git_branch.lua
index 48625f5..dcc9775 100644
--- a/lua/lualine/components/branch/git_branch.lua
+++ b/lua/lualine/components/branch/git_branch.lua
@@ -76,6 +76,12 @@ function M.find_git_dir(dir_path)
 
   -- get file dir so we can search from that dir
   local file_dir = dir_path or vim.fn.expand('%:p:h')
+
+  -- extract correct file dir from terminals
+  if file_dir and file_dir:match('term://.*') then
+    file_dir = vim.fn.expand(file_dir:gsub('term://(.+)//.+', '%1'))
+  end
+
   local root_dir = file_dir
   -- Search upward for .git file or folder
   while root_dir do
diff --git a/lua/lualine/components/filetype.lua b/lua/lualine/components/filetype.lua
index 673379d..748359f 100644
--- a/lua/lualine/components/filetype.lua
+++ b/lua/lualine/components/filetype.lua
@@ -40,6 +40,9 @@ function M:apply_icon()
       icon = ''
       icon_highlight_group = 'DevIconDefault'
     end
+    if icon then
+      icon = icon .. ' '
+    end
     if self.options.colored then
       local highlight_color = modules.utils.extract_highlight_colors(icon_highlight_group, 'fg')
       if highlight_color then
@@ -57,6 +60,9 @@ function M:apply_icon()
     ok = vim.fn.exists('*WebDevIconsGetFileTypeSymbol')
     if ok ~= 0 then
       icon = vim.fn.WebDevIconsGetFileTypeSymbol()
+      if icon then
+        icon = icon .. ' '
+      end
     end
   end
 
@@ -69,7 +75,7 @@ function M:apply_icon()
   elseif type(self.options.icon) == 'table' and self.options.icon.align == 'right' then
     self.status = self.status .. ' ' .. icon
   else
-    self.status = icon .. ' ' .. self.status
+    self.status = icon .. self.status
   end
 end
 
diff --git a/lua/lualine/extensions/fzf.lua b/lua/lualine/extensions/fzf.lua
index 00fea2b..956a0e9 100644
--- a/lua/lualine/extensions/fzf.lua
+++ b/lua/lualine/extensions/fzf.lua
@@ -5,10 +5,12 @@ works with both https://github.com/junegunn/fzf.vim and https://github.com/ibhag
 -- fzf-lua must be set-up in split mode
 ]]
 
-local fzf_lua, _ = pcall(require, 'fzf-lua')
+local function has_fzf()
+  return pcall(require, 'fzf-lua')
+end
 
 local function fzf_picker()
-  if not fzf_lua then
+  if not has_fzf() then
     return ''
   end
 
@@ -17,16 +19,13 @@ local function fzf_picker()
 end
 
 local function fzf_element()
-  if not fzf_lua then
+  if not has_fzf() then
     return ''
   end
 
-  local info_string = vim.inspect(require('fzf-lua').get_info()['selected'])
-  local lines = {}
-  for w in info_string:gsub('"', ''):gmatch('%S+') do
-    table.insert(lines, w)
-  end
-  return lines[1]
+  local fzf = require('fzf-lua')
+  local selected = fzf.get_info().selected
+  return fzf.path.entry_to_file(selected).path
 end
 
 local function fzf_statusline()
diff --git a/lua/lualine/extensions/mason.lua b/lua/lualine/extensions/mason.lua
index 16ed56b..5432fe6 100644
--- a/lua/lualine/extensions/mason.lua
+++ b/lua/lualine/extensions/mason.lua
@@ -1,8 +1,11 @@
 -- lualine extension for mason.nvim
 
-local ok, mason_registry = pcall(require, 'mason-registry')
-if not ok then
-  return ''
+local mason_registry
+local function maybe_set_registry()
+  local ok, registry = pcall(require, 'mason-registry')
+  if ok then
+    mason_registry = registry
+  end
 end
 
 local M = {}
@@ -15,6 +18,7 @@ M.sections = {
   },
   lualine_b = {
     function()
+      maybe_set_registry()
       return 'Installed: ' .. #mason_registry.get_installed_packages() .. '/' .. #mason_registry.get_all_package_specs()
     end,
   },


Test patch:
diff --git a/tests/spec/component_spec.lua b/tests/spec/component_spec.lua
index 42dd2bc..7be377b 100644
--- a/tests/spec/component_spec.lua
+++ b/tests/spec/component_spec.lua
@@ -323,7 +323,7 @@ describe('Filetype component', function()
       colored = true,
       icon_only = false,
     }
-    assert_component('filetype', opts, '%#MyCompHl_normal#*%#lualine_c_normal# lua%#lualine_c_normal#')
+    assert_component('filetype', opts, '%#MyCompHl_normal#* %#lualine_c_normal#lua%#lualine_c_normal#')
     assert.stub(devicons.get_icon).was_called_with('test.lua')
     assert.stub(utils.extract_highlight_colors).was_called_with('test_highlight_group', 'fg')
     assert.stub(hl.create_component_highlight_group).was_called_with(
@@ -393,7 +393,7 @@ describe('Filetype component', function()
       colored = false,
       icon_only = true,
     }
-    assert_component('filetype', opts, '*')
+    assert_component('filetype', opts, '* ')
     assert.stub(devicons.get_icon).was_called_with('test.lua')
     assert.stub(utils.extract_highlight_colors).was_not_called()
     assert.stub(hl.create_component_highlight_group).was_not_called()
@@ -426,7 +426,7 @@ describe('Filetype component', function()
       icon_only = false,
       icon = { align = 'right' }
     }
-    assert_component('filetype', opts, 'lua *')
+    assert_component('filetype', opts, 'lua * ')
     assert.stub(devicons.get_icon).was_called_with('test.lua')
     assert.stub(utils.extract_highlight_colors).was_not_called()
     assert.stub(hl.create_component_highlight_group).was_not_called()