Skip to content

Commit

Permalink
feat(statusline)!: make section_git() prefer 'mini.git' data
Browse files Browse the repository at this point in the history
If 'mini.git' is not enabled, fall back to pure HEAD name from
'gitsigns.nvim'.
  • Loading branch information
echasnovski committed May 21, 2024
1 parent 9d43bd6 commit 2d187c1
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 63 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@

- Explicitly block all events in `open()` during startup for a better performance.

## mini.statusline

- BREAKING FEATURE: `section_git()` now prefers using data from 'mini.git' with fallback on pure HEAD data from 'lewis6991/gistigns.nvim'.

## mini.tabline

- FEATURE: Implement `config.format` for custom label formatting.
Expand Down
21 changes: 13 additions & 8 deletions doc/mini-statusline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ Features:
Suggested dependencies (provide extra functionality, statusline will work
without them):
- Nerd font (to support extra icons).
- Plugin 'lewis6991/gitsigns.nvim' for Git information in
|MiniStatusline.section_git|. If missing, no section will be shown.
- Plugin 'nvim-tree/nvim-web-devicons' for filetype icons in
`MiniStatusline.section_fileinfo`. If missing, no icons will be shown.

- Enabled |MiniGit| module for |MiniStatusline.section_git()|.
Falls back to using 'lewis6991/gitsigns.nvim' plugin or shows nothing.

- Plugin 'nvim-tree/nvim-web-devicons' for filetype icons
in |MiniStatusline.section_fileinfo()|. If missing, no icons will be shown.

# Setup ~

Expand Down Expand Up @@ -213,11 +215,14 @@ Return ~
`MiniStatusline.section_git`({args})
Section for Git information

Normal output contains name of `HEAD` (via |b:gitsigns_head|) and chunk
information (via |b:gitsigns_status|). Short output - only name of `HEAD`.
Note: requires 'lewis6991/gitsigns' plugin.
Shows Git summary from |MiniGit| (should be set up; recommended). To tweak
formatting of what data is shown, modify buffer-local summary string directly
as described in |MiniGit-examples|.

Short output is returned if window width is lower than `args.trunc_width`.
If 'mini.git' is not set up, section falls back on 'lewis6991/gitsigns' data
or showing empty string.

Empty string is returned if window width is lower than `args.trunc_width`.

Parameters ~
{args} `(table)` Section arguments. Use `args.icon` to supply your own icon.
Expand Down
35 changes: 18 additions & 17 deletions lua/mini/statusline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
--- Suggested dependencies (provide extra functionality, statusline will work
--- without them):
--- - Nerd font (to support extra icons).
--- - Plugin 'lewis6991/gitsigns.nvim' for Git information in
--- |MiniStatusline.section_git|. If missing, no section will be shown.
--- - Plugin 'nvim-tree/nvim-web-devicons' for filetype icons in
--- `MiniStatusline.section_fileinfo`. If missing, no icons will be shown.
---
--- - Enabled |MiniGit| module for |MiniStatusline.section_git()|.
--- Falls back to using 'lewis6991/gitsigns.nvim' plugin or shows nothing.
---
--- - Plugin 'nvim-tree/nvim-web-devicons' for filetype icons
--- in |MiniStatusline.section_fileinfo()|. If missing, no icons will be shown.
---
--- # Setup ~
---
Expand Down Expand Up @@ -258,27 +260,26 @@ end

--- Section for Git information
---
--- Normal output contains name of `HEAD` (via |b:gitsigns_head|) and chunk
--- information (via |b:gitsigns_status|). Short output - only name of `HEAD`.
--- Note: requires 'lewis6991/gitsigns' plugin.
--- Shows Git summary from |MiniGit| (should be set up; recommended). To tweak
--- formatting of what data is shown, modify buffer-local summary string directly
--- as described in |MiniGit-examples|.
---
--- Short output is returned if window width is lower than `args.trunc_width`.
--- If 'mini.git' is not set up, section falls back on 'lewis6991/gitsigns' data
--- or showing empty string.
---
--- Empty string is returned if window width is lower than `args.trunc_width`.
---
---@param args __statusline_args Use `args.icon` to supply your own icon.
---
---@return __statusline_section
MiniStatusline.section_git = function(args)
if H.isnt_normal_buffer() then return '' end
if MiniStatusline.is_truncated(args.trunc_width) then return '' end

local head = vim.b.gitsigns_head or '-'
local signs = MiniStatusline.is_truncated(args.trunc_width) and '' or (vim.b.gitsigns_status or '')
local icon = args.icon or (H.get_config().use_icons and '' or 'Git')
local summary = vim.b.minigit_summary_string or vim.b.gitsigns_head
if summary == nil then return '' end

if signs == '' then
if head == '-' or head == '' then return '' end
return string.format('%s %s', icon, head)
end
return string.format('%s %s %s', icon, head, signs)
local icon = args.icon or (H.get_config().use_icons and '' or 'Git')
return string.format('%s %s', icon, summary == '' and '-' or summary)
end

--- Section for Neovim's builtin diagnostics
Expand Down
6 changes: 4 additions & 2 deletions readmes/mini-statusline.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

### Minimal and fast statusline module with opinionated default look

For full experience needs [Nerd font](https://www.nerdfonts.com/), [lewis6991/gitsigns.nvim](https://github.com/lewis6991/gitsigns.nvim) plugin, and [nvim-tree/nvim-web-devicons](https://github.com/nvim-tree/nvim-web-devicons) plugin (but works without any of them).
For full experience needs (still works without any of suggestions):

- [Nerd font](https://www.nerdfonts.com/) and [nvim-tree/nvim-web-devicons](https://github.com/nvim-tree/nvim-web-devicons) plugin to show icons.
- Enabled ['mini.git'](https://github.com/echasnovski/mini.nvim/blob/main/readmes/mini-git.md) modules to show Git related information. Can fall back to using [lewis6991/gitsigns.nvim](https://github.com/lewis6991/gitsigns.nvim) plugin.

See more details in [Features](#features) and [help file](../doc/mini-statusline.txt).

Expand Down Expand Up @@ -161,4 +164,3 @@ Here are code snippets for some common installation methods (use only one):
## Similar plugins

- [nvim-lualine/lualine.nvim](https://github.com/nvim-lualine/lualine.nvim)
- [famiu/feline.nvim](https://github.com/famiu/feline.nvim)
65 changes: 29 additions & 36 deletions tests/test_statusline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,12 @@ end
-- Mocks
local mock_devicons = function() child.cmd('set rtp+=tests/dir-statusline') end

local mock_gitsigns = function(head, status)
local cmd_head = ([[lua vim.b.gitsigns_head = '%s']]):format(head or 'main')
local cmd_status = ([[lua vim.b.gitsigns_status = '%s']]):format(status or '+1 ~2 -3')

-- Mock for current buffer
child.cmd(cmd_head)
child.cmd(cmd_status)

-- Mock for future buffers
child.cmd('augroup MockGitsigns')
child.cmd('au!')
child.cmd(('au BufEnter * %s'):format(cmd_head))
child.cmd(('au BufEnter * %s'):format(cmd_status))
child.cmd('augroup END')
local mock_gitsigns = function()
child.b.gitsigns_head, child.b.gitsigns_status = 'main', '+1 ~2 -3'
end

local mock_minigit = function() child.b.minigit_summary_string = 'main|bisect (MM)' end

local mock_diagnostics = function() child.cmd('luafile tests/dir-statusline/mock-diagnostics.lua') end

local mocked_filepath = vim.fn.fnamemodify('tests/dir-statusline/mocked.lua', ':p')
Expand Down Expand Up @@ -457,51 +447,54 @@ T['section_filename()']['respects `args.trunc_width`'] = function()
eq(child.lua_get('MiniStatusline.section_filename({ trunc_width = 100 })'), '%f%m%r')
end

T['section_git()'] = new_set({ hooks = { pre_case = mock_gitsigns } })
T['section_git()'] = new_set({ hooks = { pre_case = mock_minigit } })

T['section_git()']['works'] = function()
eq(child.lua_get('MiniStatusline.section_git({})'), ' main +1 ~2 -3')
eq(child.lua_get('MiniStatusline.section_git({})'), ' main|bisect (MM)')

-- Should show signs even if there is no branch
child.b.gitsigns_head = nil
eq(child.lua_get('MiniStatusline.section_git({})'), ' - +1 ~2 -3')
-- Should return non-empty string even if there is no branch
child.b.minigit_summary_string = ''
eq(child.lua_get('MiniStatusline.section_git({})'), ' -')

-- Should return empty string if no Git data is found
child.b.minigit_summary_string = nil
eq(child.lua_get('MiniStatusline.section_git({})'), '')
end

T['section_git()']["falls back to 'gitsigns.nvim'"] = function()
child.b.minigit_summary_string = nil
mock_gitsigns()

-- Should not have trailing whitespace if git status is empty
child.b.gitsigns_head, child.b.gitsigns_status = 'main', ''
eq(child.lua_get('MiniStatusline.section_git({})'), ' main')

-- Should return empty string if no Git is found
child.b.gitsigns_head, child.b.gitsigns_status = nil, nil
eq(child.lua_get('MiniStatusline.section_git({})'), '')
-- Should return non-empty string even if there is no branch
child.b.gitsigns_head = ''
eq(child.lua_get('MiniStatusline.section_git({})'), ' -')

child.b.gitsigns_head, child.b.gitsigns_status = '', ''
-- Should return empty string if no Git data is found
child.b.gitsigns_head = nil
eq(child.lua_get('MiniStatusline.section_git({})'), '')
end

T['section_git()']['respects `args.trunc_width`'] = function()
set_width(100)
eq(child.lua_get('MiniStatusline.section_git({ trunc_width = 100 })'), ' main +1 ~2 -3')
eq(child.lua_get('MiniStatusline.section_git({ trunc_width = 100 })'), ' main|bisect (MM)')
set_width(99)
eq(child.lua_get('MiniStatusline.section_git({ trunc_width = 100 })'), ' main')
eq(child.lua_get('MiniStatusline.section_git({ trunc_width = 100 })'), '')
end

T['section_git()']['respects `args.icon`'] = function()
eq(child.lua_get([[MiniStatusline.section_git({icon = 'A'})]]), 'A main +1 ~2 -3')
eq(child.lua_get([[MiniStatusline.section_git({icon = 'AAA'})]]), 'AAA main +1 ~2 -3')
eq(child.lua_get([[MiniStatusline.section_git({ icon = 'A' })]]), 'A main|bisect (MM)')
eq(child.lua_get([[MiniStatusline.section_git({ icon = 'AAA' })]]), 'AAA main|bisect (MM)')
end

T['section_git()']['respects `config.use_icons`'] = function()
child.lua('MiniStatusline.config.use_icons = false')
eq(child.lua_get([[MiniStatusline.section_git({})]]), 'Git main +1 ~2 -3')
eq(child.lua_get([[MiniStatusline.section_git({})]]), 'Git main|bisect (MM)')

-- Should also use buffer local config
child.b.ministatusline_config = { use_icons = true }
eq(child.lua_get([[MiniStatusline.section_git({})]]), ' main +1 ~2 -3')
end

T['section_git()']['is shown only in normal buffers'] = function()
child.cmd('help')
eq(child.lua_get('MiniStatusline.section_git({})'), '')
eq(child.lua_get([[MiniStatusline.section_git({})]]), ' main|bisect (MM)')
end

T['section_location()'] = new_set()
Expand Down

0 comments on commit 2d187c1

Please sign in to comment.