comparison autoload/lawrencium/annotate.vim @ 139:065625e1bb31

Split plugin file into multiple extensions.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 13 Jun 2016 09:32:34 -0700
parents
children 652a6f5df0f3
comparison
equal deleted inserted replaced
138:a2d823c82e5f 139:065625e1bb31
1
2 function! lawrencium#annotate#init() abort
3 call lawrencium#add_command("-bang -nargs=? -complete=customlist,lawrencium#list_repo_files Hgannotate :call lawrencium#annotate#HgAnnotate(<bang>0, 0, <f-args>)")
4 call lawrencium#add_command("-bang -nargs=? -complete=customlist,lawrencium#list_repo_files Hgwannotate :call lawrencium#annotate#HgAnnotate(<bang>0, 1, <f-args>)")
5
6 call lawrencium#add_reader('annotate', 'lawrencium#annotate#read')
7 endfunction
8
9 function! lawrencium#annotate#read(repo, path_parts, full_path) abort
10 let l:cmd_args = ['-c', '-n', '-u', '-d', '-q']
11 if a:path_parts['value'] == 'v=1'
12 call insert(l:cmd_args, '-v', 0)
13 endif
14 call add(l:cmd_args, a:full_path)
15 call a:repo.ReadCommandOutput('annotate', l:cmd_args)
16 endfunction
17
18 function! lawrencium#annotate#HgAnnotate(bang, verbose, ...) abort
19 " Open the file to annotate if needed.
20 if a:0 > 0
21 call lawrencium#vimutils#HgEdit(a:bang, a:1)
22 endif
23
24 " Get the Lawrencium path for the annotated file.
25 let l:path = expand('%:p')
26 let l:bufnr = bufnr('%')
27 let l:repo = lawrencium#hg_repo()
28 let l:value = a:verbose ? 'v=1' : ''
29 let l:annotation_path = l:repo.GetLawrenciumPath(l:path, 'annotate', l:value)
30
31 " Check if we're trying to annotate something with local changes.
32 let l:has_local_edits = 0
33 let l:path_status = l:repo.RunCommand('status', l:path)
34 if l:path_status != ''
35 call lawrencium#trace("Found local edits for '" . l:path . "'. Will annotate parent revision.")
36 let l:has_local_edits = 1
37 endif
38
39 if l:has_local_edits
40 " Just open the output of the command.
41 echom "Local edits found, will show the annotations for the parent revision."
42 execute 'edit ' . fnameescape(l:annotation_path)
43 setlocal nowrap nofoldenable
44 setlocal filetype=hgannotate
45 else
46 " Store some info about the current buffer.
47 let l:cur_topline = line('w0') + &scrolloff
48 let l:cur_line = line('.')
49 let l:cur_wrap = &wrap
50 let l:cur_foldenable = &foldenable
51
52 " Open the annotated file in a split buffer on the left, after
53 " having disabled wrapping and folds on the current file.
54 " Make both windows scroll-bound.
55 setlocal scrollbind nowrap nofoldenable
56 execute 'keepalt leftabove vsplit ' . fnameescape(l:annotation_path)
57 setlocal nonumber
58 setlocal scrollbind nowrap nofoldenable foldcolumn=0
59 setlocal filetype=hgannotate
60
61 " When the annotated buffer is deleted, restore the settings we
62 " changed on the current buffer, and go back to that buffer.
63 let l:annotate_buffer = lawrencium#buffer_obj()
64 call l:annotate_buffer.OnDelete('execute bufwinnr(' . l:bufnr . ') . "wincmd w"')
65 call l:annotate_buffer.OnDelete('setlocal noscrollbind')
66 if l:cur_wrap
67 call l:annotate_buffer.OnDelete('setlocal wrap')
68 endif
69 if l:cur_foldenable
70 call l:annotate_buffer.OnDelete('setlocal foldenable')
71 endif
72
73 " Go to the line we were at in the source buffer when we
74 " opened the annotation window.
75 execute l:cur_topline
76 normal! zt
77 execute l:cur_line
78 syncbind
79
80 " Set the correct window width for the annotations.
81 if a:verbose
82 let l:last_token = match(getline('.'), '\v\d{4}:\s')
83 let l:token_end = 5
84 else
85 let l:last_token = match(getline('.'), '\v\d{2}:\s')
86 let l:token_end = 3
87 endif
88 if l:last_token < 0
89 echoerr "Can't find the end of the annotation columns."
90 else
91 let l:column_count = l:last_token + l:token_end + g:lawrencium_annotate_width_offset
92 execute "vertical resize " . l:column_count
93 setlocal winfixwidth
94 endif
95 endif
96
97 " Make the annotate buffer a Lawrencium buffer.
98 let b:mercurial_dir = l:repo.root_dir
99 let b:lawrencium_annotated_path = l:path
100 let b:lawrencium_annotated_bufnr = l:bufnr
101 call s:DefineMainCommands()
102
103 " Add some other nice commands and mappings.
104 command! -buffer Hgannotatediffsum :call s:HgAnnotate_DiffSummary()
105 command! -buffer Hgannotatelog :call s:HgAnnotate_DiffSummary(1)
106 if g:lawrencium_define_mappings
107 nnoremap <buffer> <silent> <cr> :Hgannotatediffsum<cr>
108 nnoremap <buffer> <silent> <leader><cr> :Hgannotatelog<cr>
109 endif
110
111 " Clean up when the annotate buffer is deleted.
112 let l:bufobj = lawrencium#buffer_obj()
113 call l:bufobj.OnDelete('call s:HgAnnotate_Delete(' . l:bufobj.nr . ')')
114 endfunction
115
116 function! s:HgAnnotate_Delete(bufnr) abort
117 if g:lawrencium_auto_close_buffers
118 call s:delete_dependency_buffers('lawrencium_diff_for', a:bufnr)
119 endif
120 endfunction
121
122 function! s:HgAnnotate_DiffSummary(...) abort
123 " Get the path for the diff of the revision specified under the cursor.
124 let l:line = getline('.')
125 let l:rev_hash = matchstr(l:line, '\v[a-f0-9]{12}')
126 let l:log = (a:0 > 0 ? a:1 : 0)
127
128 " Get the Lawrencium path for the diff, and the buffer object for the
129 " annotation.
130 let l:repo = lawrencium#hg_repo()
131 if l:log
132 let l:path = l:repo.GetLawrenciumPath(b:lawrencium_annotated_path, 'logpatch', l:rev_hash)
133 else
134 let l:path = l:repo.GetLawrenciumPath(b:lawrencium_annotated_path, 'diff', l:rev_hash)
135 endif
136 let l:annotate_buffer = lawrencium#buffer_obj()
137
138 " Find a window already displaying diffs for this annotation.
139 let l:diff_winnr = s:find_buffer_window('lawrencium_diff_for', l:annotate_buffer.nr)
140 if l:diff_winnr == -1
141 " Not found... go back to the main source buffer and open a bottom
142 " split with the diff for the specified revision.
143 execute bufwinnr(b:lawrencium_annotated_bufnr) . 'wincmd w'
144 execute 'rightbelow split ' . fnameescape(l:path)
145 let b:lawrencium_diff_for = l:annotate_buffer.nr
146 let b:lawrencium_quit_on_delete = 1
147 else
148 " Found! Use that window to open the diff.
149 execute l:diff_winnr . 'wincmd w'
150 execute 'edit ' . fnameescape(l:path)
151 let b:lawrencium_diff_for = l:annotate_buffer.nr
152 endif
153 endfunction
154