summaryrefslogtreecommitdiff
path: root/rc/filetype/diff.kak
diff options
context:
space:
mode:
authorJohannes Altmanninger <aclopte@gmail.com>2022-01-24 18:10:39 +0100
committerJohannes Altmanninger <aclopte@gmail.com>2022-01-25 14:15:01 +0100
commitbf239ba77afee503f5b9bd9cf392ca2ad0fd4f8f (patch)
treea97e3970f232da3dfa85d6bc077779e27bfe4e49 /rc/filetype/diff.kak
parentb84abd57de39facb8159b6a0f6f6390268ff54d0 (diff)
rc diff: introduce diff-jump, replacing git-diff-goto-source
git-diff-goto-source is specific to diffs produced by Git. This patch generalizes the logic and moves it to a new diff-jump in diff.kak. The main differences are: - diff-jump handles plain file diffs (i.e. without the -r option). These have no "diff" line. This means that it needs to parse +++/--- instead. - diff-jump can go to the old file, not just the new one. - diff-jump allows to override the base directory and the number of directory components to strip. git-diff-goto-source was implemented with several nested try/catch blocks. Implementing the extra features would have added more nesting, redundancy or hidden options. To avoid that, I ported the parsing logic to Perl (which git.kak already depends on). Maybe it's possible to do the same in awk. Potential concerns: - We could move diff-jump to a new rc/tools/diff.kak but then it's not obvious where the "diff" module belongs to. - Should diff "diff-jump -1" be spelled "diff-jump -p1"? In future, the diff parser could be reused to implement a vimdiff-style feature: given a diff and the "old" line number, we can compute the corresponding "new" line number. Perhaps diff-jump should get a -client argument.
Diffstat (limited to 'rc/filetype/diff.kak')
-rw-r--r--rc/filetype/diff.kak130
1 files changed, 129 insertions, 1 deletions
diff --git a/rc/filetype/diff.kak b/rc/filetype/diff.kak
index c0cb4523..586bb32c 100644
--- a/rc/filetype/diff.kak
+++ b/rc/filetype/diff.kak
@@ -1,5 +1,6 @@
hook global BufCreate .*\.(diff|patch) %{
set-option buffer filetype diff
+ map buffer normal <ret> %{: diff-jump<ret>}
}
hook global WinSetOption filetype=diff %{
@@ -18,6 +19,133 @@ hook -group diff-highlight global WinSetOption filetype=diff %{
hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/diff }
}
+define-command diff-jump \
+ -docstring %{diff-jump [<switches>] [<directory>]: edit the diff's source file at the cursor position.
+Paths are resolved relative to <directory>, or the current working directory if unspecified.
+
+Switches:
+ - jump to the old file instead of the new file
+ -<num> strip <num> leading directory components, like -p<num> in patch(1). Defaults to 1 if there is a 'diff' line (as printed by 'diff -r'), or 0 otherwise.} \
+ -params .. -file-completion %{
+ evaluate-commands -draft -save-regs c %{
+ # Save the column because we will move the cursor.
+ set-register c %val{cursor_column}
+ # If there is a "diff" line, we don't need to look further back.
+ try %{
+ execute-keys %{<a-l><semicolon><a-?>^diff\b<ret><a-x>}
+ } catch %{
+ # A single file diff won't have a diff line. Start parsing from
+ # the buffer start, so we can tell if +++/--- lines are headers
+ # or content.
+ execute-keys Gk
+ }
+ evaluate-commands %sh{
+ printf %s "$kak_selection" |
+ column=$kak_reg_c perl -we '
+ sub quote {
+ $SQ = "'\''";
+ $token = shift;
+ $token =~ s/$SQ/$SQ$SQ/g;
+ return "$SQ$token$SQ";
+ }
+ sub fail {
+ $reason = shift;
+ print "fail " . quote("diff-jump: $reason");
+ exit 1;
+ }
+ $version = "+", $other_version = "-";
+ $strip = undef;
+ $directory = $ENV{PWD};
+ $seen_ddash = 0;
+ foreach (@ARGV) {
+ if ($seen_ddash or !m{^-}) {
+ $directory = $_;
+ } elsif ($_ eq "-") {
+ $version = "-", $other_version = "+";
+ } elsif (m{^-(\d+)$}) {
+ $strip = $1;
+ } elsif ($_ eq "--") {
+ $seen_ddash = 1;
+ } else {
+ fail "unknown option: $_";
+ }
+ }
+ $have_diff_line = 0;
+ $state = "header";
+ while (<STDIN>) {
+ $last_line = $_;
+ if (m{^diff\b}) {
+ $state = "header";
+ $have_diff_line = 1;
+ if (m{^diff -\S* (\S+) (\S+)$}) {
+ $fallback_file = $version eq "+" ? $2 : $1;
+ }
+ next;
+ }
+ if ($state eq "header") {
+ if (m{^[$version]{3} ([^\t\n]+)}) {
+ $file = $1;
+ next;
+ }
+ if (m{^[$other_version]{3} ([^\t\n]+)}) {
+ $fallback_file = $1;
+ next;
+ }
+ }
+ if (m{^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@}) {
+ $state = "contents";
+ $line = ($version eq "+" ? $2 : $1) - 1;
+ } elsif (m{^[ $version]}) {
+ $line++ if defined $line;
+ }
+ }
+ if (not defined $file) {
+ $file = $fallback_file;
+ }
+ if (not defined $file) {
+ fail "missing diff header";
+ }
+ if (not defined $strip) {
+ # A "diff -r" or "git diff" adds "diff" lines to
+ # the output. If no such line is present, we have
+ # a plain diff between files (not directories), so
+ # there should be no need to strip the directory.
+ $strip = $have_diff_line ? 1 : 0;
+ }
+ $file =~ s,^([^/]+/+){$strip},, or fail "directory prefix underflow";
+ $filepath = "$directory/$file";
+
+ if (defined $line) {
+ $column = $ENV{column} - 1; # Account for [ +-] diff prefix.
+ # If the cursor was on a hunk header, go to the section header if possible.
+ if ($last_line =~ m{^(@@ -\d+(?:,\d+)? \+\d+(?:,\d+) @@ )([^\n]*)}) {
+ $hunk_header_prefix = $1;
+ $hunk_header_from_userdiff = $2;
+ open FILE, "<", $filepath or fail "failed to open file: $!: $filepath";
+ @lines = <FILE>;
+ for (my $i = $line - 1; $i >= 0 && $i < scalar @lines; $i--) {
+ if ($lines[$i] !~ m{\Q$hunk_header_from_userdiff}) {
+ next;
+ }
+ $line = $i + 1;
+ # Re-add 1 because the @@ line does not have a [ +-] diff prefix.
+ $column = $column + 1 - length $hunk_header_prefix;
+ last;
+ }
+ }
+ }
+
+ printf "set-register c %s $line $column", quote($filepath);
+ ' -- "$@"
+ }
+ evaluate-commands -client %val{client} %{
+ evaluate-commands -try-client %opt{jumpclient} %{
+ edit -existing -- %reg{c}
+ }
+ }
+ }
+}
+
ยง
define-command \
@@ -31,7 +159,7 @@ define-command \
execute-keys '"ez'
} catch %{
execute-keys '"oz'
- fail 'Not in a diff file'
+ fail 'Not in a diff file'
}
}
}