Skip to content

Commit

Permalink
[nvim] replace vim-signify with gitsigns.nvim
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitmel committed Dec 10, 2021
1 parent a039f97 commit 437c076
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 9 deletions.
30 changes: 30 additions & 0 deletions nvim/autoload/airline/extensions/dotfiles_gitsigns_nvim.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
" Based on <https://github.com/vim-airline/vim-airline/blob/bf5d785932b5bdedcb747311a8536931dd5241cc/autoload/airline/extensions/hunks.vim>

function! airline#extensions#dotfiles_gitsigns_nvim#init(ext) abort
let s:non_zero_only = get(g:, 'airline#extensions#hunks#non_zero_only', 0)
let s:hunk_symbols = get(g:, 'airline#extensions#hunks#hunk_symbols', ['+', '~', '-'])

call airline#parts#define_function('hunks', 'airline#extensions#dotfiles_gitsigns_nvim#get_hunks')
endfunction

function! airline#extensions#dotfiles_gitsigns_nvim#get_hunks() abort
if !get(w:, 'airline_active', 0) | return '' | endif
let min_winwidth = get(airline#parts#get('hunks'), 'minwidth', 100)
if airline#util#winwidth() < min_winwidth | return '' | endif

let status = get(b:, 'gitsigns_status_dict', {})
let hunks = [get(status, 'added', 0), get(status, 'changed', 0), get(status, 'removed', 0)]

let str = ''
for i in range(3)
if s:non_zero_only && hunks[i] == 0 | continue | endif
let str .= s:hunk_symbols[i] . hunks[i] . ' '
endfor

let has_branch_ext = index(airline#extensions#get_loaded_extensions(), 'branch') >= 0
if !has_branch_ext && str[-1:] == ' '
" branch extension not loaded, skip trailing whitespace
let str = str[0:-2]
endif
return str
endfunction
5 changes: 5 additions & 0 deletions nvim/colors/dotfiles.vim
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,11 @@
hi! link SignifySignAdd DiffAdd
hi! link SignifySignChange DiffText
hi! link SignifySignDelete DiffDelete
hi! link GitSignsAdd DiffAdd
hi! link GitSignsDelete DiffDelete
hi! link GitSignsTopDelete GitSignsTopDelete
hi! link GitSignsChange DiffText
hi! link GitSignsChangeDelete GitSignsChange
" }}}

" Vim scripts {{{
Expand Down
25 changes: 18 additions & 7 deletions nvim/dotfiles/plugins-list.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ let s:plug = function('dotfiles#plugman#register')
" All plugin definitions are written as links to github.com (instead of the
" typical `user/repo`) so that I can move the cursor over any and press `gx`.

" General-purpose libraries {{{
" Required by:
" 1. gitsigns.nvim
call s:plug('https://github.com/nvim-lua/plenary.nvim')
" Required by:
" 1. LineJuggler
call s:plug('https://github.com/inkarkat/vim-ingo-library')
" }}}

" Files {{{
" Useful filesystem command wrappers: `:Rename`, `:Delete`, `:Chmod` etc.
call s:plug('https://github.com/tpope/vim-eunuch')
Expand Down Expand Up @@ -65,8 +74,6 @@ let s:plug = function('dotfiles#plugman#register')
" languages which use English keywords for delimiting blocks (Vimscript, Lua,
" Ruby etc).
call s:plug('https://github.com/andymass/vim-matchup')
" A REALLY huge library. Used by LineJuggler.
call s:plug('https://github.com/inkarkat/vim-ingo-library')
" Adds commands for inserting and moving around nearby lines: `[e` and `]e`
" for swapping two lines, `]<Space>` and `[<Space>` for inserting blank ones,
" `[d` and `]d` for duplicating the current line.
Expand Down Expand Up @@ -130,13 +137,17 @@ let s:plug = function('dotfiles#plugman#register')
call s:plug('https://github.com/tpope/vim-rhubarb')
call s:plug('https://github.com/shumphrey/fugitive-gitlab.vim')
" Show change markers (relative to what is in the Git repository) in the
" sign column. vim-gitgutter[1] is a bit slow and creates lots of temporary
" sign column. vim-gitgutter is a bit slow and creates lots of temporary
" files with `tempname()` (and then doesn't delete them), but vim-signify
" flashes between results of `git diff` and `git diff --staged` when the
" file is staged. I'll consider using gitsigns.nvim[2].
" [1]: <https://github.com/airblade/vim-gitgutter>
" [2]: <https://github.com/lewis6991/gitsigns.nvim>
call s:plug('https://github.com/mhinz/vim-signify', (has('nvim') || has('patch-8.0.902')) ? {} : { 'branch': 'legacy' })
" file is staged.
if has('nvim-0.5.0')
call s:plug('https://github.com/lewis6991/gitsigns.nvim')
elseif has('nvim') || has('patch-8.0.902')
call s:plug('https://github.com/mhinz/vim-signify')
else
call s:plug('https://github.com/airblade/vim-gitgutter')
endif
endif
" }}}

Expand Down
40 changes: 38 additions & 2 deletions nvim/plugin/git.vim
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let g:signify_priority = g:gitgutter_sign_priority
let g:gitgutter_sign_added = '+'
let g:gitgutter_sign_modified = '~'
let g:gitgutter_sign_removed = '_'
let g:gitgutter_sign_removed_first_line = ''
let g:gitgutter_sign_removed_first_line = "\u203e"
let g:gitgutter_sign_removed_above_and_below = g:gitgutter_sign_removed . g:gitgutter_sign_removed_first_line
let g:gitgutter_sign_modified_removed = g:gitgutter_sign_modified . g:gitgutter_sign_removed
" Mirror the look of gitgutter here. TODO: Port this to vim-signify:
Expand All @@ -21,8 +21,32 @@ let g:signify_sign_delete_first_line = g:gitgutter_sign_removed_first_line
let g:signify_sign_change = g:gitgutter_sign_modified
let g:signify_sign_change_delete = g:gitgutter_sign_modified_removed

lua <<EOF
local ok, gitsigns = pcall(require, 'gitsigns')
vim.g.gitsigns_nvim_available = ok
if not ok then return end
gitsigns.setup({
signs = {
add = { text = vim.g.gitgutter_sign_added };
delete = { text = vim.g.gitgutter_sign_removed };
topdelete = { text = vim.g.gitgutter_sign_removed_first_line };
change = { text = vim.g.gitgutter_sign_modified };
changedelete = { text = vim.g.gitgutter_sign_modified_removed };
};
sign_priority = vim.g.gitgutter_sign_priority;
preview_config = {
border = 'none';
col = 0;
row = 1;
};
-- Disable the default mappings, we will define our own, and via the
-- intended way.
keymaps = {};
})
EOF

" mappings {{{
let g:gitgutter_map_keys = 0

nnoremap <leader>gg :<C-u>G
nnoremap <leader>g :<C-u>Git<space>
nnoremap <leader>gs :<C-u>vertical Git<CR>
Expand All @@ -37,10 +61,22 @@ let g:signify_sign_change_delete = g:gitgutter_sign_modified_removed
nnoremap <leader>gl :<C-u>Gclog<CR>
nnoremap <leader>gp :<C-u>Git push
nnoremap <leader>gP :<C-u>Git push --force-with-lease
let g:gitgutter_map_keys = 0

" Jump to the next/previous change in the diff mode because I replace the
" built-in mappings with coc.nvim's for jumping through diagnostics.
" TODO: untangle these keybindings. Use `g` for diagnostics and `c` for diff hunks.
nnoremap [g [c
nnoremap ]g ]c
if g:gitsigns_nvim_available
nnoremap <silent><expr> [g &diff ? '[c' : "\<Cmd>Gitsigns prev_hunk\<CR>"
nnoremap <silent><expr> ]g &diff ? ']c' : "\<Cmd>Gitsigns next_hunk\<CR>"
onoremap <silent> ih :<C-u>Gitsigns select_hunk<CR>
xnoremap <silent> ih :<C-u>Gitsigns select_hunk<CR>
endif

" }}}

" Fugitive.vim handlers {{{
Expand Down
3 changes: 3 additions & 0 deletions nvim/plugin/interface.vim
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ let &history = max([&history, 10000])
if dotfiles#plugman#is_registered('vim-gitgutter') || dotfiles#plugman#is_registered('vim-signify')
let g:airline_extensions += ['hunks']
endif
if dotfiles#plugman#is_registered('gitsigns.nvim')
let g:airline_extensions += ['dotfiles_gitsigns_nvim']
endif
if dotfiles#plugman#is_registered('coc.nvim')
let g:airline_extensions += ['coc', 'dotfiles_coclist']
endif
Expand Down

0 comments on commit 437c076

Please sign in to comment.