From cdabe4762cc2f41d64a092b879fe09370c924cf8 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Fri, 29 Jun 2018 14:04:14 +1200 Subject: Add block removal feature --- README.md | 3 ++ autoload/diff/prune.vim | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ doc/diff_prune.txt | 5 +++- 3 files changed, 82 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8761d5c..1ef1769 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,9 @@ mappings in normal and visual mode to "undo" lines of changes defined by a linewise motion or visual mode selection: leading minus signs are removed, and lines with leading plus signs are deleted. +If the changes result in a diff block or file block having no changes left, it +is also removed. + This can be handy for using with the `-e` or `--edit` option to `git-add`, which allows you to edit a diff before applying changes to the staging area. diff --git a/autoload/diff/prune.vim b/autoload/diff/prune.vim index 2379683..cfbce97 100644 --- a/autoload/diff/prune.vim +++ b/autoload/diff/prune.vim @@ -14,4 +14,79 @@ function! diff#prune#Prune(type) abort silent execute l:range.'global/^+/d' let @/ = l:search_save + " Now we need to look for any blocks or files to remove if they have no + " changes in them anymore + let l:deletions = {} + for l:li in range(1, line('$') + 1) + + " Flag for the end of the buffer (one past the last line) + let l:eof = l:li > line('$') + + " If this index corresponds to a real line, cache its value + if !l:eof + let l:line = getline(l:li) + let l:deletions[l:li] = 0 + endif + + " Flags for whether this iteration constitutes the start of a new file, a + " new block, or a changed line + let l:file = stridx(l:line, 'diff') == 0 && !l:eof + let l:block = stridx(l:line, '@@') == 0 && !l:eof + let l:change = (stridx(l:line, '+') == 0 || stridx(l:line, '-') == 0) + \ && !l:eof + \ && exists('l:file_start') + \ && exists('l:block_start') + + " End of old file: flag previous file lines for deletion if no changes, + " clear file start and changes variables + if l:file || l:eof + if exists('l:file_start') && l:file_changes == 0 + for l:di in range(l:file_start, l:li - 1) + let l:deletions[l:di] = 1 + endfor + endif + unlet! l:file_start l:file_changes + endif + + " Start of new file: set start line, start new changes counter + if l:file + let l:file_start = l:li + let l:file_changes = 0 + endif + + " End of old block: flag previous block lines for deletion if no changes, + " clear block start and changes variables + if l:block || l:file || l:eof + if exists('l:block_start') && l:block_changes == 0 + for l:di in range(l:block_start, l:li - 1) + let l:deletions[l:di] = 1 + endfor + endif + unlet! l:block_start l:block_changes + endif + + " Start of new block: set start line, start new changes counter + if l:block + let l:block_start = l:li + let l:block_changes = 0 + endif + + " If this is a changed line, bump the counters for this file and block + if l:change + let l:file_changes += 1 + let l:block_changes += 1 + endif + + endfor + + " Delete any flagged lines, going in reverse order so we don't reset any + " indices as we go + let l:di = line('$') + while l:di > 0 + if l:deletions[l:di] + silent execute l:di.'delete' + endif + let l:di -= 1 + endwhile + endfunction diff --git a/doc/diff_prune.txt b/doc/diff_prune.txt index ce6c830..ed1932b 100644 --- a/doc/diff_prune.txt +++ b/doc/diff_prune.txt @@ -1,4 +1,4 @@ -*diff_prune.txt* For Vim version 7.0 Last change: 2018 June 24 +*diff_prune.txt* For Vim version 7.0 Last change: 2018 June 29 DESCRIPTION *diff_prune* @@ -7,6 +7,9 @@ mappings in normal and visual mode to reverse changes defined by a linewise motion or visual mode selection: leading minus signs are removed, and lines with leading plus signs are deleted. +If the changes result in a diff block or file block having no changes left, it +is also removed. + This can be handy for using with the `-e` or `--edit` option to `git-add`, which allows you to edit a diff before applying changes to the staging area. -- cgit v1.2.3 From 732255096da0a4f0ff705857c10af39caa557ebb Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Fri, 29 Jun 2018 14:04:54 +1200 Subject: Bump VERSION --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index ee1372d..0d91a54 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2.2 +0.3.0 -- cgit v1.2.3