Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-70 - Add git sidebar coloring support #72

Merged
merged 3 commits into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ let g:minimap_auto_start_win_enter = 1
| `g:minimap_close_buftypes` | `[]` | close minimap for specific buffer types |
| `g:minimap_left` | `0` | if set minimap window will append left |
| `g:minimap_highlight_range` | `0` | if set minimap will highlight range of visible lines |
| `g:minimap_git_colors` | `0` | if set minimap will highlight range of changes as reported by git |
| `g:minimap_diffadd_color` | `diffAdded` | the color group for added lines (if git_colors is enabled) |
| `g:minimap_diffremove_color` | `diffRemoved` | the color group for removed lines (if git_colors is enabled) |
| `g:minimap_diff_color` | `diffLine` | the color group for modified lines (if git_colors is enabled) |

### 💬 F.A.Q

Expand Down
80 changes: 79 additions & 1 deletion autoload/minimap/vim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ function! s:open_window() abort

let &cpoptions = cpoptions_save

let g:id_list = []
execute 'wincmd p'
call s:refresh_minimap(1)
call s:update_highlight()
Expand Down Expand Up @@ -374,6 +375,70 @@ function! s:update_highlight() abort
let pos = s:buffer_to_map(curr, total, mmheight)
call s:highlight_line(winid, pos)
endif

if g:minimap_git_colors
" Get git info
let git_call = 'git diff -U0 -- ' . expand('%')
let git_diff = substitute(system(git_call), '\n\+&', '', '') | silent echo strtrans(git_diff)

let lines = split(git_diff, '\n')
let diff_list = []
for line in lines
if line[0] ==? '@'
let this_diff = {}

let blobs = split(line, ' ')
let del_info = blobs[1]
let add_info = blobs[2]

" Parse newfile info
let add_info = split(add_info, ',')
let add_start = str2nr(add_info[0])
let add_len = 0
if len(add_info) > 1
let add_len = abs(str2nr(add_info[1]))
endif
" Parse oldfile info
let del_info = split(del_info, ',')
let del_len = 0
if len(del_info) > 1
let del_len = abs(str2nr(del_info[1]))
endif

" Get diff type + end line
let this_diff['start'] = add_start
let this_diff['end'] = this_diff['start'] + add_len
if add_len != 0 && del_len != 0
let this_diff['color'] = g:minimap_diff_color
elseif add_len != 0
let this_diff['color'] = g:minimap_diffadd_color
elseif del_len != 0
let this_diff['color'] = g:minimap_diffremove_color
let this_diff['end'] = this_diff['start'] + 1
else
let this_diff['color'] = g:minimap_diff_color
let this_diff['end'] = this_diff['start'] + 1
endif

ZNielsen marked this conversation as resolved.
Show resolved Hide resolved
" Map locations to minimap
let this_diff['start'] = s:buffer_to_map(this_diff['start'], total, mmheight) - 1
let this_diff['end'] = s:buffer_to_map(this_diff['end'], total, mmheight) + 1
" Add to list
let diff_list = add(diff_list, this_diff)
endif
endfor

" Delete the old lines before drawing the new ones
for id in g:id_list
silent! call matchdelete(id, winid) " require vim 8.1.1084+ or neovim 0.5.0+
endfor
" Color lines, creating a new id for each section
let g:id_list = []
for a_diff in diff_list
call add(g:id_list, g:minimap_cursorline_matchid + (1 + len(g:id_list)))
call s:set_range_color(winid, a_diff['start'], max([a_diff['start']+1, a_diff['end']]), a_diff['color'], g:id_list[-1])
endfor
endif
endfunction

" Translates a line in a buffer to its respective pos in the map.
Expand All @@ -386,9 +451,22 @@ function! s:highlight_line(winid, pos) abort
call matchadd(g:minimap_highlight, '\%' . a:pos . 'l', 100, g:minimap_cursorline_matchid, { 'window': a:winid })
endfunction

function! s:set_range_color(winid, startpos, endpos, set_color, match_id) abort
call matchadd(a:set_color, '\%>' . a:startpos . 'l\%<' . a:endpos . 'l', 100, a:match_id, { 'window': a:winid })
endfunction
function! s:highlight_range(winid, startpos, endpos) abort
" Delete the old one before drawing
silent! call matchdelete(g:minimap_cursorline_matchid, a:winid) " require vim 8.1.1084+ or neovim 0.5.0+
call matchadd(g:minimap_highlight, '\%>' . a:startpos . 'l\%<' . a:endpos . 'l', 100, g:minimap_cursorline_matchid, { 'window': a:winid })
call s:set_range_color(a:winid, a:startpos, a:endpos, g:minimap_highlight, g:minimap_cursorline_matchid)
endfunction
function! s:set_diffadd_range(winid, startpos, endpos) abort
call s:set_range_color(a:winid, a:startpos, a:endpos, g:minimap_diffadd_color, g:minimap_add_matchid)
endfunction
function! s:set_diffremove_range(winid, startpos, endpos) abort
call s:set_range_color(a:winid, a:startpos, a:endpos, g:minimap_diffremove_color, g:minimap_rem_matchid)
endfunction
function! s:set_diff_range(winid, startpos, endpos) abort
call s:set_range_color(a:winid, a:startpos, a:endpos, g:minimap_diff_color, g:minimap_diff_matchid)
Copy link
Collaborator Author

@ZNielsen ZNielsen May 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These functions (set_diff(add|remove|)_range) aren't actually used. I used them for a bit, but then went with the dictionary solution above. I left them in because they seemed like nice convenience functions, but they can be removed with no repercussions if desired.

endfunction

" Clears matches of current window only.
Expand Down
16 changes: 16 additions & 0 deletions plugin/minimap.vim
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ if !exists('g:minimap_highlight_range')
let g:minimap_highlight_range = 0
endif

if !exists('g:minimap_git_colors')
let g:minimap_git_colors = 0
endif

if !exists('g:minimap_diffadd_color')
let g:minimap_diffadd_color = 'DiffAdd'
endif

if !exists('g:minimap_diffremove_color')
let g:minimap_diffremove_color = 'DiffDelete'
endif

if !exists('g:minimap_diff_color')
let g:minimap_diff_color = 'DiffChange'
endif

ZNielsen marked this conversation as resolved.
Show resolved Hide resolved
if g:minimap_auto_start == 1
augroup MinimapAutoStart
au!
Expand Down