diff options
| author | Johannes Altmanninger <aclopte@gmail.com> | 2022-01-24 18:10:39 +0100 |
|---|---|---|
| committer | Johannes Altmanninger <aclopte@gmail.com> | 2022-01-25 14:15:01 +0100 |
| commit | bf239ba77afee503f5b9bd9cf392ca2ad0fd4f8f (patch) | |
| tree | a97e3970f232da3dfa85d6bc077779e27bfe4e49 | |
| parent | b84abd57de39facb8159b6a0f6f6390268ff54d0 (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.
| -rw-r--r-- | rc/filetype/diff.kak | 130 | ||||
| -rw-r--r-- | rc/tools/git.kak | 102 |
2 files changed, 131 insertions, 101 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' } } } diff --git a/rc/tools/git.kak b/rc/tools/git.kak index c79b620f..aa2da10f 100644 --- a/rc/tools/git.kak +++ b/rc/tools/git.kak @@ -339,107 +339,9 @@ define-command -params 1.. \ esac }} -# Options needed by git-diff-goto-source command -declare-option -hidden str git_diff_hunk_filename -declare-option -hidden int git_diff_hunk_line_num_start -declare-option -hidden int git_diff_go_to_line_num -declare-option -hidden str git_diff_git_dir -declare-option -hidden str git_diff_section_heading -declare-option -hidden int git_diff_cursor_column - # Works within :git diff and :git show define-command git-diff-goto-source \ -docstring 'Navigate to source by pressing the enter key in hunks when git diff is displayed. Works within :git diff and :git show' %{ - try %{ - set-option global git_diff_git_dir %sh{ - git rev-parse --show-toplevel - } - # We will need this later. Need to subtract 1 because a diff has an initial column - # for -,+,<space> - set-option global git_diff_cursor_column %sh{ echo $(($kak_cursor_column-1)) } - - # This function works_within a hunk or in the diff header. - # - On a context line or added line, it will navigate to that line. - # - On a deleted line, it will navigate to the context line or added line just above. - # - On a @@ line (i.e. a "hunk header") this will navigate to section heading (see below). - # - A diff header contains lines starting with "diff", "index", "+++", and "---". - # Inside a diff header, this will navigate to the first line of the file. - execute-keys -draft 'x<a-k>^[@ +-]|^diff|^index<ret>' - - # Find the source filename for the current hunk (reverse search) - evaluate-commands -draft %{ - # First look for the "diff" line. because "+++" may be part of a diff. - execute-keys 'x<semicolon><a-/>^diff<ret></>^\+\+\+ \w([^\n]*)<ret>' - set-option global git_diff_hunk_filename %reg{1} - } - - try %{ - # Are we inside the diff header? If so simply go to the first line of the file. - # The diff header is everything before the first hunk header. - execute-keys -draft 'x<semicolon><a-?>^diff<ret><a-K>^@@<ret>' - edit -existing "%opt{git_diff_git_dir}%opt{git_diff_hunk_filename}" 1 - } catch %{ - # Find the source line at which the current hunk starts (reverse search) - evaluate-commands -draft %{ - execute-keys 'x<semicolon><a-/>^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@<ret>' - set-option buffer git_diff_hunk_line_num_start %reg{1} - } - # If we're already on a hunk header (i.e. a line that starts with @@) then - # our behavior changes slightly: we need to go look for the section heading. - # For example take this hunk header: @@ -123,4 +123,4 @@ fn some_function_name_possibly - # Here the section heading is "fn some_function_name_possibly". Please note that the section - # heading is NOT necessarily at the hunk start line so we can't trivially extract that. - try %{ - # First things first, are we on a hunk header? If not, head to the nearest `catch` - execute-keys -draft 'x<a-k>^@@<ret>' - evaluate-commands -try-client %opt{jumpclient} %{ - # Now attempt to find the section heading! - try %{ - # First extract the section heading. - evaluate-commands -draft %{ - execute-keys 'xs^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@ ([^\n]*)<ret>' - set-option global git_diff_section_heading %reg{2} - } - # Go to the hunk start in the source file. The section header will be above us. - edit -existing "%opt{git_diff_git_dir}%opt{git_diff_hunk_filename}" %opt{git_diff_hunk_line_num_start} - # Search for the raw text of the section, like "fn some_function_name_possibly". That should work most of the time. - set-register / "\Q%opt{git_diff_section_heading}" - # Search backward from where the cursor is now. - # Note that the hunk line number is NOT located at the same place as the section heading. - # After we have found it, adjust the cursor and center the viewport as if we had directly jumped - # to the first character of the section header with and `edit` command. - execute-keys "<a-/><ret><a-semicolon><semicolon>vc" - } catch %{ - # There is no section heading, or we can't find it in the source file, - # so just go to the hunk start line. - # NOTE that we DONT go to the saved cursor column, - # because our cursor column will be fixed to the start of the section heading - edit -existing "%opt{git_diff_git_dir}%opt{git_diff_hunk_filename}" %opt{git_diff_hunk_line_num_start} - } - } - } catch %{ - # This catch deals with the typical case. We're somewhere on either: - # (a) A context line i.e. lines starting with ' ' - # or (b) On a line removal i.e. lines starting with '-' - # or (c) On a line addition i.e. lines starting with '+' - # So now try to figure out a line offset + git_diff_hunk_line_num_start that we need to go to - # Ignoring any diff lines starting with `-`, how many lines from where we - # pressed <ret> till the start of the hunk? - evaluate-commands -draft %{ - execute-keys '<a-?>^@@<ret>J<a-s><a-K>^-<ret>' - set-option global git_diff_go_to_line_num %sh{ - set -- $kak_reg_hash - line=$(($#+$kak_opt_git_diff_hunk_line_num_start-1)) - echo $line - } - } - evaluate-commands -try-client %opt{jumpclient} %{ - # Open the source file at the appropriate line number and cursor column - edit -existing "%opt{git_diff_git_dir}%opt{git_diff_hunk_filename}" %opt{git_diff_go_to_line_num} %opt{git_diff_cursor_column} - } - } - } - } catch %{ - fail "git-diff-goto-source: unable to navigate to source. Use only inside a diff" - } + require-module diff + diff-jump %sh{ git rev-parse --show-toplevel } } |
