Skip to content

Commit b967135

Browse files
stefanhallerpaulusmack
authored andcommitted
gitk: Synchronize highlighting in file view when scrolling diff
Whenever the diff pane scrolls, highlight the corresponding file in the file list on the right. For a large commit with many files and long per-file diffs, this makes it easier to keep track of what you're looking at. This allows simplifying the prevfile and nextfile functions, because all they have to do is scroll the diff pane. In some situations we want to suppress this mechanism, for example when clicking on a file in the file list to select it, or when searching in the diff, in which case we want to highlight based on the current search hit and not the top line visible. In these cases it's not sufficient to set a "suppress" flag before scrolling and reset it afterwards, because the scrolltext notification is sent deferred from a timer or some such; so we need to remember the scroll position for which we want to suppress the auto-highlighting until the next call to scrolltext; a bit ugly, but does the job. Signed-off-by: Stefan Haller <[email protected]> Signed-off-by: Paul Mackerras <[email protected]>
1 parent 5be4d35 commit b967135

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

gitk

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3309,6 +3309,7 @@ proc sel_flist {w x y} {
33093309
} else {
33103310
catch {$ctext yview [lindex $difffilestart [expr {$l - 2}]]}
33113311
}
3312+
suppress_highlighting_file_for_current_scrollpos
33123313
}
33133314

33143315
proc pop_flist_menu {w X Y x y} {
@@ -7947,44 +7948,52 @@ proc changediffdisp {} {
79477948
$ctext tag conf dresult -elide [lindex $diffelide 1]
79487949
}
79497950

7950-
proc highlightfile {loc cline} {
7951-
global ctext cflist cflist_top
7951+
proc highlightfile {cline} {
7952+
global cflist cflist_top
79527953

7953-
$ctext yview $loc
79547954
$cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
79557955
$cflist tag add highlight $cline.0 "$cline.0 lineend"
79567956
$cflist see $cline.0
79577957
set cflist_top $cline
79587958
}
79597959

7960+
proc highlightfile_for_scrollpos {topidx} {
7961+
global difffilestart
7962+
7963+
if {![info exists difffilestart]} return
7964+
7965+
set top [lindex [split $topidx .] 0]
7966+
if {$difffilestart eq {} || $top < [lindex $difffilestart 0]} {
7967+
highlightfile 0
7968+
} else {
7969+
highlightfile [expr {[bsearch $difffilestart $top] + 2}]
7970+
}
7971+
}
7972+
79607973
proc prevfile {} {
79617974
global difffilestart ctext cmitmode
79627975

79637976
if {$cmitmode eq "tree"} return
79647977
set prev 0.0
7965-
set prevline 1
79667978
set here [$ctext index @0,0]
79677979
foreach loc $difffilestart {
79687980
if {[$ctext compare $loc >= $here]} {
7969-
highlightfile $prev $prevline
7981+
$ctext yview $prev
79707982
return
79717983
}
79727984
set prev $loc
7973-
incr prevline
79747985
}
7975-
highlightfile $prev $prevline
7986+
$ctext yview $prev
79767987
}
79777988

79787989
proc nextfile {} {
79797990
global difffilestart ctext cmitmode
79807991

79817992
if {$cmitmode eq "tree"} return
79827993
set here [$ctext index @0,0]
7983-
set line 1
79847994
foreach loc $difffilestart {
7985-
incr line
79867995
if {[$ctext compare $loc > $here]} {
7987-
highlightfile $loc $line
7996+
$ctext yview $loc
79887997
return
79897998
}
79907999
}
@@ -8046,6 +8055,8 @@ proc incrsearch {name ix op} {
80468055
set here [$ctext search $searchdirn -- $searchstring anchor]
80478056
if {$here ne {}} {
80488057
$ctext see $here
8058+
suppress_highlighting_file_for_current_scrollpos
8059+
highlightfile_for_scrollpos $here
80498060
}
80508061
searchmarkvisible 1
80518062
}
@@ -8071,6 +8082,8 @@ proc dosearch {} {
80718082
return
80728083
}
80738084
$ctext see $match
8085+
suppress_highlighting_file_for_current_scrollpos
8086+
highlightfile_for_scrollpos $match
80748087
set mend "$match + $mlen c"
80758088
$ctext tag add sel $match $mend
80768089
$ctext mark unset anchor
@@ -8097,6 +8110,8 @@ proc dosearchback {} {
80978110
return
80988111
}
80998112
$ctext see $match
8113+
suppress_highlighting_file_for_current_scrollpos
8114+
highlightfile_for_scrollpos $match
81008115
set mend "$match + $ml c"
81018116
$ctext tag add sel $match $mend
81028117
$ctext mark unset anchor
@@ -8137,8 +8152,25 @@ proc searchmarkvisible {doall} {
81378152
}
81388153
}
81398154

8155+
proc suppress_highlighting_file_for_current_scrollpos {} {
8156+
global ctext suppress_highlighting_file_for_this_scrollpos
8157+
8158+
set suppress_highlighting_file_for_this_scrollpos [$ctext index @0,0]
8159+
}
8160+
81408161
proc scrolltext {f0 f1} {
8141-
global searchstring
8162+
global searchstring cmitmode ctext
8163+
global suppress_highlighting_file_for_this_scrollpos
8164+
8165+
if {$cmitmode ne "tree"} {
8166+
set topidx [$ctext index @0,0]
8167+
if {![info exists suppress_highlighting_file_for_this_scrollpos]
8168+
|| $topidx ne $suppress_highlighting_file_for_this_scrollpos} {
8169+
highlightfile_for_scrollpos $topidx
8170+
}
8171+
}
8172+
8173+
catch {unset suppress_highlighting_file_for_this_scrollpos}
81428174

81438175
.bleft.bottom.sb set $f0 $f1
81448176
if {$searchstring ne {}} {

0 commit comments

Comments
 (0)