From 5f1262e13fdc018aaaf28a7fd906da6fa631c9de Mon Sep 17 00:00:00 2001 From: tamago324 Date: Thu, 23 Jan 2020 02:13:52 +0900 Subject: [PATCH 1/4] Support Vim and Windows --- README.md | 6 ++-- autoload/blamer.vim | 83 +++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 80 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index f9ef702..d945a09 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,11 @@ # blamer.nvim -A git blame plugin for neovim inspired by VS Code's GitLens plugin. +A git blame plugin for (neo)vim inspired by VS Code's GitLens plugin. ![blamer gif](https://res.cloudinary.com/djg49e1u9/image/upload/c_crop,h_336/v1579092411/blamer_mkv07c.gif) +Note: For Vim, a popup feature is required. + ## Installation #### vim-plug @@ -24,7 +26,7 @@ call plug#end() #### Enabled -Enables blamer on neovim startup. +Enables blamer on (neo)vim startup. You can toggle blamer on/off with the `:BlamerToggle` command. diff --git a/autoload/blamer.vim b/autoload/blamer.vim index 7fcd18e..81f28b2 100644 --- a/autoload/blamer.vim +++ b/autoload/blamer.vim @@ -13,10 +13,18 @@ let s:blamer_date_format = get(g:, 'blamer_date_format', '%d/%m/%y %H:%M') let s:blamer_user_name = '' let s:blamer_user_email = '' let s:blamer_info_fields = filter(map(split(s:blamer_template, ' '), {key, val -> matchstr(val, '\m\C<\zs.\{-}\ze>')}), {idx, val -> val != ''}) -let s:blamer_namespace = nvim_create_namespace('blamer') +if exists('*nvim_create_namespace') + let s:blamer_namespace = nvim_create_namespace('blamer') +endif +if exists('*prop_type_add') + let s:prop_type_name = 'blamer_popup_marker' +endif let s:blamer_delay = get(g:, 'blamer_delay', 1000) let s:blamer_timer_id = -1 +let s:is_windows = has('win16') || has('win32') || has('win64') || has('win95') +let s:is_not_work = !has('nvim') && !exists('*popup_create') + function! s:Head(array) abort if len(a:array) == 0 return '' @@ -109,23 +117,68 @@ function! blamer#SetVirtualText(buffer_number, line_number, message) abort call nvim_buf_set_virtual_text(a:buffer_number, s:blamer_namespace, l:line_index, [[s:blamer_prefix . a:message, 'Blamer']], {}) endfunction +function! blamer#CreatePopup(buffer_number, line_number, message) abort + let l:col = strlen(getline(a:line_number)) + let l:col = l:col == 0 ? 1 : l:col + let l:propid = a:line_number . l:col + + if empty(prop_type_get(s:prop_type_name, {'bufnr': a:buffer_number})) + call prop_type_add(s:prop_type_name, {'bufnr': a:buffer_number}) + endif + + call prop_add(a:line_number, l:col, { + \ 'type': s:prop_type_name, + \ 'bufnr': a:buffer_number, + \ 'length': 0, + \ 'id': l:propid, + \}) + + let l:popup_winid = popup_create(s:blamer_prefix . a:message, { + \ 'textprop': 'blamer_popup_marker', + \ 'textpropid': l:propid, + \ 'line': -1, + \ 'col': l:col == 1 ? 1 : 2, + \ 'fixed': 1, + \ 'wrap': 0, + \ 'highlight': 'Blamer' + \}) +endfunctio + function! blamer#Show() abort - let l:file_path = expand('%:p') + if g:blamer_enabled == 0 || s:is_not_work + return + endif + + let l:file_path = s:substitute_path_separator(expand('%:p')) if s:IsFileInPath(l:file_path, s:git_root) == 0 return endif let l:buffer_number = bufnr('') - let l:line_numbers = s:GetLines() - for line_number in l:line_numbers + let l:line_numbers = s:GetLines() + for line_number in l:line_numbers let l:message = blamer#GetMessage(l:file_path, line_number, '+1') - call blamer#SetVirtualText(l:buffer_number, line_number, l:message) + if has('nvim') + call blamer#SetVirtualText(l:buffer_number, line_number, l:message) + else + call blamer#CreatePopup(l:buffer_number, line_number, l:message) + endif endfor endfunction function! blamer#Hide() abort let l:current_buffer_number = bufnr('') - call nvim_buf_clear_namespace(l:current_buffer_number, s:blamer_namespace, 0, -1) + if has('nvim') + call nvim_buf_clear_namespace(l:current_buffer_number, s:blamer_namespace, 0, -1) + else + if !empty(prop_type_get(s:prop_type_name, {'bufnr': l:current_buffer_number})) + call prop_remove({ + \ 'type': s:prop_type_name, + \ 'bufnr': l:current_buffer_number, + \ 'all': 1, + \}) + endif + endif endfunction function! blamer#Refresh() abort @@ -163,7 +216,18 @@ function! blamer#Init() abort return endif - let l:result = split(system('git rev-parse --show-toplevel 2>/dev/null'), '\n') + if s:is_not_work + echohl ErrorMsg + echomsg '[blamer.nvim] Needs popup feature.' + echohl None + return + endif + + if s:is_windows + let l:result = split(system('git rev-parse --show-toplevel 2>NUL'), '\n') + else + let l:result = split(system('git rev-parse --show-toplevel 2>/dev/null'), '\n') + endif let s:git_root = s:Head(l:result) if s:git_root == '' @@ -180,5 +244,10 @@ function! blamer#Init() abort augroup END endfunction +" from neomru +function! s:substitute_path_separator(path) abort + return s:is_windows ? substitute(a:path, '\\', '/', 'g') : a:path +endfunction + let &cpo = s:save_cpo unlet s:save_cpo From 0a7eb372173d8986ce231bf475a68b83b7753e11 Mon Sep 17 00:00:00 2001 From: tamago324 Date: Mon, 3 Feb 2020 12:46:03 +0900 Subject: [PATCH 2/4] Fix variable name --- autoload/blamer.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload/blamer.vim b/autoload/blamer.vim index 81f28b2..9e0f12a 100644 --- a/autoload/blamer.vim +++ b/autoload/blamer.vim @@ -23,7 +23,7 @@ let s:blamer_delay = get(g:, 'blamer_delay', 1000) let s:blamer_timer_id = -1 let s:is_windows = has('win16') || has('win32') || has('win64') || has('win95') -let s:is_not_work = !has('nvim') && !exists('*popup_create') +let s:missing_popup_feature = !has('nvim') && !exists('*popup_create') function! s:Head(array) abort if len(a:array) == 0 From 93bc83a52330ccd51e5443830d6ba1951a16e5dc Mon Sep 17 00:00:00 2001 From: tamago324 Date: Mon, 3 Feb 2020 17:13:03 +0900 Subject: [PATCH 3/4] Fix error --- autoload/blamer.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/autoload/blamer.vim b/autoload/blamer.vim index 9e0f12a..0dffbdd 100644 --- a/autoload/blamer.vim +++ b/autoload/blamer.vim @@ -145,7 +145,7 @@ function! blamer#CreatePopup(buffer_number, line_number, message) abort endfunctio function! blamer#Show() abort - if g:blamer_enabled == 0 || s:is_not_work + if g:blamer_enabled == 0 || s:missing_popup_feature return endif @@ -216,7 +216,7 @@ function! blamer#Init() abort return endif - if s:is_not_work + if s:missing_popup_feature echohl ErrorMsg echomsg '[blamer.nvim] Needs popup feature.' echohl None From 5c72be9fbeb4437076ef10d88b3ffddbdb2640a8 Mon Sep 17 00:00:00 2001 From: tamago324 Date: Mon, 3 Feb 2020 17:16:20 +0900 Subject: [PATCH 4/4] Fix typo --- autoload/blamer.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload/blamer.vim b/autoload/blamer.vim index 0dffbdd..5816b8d 100644 --- a/autoload/blamer.vim +++ b/autoload/blamer.vim @@ -142,7 +142,7 @@ function! blamer#CreatePopup(buffer_number, line_number, message) abort \ 'wrap': 0, \ 'highlight': 'Blamer' \}) -endfunctio +endfunction function! blamer#Show() abort if g:blamer_enabled == 0 || s:missing_popup_feature