| 1 |
-- Copyright (c) 2020-2021 hoob3rt |
| 2 |
-- MIT license, see LICENSE for more details. |
| 3 |
|
| 4 |
local lualine_require = require('lualine_require') |
| 5 |
local require = lualine_require.require |
| 6 |
local modules = lualine_require.lazy_require { |
| 7 |
utils = 'lualine.utils.utils', |
| 8 |
notice = 'lualine.utils.notices', |
| 9 |
fn_store = 'lualine.utils.fn_store', |
| 10 |
} |
| 11 |
local is_valid_filename = lualine_require.is_valid_filename |
| 12 |
local sep = lualine_require.sep |
| 13 |
|
| 14 |
--- function that loads specific type of component |
| 15 |
local component_types = { |
| 16 |
-- loads custom component |
| 17 |
custom = function(component) |
| 18 |
return component[1](component) |
| 19 |
end, |
| 20 |
--- loads lua functions as component |
| 21 |
lua_fun = function(component) |
| 22 |
return require('lualine.components.special.function_component')(component) |
| 23 |
end, |
| 24 |
--- loads lua modules as components (ones in /lua/lualine/components/) |
| 25 |
mod = function(component) |
| 26 |
local ok, loaded_component = pcall(require, 'lualine.components.' .. component[1]) |
| 27 |
if ok then |
| 28 |
component.component_name = component[1] |
| 29 |
if type(loaded_component) == 'table' then |
| 30 |
loaded_component = loaded_component(component) |
| 31 |
elseif type(loaded_component) == 'function' then |
| 32 |
component[1] = loaded_component |
| 33 |
loaded_component = require('lualine.components.special.function_component')(component) |
| 34 |
end |
| 35 |
return loaded_component |
| 36 |
end |
| 37 |
end, |
| 38 |
--- loads builtin statusline patterns as component |
| 39 |
stl = function(component) |
| 40 |
local stl_expr = component[1] -- Vim's %p %l statusline elements |
| 41 |
component[1] = function() |
| 42 |
return stl_expr |
| 43 |
end |
| 44 |
return require('lualine.components.special.function_component')(component) |
| 45 |
end, |
| 46 |
--- loads variables & options (g:,go:,b:,bo:...) as components |
| 47 |
var = function(component) |
| 48 |
return require('lualine.components.special.vim_var_component')(component) |
| 49 |
end, |
| 50 |
--- loads vim functions and lua expressions as components |
| 51 |
['_'] = function(component) |
| 52 |
return require('lualine.components.special.eval_func_component')(component) |
| 53 |
end, |
| 54 |
} |
| 55 |
|
| 56 |
---load a component from component config |
| 57 |
---@param component table component + component options |
| 58 |
---@return table the loaded & initialized component |
| 59 |
local function component_loader(component) |
| 60 |
if type(component[1]) == 'function' then |
| 61 |
return component_types.lua_fun(component) |
| 62 |
elseif type(component[1]) == 'string' then |
| 63 |
-- load the component |
| 64 |
if component.type ~= nil then |
| 65 |
if component_types[component.type] and component.type ~= 'lua_fun' then |
| 66 |
return component_types[component.type](component) |
| 67 |
elseif component.type == 'vim_fun' or component.type == 'lua_expr' then |
| 68 |
return component_types['_'](component) |
| 69 |
else |
| 70 |
modules.notice.add_notice(string.format( |
| 71 |
[[ |
| 72 |
### component.type |
| 73 |
|
| 74 |
component type '%s' isn't recognised. Check if spelling is correct.]], |
| 75 |
component.type |
| 76 |
)) |
| 77 |
end |
| 78 |
end |
| 79 |
local loaded_component = component_types.mod(component) |
| 80 |
if loaded_component then |
| 81 |
return loaded_component |
| 82 |
elseif string.char(component[1]:byte(1)) == '%' then |
| 83 |
return component_types.stl(component) |
| 84 |
elseif component[1]:find('[gvtwb]?o?:') == 1 then |
| 85 |
return component_types.var(component) |
| 86 |
else |
| 87 |
return component_types['_'](component) |
| 88 |
end |
| 89 |
elseif type(component[1]) == 'table' then |
| 90 |
return component_types.custom(component) |
| 91 |
end |
| 92 |
end |
| 93 |
|
| 94 |
--- Shows notice about invalid types passed as component |
| 95 |
--- @param index number the index of component in section table |
| 96 |
--- @param component table containing component elements |
| 97 |
--- return bool whether check passed or not |
| 98 |
local function is_valid_component_type(index, component) |
| 99 |
if type(component_types) == 'table' and type(index) == 'number' then |
| 100 |
return true |
| 101 |
end |
| 102 |
modules.notice.add_notice(string.format( |
| 103 |
[[ |
| 104 |
### Unrecognized component |
| 105 |
Only functions, strings and tables can be used as component. |
| 106 |
You seem to have a `%s` as component indexed as `%s`. |
| 107 |
Something like: |
| 108 |
```lua |
| 109 |
%s = %s, |
| 110 |
``` |
| 111 |
|
| 112 |
This commonly occurs when you forget to pass table with option for component. |
| 113 |
When a component has option that component needs to be a table which holds |
| 114 |
the component as first element and the options as key value pairs. |
| 115 |
For example: |
| 116 |
```lua |
| 117 |
lualine_c = { |
| 118 |
{'diagnostics', |
| 119 |
sources = {'nvim'}, |
| 120 |
} |
| 121 |
} |
| 122 |
``` |
| 123 |
Notice the inner extra {} surrounding the component and it's options. |
| 124 |
Make sure your config follows this. |
| 125 |
]], |
| 126 |
type(component), |
| 127 |
index, |
| 128 |
index, |
| 129 |
vim.inspect(component) |
| 130 |
)) |
| 131 |
return false |
| 132 |
end |
| 133 |
|
| 134 |
---loads all the section from a config |
| 135 |
---@param sections table list of sections |
| 136 |
---@param options table global options table |
| 137 |
local function load_sections(sections, options) |
| 138 |
for section_name, section in pairs(sections) do |
| 139 |
for index, component in pairs(section) do |
| 140 |
if (type(component) == 'string' or type(component) == 'function') or modules.utils.is_component(component) then |
| 141 |
component = { component } |
| 142 |
end |
| 143 |
if is_valid_component_type(index, component) then |
| 144 |
component.self = {} |
| 145 |
component.self.section = section_name:match('lualine_(.*)') |
| 146 |
-- apply default args |
| 147 |
component = vim.tbl_extend('keep', component, options) |
| 148 |
section[index] = component_loader(component) |
| 149 |
end |
| 150 |
end |
| 151 |
end |
| 152 |
end |
| 153 |
|
| 154 |
---loads all the configs (active, inactive, tabline) |
| 155 |
---@param config table user config |
| 156 |
local function load_components(config) |
| 157 |
local sec_names = { 'sections', 'inactive_sections', 'tabline', 'winbar', 'inactive_winbar' } |
| 158 |
for _, section in ipairs(sec_names) do |
| 159 |
load_sections(config[section], config.options) |
| 160 |
end |
| 161 |
end |
| 162 |
|
| 163 |
---loads all the extensions |
| 164 |
---@param config table user config |
| 165 |
local function load_extensions(config) |
| 166 |
local loaded_extensions = {} |
| 167 |
local sec_names = { 'sections', 'inactive_sections', 'winbar', 'inactive_winbar' } |
| 168 |
for _, extension in pairs(config.extensions) do |
| 169 |
if type(extension) == 'string' then |
| 170 |
local ok |
| 171 |
ok, extension = pcall(require, 'lualine.extensions.' .. extension) |
| 172 |
if not ok then |
| 173 |
modules.notice.add_notice(string.format( |
| 174 |
[[ |
| 175 |
### Extensions |
| 176 |
Extension named `%s` was not found . Check if spelling is correct. |
| 177 |
]], |
| 178 |
extension |
| 179 |
)) |
| 180 |
end |
| 181 |
end |
| 182 |
if type(extension) == 'table' then |
| 183 |
local local_extension = modules.utils.deepcopy(extension) |
| 184 |
for _, section in ipairs(sec_names) do |
| 185 |
if local_extension[section] then |
| 186 |
-- break shared refs across sections (e.g. inactive_winbar = winbar) so load_sections doesn't mutate twice (#1507) |
| 187 |
local_extension[section] = modules.utils.deepcopy(local_extension[section]) |
| 188 |
load_sections(local_extension[section], config.options) |
| 189 |
end |
| 190 |
end |
| 191 |
if type(local_extension.init) == 'function' then |
| 192 |
local_extension.init() |
| 193 |
end |
| 194 |
table.insert(loaded_extensions, local_extension) |
| 195 |
end |
| 196 |
end |
| 197 |
config.extensions = loaded_extensions |
| 198 |
end |
| 199 |
|
| 200 |
---loads sections and extensions or entire user config |
| 201 |
---@param config table user config |
| 202 |
local function load_all(config) |
| 203 |
require('lualine.component')._reset_components() |
| 204 |
modules.fn_store.clear_fns() |
| 205 |
require('lualine.utils.nvim_opts').reset_cache() |
| 206 |
load_components(config) |
| 207 |
load_extensions(config) |
| 208 |
end |
| 209 |
|
| 210 |
---loads a theme from lua module |
| 211 |
---prioritizes external themes (from user config or other plugins) over the bundled ones |
| 212 |
---@param theme_name string |
| 213 |
---@return table theme definition from module |
| 214 |
local function load_theme(theme_name) |
| 215 |
assert(is_valid_filename(theme_name), 'Invalid filename') |
| 216 |
local retval |
| 217 |
local path = table.concat { 'lua/lualine/themes/', theme_name, '.lua' } |
| 218 |
local files = vim.api.nvim_get_runtime_file(path, true) |
| 219 |
if #files <= 0 then |
| 220 |
path = table.concat { 'lua/lualine/themes/', theme_name, '/init.lua' } |
| 221 |
files = vim.api.nvim_get_runtime_file(path, true) |
| 222 |
end |
| 223 |
local n_files = #files |
| 224 |
if n_files == 0 then |
| 225 |
-- No match found on runtimepath. Fall back to package.path |
| 226 |
local file = |
| 227 |
assert(package.searchpath('lualine.themes.' .. theme_name, package.path), 'Theme ' .. theme_name .. ' not found') |
| 228 |
retval = dofile(file) |
| 229 |
elseif n_files == 1 then |
| 230 |
-- when only one is found run that and return it's return value |
| 231 |
retval = dofile(files[1]) |
| 232 |
else |
| 233 |
-- put entries from user config path in front |
| 234 |
local user_config_path = vim.fn.stdpath('config') |
| 235 |
table.sort(files, function(a, b) |
| 236 |
local pattern = table.concat { user_config_path, sep } |
| 237 |
return string.match(a, pattern) or not string.match(b, pattern) |
| 238 |
end) |
| 239 |
-- More then 1 found . Use the first one that isn't in lualines repo |
| 240 |
local lualine_repo_pattern = table.concat({ 'lualine.nvim', 'lua', 'lualine' }, sep) |
| 241 |
local file_found = false |
| 242 |
for _, file in ipairs(files) do |
| 243 |
if not file:find(lualine_repo_pattern) then |
| 244 |
retval = dofile(file) |
| 245 |
file_found = true |
| 246 |
break |
| 247 |
end |
| 248 |
end |
| 249 |
if not file_found then |
| 250 |
-- This shouldn't happen but somehow we have multiple files but they |
| 251 |
-- appear to be in lualines repo . Just run the first one |
| 252 |
retval = dofile(files[1]) |
| 253 |
end |
| 254 |
end |
| 255 |
return retval |
| 256 |
end |
| 257 |
|
| 258 |
return { |
| 259 |
load_all = load_all, |
| 260 |
load_theme = load_theme, |
| 261 |
} |