diff --git a/README.md b/README.md index 053ae8e..86cfe67 100644 --- a/README.md +++ b/README.md @@ -451,6 +451,7 @@ sections = { hint = nil, -- Changes diagnostic's hint color } symbols = {error = 'E', warn = 'W', info = 'I', hint = 'H'} + colored = true, -- displays diagnostics status in color if set to true update_in_insert = false, -- Update diagnostics in insert mode always_visible = false, -- Show diagnostics even if count is 0, boolean or function returning boolean } @@ -458,6 +459,30 @@ sections = { } ``` +#### diff component options + +```lua +sections = { + lualine_a = { + { + 'diff', + colored = true, -- displays diff status in color if set to true + -- all colors are in format #rrggbb + diff_color = { + added = nil, -- changes diff's added color + modified = nil, -- changes diff's modified color + removed = nil, -- changes diff's removed color + } + symbols = {added = '+', modified = '~', removed = '-'} -- changes diff symbols + source = nil, -- A function that works as a data source for diff. + -- it must return a table like + -- {added = add_count, modified = modified_count, removed = removed_count } + -- Or nil on failure. Count <= 0 won't be displayed. + } + } +} +``` + #### filename component options ```lua @@ -488,30 +513,6 @@ sections = { } ``` -#### diff component options - -```lua -sections = { - lualine_a = { - { - 'diff', - colored = true, -- displays diff status in color if set to true - -- all colors are in format #rrggbb - diff_color = { - added = nil, -- changes diff's added color - modified = nil, -- changes diff's modified color - removed = nil, -- changes diff's removed color - } - symbols = {added = '+', modified = '~', removed = '-'} -- changes diff symbols - source = nil, -- A function that works as a data source for diff. - -- it must return a table like - -- {added = add_count, modified = modified_count, removed = removed_count } - -- Or nil on failure. Count <= 0 won't be displayed. - } - } -} -``` - #### tabs component options ```lua diff --git a/doc/lualine.txt b/doc/lualine.txt index 834e951..48e90dc 100644 --- a/doc/lualine.txt +++ b/doc/lualine.txt @@ -439,6 +439,7 @@ will be. hint = nil, -- Changes diagnostic's hint color } symbols = {error = 'E', warn = 'W', info = 'I', hint = 'H'} + colored = true, -- displays diagnostics status in color if set to true update_in_insert = false, -- Update diagnostics in insert mode always_visible = false, -- Show diagnostics even if count is 0, boolean or function returning boolean } @@ -447,6 +448,31 @@ will be. < + *lualine-diff-component-options* + +> + sections = { + lualine_a = { + { + 'diff', + colored = true, -- displays diff status in color if set to true + -- all colors are in format #rrggbb + diff_color = { + added = nil, -- changes diff's added color + modified = nil, -- changes diff's modified color + removed = nil, -- changes diff's removed color + } + symbols = {added = '+', modified = '~', removed = '-'} -- changes diff symbols + source = nil, -- A function that works as a data source for diff. + -- it must return a table like + -- {added = add_count, modified = modified_count, removed = removed_count } + -- Or nil on failure. Count <= 0 won't be displayed. + } + } + } +< + + *lualine-filename-component-options* > @@ -479,31 +505,6 @@ will be. < - *lualine-diff-component-options* - -> - sections = { - lualine_a = { - { - 'diff', - colored = true, -- displays diff status in color if set to true - -- all colors are in format #rrggbb - diff_color = { - added = nil, -- changes diff's added color - modified = nil, -- changes diff's modified color - removed = nil, -- changes diff's removed color - } - symbols = {added = '+', modified = '~', removed = '-'} -- changes diff symbols - source = nil, -- A function that works as a data source for diff. - -- it must return a table like - -- {added = add_count, modified = modified_count, removed = removed_count } - -- Or nil on failure. Count <= 0 won't be displayed. - } - } - } -< - - *lualine-tabs-component-options* > diff --git a/lua/lualine/components/diagnostics/init.lua b/lua/lualine/components/diagnostics/init.lua index f0700db..47aea00 100644 --- a/lua/lualine/components/diagnostics/init.lua +++ b/lua/lualine/components/diagnostics/init.lua @@ -60,29 +60,34 @@ function M:init(options) end -- Initialize variable to store last update so we can use it in insert -- mode for no update_in_insert - self.last_update = '' + self.last_diagnostics_count = {} end function M:update_status() - if not self.options.update_in_insert and vim.api.nvim_get_mode().mode:sub(1, 1) == 'i' then - return self.last_update - end - local error_count, warning_count, info_count, hint_count = 0, 0, 0, 0 - local diagnostic_data = modules.sources.get_diagnostics(self.options.sources) - -- sum all the counts - for _, data in pairs(diagnostic_data) do - error_count = error_count + data.error - warning_count = warning_count + data.warn - info_count = info_count + data.info - hint_count = hint_count + data.hint - end + local bufnr = vim.fn.bufnr() + local diagnostics_count local result = {} - local data = { - error = error_count, - warn = warning_count, - info = info_count, - hint = hint_count, - } + if self.options.update_in_insert or vim.api.nvim_get_mode().mode:sub(1, 1) ~= 'i' then + local error_count, warning_count, info_count, hint_count = 0, 0, 0, 0 + local diagnostic_data = modules.sources.get_diagnostics(self.options.sources) + -- sum all the counts + for _, data in pairs(diagnostic_data) do + error_count = error_count + data.error + warning_count = warning_count + data.warn + info_count = info_count + data.info + hint_count = hint_count + data.hint + end + diagnostics_count = { + error = error_count, + warn = warning_count, + info = info_count, + hint = hint_count, + } + -- Save count for insert mode + self.last_diagnostics_count[bufnr] = diagnostics_count + else -- Use cached count in insert mode with update_in_insert disabled + diagnostics_count = self.last_diagnostics_count[bufnr] or { error = 0, warn = 0, info = 0, hint = 0 } + end local always_visible = false if type(self.options.always_visible) == 'boolean' then @@ -98,22 +103,18 @@ function M:update_status() colors[name] = modules.highlight.component_format_highlight(hl) end for _, section in ipairs(self.options.sections) do - if data[section] ~= nil and (always_visible or data[section] > 0) then - table.insert(result, colors[section] .. self.symbols[section] .. data[section]) + if diagnostics_count[section] ~= nil and (always_visible or diagnostics_count[section] > 0) then + table.insert(result, colors[section] .. self.symbols[section] .. diagnostics_count[section]) end end else for _, section in ipairs(self.options.sections) do - if data[section] ~= nil and (always_visible or data[section] > 0) then - table.insert(result, self.symbols[section] .. data[section]) + if diagnostics_count[section] ~= nil and (always_visible or diagnostics_count[section] > 0) then + table.insert(result, self.symbols[section] .. diagnostics_count[section]) end end end - self.last_update = '' - if result[1] ~= nil then - self.last_update = table.concat(result, ' ') - end - return self.last_update + return table.concat(result, ' ') end return M