@matheusbsbs / matheus-bagliano / commits / 0dfed77b3c
Fork 0

git-gui: Jump to original line in blame viewer

When the user clicks on a commit link within one of the columns in the blame viewer we now jump them not just to that commit/file pair but also to the line of the original file. This saves the user a lot of time, as they don't need to search through the new file data for the chunk they were previously looking at. We also restore the prior view when the user clicks the back button to return to a pior commit/file pair that they were looking at. Turned out this was quite tricky to get working in Tk. Every time I tried to jump the text widgets to the correct locations by way of the "yview moveto" or "see" subcommands Tk performed the change until the current event finished dispatching, and then reset the views back to 0, making the change never take place. Forcing Tk to run the pending events before we jump the UI resolves the issue. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Jun 4, 2007 at 04:07 UTC 0dfed77b3cc81342aee4c81e477d636a378c51cb
1 file changed +42 -13
lib/blame.tcl
+42 -13
@@ -310,10 +310,10 @@ constructor new {i_commit i_path} {
310 bind $w.file_pane <Configure> \
311 "if {{$w.file_pane} eq {%W}} {[cb _resize %h]}"
312
313 - _load $this
313 + _load $this {}
314 }
315
316 -method _load {} {
316 +method _load {jump} {
317 _hide_tooltip $this
318
319 if {$total_lines != 0 || $current_fd ne {}} {
@@ -331,6 +331,10 @@ method _load {} {
331 $i conf -state disabled
332 }
333
334 + $w_cviewer conf -state normal
335 + $w_cviewer delete 0.0 end
336 + $w_cviewer conf -state disabled
337 +
338 set highlight_line -1
339 set highlight_column {}
340 set highlight_commit {}
@@ -356,7 +360,6 @@ method _load {} {
360 } else {
361 $w_back conf -state normal
362 }
359 - lappend history [list $commit $path]
363
364 # Index 0 is always empty. There is never line 0 as
365 # we use only 1 based lines, as that matches both with
@@ -374,7 +377,7 @@ method _load {} {
377 set fd [open "| $cmd" r]
378 }
379 fconfigure $fd -blocking 0 -translation lf -encoding binary
377 - fileevent $fd readable [cb _read_file $fd]
380 + fileevent $fd readable [cb _read_file $fd $jump]
381 set current_fd $fd
382 }
383
@@ -386,7 +389,7 @@ method _history_menu {} {
389 menu $m -tearoff 0
390 }
391
389 - for {set i [expr {[llength $history] - 2}]
392 + for {set i [expr {[llength $history] - 1}]
393 } {$i >= 0} {incr i -1} {
394 set e [lindex $history $i]
395 set c [lindex $e 0]
@@ -406,21 +409,22 @@ method _history_menu {} {
409 }
410 }
411
409 - $m add command -label $t -command [cb _goback $i $c $f]
412 + $m add command -label $t -command [cb _goback $i]
413 }
414 set X [winfo rootx $w_back]
415 set Y [expr {[winfo rooty $w_back] + [winfo height $w_back]}]
416 tk_popup $m $X $Y
417 }
418
416 -method _goback {i c f} {
419 +method _goback {i} {
420 + set dat [lindex $history $i]
421 set history [lrange $history 0 [expr {$i - 1}]]
418 - set commit $c
419 - set path $f
420 - _load $this
422 + set commit [lindex $dat 0]
423 + set path [lindex $dat 1]
424 + _load $this [lrange $dat 2 5]
425 }
426
423 -method _read_file {fd} {
427 +method _read_file {fd jump} {
428 if {$fd ne $current_fd} {
429 catch {close $fd}
430 return
@@ -450,6 +454,22 @@ method _read_file {fd} {
454
455 if {[eof $fd]} {
456 close $fd
457 +
458 + # If we don't force Tk to update the widgets *right now*
459 + # none of our jump commands will cause a change in the UI.
460 + #
461 + update
462 +
463 + if {[llength $jump] == 1} {
464 + set highlight_line [lindex $jump 0]
465 + $w_file see "$highlight_line.0"
466 + } elseif {[llength $jump] == 4} {
467 + set highlight_column [lindex $jump 0]
468 + set highlight_line [lindex $jump 1]
469 + $w_file xview moveto [lindex $jump 2]
470 + $w_file yview moveto [lindex $jump 3]
471 + }
472 +
473 _exec_blame $this $w_asim @asim_data [list] {}
474 }
475 } ifdeleted { catch {close $fd} }
@@ -502,6 +522,7 @@ method _read_blame {fd cur_w cur_d cur_s} {
522 set file [string range $line 9 end]
523 set n $r_line_count
524 set lno $r_final_line
525 + set oln $r_orig_line
526 set cmit $r_commit
527
528 if {[regexp {^0{40}$} $cmit]} {
@@ -550,7 +571,7 @@ method _read_blame {fd cur_w cur_d cur_s} {
571 $i tag remove g$g $lno.0 $lno_e
572 }
573 }
553 - lset line_data $lno [list $cmit $file]
574 + lset line_data $lno [list $cmit $file $oln]
575
576 $cur_w delete $lno.0 "$lno.0 lineend"
577 if {$lno == $first_lno} {
@@ -578,6 +599,7 @@ method _read_blame {fd cur_w cur_d cur_s} {
599
600 incr n -1
601 incr lno
602 + incr oln
603 incr blame_lines
604 }
605
@@ -641,9 +663,16 @@ method _load_commit {cur_w cur_d pos} {
663 set lno [lindex [split [$cur_w index $pos] .] 0]
664 set dat [lindex $line_data $lno]
665 if {$dat ne {}} {
666 + lappend history [list \
667 + $commit $path \
668 + $highlight_column \
669 + $highlight_line \
670 + [lindex [$w_file xview] 0] \
671 + [lindex [$w_file yview] 0] \
672 + ]
673 set commit [lindex $dat 0]
674 set path [lindex $dat 1]
646 - _load $this
675 + _load $this [list [lindex $dat 2]]
676 }
677 }
678