Skip to content

Commit

Permalink
feat: Do not delete/insert comment if nothing has changed
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoomen committed May 15, 2019
1 parent 24e4e04 commit cf4f953
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
27 changes: 27 additions & 0 deletions autoload/doge/comment.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
" ==============================================================================
" Filename: comment.vim
" Maintainer: Kim Koomen <koomen@protonail.com>
" License: MIT
" ==============================================================================

let s:save_cpo = &cpoptions
set cpoptions&vim

function! doge#comment#has_changed(old_comment, new_comment) abort
if len(a:old_comment) != len(a:new_comment)
return 1
endif

for l:old_line in a:old_comment
let l:old_line_idx = index(a:old_comment, l:old_line)
let l:new_line = get(a:new_comment, l:old_line_idx)
if trim(l:old_line) !=# trim(l:new_line)
return 1
endif
endfor

return 0
endfunction

let &cpoptions = s:save_cpo
unlet s:save_cpo
24 changes: 18 additions & 6 deletions autoload/doge/generate.vim
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,15 @@ function! doge#generate#pattern(pattern) abort

if l:has_old_comment
let l:cursor_pos = getpos('.')
execute(l:old_comment_start_lnum . 'd' . l:old_comment_lines_amount)

" Preserve the old comment before deleting.
let l:old_comment = getline(l:old_comment_start_lnum, l:old_comment_end_lnum)
let l:comment_has_changed = doge#comment#has_changed(l:old_comment, l:comment)

" Delete the old comment.
if l:comment_has_changed
execute(l:old_comment_start_lnum . 'd' . l:old_comment_lines_amount)
endif

" If we have deleted a comment that is 'below' the function expression then
" our cursor moved a line too much, so revert its position. This is the
Expand All @@ -112,11 +120,15 @@ function! doge#generate#pattern(pattern) abort
let l:comment_lnum_inherited_indent = line('.')
endif

" Write the comment.
call append(
\ l:comment_lnum_insert_position,
\ map(l:comment, {k, line -> doge#indent#line(l:comment_lnum_inherited_indent, line)})
\ )
" Write the comment if it changed.
if l:comment_has_changed
call append(
\ l:comment_lnum_insert_position,
\ map(l:comment, {k, line -> doge#indent#line(l:comment_lnum_inherited_indent, line)})
\ )
else
echo '[DoGE] Comment is up-to-date, skipping'
endif

if l:has_old_comment
call setpos('.', l:cursor_pos)
Expand Down

0 comments on commit cf4f953

Please sign in to comment.