aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2018-06-29 14:05:14 +1200
committerTom Ryder <tom@sanctum.geek.nz>2018-06-29 14:05:14 +1200
commit3dea10bc5d4cb8b99f67deb7c16576be5a7659e4 (patch)
treebd72ac74e6c3063899024f614e5e80876fbfe2f3
parentBump VERSION (diff)
parentBump VERSION (diff)
downloadvim-diff-prune-3dea10bc5d4cb8b99f67deb7c16576be5a7659e4.tar.gz
vim-diff-prune-3dea10bc5d4cb8b99f67deb7c16576be5a7659e4.zip
Merge branch 'release/v0.3.0'v0.3.0
* release/v0.3.0: Bump VERSION Add block removal feature
-rw-r--r--README.md3
-rw-r--r--VERSION2
-rw-r--r--autoload/diff/prune.vim75
-rw-r--r--doc/diff_prune.txt5
4 files changed, 83 insertions, 2 deletions
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/VERSION b/VERSION
index ee1372d..0d91a54 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-0.2.2
+0.3.0
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.